|
| 1 | +// get notice from remote |
| 2 | +// such as some notices for users; some updates for users |
| 3 | +import fs from 'fs-extra' |
| 4 | +import { app, clipboard, dialog, shell } from 'electron' |
| 5 | +import { IRemoteNoticeActionType, IRemoteNoticeTriggerCount, IRemoteNoticeTriggerHook } from '#/types/enum' |
| 6 | +import { lte, gte } from 'semver' |
| 7 | +import path from 'path' |
| 8 | + |
| 9 | +import axios from 'axios' |
| 10 | +import windowManager from '../window/windowManager' |
| 11 | +import { showNotification } from '~/main/utils/common' |
| 12 | +import { isDev } from '~/universal/utils/common' |
| 13 | + |
| 14 | +// for test |
| 15 | +const REMOTE_NOTICE_URL = isDev ? 'http://localhost:8181/remote-notice.json' : 'https://picgo-1251750343.cos.accelerate.myqcloud.com/remote-notice.yml' |
| 16 | + |
| 17 | +const REMOTE_NOTICE_LOCAL_STORAGE_FILE = 'picgo-remote-notice.json' |
| 18 | + |
| 19 | +const STORE_PATH = app.getPath('userData') |
| 20 | + |
| 21 | +const REMOTE_NOTICE_LOCAL_STORAGE_PATH = path.join(STORE_PATH, REMOTE_NOTICE_LOCAL_STORAGE_FILE) |
| 22 | + |
| 23 | +class RemoteNoticeHandler { |
| 24 | + private remoteNotice: IRemoteNotice | null = null |
| 25 | + private remoteNoticeLocalCountStorage: IRemoteNoticeLocalCountStorage | null = null |
| 26 | + |
| 27 | + async init () { |
| 28 | + this.remoteNotice = await this.getRemoteNoticeInfo() |
| 29 | + this.initLocalCountStorage() |
| 30 | + } |
| 31 | + |
| 32 | + private initLocalCountStorage () { |
| 33 | + const localCountStorage = {} |
| 34 | + if (!fs.existsSync(REMOTE_NOTICE_LOCAL_STORAGE_PATH)) { |
| 35 | + fs.writeFileSync(REMOTE_NOTICE_LOCAL_STORAGE_PATH, JSON.stringify({})) |
| 36 | + } |
| 37 | + try { |
| 38 | + const localCountStorage: IRemoteNoticeLocalCountStorage = fs.readJSONSync(REMOTE_NOTICE_LOCAL_STORAGE_PATH, 'utf8') |
| 39 | + this.remoteNoticeLocalCountStorage = localCountStorage |
| 40 | + } catch (e) { |
| 41 | + console.log(e) |
| 42 | + this.remoteNoticeLocalCountStorage = localCountStorage |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private saveLocalCountStorage (newData?: IRemoteNoticeLocalCountStorage) { |
| 47 | + if (newData) { |
| 48 | + this.remoteNoticeLocalCountStorage = newData |
| 49 | + } |
| 50 | + fs.writeFileSync(REMOTE_NOTICE_LOCAL_STORAGE_PATH, JSON.stringify(this.remoteNoticeLocalCountStorage)) |
| 51 | + } |
| 52 | + |
| 53 | + private async getRemoteNoticeInfo (): Promise<IRemoteNotice | null> { |
| 54 | + try { |
| 55 | + const noticeInfo = await axios({ |
| 56 | + method: 'get', |
| 57 | + url: REMOTE_NOTICE_URL, |
| 58 | + responseType: 'json' |
| 59 | + }).then(res => res.data) as IRemoteNotice |
| 60 | + return noticeInfo |
| 61 | + } catch { |
| 62 | + return null |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * if the notice is not shown or is always shown, then show the notice |
| 68 | + * @param action |
| 69 | + */ |
| 70 | + private checkActionCount (action: IRemoteNoticeAction) { |
| 71 | + try { |
| 72 | + if (!this.remoteNoticeLocalCountStorage) { |
| 73 | + return true |
| 74 | + } |
| 75 | + const actionCount = this.remoteNoticeLocalCountStorage[action.id] |
| 76 | + if (actionCount === undefined) { |
| 77 | + if (action.triggerCount === IRemoteNoticeTriggerCount.ALWAYS) { |
| 78 | + this.remoteNoticeLocalCountStorage[action.id] = 1 // if always, count number |
| 79 | + } else { |
| 80 | + this.remoteNoticeLocalCountStorage[action.id] = true |
| 81 | + } |
| 82 | + return true |
| 83 | + } else { |
| 84 | + // here is the count of action |
| 85 | + // if not always show, then can't show |
| 86 | + if (action.triggerCount !== IRemoteNoticeTriggerCount.ALWAYS) { |
| 87 | + return false |
| 88 | + } else { |
| 89 | + const preCount = this.remoteNoticeLocalCountStorage[action.id] |
| 90 | + if (typeof preCount !== 'number') { |
| 91 | + this.remoteNoticeLocalCountStorage[action.id] = true |
| 92 | + return true |
| 93 | + } else { |
| 94 | + this.remoteNoticeLocalCountStorage[action.id] = preCount + 1 |
| 95 | + } |
| 96 | + return true |
| 97 | + } |
| 98 | + } |
| 99 | + } finally { |
| 100 | + this.saveLocalCountStorage() |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private async doActions (actions: IRemoteNoticeAction[]) { |
| 105 | + for (const action of actions) { |
| 106 | + if (this.checkActionCount(action)) { |
| 107 | + switch (action.type) { |
| 108 | + case IRemoteNoticeActionType.SHOW_DIALOG: { |
| 109 | + // SHOW DIALOG |
| 110 | + const currentWindow = windowManager.getAvailableWindow() |
| 111 | + dialog.showOpenDialog(currentWindow, action.data?.options) |
| 112 | + break |
| 113 | + } |
| 114 | + case IRemoteNoticeActionType.SHOW_NOTICE: |
| 115 | + showNotification({ |
| 116 | + title: action.data?.title || '', |
| 117 | + body: action.data?.content || '', |
| 118 | + clickToCopy: !!action.data?.copyToClipboard, |
| 119 | + copyContent: action.data?.copyToClipboard || '', |
| 120 | + clickFn () { |
| 121 | + if (action.data?.url) { |
| 122 | + shell.openExternal(action.data.url) |
| 123 | + } |
| 124 | + } |
| 125 | + }) |
| 126 | + break |
| 127 | + case IRemoteNoticeActionType.OPEN_URL: |
| 128 | + // OPEN URL |
| 129 | + shell.openExternal(action.data?.url || '') |
| 130 | + break |
| 131 | + case IRemoteNoticeActionType.COMMON: |
| 132 | + // DO COMMON CASE |
| 133 | + if (action.data?.copyToClipboard) { |
| 134 | + clipboard.writeText(action.data.copyToClipboard) |
| 135 | + } |
| 136 | + if (action.data?.url) { |
| 137 | + shell.openExternal(action.data.url) |
| 138 | + } |
| 139 | + break |
| 140 | + case IRemoteNoticeActionType.SHOW_MESSAGE_BOX: { |
| 141 | + const currentWindow = windowManager.getAvailableWindow() |
| 142 | + dialog.showMessageBox(currentWindow, { |
| 143 | + title: action.data?.title || '', |
| 144 | + message: action.data?.content || '', |
| 145 | + type: 'info', |
| 146 | + buttons: action.data?.buttons?.map(item => item.label) || ['Yes'] |
| 147 | + }).then(res => { |
| 148 | + const button = action.data?.buttons?.[res.response] |
| 149 | + if (button?.type === 'cancel') { |
| 150 | + // do nothing |
| 151 | + } else { |
| 152 | + if (button?.action) { |
| 153 | + this.doActions([button?.action]) |
| 154 | + } |
| 155 | + } |
| 156 | + }) |
| 157 | + break |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + triggerHook (hook: IRemoteNoticeTriggerHook) { |
| 165 | + if (!this.remoteNotice || !this.remoteNotice.list) { |
| 166 | + return |
| 167 | + } |
| 168 | + const actions = this.remoteNotice.list |
| 169 | + .filter(item => { |
| 170 | + if (item.versionMatch) { |
| 171 | + switch (item.versionMatch) { |
| 172 | + case 'exact': |
| 173 | + return item.versions.includes(app.getVersion()) |
| 174 | + case 'gte': |
| 175 | + return item.versions.some(version => { |
| 176 | + // appVersion >= version |
| 177 | + return gte(app.getVersion(), version) |
| 178 | + }) |
| 179 | + case 'lte': |
| 180 | + return item.versions.some(version => { |
| 181 | + // appVersion <= version |
| 182 | + return lte(app.getVersion(), version) |
| 183 | + }) |
| 184 | + } |
| 185 | + } |
| 186 | + return item.versions.includes(app.getVersion()) |
| 187 | + }) |
| 188 | + .map(item => item.actions) |
| 189 | + .reduce((pre, cur) => pre.concat(cur), []) |
| 190 | + .filter(item => item.hooks.includes(hook)) |
| 191 | + this.doActions(actions) |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +const remoteNoticeHandler = new RemoteNoticeHandler() |
| 196 | + |
| 197 | +export { |
| 198 | + remoteNoticeHandler |
| 199 | +} |
0 commit comments