mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-15 05:37:32 +00:00
feat(core): add blocksuite writer info service (#10754)
This commit is contained in:
@@ -2,7 +2,7 @@ import type { BlockMeta } from '@blocksuite/affine-model';
|
||||
import { type BlockModel, StoreExtension } from '@blocksuite/store';
|
||||
|
||||
import { FeatureFlagService } from './feature-flag-service';
|
||||
import { UserProvider } from './user-service';
|
||||
import { WriterInfoProvider } from './user-service';
|
||||
|
||||
/**
|
||||
* The service is used to add following info to the block.
|
||||
@@ -43,20 +43,20 @@ export class BlockMetaService extends StoreExtension {
|
||||
return;
|
||||
}
|
||||
|
||||
const currentUser = this._getCurrentUser();
|
||||
if (!currentUser) return;
|
||||
const writer = this._getWriterInfo();
|
||||
if (!writer) return;
|
||||
const now = getNow();
|
||||
|
||||
this.store.withoutTransact(() => {
|
||||
const isFlatModel = model.schema.model.isFlatData;
|
||||
if (!isFlatModel) {
|
||||
model['meta:createdAt'] = now;
|
||||
model['meta:createdBy'] = currentUser.id;
|
||||
model['meta:createdBy'] = writer.id;
|
||||
return;
|
||||
}
|
||||
|
||||
model.props['meta:createdAt'] = now;
|
||||
model.props['meta:createdBy'] = currentUser.id;
|
||||
model.props['meta:createdBy'] = writer.id;
|
||||
});
|
||||
};
|
||||
|
||||
@@ -65,33 +65,33 @@ export class BlockMetaService extends StoreExtension {
|
||||
return;
|
||||
}
|
||||
|
||||
const currentUser = this._getCurrentUser();
|
||||
if (!currentUser) return;
|
||||
const writer = this._getWriterInfo();
|
||||
if (!writer) return;
|
||||
const now = getNow();
|
||||
|
||||
this.store.withoutTransact(() => {
|
||||
const isFlatModel = model.schema.model.isFlatData;
|
||||
if (!isFlatModel) {
|
||||
model['meta:updatedAt'] = now;
|
||||
model['meta:updatedBy'] = currentUser.id;
|
||||
model['meta:updatedBy'] = writer.id;
|
||||
if (!model['meta:createdAt']) {
|
||||
model['meta:createdAt'] = now;
|
||||
model['meta:createdBy'] = currentUser.id;
|
||||
model['meta:createdBy'] = writer.id;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
model.props['meta:updatedAt'] = now;
|
||||
model.props['meta:updatedBy'] = currentUser.id;
|
||||
model.props['meta:updatedBy'] = writer.id;
|
||||
if (!model.props['meta:createdAt']) {
|
||||
model.props['meta:createdAt'] = now;
|
||||
model.props['meta:createdBy'] = currentUser.id;
|
||||
model.props['meta:createdBy'] = writer.id;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
private readonly _getCurrentUser = () => {
|
||||
return this.store.getOptional(UserProvider)?.getCurrentUser();
|
||||
private readonly _getWriterInfo = () => {
|
||||
return this.store.getOptional(WriterInfoProvider)?.getWriterInfo();
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
export * from './types';
|
||||
export * from './user-list-service';
|
||||
export * from './user-service';
|
||||
export * from './writer-info-service';
|
||||
|
||||
@@ -5,7 +5,6 @@ import type { Signal } from '@preact/signals-core';
|
||||
import type { AffineUserInfo } from './types';
|
||||
|
||||
export interface UserService {
|
||||
getCurrentUser(): AffineUserInfo | null;
|
||||
userInfo$(id: string): Signal<AffineUserInfo | null>;
|
||||
revalidateUserInfo(id: string): void;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
import { createIdentifier } from '@blocksuite/global/di';
|
||||
import type { ExtensionType } from '@blocksuite/store';
|
||||
|
||||
import type { AffineUserInfo } from './types';
|
||||
|
||||
export interface WriterInfoService {
|
||||
getWriterInfo(): AffineUserInfo | null;
|
||||
}
|
||||
|
||||
export const WriterInfoProvider = createIdentifier<WriterInfoService>(
|
||||
'affine-writer-info-service'
|
||||
);
|
||||
|
||||
export function WriterInfoServiceExtension(
|
||||
service: WriterInfoService
|
||||
): ExtensionType {
|
||||
return {
|
||||
setup(di) {
|
||||
di.addImpl(WriterInfoProvider, () => service);
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user