refs #2500 Remove local marker
This commit is contained in:
parent
4c81b5d95e
commit
5098c4ee67
|
@ -26,7 +26,7 @@ import path from 'path'
|
|||
import ContextMenu from 'electron-context-menu'
|
||||
import { initSplashScreen, Config } from '@trodi/electron-splashscreen'
|
||||
import openAboutWindow from 'about-window'
|
||||
import generator, { Entity, detector, NotificationType, OAuth, MegalodonInterface } from 'megalodon'
|
||||
import generator, { Entity, detector, NotificationType, OAuth } from 'megalodon'
|
||||
import sanitizeHtml from 'sanitize-html'
|
||||
import AutoLaunch from 'auto-launch'
|
||||
import minimist from 'minimist'
|
||||
|
@ -50,8 +50,6 @@ import { Proxy } from '~/src/types/proxy'
|
|||
import ProxyConfiguration from './proxy'
|
||||
import { Menu as MenuPreferences } from '~/src/types/preference'
|
||||
import { General as GeneralPreferences } from '~/src/types/preference'
|
||||
import { LocalMarker } from '~/src/types/localMarker'
|
||||
import Marker from './marker'
|
||||
import newDB from './database'
|
||||
import Settings from './settings'
|
||||
import { BaseSettings, Setting } from '~/src/types/setting'
|
||||
|
@ -128,8 +126,6 @@ const db = newDB(databasePath)
|
|||
|
||||
const preferencesDBPath = process.env.NODE_ENV === 'production' ? userData + './db/preferences.json' : 'preferences.json'
|
||||
|
||||
let markerRepo: Marker | null = null
|
||||
|
||||
const hashtagsDBPath = process.env.NODE_ENV === 'production' ? userData + '/db/hashtags.db' : 'hashtags.db'
|
||||
const hashtagsDB = new Datastore({
|
||||
filename: hashtagsDBPath,
|
||||
|
@ -1139,45 +1135,6 @@ ipcMain.handle('update-spellchecker-languages', async (_: IpcMainInvokeEvent, la
|
|||
return conf.language.spellchecker.languages
|
||||
})
|
||||
|
||||
// marker
|
||||
ipcMain.handle('get-home-marker', async (_: IpcMainInvokeEvent, ownerID: string) => {
|
||||
if (markerRepo === null) {
|
||||
return null
|
||||
}
|
||||
const marker = await markerRepo.get(ownerID, 'home')
|
||||
return marker
|
||||
})
|
||||
|
||||
ipcMain.handle('get-notifications-marker', async (_: IpcMainInvokeEvent, ownerID: string) => {
|
||||
if (markerRepo === null) {
|
||||
return null
|
||||
}
|
||||
const marker = await markerRepo.get(ownerID, 'notifications')
|
||||
return marker
|
||||
})
|
||||
|
||||
ipcMain.handle('get-mentions-marker', async (_: IpcMainInvokeEvent, ownerID: string) => {
|
||||
if (markerRepo === null) {
|
||||
return null
|
||||
}
|
||||
const marker = await markerRepo.get(ownerID, 'mentions')
|
||||
return marker
|
||||
})
|
||||
|
||||
ipcMain.on(
|
||||
'save-marker',
|
||||
async (_: IpcMainEvent, marker: LocalMarker): Promise<LocalMarker | null> => {
|
||||
if (marker.owner_id === null || marker.owner_id === undefined || marker.owner_id === '') {
|
||||
return null
|
||||
}
|
||||
if (markerRepo === null) {
|
||||
return null
|
||||
}
|
||||
const res = await markerRepo.save(marker)
|
||||
return res
|
||||
}
|
||||
)
|
||||
|
||||
// hashtag
|
||||
ipcMain.handle('save-hashtag', async (_: IpcMainInvokeEvent, tag: string) => {
|
||||
const hashtags = new Hashtags(hashtagsDB)
|
||||
|
@ -1667,25 +1624,3 @@ const decodeLanguage = (lang: string): LanguageType => {
|
|||
return Language[l]
|
||||
}
|
||||
}
|
||||
|
||||
const getMarker = async (client: MegalodonInterface, accountID: string): Promise<LocalMarker | null> => {
|
||||
let serverMarker: Entity.Marker | {} = {}
|
||||
try {
|
||||
const res = await client.getMarkers(['notifications'])
|
||||
serverMarker = res.data
|
||||
} catch (err) {
|
||||
console.warn(err)
|
||||
}
|
||||
const s = serverMarker as Entity.Marker
|
||||
if (s.notifications !== undefined) {
|
||||
return {
|
||||
timeline: 'notifications',
|
||||
last_read_id: s.notifications.last_read_id
|
||||
} as LocalMarker
|
||||
}
|
||||
if (markerRepo === null) {
|
||||
return null
|
||||
}
|
||||
const marker = await markerRepo.get(accountID, 'notifications')
|
||||
return marker
|
||||
}
|
||||
|
|
|
@ -1,77 +0,0 @@
|
|||
import { isEmpty } from 'lodash'
|
||||
import Loki, { Collection } from 'lokijs'
|
||||
import { LocalMarker } from '~/src/types/localMarker'
|
||||
|
||||
export default class Marker {
|
||||
private markers: Collection<any>
|
||||
|
||||
constructor(db: Loki) {
|
||||
this.markers = db.getCollection('markers')
|
||||
}
|
||||
|
||||
private insert(marker: LocalMarker): Promise<LocalMarker> {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
const doc: LocalMarker = this.markers.insert(marker)
|
||||
resolve(doc)
|
||||
} catch (err) {
|
||||
reject(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private update(marker: LocalMarker): Promise<LocalMarker> {
|
||||
// @ts-ignore
|
||||
return new Promise((resolve, reject) => {
|
||||
// eslint-disable-line no-unused-vars
|
||||
try {
|
||||
this.markers.findAndUpdate(
|
||||
{
|
||||
owner_id: { $eq: marker.owner_id },
|
||||
timeline: { $eq: marker.timeline }
|
||||
},
|
||||
(item: LocalMarker) => {
|
||||
item.last_read_id = marker.last_read_id
|
||||
}
|
||||
)
|
||||
return this.get(marker.owner_id, marker.timeline)
|
||||
} catch (err) {
|
||||
reject(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
public async save(marker: LocalMarker): Promise<LocalMarker> {
|
||||
return this.get(marker.owner_id, marker.timeline).then(l => {
|
||||
if (isEmpty(l)) return this.insert(marker)
|
||||
return this.update(marker)
|
||||
})
|
||||
}
|
||||
|
||||
public async get(owner_id: string, timeline: 'home' | 'notifications' | 'mentions'): Promise<LocalMarker | null> {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
const doc: LocalMarker | null = this.markers.findOne({
|
||||
owner_id: { $eq: owner_id },
|
||||
timeline: { $eq: timeline }
|
||||
})
|
||||
resolve(doc)
|
||||
} catch (err) {
|
||||
reject(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
public async list(owner_id: string): Promise<Array<LocalMarker>> {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
const docs: Array<LocalMarker> = this.markers.find({
|
||||
owner_id: { $eq: owner_id }
|
||||
})
|
||||
resolve(docs)
|
||||
} catch (err) {
|
||||
reject(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
|
@ -1,12 +1,8 @@
|
|||
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'
|
||||
import { LoadingCard } from '@/types/loading-card'
|
||||
|
||||
const win = (window as any) as MyWindow
|
||||
|
||||
export type HomeState = {
|
||||
lazyLoading: boolean
|
||||
heading: boolean
|
||||
|
@ -144,12 +140,12 @@ const actions: ActionTree<HomeState, RootState> = {
|
|||
rootState.TimelineSpace.account!.accessToken,
|
||||
rootState.App.userAgent
|
||||
)
|
||||
const localMarker: LocalMarker | null = await dispatch('getMarker').catch(err => {
|
||||
const marker: Entity.Marker | null = await dispatch('getMarker').catch(err => {
|
||||
console.error(err)
|
||||
})
|
||||
|
||||
if (rootState.TimelineSpace.timelineSetting.useMarker.home && localMarker !== null) {
|
||||
const last = await client.getStatus(localMarker.last_read_id)
|
||||
if (rootState.TimelineSpace.timelineSetting.useMarker.home && marker !== null && marker.home) {
|
||||
const last = await client.getStatus(marker.home.last_read_id)
|
||||
const lastReadStatus = last.data
|
||||
|
||||
let timeline: Array<Entity.Status | LoadingCard> = [lastReadStatus]
|
||||
|
@ -258,7 +254,7 @@ const actions: ActionTree<HomeState, RootState> = {
|
|||
}
|
||||
return res.data
|
||||
},
|
||||
[ACTION_TYPES.GET_MARKER]: async ({ rootState }): Promise<LocalMarker | null> => {
|
||||
[ACTION_TYPES.GET_MARKER]: async ({ rootState }): Promise<Entity.Marker | null> => {
|
||||
if (!rootState.TimelineSpace.timelineSetting.useMarker.home) {
|
||||
return null
|
||||
}
|
||||
|
@ -275,27 +271,13 @@ const actions: ActionTree<HomeState, RootState> = {
|
|||
} catch (err) {
|
||||
console.warn(err)
|
||||
}
|
||||
const s = serverMarker as Entity.Marker
|
||||
if (s.home !== undefined) {
|
||||
return {
|
||||
timeline: 'home',
|
||||
last_read_id: s.home.last_read_id
|
||||
} as LocalMarker
|
||||
}
|
||||
const localMarker: LocalMarker | null = await win.ipcRenderer.invoke('get-home-marker', rootState.TimelineSpace.account!.id)
|
||||
return localMarker
|
||||
return serverMarker
|
||||
},
|
||||
[ACTION_TYPES.SAVE_MARKER]: async ({ state, rootState }) => {
|
||||
const timeline = state.timeline
|
||||
if (timeline.length === 0 || timeline[0].id === 'loading-card') {
|
||||
return
|
||||
}
|
||||
win.ipcRenderer.send('save-marker', {
|
||||
owner_id: rootState.TimelineSpace.account!.id,
|
||||
timeline: 'home',
|
||||
last_read_id: timeline[0].id
|
||||
} as LocalMarker)
|
||||
|
||||
if (rootState.TimelineSpace.server!.sns === 'misskey') {
|
||||
return
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
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'
|
||||
import { LoadingCard } from '@/types/loading-card'
|
||||
|
||||
|
@ -119,18 +118,18 @@ const actions: ActionTree<NotificationsState, RootState> = {
|
|||
rootState.App.userAgent
|
||||
)
|
||||
|
||||
const localMarker: LocalMarker | null = await dispatch('getMarker').catch(err => {
|
||||
const marker: Entity.Marker | null = await dispatch('getMarker').catch(err => {
|
||||
console.error(err)
|
||||
})
|
||||
|
||||
if (rootState.TimelineSpace.timelineSetting.useMarker.notifications && localMarker !== null) {
|
||||
if (rootState.TimelineSpace.timelineSetting.useMarker.notifications && marker !== null && marker.notifications) {
|
||||
// The result does not contain max_id's notification, when we specify max_id parameter in get notifications.
|
||||
// So we need to get max_id's notification.
|
||||
const nextResponse = await client.getNotifications({ limit: 1, min_id: localMarker.last_read_id })
|
||||
const nextResponse = await client.getNotifications({ limit: 1, min_id: marker.notifications.last_read_id })
|
||||
if (nextResponse.data.length > 0) {
|
||||
const card: LoadingCard = {
|
||||
type: 'middle-load',
|
||||
since_id: localMarker.last_read_id,
|
||||
since_id: marker.notifications.last_read_id,
|
||||
// We don't need to fill this field in the first fetching.
|
||||
// Because in most cases there is no new statuses at the first fetching.
|
||||
// After new statuses are received, if the number of unread statuses is more than 30, max_id is not necessary.
|
||||
|
@ -224,7 +223,7 @@ const actions: ActionTree<NotificationsState, RootState> = {
|
|||
[ACTION_TYPES.RESET_BADGE]: () => {
|
||||
win.ipcRenderer.send('reset-badge')
|
||||
},
|
||||
[ACTION_TYPES.GET_MARKER]: async ({ rootState }): Promise<LocalMarker | null> => {
|
||||
[ACTION_TYPES.GET_MARKER]: async ({ rootState }): Promise<Entity.Marker | null> => {
|
||||
if (!rootState.TimelineSpace.timelineSetting.useMarker.notifications) {
|
||||
return null
|
||||
}
|
||||
|
@ -241,26 +240,13 @@ const actions: ActionTree<NotificationsState, RootState> = {
|
|||
} catch (err) {
|
||||
console.warn(err)
|
||||
}
|
||||
const s = serverMarker as Entity.Marker
|
||||
if (s.notifications !== undefined) {
|
||||
return {
|
||||
timeline: 'notifications',
|
||||
last_read_id: s.notifications.last_read_id
|
||||
} as LocalMarker
|
||||
}
|
||||
const localMarker: LocalMarker | null = await win.ipcRenderer.invoke('get-notifications-marker', rootState.TimelineSpace.account!.id)
|
||||
return localMarker
|
||||
return serverMarker
|
||||
},
|
||||
[ACTION_TYPES.SAVE_MARKER]: async ({ state, rootState }) => {
|
||||
const notifications = state.notifications
|
||||
if (notifications.length === 0 || notifications[0].id === 'loading-card') {
|
||||
return
|
||||
}
|
||||
win.ipcRenderer.send('save-marker', {
|
||||
owner_id: rootState.TimelineSpace.account!.id,
|
||||
timeline: 'notifications',
|
||||
last_read_id: notifications[0].id
|
||||
} as LocalMarker)
|
||||
|
||||
if (rootState.TimelineSpace.server!.sns === 'misskey') {
|
||||
return
|
||||
|
|
|
@ -1,5 +0,0 @@
|
|||
export type LocalMarker = {
|
||||
owner_id: number
|
||||
timeline: 'home' | 'notifications' | 'mentions'
|
||||
last_read_id: string
|
||||
}
|
Loading…
Reference in New Issue