-
-
Notifications
You must be signed in to change notification settings - Fork 111
/
index.ts
475 lines (420 loc) · 17.9 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/* eslint-disable @typescript-eslint/strict-boolean-expressions */
/* eslint-disable @typescript-eslint/no-unsafe-argument */
/* eslint-disable @typescript-eslint/require-await */
/* eslint-disable unicorn/no-null */
import { app } from 'electron';
import fsExtra from 'fs-extra';
import { injectable } from 'inversify';
import Jimp from 'jimp';
import { mapValues, pickBy } from 'lodash';
import { nanoid } from 'nanoid';
import path from 'path';
import { BehaviorSubject, Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { DELAY_MENU_REGISTER } from '@/constants/parameters';
import { getDefaultTidGiUrl } from '@/constants/urls';
import { IAuthenticationService } from '@services/auth/interface';
import { lazyInject } from '@services/container';
import { IDatabaseService } from '@services/database/interface';
import { i18n } from '@services/libs/i18n';
import { logger } from '@services/libs/log';
import type { IMenuService } from '@services/menu/interface';
import { IPagesService } from '@services/pages/interface';
import serviceIdentifier from '@services/serviceIdentifier';
import { SupportedStorageServices } from '@services/types';
import type { IViewService } from '@services/view/interface';
import type { IWikiService } from '@services/wiki/interface';
import { WindowNames } from '@services/windows/WindowProperties';
import type { IWorkspaceViewService } from '@services/workspacesView/interface';
import type { INewWorkspaceConfig, IWorkspace, IWorkspaceMetaData, IWorkspaceService, IWorkspacesWithMetadata, IWorkspaceWithMetadata } from './interface';
import { registerMenu } from './registerMenu';
import { workspaceSorter } from './utils';
@injectable()
export class Workspace implements IWorkspaceService {
/**
* Record from workspace id to workspace settings
*/
private workspaces: Record<string, IWorkspace> | undefined;
public workspaces$ = new BehaviorSubject<IWorkspacesWithMetadata | undefined>(undefined);
@lazyInject(serviceIdentifier.Wiki)
private readonly wikiService!: IWikiService;
@lazyInject(serviceIdentifier.Database)
private readonly databaseService!: IDatabaseService;
@lazyInject(serviceIdentifier.View)
private readonly viewService!: IViewService;
@lazyInject(serviceIdentifier.WorkspaceView)
private readonly workspaceViewService!: IWorkspaceViewService;
@lazyInject(serviceIdentifier.MenuService)
private readonly menuService!: IMenuService;
@lazyInject(serviceIdentifier.Authentication)
private readonly authService!: IAuthenticationService;
@lazyInject(serviceIdentifier.Pages)
private readonly pagesService!: IPagesService;
constructor() {
setTimeout(() => {
void registerMenu();
}, DELAY_MENU_REGISTER);
}
public getWorkspacesWithMetadata(): IWorkspacesWithMetadata {
return mapValues(this.getWorkspacesSync(), (workspace: IWorkspace, id): IWorkspaceWithMetadata => ({ ...workspace, metadata: this.getMetaDataSync(id) }));
}
public updateWorkspaceSubject(): void {
this.workspaces$.next(this.getWorkspacesWithMetadata());
}
/**
* Update items like "activate workspace1" or "open devtool in workspace1" in the menu
*/
private async updateWorkspaceMenuItems(): Promise<void> {
const newMenuItems = (await this.getWorkspacesAsList()).flatMap((workspace, index) => [
{
label: (): string => workspace.name || `Workspace ${index + 1}`,
id: workspace.id,
type: 'checkbox' as const,
checked: () => workspace.active,
click: async (): Promise<void> => {
await this.workspaceViewService.setActiveWorkspaceView(workspace.id);
// manually update menu since we have alter the active workspace
await this.menuService.buildMenu();
},
accelerator: `CmdOrCtrl+${index + 1}`,
},
{
label: () => `${workspace.name || `Workspace ${index + 1}`} ${i18n.t('Menu.DeveloperToolsActiveWorkspace')}`,
id: `${workspace.id}-devtool`,
click: async () => {
const view = this.viewService.getView(workspace.id, WindowNames.main);
if (view !== undefined) {
view.webContents.toggleDevTools();
}
},
},
]);
/* eslint-enable @typescript-eslint/no-misused-promises */
await this.menuService.insertMenu('Workspaces', newMenuItems, undefined, undefined, 'updateWorkspaceMenuItems');
}
/**
* load workspaces in sync, and ensure it is an Object
*/
private getInitWorkspacesForCache(): Record<string, IWorkspace> {
const workspacesFromDisk = this.databaseService.getSetting(`workspaces`) ?? {};
return typeof workspacesFromDisk === 'object' && !Array.isArray(workspacesFromDisk)
? mapValues(pickBy(workspacesFromDisk, (value) => value !== null) as unknown as Record<string, IWorkspace>, (workspace) => this.sanitizeWorkspace(workspace))
: {};
}
public async getWorkspaces(): Promise<Record<string, IWorkspace>> {
return this.getWorkspacesSync();
}
private getWorkspacesSync(): Record<string, IWorkspace> {
// store in memory to boost performance
if (this.workspaces === undefined) {
this.workspaces = this.getInitWorkspacesForCache();
}
return this.workspaces;
}
public async countWorkspaces(): Promise<number> {
return Object.keys(this.getWorkspacesSync()).length;
}
/**
* Get sorted workspace list
* Async so proxy type is async
*/
public async getWorkspacesAsList(): Promise<IWorkspace[]> {
return Object.values(this.getWorkspacesSync()).sort(workspaceSorter);
}
/**
* Get sorted workspace list
* Sync for internal use
*/
private getWorkspacesAsListSync(): IWorkspace[] {
return Object.values(this.getWorkspacesSync()).sort(workspaceSorter);
}
public async getSubWorkspacesAsList(workspaceID: string): Promise<IWorkspace[]> {
const workspace = this.getSync(workspaceID);
if (workspace === undefined) return [];
if (workspace.isSubWiki) return [];
return this.getWorkspacesAsListSync().filter((w) => w.mainWikiID === workspaceID).sort(workspaceSorter);
}
public getSubWorkspacesAsListSync(workspaceID: string): IWorkspace[] {
const workspace = this.getSync(workspaceID);
if (workspace === undefined) return [];
if (workspace.isSubWiki) return [];
return this.getWorkspacesAsListSync().filter((w) => w.mainWikiID === workspaceID).sort(workspaceSorter);
}
public async get(id: string): Promise<IWorkspace | undefined> {
return this.getSync(id);
}
private getSync(id: string): IWorkspace | undefined {
return this.getWorkspacesSync()[id];
}
public get$(id: string): Observable<IWorkspace | undefined> {
return this.workspaces$.pipe(map((workspaces) => workspaces?.[id]));
}
public async set(id: string, workspace: IWorkspace, immediate?: boolean): Promise<void> {
const workspaces = this.getWorkspacesSync();
const workspaceToSave = this.sanitizeWorkspace(workspace);
await this.reactBeforeWorkspaceChanged(workspaceToSave);
workspaces[id] = workspaceToSave;
this.databaseService.setSetting('workspaces', workspaces);
if (immediate === true) {
await this.databaseService.immediatelyStoreSettingsToFile();
}
// update subject so ui can react to it
this.updateWorkspaceSubject();
// menu is mostly invisible, so we don't need to update it immediately
void this.updateWorkspaceMenuItems();
}
public async update(id: string, workspaceSetting: Partial<IWorkspace>, immediate?: boolean): Promise<void> {
const workspace = this.getSync(id);
if (workspace === undefined) {
logger.error(`Could not update workspace ${id} because it does not exist`);
return;
}
await this.set(id, { ...workspace, ...workspaceSetting }, immediate);
}
public async setWorkspaces(newWorkspaces: Record<string, IWorkspace>): Promise<void> {
for (const id in newWorkspaces) {
await this.set(id, newWorkspaces[id]);
}
}
public getMainWorkspace(subWorkspace: IWorkspace): IWorkspace | undefined {
const { mainWikiID, isSubWiki, mainWikiToLink } = subWorkspace;
if (!isSubWiki) return undefined;
if (mainWikiID) return this.getSync(mainWikiID);
const mainWorkspace = (this.getWorkspacesAsListSync() ?? []).find(
(workspaceToSearch) => mainWikiToLink === workspaceToSearch.wikiFolderLocation,
);
return mainWorkspace;
}
/**
* Pure function that make sure workspace setting is consistent, or doing migration across updates
* @param workspaceToSanitize User input workspace or loaded workspace, that may contains bad values
*/
private sanitizeWorkspace(workspaceToSanitize: IWorkspace): IWorkspace {
const defaultValues: Partial<IWorkspace> = {
storageService: SupportedStorageServices.github,
backupOnInterval: true,
excludedPlugins: [],
enableHTTPAPI: false,
};
const fixingValues: Partial<IWorkspace> = {};
// we add mainWikiID in creation, we fix this value for old existed workspaces
if (workspaceToSanitize.isSubWiki && !workspaceToSanitize.mainWikiID) {
const mainWorkspace = this.getMainWorkspace(workspaceToSanitize);
if (mainWorkspace !== undefined) {
fixingValues.mainWikiID = mainWorkspace.id;
}
}
// fix WikiChannel.openTiddler in src/services/wiki/wikiOperations/executor/wikiOperationInBrowser.ts have \n on the end
if (workspaceToSanitize.tagName?.endsWith('\n') === true) {
fixingValues.tagName = workspaceToSanitize.tagName.replaceAll('\n', '');
}
// before 0.8.0, tidgi was loading http content, so lastUrl will be http protocol, but later we switch to tidgi:// protocol, so old value can't be used.
if (!workspaceToSanitize.lastUrl?.startsWith('tidgi')) {
fixingValues.lastUrl = null;
}
if (!workspaceToSanitize.homeUrl?.startsWith('tidgi')) {
fixingValues.homeUrl = getDefaultTidGiUrl(workspaceToSanitize.id);
}
if (workspaceToSanitize.tokenAuth && !workspaceToSanitize.authToken) {
fixingValues.authToken = this.authService.generateOneTimeAdminAuthTokenForWorkspaceSync(workspaceToSanitize.id);
}
return { ...defaultValues, ...workspaceToSanitize, ...fixingValues };
}
/**
* Do some side effect before config change, update other services or filesystem, with new and old values
* This happened after values sanitized
* @param newWorkspaceConfig new workspace settings
*/
private async reactBeforeWorkspaceChanged(newWorkspaceConfig: IWorkspace): Promise<void> {
const existedWorkspace = this.getSync(newWorkspaceConfig.id);
const { id, tagName } = newWorkspaceConfig;
// when update tagName of subWiki
if (existedWorkspace !== undefined && existedWorkspace.isSubWiki && typeof tagName === 'string' && tagName.length > 0 && existedWorkspace.tagName !== tagName) {
const { mainWikiToLink, wikiFolderLocation } = existedWorkspace;
if (typeof mainWikiToLink !== 'string') {
throw new TypeError(
`mainWikiToLink is null in reactBeforeWorkspaceChanged when try to updateSubWikiPluginContent, workspacesID: ${id}\n${
JSON.stringify(
this.workspaces,
)
}`,
);
}
await this.wikiService.updateSubWikiPluginContent(mainWikiToLink, wikiFolderLocation, newWorkspaceConfig, {
...newWorkspaceConfig,
tagName: existedWorkspace.tagName,
});
await this.wikiService.wikiStartup(newWorkspaceConfig);
}
}
public async getByWikiFolderLocation(wikiFolderLocation: string): Promise<IWorkspace | undefined> {
return (await this.getWorkspacesAsList()).find((workspace) => workspace.wikiFolderLocation === wikiFolderLocation);
}
public getPreviousWorkspace = async (id: string): Promise<IWorkspace | undefined> => {
const workspaceList = await this.getWorkspacesAsList();
let currentWorkspaceIndex = 0;
for (const [index, workspace] of workspaceList.entries()) {
if (workspace.id === id) {
currentWorkspaceIndex = index;
break;
}
}
if (currentWorkspaceIndex === 0) {
return workspaceList.at(-1);
}
return workspaceList[currentWorkspaceIndex - 1];
};
public getNextWorkspace = async (id: string): Promise<IWorkspace | undefined> => {
const workspaceList = await this.getWorkspacesAsList();
let currentWorkspaceIndex = 0;
for (const [index, workspace] of workspaceList.entries()) {
if (workspace.id === id) {
currentWorkspaceIndex = index;
break;
}
}
if (currentWorkspaceIndex === workspaceList.length - 1) {
return workspaceList[0];
}
return workspaceList[currentWorkspaceIndex + 1];
};
public getActiveWorkspace = async (): Promise<IWorkspace | undefined> => {
return this.getActiveWorkspaceSync();
};
public getActiveWorkspaceSync = (): IWorkspace | undefined => {
return this.getWorkspacesAsListSync().find((workspace) => workspace.active);
};
public getFirstWorkspace = async (): Promise<IWorkspace | undefined> => {
return this.getFirstWorkspaceSync();
};
public getFirstWorkspaceSync = (): IWorkspace | undefined => {
return this.getWorkspacesAsListSync()[0];
};
public async setActiveWorkspace(id: string, oldActiveWorkspaceID: string | undefined): Promise<void> {
// active new one
await this.update(id, { active: true, hibernated: false });
// de-active the other one
if (oldActiveWorkspaceID !== id) {
await this.clearActiveWorkspace(oldActiveWorkspaceID);
}
// switch from page to workspace, clear active page to switch to WikiBackground page
const activePage = this.pagesService.getActivePageSync();
// instead of switch to a wiki workspace, we simply clear active page, because wiki page logic is not implemented yet, we are still using workspace logic.
await this.pagesService.clearActivePage(activePage?.id);
}
public async clearActiveWorkspace(oldActiveWorkspaceID: string | undefined): Promise<void> {
// de-active the other one
if (typeof oldActiveWorkspaceID === 'string') {
await this.update(oldActiveWorkspaceID, { active: false });
}
}
/**
* @param id workspace id
* @param sourcePicturePath image path, could be an image in app's resource folder or temp folder, we will copy it into app data folder
*/
public async setWorkspacePicture(id: string, sourcePicturePath: string): Promise<void> {
const workspace = this.getSync(id);
if (workspace === undefined) {
throw new Error(`Try to setWorkspacePicture() but this workspace is not existed ${id}`);
}
const pictureID = nanoid();
if (workspace.picturePath === sourcePicturePath) {
return;
}
const destinationPicturePath = path.join(app.getPath('userData'), 'pictures', `${pictureID}.png`);
const newImage = await Jimp.read(sourcePicturePath);
await new Promise((resolve) => {
newImage.clone().resize(128, 128).quality(100).write(destinationPicturePath, resolve);
});
const currentPicturePath = this.getSync(id)?.picturePath;
await this.update(id, {
picturePath: destinationPicturePath,
});
if (currentPicturePath) {
try {
await fsExtra.remove(currentPicturePath);
} catch (error) {
console.error(error);
}
}
}
public async removeWorkspacePicture(id: string): Promise<void> {
const workspace = this.getSync(id);
if (workspace === undefined) {
throw new Error(`Try to removeWorkspacePicture() but this workspace is not existed ${id}`);
}
if (workspace.picturePath) {
await fsExtra.remove(workspace.picturePath);
await this.set(id, {
...workspace,
picturePath: null,
});
}
}
public async remove(id: string): Promise<void> {
const workspaces = this.getWorkspacesSync();
if (id in workspaces) {
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete workspaces[id];
this.databaseService.setSetting('workspaces', workspaces);
} else {
throw new Error(`Try to remote workspace, but id ${id} is not existed`);
}
this.updateWorkspaceSubject();
void this.updateWorkspaceMenuItems();
}
public async create(newWorkspaceConfig: INewWorkspaceConfig): Promise<IWorkspace> {
const newID = nanoid();
// find largest order
const workspaceLst = await this.getWorkspacesAsList();
let max = 0;
for (const element of workspaceLst) {
if (element.order > max) {
max = element.order;
}
}
const newWorkspace: IWorkspace = {
userName: '',
...newWorkspaceConfig,
active: false,
disableAudio: false,
disableNotifications: false,
hibernated: false,
hibernateWhenUnused: false,
homeUrl: getDefaultTidGiUrl(newID),
id: newID,
lastUrl: null,
lastNodeJSArgv: [],
order: max + 1,
picturePath: null,
subWikiFolderName: 'subwiki',
syncOnInterval: false,
syncOnStartup: true,
transparentBackground: false,
enableHTTPAPI: false,
excludedPlugins: [],
};
await this.set(newID, newWorkspace);
return newWorkspace;
}
/** to keep workspace variables (meta) that
* are not saved to disk
* badge count, error, etc
*/
private metaData: Record<string, Partial<IWorkspaceMetaData>> = {};
public getMetaData = async (id: string): Promise<Partial<IWorkspaceMetaData>> => this.getMetaDataSync(id);
private readonly getMetaDataSync = (id: string): Partial<IWorkspaceMetaData> => this.metaData[id] ?? {};
public getAllMetaData = async (): Promise<Record<string, Partial<IWorkspaceMetaData>>> => this.metaData;
public updateMetaData = async (id: string, options: Partial<IWorkspaceMetaData>): Promise<void> => {
logger.debug(`updateMetaData(${id})`, options);
this.metaData[id] = {
...this.metaData[id],
...options,
};
this.updateWorkspaceSubject();
};
public async workspaceDidFailLoad(id: string): Promise<boolean> {
const workspaceMetaData = this.getMetaDataSync(id);
return typeof workspaceMetaData?.didFailLoadErrorMessage === 'string' && workspaceMetaData.didFailLoadErrorMessage.length > 0;
}
}