refactor(core): implement doc created/updated by service (#12150)

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

- **New Features**
  - Documents now automatically track and display "created by" and "updated by" user information.
  - Document creation and update timestamps are now managed and shown more accurately.
  - Workspace and document metadata (name, avatar) updates are more responsive and reliable.
  - Document creation supports middleware for customizing properties and behavior.

- **Improvements**
  - Simplified and unified event handling for document list updates, reducing redundant event subscriptions.
  - Enhanced integration of editor and theme settings into the document creation process.
  - Explicit Yjs document initialization for improved workspace stability and reliability.
  - Consolidated journal-related metadata display in document icons and titles for clarity.

- **Bug Fixes**
  - Fixed inconsistencies in how workspace and document names are set and updated.
  - Improved accuracy of "last updated" indicators by handling timestamps automatically.

- **Refactor**
  - Removed deprecated event subjects and direct metadata manipulation in favor of more robust, reactive patterns.
  - Streamlined document creation logic across various features (quick search, journal, recording, etc.).
  - Simplified user avatar display components and removed cloud metadata dependencies.
  - Removed legacy editor setting and theme service dependencies from multiple modules.

- **Chores**
  - Updated internal APIs and interfaces to support new metadata and event handling mechanisms.
  - Cleaned up unused code and dependencies related to editor settings and theme services.
  - Skipped flaky end-to-end test to improve test suite stability.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
EYHN
2025-05-08 07:53:33 +00:00
parent 93d74ff220
commit 2d1600fa00
56 changed files with 496 additions and 458 deletions

View File

@@ -95,6 +95,8 @@ import { UserCopilotQuotaStore } from './stores/user-copilot-quota';
import { UserFeatureStore } from './stores/user-feature';
import { UserQuotaStore } from './stores/user-quota';
import { UserSettingsStore } from './stores/user-settings';
import { DocCreatedByService } from './services/doc-created-by';
import { DocUpdatedByService } from './services/doc-updated-by';
export function configureCloudModule(framework: Framework) {
configureDefaultAuthProvider(framework);
@@ -164,7 +166,9 @@ export function configureCloudModule(framework: Framework) {
framework
.scope(WorkspaceScope)
.service(WorkspaceServerService)
.service(DocCreatedByService, [WorkspaceServerService])
.scope(DocScope)
.service(DocUpdatedByService, [WorkspaceServerService])
.service(CloudDocMetaService)
.entity(CloudDocMeta, [CloudDocMetaStore, DocService, GlobalCache])
.store(CloudDocMetaStore, [WorkspaceServerService]);

View File

@@ -0,0 +1,19 @@
import { OnEvent, Service } from '@toeverything/infra';
import { DocCreated, type DocRecord } from '../../doc';
import type { DocCreateOptions } from '../../doc/types';
import type { WorkspaceServerService } from './workspace-server';
@OnEvent(DocCreated, t => t.onDocCreated)
export class DocCreatedByService extends Service {
constructor(private readonly workspaceServerService: WorkspaceServerService) {
super();
}
onDocCreated(event: { doc: DocRecord; docCreateOptions: DocCreateOptions }) {
const account = this.workspaceServerService.server?.account$.value;
if (account) {
event.doc.setCreatedBy(account.id);
}
}
}

View File

@@ -0,0 +1,37 @@
import { OnEvent, Service } from '@toeverything/infra';
import { throttle } from 'lodash-es';
import type { Transaction } from 'yjs';
import type { Doc } from '../../doc';
import { DocInitialized } from '../../doc/events';
import type { WorkspaceServerService } from './workspace-server';
@OnEvent(DocInitialized, t => t.onDocInitialized)
export class DocUpdatedByService extends Service {
constructor(private readonly workspaceServerService: WorkspaceServerService) {
super();
}
onDocInitialized(doc: Doc) {
const handleTransactionThrottled = throttle(
(trx: Transaction) => {
if (trx.local) {
const account = this.workspaceServerService.server?.account$.value;
if (account) {
doc.setUpdatedBy(account.id);
}
}
},
1000,
{
leading: true,
trailing: true,
}
);
doc.yDoc.on('afterTransaction', handleTransactionThrottled);
this.disposables.push(() => {
doc.yDoc.off('afterTransaction', handleTransactionThrottled);
handleTransactionThrottled.cancel();
});
}
}