Skip to content

Commit

Permalink
refs #2500 Change hashtags database to sqlite3
Browse files Browse the repository at this point in the history
  • Loading branch information
h3poteto committed Dec 31, 2022
1 parent a3857a4 commit 0009116
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 58 deletions.
12 changes: 12 additions & 0 deletions src/main/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ FOREIGN KEY (account_id) REFERENCES accounts(id) ON DELETE CASCADE)',
}
}
)
db.run(
'CREATE TABLE IF NOT EXISTS hashtags(\
id INTEGER PRIMARY KEY, \
tag TEXT NOT NULL, \
account_id INTEGER UNIQUE NOT NULL, \
FOREIGN KEY (account_id) REFERENCES accounts(id) ON DELETE CASCADE)',
err => {
if (err) {
console.error('failed to create hashtags: ', err)
}
}
)
})

return db
Expand Down
87 changes: 53 additions & 34 deletions src/main/hashtags.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,63 @@
import Datastore from 'nedb'
import sqlite3 from 'sqlite3'
import { LocalTag } from '~/src/types/localTag'

export default class Hashtags {
private db: Datastore
export const listTags = (db: sqlite3.Database, accountId: number): Promise<Array<LocalTag>> => {
return new Promise((resolve, reject) => {
db.all('SELECT * FROM hashtags WHERE account_id = ?', accountId, (err, rows) => {
if (err) {
reject(err)
}
resolve(
rows.map(r => ({
id: r.id,
tagName: r.tag,
accountId: r.account_id
}))
)
})
})
}

constructor(db: Datastore) {
this.db = db
this.db.ensureIndex({ fieldName: 'tagName', unique: true }, _ => {})
}
export const insertTag = (db: sqlite3.Database, accountId: number, tag: string): Promise<LocalTag> => {
return new Promise((resolve, reject) => {
db.serialize(() => {
db.run('BEGIN TRANSACTION')

listTags(): Promise<Array<LocalTag>> {
return new Promise((resolve, reject) => {
this.db.find<LocalTag>({}, (err, docs) => {
if (err) return reject(err)
resolve(docs)
})
})
}
db.get('SELECT * FROM hashtags WHERE id = ? AND tag = ?', [accountId, tag], (err, row) => {
if (err) {
reject(err)
}
if (row) {
resolve({
id: row.id,
tagName: row.tag,
accountId: row.account_id
})
}

insertTag(tag: string): Promise<LocalTag> {
return new Promise((resolve, reject) => {
this.db.insert({ tagName: tag }, (err, doc) => {
if (err) return reject(err)
resolve(doc)
db.run('INSERT INTO hashtags(tag, account_id) VALUES (?, ?)', [accountId, tag], function (err) {
if (err) {
reject(err)
}
db.run('COMMIT')
resolve({
id: this.lastID,
tagName: tag,
accountId: accountId
})
})
})
})
}
})
}

removeTag(localTag: LocalTag): Promise<number> {
return new Promise((resolve, reject) => {
this.db.remove(
{
tagName: localTag.tagName
},
{ multi: true },
(err, numRemoved) => {
if (err) return reject(err)
resolve(numRemoved)
}
)
export const removeTag = (db: sqlite3.Database, tag: LocalTag): Promise<null> => {
return new Promise((resolve, reject) => {
db.run('DELETE FROM hashtags WHERE id = ?', tag.id, err => {
if (err) {
reject(err)
}
resolve(null)
})
}
})
}
21 changes: 6 additions & 15 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import { getAccount, insertAccount, listAccounts } from './account'
// import { StreamingURL, UserStreaming, DirectStreaming, LocalStreaming, PublicStreaming, ListStreaming, TagStreaming } from './websocket'
import Preferences from './preferences'
import Fonts from './fonts'
import Hashtags from './hashtags'
import i18next from '~/src/config/i18n'
import { i18n as I18n } from 'i18next'
import Language, { LanguageType } from '../constants/language'
Expand All @@ -54,6 +53,7 @@ import Settings from './settings'
import { BaseSettings, Setting } from '~/src/types/setting'
import { insertServer } from './server'
import { LocalServer } from '~src/types/localServer'
import { insertTag, listTags, removeTag } from './hashtags'

/**
* Context menu
Expand Down Expand Up @@ -125,12 +125,6 @@ const db = newDB(databasePath)

const preferencesDBPath = process.env.NODE_ENV === 'production' ? userData + './db/preferences.json' : 'preferences.json'

const hashtagsDBPath = process.env.NODE_ENV === 'production' ? userData + '/db/hashtags.db' : 'hashtags.db'
const hashtagsDB = new Datastore({
filename: hashtagsDBPath,
autoload: true
})

const settingsDBPath = process.env.NODE_ENV === 'production' ? userData + './db/settings.json' : 'settings.json'

/**
Expand Down Expand Up @@ -1113,20 +1107,17 @@ ipcMain.handle('update-spellchecker-languages', async (_: IpcMainInvokeEvent, la
})

// hashtag
ipcMain.handle('save-hashtag', async (_: IpcMainInvokeEvent, tag: string) => {
const hashtags = new Hashtags(hashtagsDB)
await hashtags.insertTag(tag)
ipcMain.handle('save-hashtag', async (_: IpcMainInvokeEvent, req: { accountId: number; tag: string }) => {
await insertTag(db, req.accountId, req.tag)
})

ipcMain.handle('list-hashtags', async (_: IpcMainInvokeEvent) => {
const hashtags = new Hashtags(hashtagsDB)
const tags = await hashtags.listTags()
ipcMain.handle('list-hashtags', async (_: IpcMainInvokeEvent, accountId: number) => {
const tags = await listTags(db, accountId)
return tags
})

ipcMain.handle('remove-hashtag', async (_: IpcMainInvokeEvent, tag: LocalTag) => {
const hashtags = new Hashtags(hashtagsDB)
await hashtags.removeTag(tag)
await removeTag(db, tag)
})

// Fonts
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/store/TimelineSpace/Contents/Hashtag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export type HashtagModuleState = HashtagModule & HashtagState
const state = (): HashtagState => ({})

const actions: ActionTree<HashtagState, RootState> = {
saveTag: async ({ dispatch }, tag: string) => {
await win.ipcRenderer.invoke('save-hashtag', tag)
saveTag: async ({ dispatch, rootState }, tag: string) => {
await win.ipcRenderer.invoke('save-hashtag', { accountId: rootState.TimelineSpace.account!.id, tag })
dispatch('TimelineSpace/SideMenu/listTags', {}, { root: true })
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/renderer/store/TimelineSpace/Contents/Hashtag/List.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { RootState } from '@/store'
import { MyWindow } from '~/src/types/global'
import { toRaw } from 'vue'

const win = window as any as MyWindow
const win = (window as any) as MyWindow

export type ListState = {
tags: Array<LocalTag>
Expand All @@ -30,14 +30,14 @@ export const ACTION_TYPES = {
}

const actions: ActionTree<ListState, RootState> = {
[ACTION_TYPES.LIST_TAGS]: async ({ commit }) => {
const tags: Array<LocalTag> = await win.ipcRenderer.invoke('list-hashtags')
[ACTION_TYPES.LIST_TAGS]: async ({ rootState, commit }) => {
const tags: Array<LocalTag> = await win.ipcRenderer.invoke('list-hashtags', rootState.TimelineSpace.account!.id)
commit(MUTATION_TYPES.UPDATE_TAGS, tags)
return tags
},
[ACTION_TYPES.REMOVE_TAG]: async ({ dispatch }, tag: LocalTag) => {
await win.ipcRenderer.invoke('remove-hashtag', toRaw(tag))
dispatch('listTags')
dispatch(ACTION_TYPES.LIST_TAGS)
dispatch('TimelineSpace/SideMenu/listTags', {}, { root: true })
return 'deleted'
}
Expand Down
6 changes: 4 additions & 2 deletions src/renderer/store/TimelineSpace/SideMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,10 @@ const actions: ActionTree<SideMenuState, RootState> = {
commit(MUTATION_TYPES.CHANGE_COLLAPSE, value)
return value
},
[ACTION_TYPES.LIST_TAGS]: async ({ commit }) => {
const tags: Array<LocalTag> = await win.ipcRenderer.invoke('list-hashtags')
[ACTION_TYPES.LIST_TAGS]: async ({ rootState, commit }) => {
// TODO: Can not get account because too early.
// It should be executed after TimelineSpace obtain local account.
const tags: Array<LocalTag> = await win.ipcRenderer.invoke('list-hashtags', rootState.TimelineSpace.account!.id)
commit(MUTATION_TYPES.UPDATE_TAGS, tags)
return tags
}
Expand Down
3 changes: 2 additions & 1 deletion src/types/localTag.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export type LocalTag = {
id: number
tagName: string
_id?: string
accountId: number
}

0 comments on commit 0009116

Please sign in to comment.