|
| 1 | +import { LocalStorage } from 'quasar' |
| 2 | +import BrowserApi from 'src/app/BrowserApi' |
| 3 | +import { GITHUB_AUTO_SYNC, GITHUB_AUTO_SYNC_LASTUPDATE } from 'src/boot/constants' |
| 4 | +import { ExecutionResult } from 'src/core/domain/ExecutionResult' |
| 5 | +import { Notebook } from 'src/notes/models/Notebook' |
| 6 | +import { useNotesStore } from 'src/notes/stores/NotesStore' |
| 7 | +import { Space } from 'src/spaces/models/Space' |
| 8 | +import { useSpacesStore } from 'src/spaces/stores/spacesStore' |
| 9 | +import { GithubCommands } from 'src/tabsets/commands/github/GithubCommands' |
| 10 | +import { |
| 11 | + NoteEvent, |
| 12 | + SpaceEvent, |
| 13 | + TabEvent, |
| 14 | + TabsetEvent, |
| 15 | + TabsetEventType, |
| 16 | +} from 'src/tabsets/commands/github/GithubWriteEventCommand' |
| 17 | +import { Tab } from 'src/tabsets/models/Tab' |
| 18 | +import { Tabset } from 'src/tabsets/models/Tabset' |
| 19 | +import { useTabsetsStore } from 'src/tabsets/stores/tabsetsStore' |
| 20 | +import { useUiStore } from 'src/ui/stores/uiStore' |
| 21 | + |
| 22 | +export class GithubReadEventsCommand extends GithubCommands<string> { |
| 23 | + constructor(public lastUpdate: number) { |
| 24 | + super() |
| 25 | + } |
| 26 | + |
| 27 | + async execute(): Promise<ExecutionResult<string>> { |
| 28 | + const githubPath = 'events' |
| 29 | + //console.log('useData', useData, this.event) |
| 30 | + if (!LocalStorage.getItem(GITHUB_AUTO_SYNC)) { |
| 31 | + return Promise.resolve(new ExecutionResult('done', 'not active')) |
| 32 | + } |
| 33 | + try { |
| 34 | + const events = await this.githubGetContentRequest(githubPath) |
| 35 | + //console.log('events', events) |
| 36 | + // if (events.status == 404) { |
| 37 | + // await this.githubPutContentRequest(githubPath, useData) |
| 38 | + |
| 39 | + if (events.ok) { |
| 40 | + let firstErrorOccuredAt: number | undefined = undefined |
| 41 | + let lastSuccessOccuredAt: number | undefined = undefined |
| 42 | + |
| 43 | + const existing = await events.json() |
| 44 | + const decodedContent = decodeURIComponent(escape(window.atob(existing.content))) |
| 45 | + // console.log('got existing', decodedContent) |
| 46 | + let index = 0 |
| 47 | + const lines = decodedContent.split('\n') |
| 48 | + for (const line of lines) { |
| 49 | + if (line.trim().length === 0) { |
| 50 | + continue |
| 51 | + } |
| 52 | + const parts = line.split('|') |
| 53 | + let timestampLine = 0 |
| 54 | + try { |
| 55 | + timestampLine = Number.parseInt(parts[0]!) |
| 56 | + if (timestampLine < this.lastUpdate) { |
| 57 | + continue |
| 58 | + } |
| 59 | + |
| 60 | + const progress = Math.round((100 * index) / lines.length) |
| 61 | + useUiStore().setProgress(progress / 100, `syncing... ${progress}%`) |
| 62 | + |
| 63 | + if (parts.length < 5) { |
| 64 | + console.warn(`found incomplete line ${line}"`) |
| 65 | + continue |
| 66 | + } |
| 67 | + |
| 68 | + switch (parts[1]) { |
| 69 | + case 'tabset': |
| 70 | + if (parts[3] && parts[5]) { |
| 71 | + // 1743063915313|tabset|added|0339a468-4df1-4e00-94ba-496171cf8d88||Mails |
| 72 | + const tabsetEvent = new TabsetEvent( |
| 73 | + parts[2] as TabsetEventType, |
| 74 | + parts[3], |
| 75 | + parts[4], |
| 76 | + parts[5]!, |
| 77 | + parts[6] ? parts[6].split(',') : [], |
| 78 | + ) |
| 79 | + if (tabsetEvent.event === 'added') { |
| 80 | + if (tabsetEvent.parentId) { |
| 81 | + // console.log('processing line', line) |
| 82 | + const folderChain = useTabsetsStore().getFolderChain(tabsetEvent.parentId) |
| 83 | + // console.log('folderChain', folderChain) |
| 84 | + if (folderChain.length > 0) { |
| 85 | + const rootTabset = useTabsetsStore().getTabset(folderChain[folderChain.length - 1]!) |
| 86 | + //console.log('found root tabset', rootTabset) |
| 87 | + const tabsetOrFolder = useTabsetsStore().getActiveFolder(rootTabset!, tabsetEvent.parentId) |
| 88 | + //console.log('tabsetOrfolder', tabsetOrFolder) |
| 89 | + if (tabsetOrFolder) { |
| 90 | + const newTabset = new Tabset( |
| 91 | + tabsetEvent.tabsetId, |
| 92 | + tabsetEvent.name, |
| 93 | + [], |
| 94 | + [], |
| 95 | + tabsetEvent.spaces, |
| 96 | + ) |
| 97 | + tabsetOrFolder.folders.push(newTabset) |
| 98 | + await useTabsetsStore().saveTabset(rootTabset!) |
| 99 | + } |
| 100 | + } |
| 101 | + } else { |
| 102 | + console.log('processing line II', line) |
| 103 | + const newTabset = new Tabset(tabsetEvent.tabsetId, tabsetEvent.name, [], [], tabsetEvent.spaces) |
| 104 | + await useTabsetsStore().addTabset(newTabset) |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + break |
| 109 | + case 'tab': |
| 110 | + if (parts[3] && parts[6]) { |
| 111 | + const tabsetEvent = new TabEvent(parts[2] as TabsetEventType, parts[3], parts[4], parts[5]!, parts[6]) |
| 112 | + const ts = useTabsetsStore().getTabset(tabsetEvent.tabsetId) |
| 113 | + if (!ts) { |
| 114 | + console.log('could not find tabset for id', tabsetEvent.tabsetId) |
| 115 | + break |
| 116 | + } |
| 117 | + if (tabsetEvent.event === 'added') { |
| 118 | + ts.tabs.push( |
| 119 | + new Tab(tabsetEvent.tabId!, BrowserApi.createChromeTabObject(tabsetEvent.name, tabsetEvent.url!)), |
| 120 | + ) |
| 121 | + await useTabsetsStore().saveTabset(ts) |
| 122 | + } |
| 123 | + } |
| 124 | + break |
| 125 | + case 'space': |
| 126 | + if (parts[3]) { |
| 127 | + const spaceEvent = new SpaceEvent(parts[2] as TabsetEventType, parts[3]!, undefined, parts[5]!) |
| 128 | + if (spaceEvent.event === 'added') { |
| 129 | + // useTabsetsStore().addTabset(new Tabset(tabsetEvent.tabsetId!, tabsetEvent.name, [])) |
| 130 | + const space = new Space(spaceEvent.spaceId, spaceEvent.name) |
| 131 | + console.log('space', space) |
| 132 | + await useSpacesStore().addSpace(space) |
| 133 | + } |
| 134 | + } |
| 135 | + break |
| 136 | + case 'note': |
| 137 | + // console.log('processing line', line) |
| 138 | + if (parts[3]) { |
| 139 | + const decoded = atob(parts[5]!) |
| 140 | + //console.log('decoded', decoded) |
| 141 | + const noteEvent = new NoteEvent( |
| 142 | + parts[2] as TabsetEventType, |
| 143 | + parts[3]!, |
| 144 | + parts[4]!, |
| 145 | + JSON.parse(decoded), |
| 146 | + ) |
| 147 | + if (noteEvent.event === 'added') { |
| 148 | + //console.log('noteEvent', noteEvent) |
| 149 | + // const notebook = new Notebook(uid(), noteEvent.tabsetId, NotebookType.TABSET, noteEvent.name) |
| 150 | + // console.log('notebook', notebook) |
| 151 | + await useNotesStore().saveNotebook(noteEvent.content as unknown as Notebook) |
| 152 | + } |
| 153 | + } |
| 154 | + break |
| 155 | + default: |
| 156 | + console.log(`unknown part '${parts[1]}' in '${line}'`) |
| 157 | + } |
| 158 | + } catch (err: any) { |
| 159 | + if (!firstErrorOccuredAt) { |
| 160 | + firstErrorOccuredAt = timestampLine - 1 |
| 161 | + } |
| 162 | + console.warn('issue with sync: ', err) |
| 163 | + } |
| 164 | + lastSuccessOccuredAt = timestampLine |
| 165 | + index++ |
| 166 | + } |
| 167 | + |
| 168 | + // const sha = existing['sha' as keyof object] |
| 169 | + // await this.githubPutContentRequest(githubPath, decodedContent + useData, sha) |
| 170 | + console.log( |
| 171 | + `sync finished with firstErrorOccuredAt ${firstErrorOccuredAt}, lastSuccessOccuredAt ${lastSuccessOccuredAt}`, |
| 172 | + ) |
| 173 | + if (firstErrorOccuredAt) { |
| 174 | + LocalStorage.setItem(GITHUB_AUTO_SYNC_LASTUPDATE, firstErrorOccuredAt) |
| 175 | + } else if (lastSuccessOccuredAt) { |
| 176 | + LocalStorage.setItem(GITHUB_AUTO_SYNC_LASTUPDATE, lastSuccessOccuredAt) |
| 177 | + } |
| 178 | + } |
| 179 | + useUiStore().stopProgress() |
| 180 | + |
| 181 | + // const result = await this.githubPutContentRequest('logs/' + today, useData) |
| 182 | + // console.log('result', result) |
| 183 | + return Promise.resolve(new ExecutionResult('', 'done')) |
| 184 | + } catch (error) { |
| 185 | + return Promise.reject(error) |
| 186 | + } |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +GithubReadEventsCommand.prototype.toString = function cmdToString() { |
| 191 | + return `GithubReadEventsCommand: {timestamp: ${this.lastUpdate}` |
| 192 | +} |
0 commit comments