diff --git a/src/main/cache/hashtag.ts b/src/main/cache/hashtag.ts new file mode 100644 index 00000000..bbc1d0ec --- /dev/null +++ b/src/main/cache/hashtag.ts @@ -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> { + return new Promise((resolve, reject) => { + this.db.find({}, (err, docs) => { + if (err) return reject(err) + resolve(docs) + }) + }) + } + + insertHashtag(tag: string): Promise { + return new Promise((resolve, reject) => { + this.db.insert({ tagName: tag }, (err, doc) => { + if (err) return reject(err) + resolve(doc) + }) + }) + } +} diff --git a/src/main/index.ts b/src/main/index.ts index e86a552b..f167fb23 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -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()