refs #677 Customize unread notification and save the settings

This commit is contained in:
AkiraFukushima 2018-11-07 22:48:50 +09:00
parent 70d5ae3262
commit bb124d886c
6 changed files with 162 additions and 22 deletions

View File

@ -17,6 +17,7 @@ import StreamingManager from './streaming_manager'
import Preferences from './preferences'
import Fonts from './fonts'
import Hashtags from './hashtags'
import UnreadNotification from './unread_notification'
import i18n from '../config/i18n'
import Language from '../constants/language'
@ -70,6 +71,13 @@ let hashtagsDB = new Datastore({
autoload: true
})
const unreadNotificationDBPath = process.env.NODE_ENV === 'production'
? userData + '/db/unread_notification.db'
: 'unread_notification.db'
const unreadNotification = new UnreadNotification(unreadNotificationDBPath)
unreadNotification.initialize()
.catch(err => log.error(err))
const preferencesDBPath = process.env.NODE_ENV === 'production'
? userData + './db/preferences.json'
: 'preferences.json'
@ -762,6 +770,31 @@ ipcMain.on('list-fonts', (event, _) => {
})
})
// Unread notifications
ipcMain.on('get-unread-notification', (event, accountID) => {
unreadNotification.findOne({
accountID: accountID
})
.then(doc => {
event.sender.send('response-get-unread-notification', doc)
})
.catch(err => {
event.sender.send('error-get-unread-notification', err)
})
})
ipcMain.on('update-unread-notification', (event, obj) => {
const { accountID } = obj
unreadNotification.insertOrUpdate(accountID, obj)
.then(_ => {
event.sender.send('response-update-unread-notification', true)
})
.catch(err => {
console.error(err)
event.sender.send('error-update-unread-notification', err)
})
})
// Application control
ipcMain.on('relaunch', (event, _) => {
app.relaunch()

View File

@ -0,0 +1,63 @@
import empty from 'is-empty'
import Datastore from 'nedb'
export default class UnreadNotification {
constructor (path) {
this.db = new Datastore({
filename: path,
autoload: true
})
}
async initialize () {
await this.updateUnique()
}
updateUnique () {
return new Promise((resolve, reject) => {
// At first, remove old index.
this.db.removeIndex('accountID', (err) => {
if (err) reject(err)
// Add unique index.
this.db.ensureIndex({ fieldName: 'accountID', unique: true, sparse: true }, (err) => {
if (err) reject(err)
resolve(null)
})
})
})
}
insertOrUpdate (accountID, obj) {
return new Promise((resolve, reject) => {
this.db.update(
{
accountID: accountID
},
obj,
{
upsert: true
},
(err, num) => {
if (err) return reject(err)
resolve(num)
})
})
}
findOne (obj) {
return new Promise((resolve, reject) => {
this.db.findOne(obj, (err, doc) => {
if (err) return reject(err)
if (empty(doc)) return reject(new EmptyRecordError('empty'))
resolve(doc)
})
})
}
}
class EmptyRecordError extends Error {
constructor (msg) {
super(msg)
this.name = 'EmptyRecordError'
}
}

View File

@ -48,6 +48,7 @@ export default {
})
},
created () {
this.$store.commit('Settings/changeAccountID', this.id())
this.$router.push(`/${this.id()}/settings/general`)
},
methods: {

View File

@ -60,7 +60,9 @@ export default {
return this.$store.state.Settings.Timeline.unread_notification.home
},
set (value) {
this.$store.commit('Settings/Timeline/changeHome', value)
this.$store.dispatch('Settings/Timeline/changeUnreadNotification', {
home: value
})
}
},
direct: {
@ -68,7 +70,9 @@ export default {
return this.$store.state.Settings.Timeline.unread_notification.direct
},
set (value) {
this.$store.commit('Settings/Timeline/changeDirect', value)
this.$store.dispatch('Settings/Timeline/changeUnreadNotification', {
direct: value
})
}
},
favourite: {
@ -76,7 +80,9 @@ export default {
return this.$store.state.Settings.Timeline.unread_notification.favourite
},
set (value) {
this.$store.commit('Settings/Timeline/changeFavourite', value)
this.$store.dispatch('Settings/Timeline/changeUnreadNotification', {
favourite: value
})
}
},
local: {
@ -84,7 +90,9 @@ export default {
return this.$store.state.Settings.Timeline.unread_notification.local
},
set (value) {
this.$store.commit('Settings/Timeline/changeLocal', value)
this.$store.dispatch('Settings/Timeline/changeUnreadNotification', {
local: value
})
}
},
public: {
@ -92,9 +100,14 @@ export default {
return this.$store.state.Settings.Timeline.unread_notification.public
},
set (value) {
this.$store.commit('Settings/Timeline/changePublic', value)
this.$store.dispatch('Settings/Timeline/changeUnreadNotification', {
public: value
})
}
}
},
async created () {
await this.$store.dispatch('Settings/Timeline/loadUnreadNotification')
}
}
</script>

View File

@ -6,5 +6,13 @@ export default {
modules: {
General,
Timeline
},
state: {
accountID: null
},
mutations: {
changeAccountID (state, id) {
state.accountID = id
}
}
}

View File

@ -1,30 +1,52 @@
import { ipcRenderer } from 'electron'
export default {
namespaced: true,
state: {
unread_notification: {
home: false,
home: true,
direct: false,
favourite: false,
local: false,
local: true,
public: false
}
},
mutations: {
changeHome (state, value) {
state.unread_notification.home = value
},
changeDirect (state, value) {
state.unread_notification.direct = value
},
changeFavourite (state, value) {
state.unread_notification.favourite = value
},
changeLocal (state, value) {
state.unread_notification.local = value
},
changePublic (state, value) {
state.unread_notification.public = value
updateUnreadNotification (state, settings) {
state.unread_notification = settings
}
},
actions: {}
actions: {
loadUnreadNotification ({ commit, rootState }) {
return new Promise((resolve, reject) => {
ipcRenderer.once('response-get-unread-notification', (event, settings) => {
ipcRenderer.removeAllListeners('error-get-unread-notification')
commit('updateUnreadNotification', settings)
resolve(settings)
})
ipcRenderer.once('error-get-unread-notification', (event, err) => {
ipcRenderer.removeAllListeners('response-get-unread-notification')
resolve(null)
})
ipcRenderer.send('get-unread-notification', rootState.Settings.accountID)
})
},
changeUnreadNotification ({ dispatch, state, rootState }, timeline) {
const settings = Object.assign({}, state.unread_notification, timeline, {
accountID: rootState.Settings.accountID
})
return new Promise((resolve, reject) => {
ipcRenderer.once('response-update-unread-notification', (event, _) => {
ipcRenderer.removeAllListeners('error-update-unread-notification')
dispatch('loadUnreadNotification')
resolve(settings)
})
ipcRenderer.once('error-update-unread-notification', (event, err) => {
ipcRenderer.removeAllListeners('response-update-unread-notification')
reject(err)
})
ipcRenderer.send('update-unread-notification', settings)
})
}
}
}