From f06c62c2b85ec00f7d5ccff894c55caefb164563 Mon Sep 17 00:00:00 2001 From: tiddlygit-test Date: Sun, 3 Jan 2021 17:51:50 +0800 Subject: [PATCH] feat: AuthService --- src/services/auth/index.ts | 63 +++++++++++++++++++++++++++++-- src/services/serviceIdentifier.ts | 1 + src/services/wiki/index.ts | 14 +++++-- 3 files changed, 71 insertions(+), 7 deletions(-) diff --git a/src/services/auth/index.ts b/src/services/auth/index.ts index 3f10bcc5..c6a37b2a 100644 --- a/src/services/auth/index.ts +++ b/src/services/auth/index.ts @@ -1,15 +1,72 @@ +/* eslint-disable unicorn/no-null */ import { injectable } from 'inversify'; import settings from 'electron-settings'; +import { IUserInfo as IAuthingUserInfo } from '@services/types'; const defaultUserInfos = { userName: 'TiddlyGit User', - -} + authing: undefined as IAuthingUserInfo | undefined, +}; +export type IUserInfos = typeof defaultUserInfos; /** * Handle login to Github GitLab Coding.net */ @injectable() export class Authentication { + cachedUserInfo: IUserInfos; + readonly version = '2021.1'; -} \ No newline at end of file + constructor() { + this.cachedUserInfo = this.getInitUserInfoForCache(); + } + + /** + * load UserInfos in sync, and ensure it is an Object + */ + getInitUserInfoForCache = (): IUserInfos => { + let userInfosFromDisk = settings.getSync(`userInfo.${this.version}`) ?? {}; + userInfosFromDisk = typeof userInfosFromDisk === 'object' && !Array.isArray(userInfosFromDisk) ? userInfosFromDisk : {}; + return { ...defaultUserInfos, ...this.sanitizeUserInfo(userInfosFromDisk) }; + }; + + private sanitizeUserInfo(info: Partial): Partial { + return info; + } + + /** + * Batch update all UserInfos + */ + private async setUserInfos(newUserInfos: IUserInfos): Promise { + await settings.set(`userInfos.${this.version}`, newUserInfos as any); + } + + /** + * get UserInfos, may return cached version + */ + public getUserInfos = (): IUserInfos => { + // store in memory to boost performance + if (this.cachedUserInfo === undefined) { + return this.getInitUserInfoForCache(); + } + return this.cachedUserInfo; + }; + + public get(key: K): IUserInfos[K] | undefined { + if (this.cachedUserInfo[key] !== null && this.cachedUserInfo[key] !== undefined) { + return this.cachedUserInfo[key]; + } + } + + public async reset(): Promise { + await settings.unset(); + const UserInfos = this.getUserInfos(); + this.cachedUserInfo = UserInfos; + await this.setUserInfos(UserInfos); + // TODO: sendToAllWindows + // Object.keys(UserInfos).forEach((key) => { + // const value = UserInfos[key as keyof IUserInfos]; + // this.windowService.sendToAllWindows(UserInfoChannel.update, key, value); + // }); + } +} diff --git a/src/services/serviceIdentifier.ts b/src/services/serviceIdentifier.ts index 999c0b8d..5b71daa2 100644 --- a/src/services/serviceIdentifier.ts +++ b/src/services/serviceIdentifier.ts @@ -1,4 +1,5 @@ export default { + Authentication: Symbol.for('Authentication'), Window: Symbol.for('Window'), Preference: Symbol.for('Preference'), Notification: Symbol.for('Notification'), diff --git a/src/services/wiki/index.ts b/src/services/wiki/index.ts index faf33b28..c6940a76 100644 --- a/src/services/wiki/index.ts +++ b/src/services/wiki/index.ts @@ -10,6 +10,7 @@ import chokidar from 'chokidar'; import { trim, compact, debounce } from 'lodash'; import serviceIdentifiers from '@services/serviceIdentifier'; +import { Authentication } from '@services/auth'; import { Window } from '@services/windows'; import { Preference } from '@services/preferences'; import { View } from '@services/view'; @@ -28,6 +29,7 @@ import { updateSubWikiPluginContent } from './update-plugin-content'; @injectable() export class Wiki { constructor( + @inject(serviceIdentifiers.Authentication) private readonly authService: Authentication, @inject(serviceIdentifiers.Window) private readonly windowService: Window, @inject(serviceIdentifiers.Preference) private readonly preferenceService: Preference, @inject(serviceIdentifiers.Workspace) private readonly workspaceService: Workspace, @@ -325,17 +327,21 @@ export class Wiki { // do nothing } - const userName = this.preferenceService.get('userName'); - const userInfo = this.preferenceService.get('github-user-info'); + const userName = this.authService.get('userName') ?? ''; + const userInfo = this.authService.get('authing'); const { name: wikiPath, gitUrl: githubRepoUrl, port, isSubWiki, id } = workspace; // if is main wiki if (!isSubWiki) { this.setWikiStarted(wikiPath); await this.startNodeJSWiki(wikiPath, port, userName, id); - userInfo && watchWiki(wikiPath, githubRepoUrl, userInfo, path.join(wikiPath, TIDDLERS_PATH)); + if (userInfo !== undefined) { + await this.watchWiki(wikiPath, githubRepoUrl, userInfo, path.join(wikiPath, TIDDLERS_PATH)); + } } else { // if is private repo wiki - userInfo && watchWiki(wikiPath, githubRepoUrl, userInfo); + if (userInfo !== undefined) { + await this.watchWiki(wikiPath, githubRepoUrl, userInfo); + } // if we are creating a sub-wiki, restart the main wiki to load content from private wiki const mainWikiPath = workspace.mainWikiToLink; if (!this.justStartedWiki[mainWikiPath]) {