refs #985 Add cache db for accounts

This commit is contained in:
AkiraFukushima 2019-08-08 23:39:27 +09:00
parent fafc41a32d
commit e01156df08
4 changed files with 67 additions and 1 deletions

37
src/main/cache/account.ts vendored Normal file
View File

@ -0,0 +1,37 @@
import { isEmpty } from 'lodash'
import Datastore from 'nedb'
import { CachedAccount } from '~/src/types/cachedAccount'
export default class AccountCache {
private db: Datastore
constructor(path: string) {
this.db = new Datastore({
filename: path,
autoload: true
})
}
listAccounts(ownerID: string): Promise<Array<CachedAccount>> {
return new Promise((resolve, reject) => {
this.db.find<CachedAccount>({ owner_id: ownerID }, (err, docs) => {
if (err) return reject(err)
resolve(docs)
})
})
}
insertAccount(ownerID: string, acct: string): Promise<CachedAccount> {
return new Promise((resolve, reject) => {
// At first confirm records for unique.
this.db.findOne<CachedAccount>({ owner_id: ownerID, acct: acct }, (err, doc) => {
if (err) return reject(err)
if (!isEmpty(doc)) return reject(new Error('Record already exists'))
this.db.insert<CachedAccount>({ owner_id: ownerID, acct: acct }, (err, doc) => {
if (err) return reject(err)
resolve(doc)
})
})
})
}
}

View File

@ -41,6 +41,8 @@ import { UnreadNotification as UnreadNotificationConfig } from '~/src/types/unre
import { Notify } from '~/src/types/notify'
import { StreamingError } from '~/src/errors/streamingError'
import HashtagCache from './cache/hashtag'
import AccountCache from './cache/account'
import { InsertAccountCache } from '~/src/types/insertAccountCache'
/**
* Context menu
@ -107,6 +109,9 @@ const preferencesDBPath = process.env.NODE_ENV === 'production' ? userData + './
const hashtagCachePath = process.env.NODE_ENV === 'production' ? userData + '/cache/hashtag.db' : 'cache/hashtag.db'
const hashtagCache = new HashtagCache(hashtagCachePath)
const accountCachePath = process.env.NODE_ENV === 'production' ? userData + '/cache/account.db' : 'cache/account.db'
const accountCache = new AccountCache(accountCachePath)
const soundBasePath =
process.env.NODE_ENV === 'development' ? path.join(__dirname, '../../build/sounds/') : path.join(process.resourcesPath!, 'build/sounds/')
@ -1005,7 +1010,22 @@ ipcMain.on('insert-cache-hashtags', (event: Event, tags: Array<string>) => {
tags.map(async name => {
await hashtagCache.insertHashtag(name)
})
event.sender.send('response-insert-cache-hashtag')
event.sender.send('response-insert-cache-hashtags')
})
ipcMain.on('get-cache-accounts', async (event: Event, ownerID: string) => {
const accounts = await accountCache.listAccounts(ownerID)
event.sender.send('response-get-cache-accounts', accounts)
})
ipcMain.on('insert-cache-accounts', (event: Event, obj: InsertAccountCache) => {
const { ownerID, accts } = obj
accts.map(async acct => {
await accountCache.insertAccount(ownerID, acct).catch(err => {
console.warn(err)
})
})
event.sender.send('response-insert-cache-accounts')
})
// Application control

View File

@ -0,0 +1,5 @@
export type CachedAccount = {
_id?: string
acct: string
owner_id: string
}

View File

@ -0,0 +1,4 @@
export type InsertAccountCache = {
ownerID: string
accts: Array<string>
}