refs #574 Save marker in local db every time receive a new status in notifications

This commit is contained in:
AkiraFukushima 2021-06-10 15:27:04 +09:00
parent ba1574116f
commit 0e354c474d
No known key found for this signature in database
GPG Key ID: B6E51BAC4DE1A957
7 changed files with 40 additions and 18 deletions

View File

@ -147,6 +147,7 @@ const markerDB = new Datastore({
filename: markerDBPath, filename: markerDBPath,
autoload: true autoload: true
}) })
const markerRepo = new Marker(markerDB)
/** /**
* Cache path * Cache path
@ -1147,8 +1148,7 @@ ipcMain.handle('update-spellchecker-languages', async (_: IpcMainInvokeEvent, la
// marker // marker
ipcMain.handle('save-marker', async (_: IpcMainInvokeEvent, marker: LocalMarker) => { ipcMain.handle('save-marker', async (_: IpcMainInvokeEvent, marker: LocalMarker) => {
const repo = new Marker(markerDB) await markerRepo.save(marker)
await repo.save(marker)
}) })
// hashtag // hashtag

View File

@ -25,39 +25,39 @@ export default class Marker {
// eslint-disable-line no-unused-vars // eslint-disable-line no-unused-vars
this.db.update( this.db.update(
{ {
acct: marker.acct, owner_id: marker.owner_id,
timeline: marker.timeline timeline: marker.timeline
}, },
{ $set: marker }, { $set: marker },
{ multi: false }, { multi: false },
err => { err => {
if (err) return reject(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<LocalMarker> { public async save(marker: LocalMarker): Promise<LocalMarker> {
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) if (isEmpty(l)) return this.insert(marker)
return this.update(marker) return this.update(marker)
}) })
} }
public async get(acct: string, timeline: 'home' | 'notifications'): Promise<LocalMarker> { public async get(owner_id: string, timeline: 'home' | 'notifications'): Promise<LocalMarker> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.db.findOne<LocalMarker>({ acct: acct, timeline: timeline }, (err, doc) => { this.db.findOne<LocalMarker>({ owner_id: owner_id, timeline: timeline }, (err, doc) => {
if (err) return reject(err) if (err) return reject(err)
resolve(doc) resolve(doc)
}) })
}) })
} }
public async list(acct: string): Promise<Array<LocalMarker>> { public async list(owner_id: string): Promise<Array<LocalMarker>> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.db this.db
.find<LocalMarker>({ acct: acct }) .find<LocalMarker>({ owner_id: owner_id })
.exec((err, docs) => { .exec((err, docs) => {
if (err) return reject(err) if (err) return reject(err)
resolve(docs) resolve(docs)

View File

@ -94,6 +94,10 @@ export default {
this.focusedId = previousFocusedId this.focusedId = previousFocusedId
}) })
}) })
if (this.heading && this.timeline.length > 0) {
this.$store.dispatch('TimelineSpace/Contents/Home/saveMarker', this.timeline[0].id)
}
}, },
beforeUpdate() { beforeUpdate() {
if (this.$store.state.TimelineSpace.SideMenu.unreadHomeTimeline && this.heading) { if (this.$store.state.TimelineSpace.SideMenu.unreadHomeTimeline && this.heading) {

View File

@ -47,10 +47,12 @@ export default {
...mapState({ ...mapState({
openSideBar: state => state.TimelineSpace.Contents.SideBar.openSideBar, openSideBar: state => state.TimelineSpace.Contents.SideBar.openSideBar,
startReload: state => state.TimelineSpace.HeaderMenu.reload, startReload: state => state.TimelineSpace.HeaderMenu.reload,
backgroundColor: state => state.App.theme.background_color, backgroundColor: state => state.App.theme.background_color
lazyLoading: state => state.TimelineSpace.Contents.Notifications.lazyLoading, }),
heading: state => state.TimelineSpace.Contents.Notifications.heading, ...mapState('TimelineSpace/Contents/Notifications', {
unread: state => state.TimelineSpace.Contents.Notifications.unreadNotifications lazyLoading: state => state.lazyLoading,
heading: state => state.heading,
unread: state => state.unreadNotifications
}), }),
...mapGetters('TimelineSpace/Contents/Notifications', ['handledNotifications', 'filters']), ...mapGetters('TimelineSpace/Contents/Notifications', ['handledNotifications', 'filters']),
...mapGetters('TimelineSpace/Modals', ['modalOpened']), ...mapGetters('TimelineSpace/Modals', ['modalOpened']),
@ -79,6 +81,10 @@ export default {
this.focusedId = previousFocusedId this.focusedId = previousFocusedId
}) })
}) })
if (this.heading && this.handledNotifications.length > 0) {
this.$store.dispatch('TimelineSpace/Contents/Notifications/saveMarker', this.handledNotifications[0].id)
}
}, },
beforeUpdate() { beforeUpdate() {
if (this.$store.state.TimelineSpace.SideMenu.unreadNotifications) { if (this.$store.state.TimelineSpace.SideMenu.unreadNotifications) {
@ -113,6 +119,11 @@ export default {
this.$store.commit('TimelineSpace/Contents/Notifications/mergeNotifications') this.$store.commit('TimelineSpace/Contents/Notifications/mergeNotifications')
this.$store.dispatch('TimelineSpace/Contents/Notifications/resetBadge') 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: { methods: {

View File

@ -141,11 +141,10 @@ const actions: ActionTree<HomeState, RootState> = {
}) })
}, },
saveMarker: async ({ rootState }, id: string) => { saveMarker: async ({ rootState }, id: string) => {
const acct = `@${rootState.TimelineSpace.account.username}@${rootState.TimelineSpace.account.domain}`
await win.ipcRenderer.invoke('save-marker', { await win.ipcRenderer.invoke('save-marker', {
acct: acct, owner_id: rootState.TimelineSpace.account._id,
timeline: 'home', timeline: 'home',
lastReadID: id last_read_id: id
} as LocalMarker) } as LocalMarker)
} }
} }

View File

@ -1,6 +1,7 @@
import generator, { Entity, FilterContext, NotificationType } from 'megalodon' import generator, { Entity, FilterContext, NotificationType } from 'megalodon'
import { Module, MutationTree, ActionTree, GetterTree } from 'vuex' import { Module, MutationTree, ActionTree, GetterTree } from 'vuex'
import { RootState } from '@/store' import { RootState } from '@/store'
import { LocalMarker } from '~/src/types/localMarker'
import { MyWindow } from '~/src/types/global' import { MyWindow } from '~/src/types/global'
const win = (window as any) as MyWindow const win = (window as any) as MyWindow
@ -135,6 +136,13 @@ const actions: ActionTree<NotificationsState, RootState> = {
}, },
resetBadge: () => { resetBadge: () => {
win.ipcRenderer.send('reset-badge') 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)
} }
} }

View File

@ -1,5 +1,5 @@
export type LocalMarker = { export type LocalMarker = {
acct: string owner_id: string
timeline: 'home' | 'notifications' timeline: 'home' | 'notifications'
lastReadID: string last_read_id: string
} }