refs #985 Add cache db for hashtags

This commit is contained in:
AkiraFukushima 2019-07-31 00:17:30 +09:00
parent e3b5ef364d
commit 7301970495
2 changed files with 52 additions and 0 deletions

29
src/main/cache/hashtag.ts vendored Normal file
View File

@ -0,0 +1,29 @@
import Datastore from 'nedb'
import { LocalTag } from '~/src/types/localTag'
export default class HashtagCache {
private db: Datastore
constructor(db: Datastore) {
this.db = db
this.db.ensureIndex({ fieldName: 'tagName', unique: true }, _ => {})
}
listTags(): Promise<Array<LocalTag>> {
return new Promise((resolve, reject) => {
this.db.find<LocalTag>({}, (err, docs) => {
if (err) return reject(err)
resolve(docs)
})
})
}
insertHashtag(tag: string): Promise<LocalTag> {
return new Promise((resolve, reject) => {
this.db.insert({ tagName: tag }, (err, doc) => {
if (err) return reject(err)
resolve(doc)
})
})
}
}

View File

@ -40,6 +40,7 @@ import { LocalTag } from '~/src/types/localTag'
import { UnreadNotification as UnreadNotificationConfig } from '~/src/types/unreadNotification'
import { Notify } from '~/src/types/notify'
import { StreamingError } from '~/src/errors/streamingError'
import HashtagCache from './cache/hashtag'
/**
* Context menu
@ -100,6 +101,15 @@ unreadNotification.initialize().catch((err: Error) => log.error(err))
const preferencesDBPath = process.env.NODE_ENV === 'production' ? userData + './db/preferences.json' : 'preferences.json'
/**
* Cache path
*/
const hashtagCachePath = process.env.NODE_ENV === 'production' ? userData + '/cache/hashtag.db' : 'cache/hashtag.db'
const hashtagCacheDB = new Datastore({
filename: hashtagCachePath,
autoload: true
})
const soundBasePath =
process.env.NODE_ENV === 'development' ? path.join(__dirname, '../../build/sounds/') : path.join(process.resourcesPath!, 'build/sounds/')
@ -984,6 +994,19 @@ ipcMain.on('update-unread-notification', (event: Event, config: UnreadNotificati
})
})
// Cache
ipcMain.on('get-cache-hashtag', async (event: Event) => {
const db = new HashtagCache(hashtagCacheDB)
const tags = await db.listTags()
event.sender.send('response-get-cache-hashtag', tags)
})
ipcMain.on('insert-cache-hashtag', async (event: Event, name: string) => {
const db = new HashtagCache(hashtagCacheDB)
const tag = await db.insertHashtag(name)
event.sender.send('response-insert-cache-hashtag', tag)
})
// Application control
ipcMain.on('relaunch', () => {
app.relaunch()