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

This commit is contained in:
AkiraFukushima 2021-06-10 12:03:51 +09:00
parent 1c250f7008
commit ba1574116f
No known key found for this signature in database
GPG Key ID: B6E51BAC4DE1A957
5 changed files with 103 additions and 0 deletions

View File

@ -54,6 +54,8 @@ import ProxyConfiguration from './proxy'
import confirm from './timelines'
import { EnabledTimelines } from '~/src/types/enabledTimelines'
import { Menu as MenuPreferences } from '~/src/types/preference'
import { LocalMarker } from '~/src/types/localMarker'
import Marker from './marker'
/**
* Context menu
@ -140,6 +142,12 @@ unreadNotification.initialize().catch((err: Error) => log.error(err))
const preferencesDBPath = process.env.NODE_ENV === 'production' ? userData + './db/preferences.json' : 'preferences.json'
const markerDBPath = process.env.NODE_ENV === 'production' ? userData + '/db/marker.db' : 'marker.db'
const markerDB = new Datastore({
filename: markerDBPath,
autoload: true
})
/**
* Cache path
*/
@ -1137,6 +1145,12 @@ ipcMain.handle('update-spellchecker-languages', async (_: IpcMainInvokeEvent, la
return conf.language.spellchecker.languages
})
// marker
ipcMain.handle('save-marker', async (_: IpcMainInvokeEvent, marker: LocalMarker) => {
const repo = new Marker(markerDB)
await repo.save(marker)
})
// hashtag
ipcMain.handle('save-hashtag', async (_: IpcMainInvokeEvent, tag: string) => {
const hashtags = new Hashtags(hashtagsDB)

67
src/main/marker.ts Normal file
View File

@ -0,0 +1,67 @@
import { isEmpty } from 'lodash'
import Datastore from 'nedb'
import { LocalMarker } from '~/src/types/localMarker'
export default class Marker {
private db: Datastore
constructor(db: Datastore) {
this.db = db
this.db.persistence.setAutocompactionInterval(60000) // milliseconds
}
private insert(marker: LocalMarker): Promise<LocalMarker> {
return new Promise((resolve, reject) => {
this.db.insert(marker, (err, doc) => {
if (err) return reject(err)
resolve(doc)
})
})
}
private update(marker: LocalMarker): Promise<LocalMarker> {
// @ts-ignore
return new Promise((resolve, reject) => {
// eslint-disable-line no-unused-vars
this.db.update(
{
acct: marker.acct,
timeline: marker.timeline
},
{ $set: marker },
{ multi: false },
err => {
if (err) return reject(err)
return this.get(marker.acct, marker.timeline)
}
)
})
}
public async save(marker: LocalMarker): Promise<LocalMarker> {
return this.get(marker.acct, marker.timeline).then(l => {
if (isEmpty(l)) return this.insert(marker)
return this.update(marker)
})
}
public async get(acct: string, timeline: 'home' | 'notifications'): Promise<LocalMarker> {
return new Promise((resolve, reject) => {
this.db.findOne<LocalMarker>({ acct: acct, timeline: timeline }, (err, doc) => {
if (err) return reject(err)
resolve(doc)
})
})
}
public async list(acct: string): Promise<Array<LocalMarker>> {
return new Promise((resolve, reject) => {
this.db
.find<LocalMarker>({ acct: acct })
.exec((err, docs) => {
if (err) return reject(err)
resolve(docs)
})
})
}
}

View File

@ -127,6 +127,11 @@ export default {
this.$store.commit('TimelineSpace/Contents/Home/changeHeading', true)
this.$store.commit('TimelineSpace/Contents/Home/mergeTimeline')
}
},
timeline: function (newState, _oldState) {
if (this.heading && newState.length > 0) {
this.$store.dispatch('TimelineSpace/Contents/Home/saveMarker', newState[0].id)
}
}
},
methods: {

View File

@ -1,6 +1,10 @@
import generator, { Entity, FilterContext } 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
export type HomeState = {
lazyLoading: boolean
@ -135,6 +139,14 @@ const actions: ActionTree<HomeState, RootState> = {
.finally(() => {
commit(MUTATION_TYPES.CHANGE_LAZY_LOADING, false)
})
},
saveMarker: async ({ rootState }, id: string) => {
const acct = `@${rootState.TimelineSpace.account.username}@${rootState.TimelineSpace.account.domain}`
await win.ipcRenderer.invoke('save-marker', {
acct: acct,
timeline: 'home',
lastReadID: id
} as LocalMarker)
}
}

5
src/types/localMarker.ts Normal file
View File

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