From 0e354c474df4cecbd81dff92c03c5af3e69dbf96 Mon Sep 17 00:00:00 2001 From: AkiraFukushima Date: Thu, 10 Jun 2021 15:27:04 +0900 Subject: [PATCH] refs #574 Save marker in local db every time receive a new status in notifications --- src/main/index.ts | 4 ++-- src/main/marker.ts | 14 +++++++------- .../TimelineSpace/Contents/Home.vue | 4 ++++ .../TimelineSpace/Contents/Notifications.vue | 19 +++++++++++++++---- .../store/TimelineSpace/Contents/Home.ts | 5 ++--- .../TimelineSpace/Contents/Notifications.ts | 8 ++++++++ src/types/localMarker.ts | 4 ++-- 7 files changed, 40 insertions(+), 18 deletions(-) diff --git a/src/main/index.ts b/src/main/index.ts index a7f8e86d..0dc0220e 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -147,6 +147,7 @@ const markerDB = new Datastore({ filename: markerDBPath, autoload: true }) +const markerRepo = new Marker(markerDB) /** * Cache path @@ -1147,8 +1148,7 @@ ipcMain.handle('update-spellchecker-languages', async (_: IpcMainInvokeEvent, la // marker ipcMain.handle('save-marker', async (_: IpcMainInvokeEvent, marker: LocalMarker) => { - const repo = new Marker(markerDB) - await repo.save(marker) + await markerRepo.save(marker) }) // hashtag diff --git a/src/main/marker.ts b/src/main/marker.ts index a000a484..700c125e 100644 --- a/src/main/marker.ts +++ b/src/main/marker.ts @@ -25,39 +25,39 @@ export default class Marker { // eslint-disable-line no-unused-vars this.db.update( { - acct: marker.acct, + owner_id: marker.owner_id, timeline: marker.timeline }, { $set: marker }, { multi: false }, err => { if (err) return reject(err) - return this.get(marker.acct, marker.timeline) + return this.get(marker.owner_id, marker.timeline) } ) }) } public async save(marker: LocalMarker): Promise { - return this.get(marker.acct, marker.timeline).then(l => { + return this.get(marker.owner_id, marker.timeline).then(l => { if (isEmpty(l)) return this.insert(marker) return this.update(marker) }) } - public async get(acct: string, timeline: 'home' | 'notifications'): Promise { + public async get(owner_id: string, timeline: 'home' | 'notifications'): Promise { return new Promise((resolve, reject) => { - this.db.findOne({ acct: acct, timeline: timeline }, (err, doc) => { + this.db.findOne({ owner_id: owner_id, timeline: timeline }, (err, doc) => { if (err) return reject(err) resolve(doc) }) }) } - public async list(acct: string): Promise> { + public async list(owner_id: string): Promise> { return new Promise((resolve, reject) => { this.db - .find({ acct: acct }) + .find({ owner_id: owner_id }) .exec((err, docs) => { if (err) return reject(err) resolve(docs) diff --git a/src/renderer/components/TimelineSpace/Contents/Home.vue b/src/renderer/components/TimelineSpace/Contents/Home.vue index b463bad1..d9b598df 100644 --- a/src/renderer/components/TimelineSpace/Contents/Home.vue +++ b/src/renderer/components/TimelineSpace/Contents/Home.vue @@ -94,6 +94,10 @@ export default { this.focusedId = previousFocusedId }) }) + + if (this.heading && this.timeline.length > 0) { + this.$store.dispatch('TimelineSpace/Contents/Home/saveMarker', this.timeline[0].id) + } }, beforeUpdate() { if (this.$store.state.TimelineSpace.SideMenu.unreadHomeTimeline && this.heading) { diff --git a/src/renderer/components/TimelineSpace/Contents/Notifications.vue b/src/renderer/components/TimelineSpace/Contents/Notifications.vue index 2e68fd56..b15fde97 100644 --- a/src/renderer/components/TimelineSpace/Contents/Notifications.vue +++ b/src/renderer/components/TimelineSpace/Contents/Notifications.vue @@ -47,10 +47,12 @@ export default { ...mapState({ openSideBar: state => state.TimelineSpace.Contents.SideBar.openSideBar, startReload: state => state.TimelineSpace.HeaderMenu.reload, - backgroundColor: state => state.App.theme.background_color, - lazyLoading: state => state.TimelineSpace.Contents.Notifications.lazyLoading, - heading: state => state.TimelineSpace.Contents.Notifications.heading, - unread: state => state.TimelineSpace.Contents.Notifications.unreadNotifications + backgroundColor: state => state.App.theme.background_color + }), + ...mapState('TimelineSpace/Contents/Notifications', { + lazyLoading: state => state.lazyLoading, + heading: state => state.heading, + unread: state => state.unreadNotifications }), ...mapGetters('TimelineSpace/Contents/Notifications', ['handledNotifications', 'filters']), ...mapGetters('TimelineSpace/Modals', ['modalOpened']), @@ -79,6 +81,10 @@ export default { this.focusedId = previousFocusedId }) }) + + if (this.heading && this.handledNotifications.length > 0) { + this.$store.dispatch('TimelineSpace/Contents/Notifications/saveMarker', this.handledNotifications[0].id) + } }, beforeUpdate() { if (this.$store.state.TimelineSpace.SideMenu.unreadNotifications) { @@ -113,6 +119,11 @@ export default { this.$store.commit('TimelineSpace/Contents/Notifications/mergeNotifications') this.$store.dispatch('TimelineSpace/Contents/Notifications/resetBadge') } + }, + handledNotifications: function (newState, _oldState) { + if (this.heading && newState.length > 0) { + this.$store.dispatch('TimelineSpace/Contents/Notifications/saveMarker', newState[0].id) + } } }, methods: { diff --git a/src/renderer/store/TimelineSpace/Contents/Home.ts b/src/renderer/store/TimelineSpace/Contents/Home.ts index 56659587..8b5aebaa 100644 --- a/src/renderer/store/TimelineSpace/Contents/Home.ts +++ b/src/renderer/store/TimelineSpace/Contents/Home.ts @@ -141,11 +141,10 @@ const actions: ActionTree = { }) }, saveMarker: async ({ rootState }, id: string) => { - const acct = `@${rootState.TimelineSpace.account.username}@${rootState.TimelineSpace.account.domain}` await win.ipcRenderer.invoke('save-marker', { - acct: acct, + owner_id: rootState.TimelineSpace.account._id, timeline: 'home', - lastReadID: id + last_read_id: id } as LocalMarker) } } diff --git a/src/renderer/store/TimelineSpace/Contents/Notifications.ts b/src/renderer/store/TimelineSpace/Contents/Notifications.ts index 1b7fe833..4dfd1562 100644 --- a/src/renderer/store/TimelineSpace/Contents/Notifications.ts +++ b/src/renderer/store/TimelineSpace/Contents/Notifications.ts @@ -1,6 +1,7 @@ import generator, { Entity, FilterContext, NotificationType } from 'megalodon' import { Module, MutationTree, ActionTree, GetterTree } from 'vuex' import { RootState } from '@/store' +import { LocalMarker } from '~/src/types/localMarker' import { MyWindow } from '~/src/types/global' const win = (window as any) as MyWindow @@ -135,6 +136,13 @@ const actions: ActionTree = { }, resetBadge: () => { win.ipcRenderer.send('reset-badge') + }, + saveMarker: async ({ rootState }, id: string) => { + await win.ipcRenderer.invoke('save-marker', { + owner_id: rootState.TimelineSpace.account._id, + timeline: 'notifications', + last_read_id: id + } as LocalMarker) } } diff --git a/src/types/localMarker.ts b/src/types/localMarker.ts index 95712762..c32e5330 100644 --- a/src/types/localMarker.ts +++ b/src/types/localMarker.ts @@ -1,5 +1,5 @@ export type LocalMarker = { - acct: string + owner_id: string timeline: 'home' | 'notifications' - lastReadID: string + last_read_id: string }