mirror of
https://github.com/toeverything/AFFiNE.git
synced 2024-12-18 05:31:31 +03:00
Merge pull request #67 from toeverything/feat/userConfig
Feat/user config
This commit is contained in:
commit
2a19006196
@ -6,40 +6,40 @@ import { PageConfigItem } from './types';
|
||||
|
||||
/** Operate the user configuration at the workspace level */
|
||||
export class UserConfig extends ServiceBaseClass {
|
||||
private async fetch_recent_pages(
|
||||
private async _fetchRecentPages(
|
||||
workspace: string
|
||||
): Promise<Record<string, Array<PageConfigItem>>> {
|
||||
const workspace_db_block = await this.getWorkspaceDbBlock(workspace);
|
||||
const recent_work_pages =
|
||||
workspace_db_block.getDecoration<
|
||||
const workspaceDbBlock = await this.getWorkspaceDbBlock(workspace);
|
||||
const recentWorkPages =
|
||||
workspaceDbBlock.getDecoration<
|
||||
Record<string, Array<PageConfigItem>>
|
||||
>(RECENT_PAGES) || {};
|
||||
return recent_work_pages;
|
||||
return recentWorkPages;
|
||||
}
|
||||
|
||||
private async save_recent_pages(
|
||||
private async _saveRecentPages(
|
||||
workspace: string,
|
||||
recentPages: Record<string, Array<PageConfigItem>>
|
||||
) {
|
||||
const workspace_db_block = await this.getWorkspaceDbBlock(workspace);
|
||||
workspace_db_block.setDecoration(RECENT_PAGES, recentPages);
|
||||
const workspaceDbBlock = await this.getWorkspaceDbBlock(workspace);
|
||||
workspaceDbBlock.setDecoration(RECENT_PAGES, recentPages);
|
||||
}
|
||||
|
||||
async getUserInitialPage(
|
||||
workspace: string,
|
||||
userId: string
|
||||
): Promise<string> {
|
||||
const recent_pages = await this.getRecentPages(workspace, userId);
|
||||
if (recent_pages.length > 0) {
|
||||
return recent_pages[0].id;
|
||||
const recentPages = await this.getRecentPages(workspace, userId);
|
||||
if (recentPages.length > 0) {
|
||||
return recentPages[0].id;
|
||||
}
|
||||
|
||||
const db = await this.database.getDatabase(workspace);
|
||||
const new_page = await db.get('page');
|
||||
const newPage = await db.get('page');
|
||||
|
||||
await this.get_dependency(PageTree).addPage(workspace, new_page.id);
|
||||
await this.addRecentPage(workspace, userId, new_page.id);
|
||||
return new_page.id;
|
||||
await this.get_dependency(PageTree).addPage(workspace, newPage.id);
|
||||
await this.addRecentPage(workspace, userId, newPage.id);
|
||||
return newPage.id;
|
||||
}
|
||||
|
||||
async getRecentPages(
|
||||
@ -47,13 +47,10 @@ export class UserConfig extends ServiceBaseClass {
|
||||
userId: string,
|
||||
topNumber = 5
|
||||
): Promise<PageConfigItem[]> {
|
||||
const recent_work_pages = await this.fetch_recent_pages(workspace);
|
||||
const recent_pages = (recent_work_pages[userId] || []).slice(
|
||||
0,
|
||||
topNumber
|
||||
);
|
||||
const recentWorkPages = await this._fetchRecentPages(workspace);
|
||||
const recentPages = (recentWorkPages[userId] || []).slice(0, topNumber);
|
||||
const db = await this.database.getDatabase(workspace);
|
||||
for (const item of recent_pages) {
|
||||
for (const item of recentPages) {
|
||||
const page = await db.get(item.id as 'page');
|
||||
item.title =
|
||||
page
|
||||
@ -61,39 +58,39 @@ export class UserConfig extends ServiceBaseClass {
|
||||
?.value?.map(v => v.text)
|
||||
.join('') || 'Untitled';
|
||||
}
|
||||
return recent_pages;
|
||||
return recentPages;
|
||||
}
|
||||
|
||||
async addRecentPage(workspace: string, userId: string, pageId: string) {
|
||||
const recent_work_pages = await this.fetch_recent_pages(workspace);
|
||||
let recent_pages = recent_work_pages[userId] || [];
|
||||
recent_pages = recent_pages.filter(item => item.id !== pageId);
|
||||
recent_pages.unshift({
|
||||
const recentWorkPages = await this._fetchRecentPages(workspace);
|
||||
let recentPages = recentWorkPages[userId] || [];
|
||||
recentPages = recentPages.filter(item => item.id !== pageId);
|
||||
recentPages.unshift({
|
||||
id: pageId,
|
||||
lastOpenTime: Date.now(),
|
||||
});
|
||||
recent_work_pages[userId] = recent_pages;
|
||||
await this.save_recent_pages(workspace, recent_work_pages);
|
||||
recentWorkPages[userId] = recentPages;
|
||||
await this._saveRecentPages(workspace, recentWorkPages);
|
||||
}
|
||||
|
||||
async removePage(workspace: string, pageId: string) {
|
||||
const recent_work_pages = await this.fetch_recent_pages(workspace);
|
||||
for (const key in recent_work_pages) {
|
||||
recent_work_pages[key] = recent_work_pages[key].filter(
|
||||
const recentWorkPages = await this._fetchRecentPages(workspace);
|
||||
for (const key in recentWorkPages) {
|
||||
recentWorkPages[key] = recentWorkPages[key].filter(
|
||||
item => item.id !== pageId
|
||||
);
|
||||
}
|
||||
await this.save_recent_pages(workspace, recent_work_pages);
|
||||
await this._saveRecentPages(workspace, recentWorkPages);
|
||||
}
|
||||
|
||||
async observe(
|
||||
{ workspace }: { workspace: string },
|
||||
callback: ObserveCallback
|
||||
): Promise<ReturnUnobserve> {
|
||||
const workspace_db_block = await this.getWorkspaceDbBlock(workspace);
|
||||
const workspaceDbBlock = await this.getWorkspaceDbBlock(workspace);
|
||||
const unobserveWorkspace = await this._observe(
|
||||
workspace,
|
||||
workspace_db_block.id,
|
||||
workspaceDbBlock.id,
|
||||
(states, block) => {
|
||||
callback(states, block);
|
||||
}
|
||||
@ -105,24 +102,24 @@ export class UserConfig extends ServiceBaseClass {
|
||||
}
|
||||
|
||||
async unobserve({ workspace }: { workspace: string }) {
|
||||
const workspace_db_block = await this.getWorkspaceDbBlock(workspace);
|
||||
await this._unobserve(workspace, workspace_db_block.id);
|
||||
const workspaceDbBlock = await this.getWorkspaceDbBlock(workspace);
|
||||
await this._unobserve(workspace, workspaceDbBlock.id);
|
||||
}
|
||||
|
||||
async getWorkspaceName(workspace: string): Promise<string> {
|
||||
const workspace_db_block = await this.getWorkspaceDbBlock(workspace);
|
||||
const workspaceDbBlock = await this.getWorkspaceDbBlock(workspace);
|
||||
const workspaceName =
|
||||
workspace_db_block.getDecoration<string>(WORKSPACE_CONFIG) || '';
|
||||
workspaceDbBlock.getDecoration<string>(WORKSPACE_CONFIG) || '';
|
||||
return workspaceName;
|
||||
}
|
||||
|
||||
async getWorkspaceId(workspace: string): Promise<string> {
|
||||
const workspace_db_block = await this.getWorkspaceDbBlock(workspace);
|
||||
return workspace_db_block.id;
|
||||
const workspaceDbBlock = await this.getWorkspaceDbBlock(workspace);
|
||||
return workspaceDbBlock.id;
|
||||
}
|
||||
|
||||
async setWorkspaceName(workspace: string, workspaceName: string) {
|
||||
const workspace_db_block = await this.getWorkspaceDbBlock(workspace);
|
||||
workspace_db_block.setDecoration(WORKSPACE_CONFIG, workspaceName);
|
||||
const workspaceDbBlock = await this.getWorkspaceDbBlock(workspace);
|
||||
workspaceDbBlock.setDecoration(WORKSPACE_CONFIG, workspaceName);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user