refs #958 Notify notification in main process instead of renderer process
This commit is contained in:
parent
d89516961a
commit
23ce3821c1
|
@ -9,7 +9,9 @@ import {
|
|||
BrowserWindow,
|
||||
BrowserWindowConstructorOptions,
|
||||
MenuItemConstructorOptions,
|
||||
Event
|
||||
Event,
|
||||
Notification,
|
||||
NotificationConstructorOptions
|
||||
} from 'electron'
|
||||
import Datastore from 'nedb'
|
||||
import { isEmpty } from 'lodash'
|
||||
|
@ -20,7 +22,8 @@ import path from 'path'
|
|||
import ContextMenu from 'electron-context-menu'
|
||||
import { initSplashScreen, Config } from '@trodi/electron-splashscreen'
|
||||
import openAboutWindow from 'about-window'
|
||||
import { Status, Notification } from 'megalodon'
|
||||
import { Status, Notification as RemoteNotification, Account as RemoteAccount } from 'megalodon'
|
||||
import sanitizeHtml from 'sanitize-html'
|
||||
|
||||
import Authentication from './auth'
|
||||
import Account from './account'
|
||||
|
@ -34,7 +37,7 @@ import Language from '../constants/language'
|
|||
import { LocalAccount } from '~/src/types/localAccount'
|
||||
import { LocalTag } from '~/src/types/localTag'
|
||||
import { UnreadNotification as UnreadNotificationConfig } from '~/src/types/unreadNotification'
|
||||
import { AccountNotification } from '~/src/types/accountNotification'
|
||||
import { Notify } from '~/src/types/notify'
|
||||
import { StreamingError } from '~/src/errors/streamingError'
|
||||
|
||||
/**
|
||||
|
@ -363,7 +366,7 @@ ipcMain.on('update-account', (event: Event, acct: LocalAccount) => {
|
|||
ipcMain.on('remove-account', (event: Event, id: string) => {
|
||||
accountManager
|
||||
.removeAccount(id)
|
||||
.then((id) => {
|
||||
.then(id => {
|
||||
stopUserStreaming(id)
|
||||
event.sender.send('response-remove-account', id)
|
||||
})
|
||||
|
@ -444,13 +447,18 @@ ipcMain.on('start-all-user-streamings', (event: Event, accounts: Array<LocalAcco
|
|||
(update: Status) => {
|
||||
event.sender.send(`update-start-all-user-streamings-${id}`, update)
|
||||
},
|
||||
(notification: Notification) => {
|
||||
const accountNotification: AccountNotification = {
|
||||
id: id,
|
||||
notification: notification
|
||||
(notification: RemoteNotification) => {
|
||||
const preferences = new Preferences(preferencesDBPath)
|
||||
preferences.load().then(conf => {
|
||||
const options = createNotification(notification, conf.notification.notify)
|
||||
if (options !== null) {
|
||||
const notify = new Notification(options)
|
||||
notify.on('click', _ => {
|
||||
event.sender.send('open-notification-tab', id)
|
||||
})
|
||||
notify.show()
|
||||
}
|
||||
// To notiy badge
|
||||
event.sender.send('notification-start-all-user-streamings', accountNotification)
|
||||
})
|
||||
// To update notification timeline
|
||||
event.sender.send(`notification-start-all-user-streamings-${id}`, notification)
|
||||
|
||||
|
@ -1138,3 +1146,58 @@ async function reopenWindow() {
|
|||
return null
|
||||
}
|
||||
}
|
||||
|
||||
const createNotification = (notification: RemoteNotification, notifyConfig: Notify): NotificationConstructorOptions | null => {
|
||||
switch (notification.type) {
|
||||
case 'favourite':
|
||||
if (notifyConfig.favourite) {
|
||||
return {
|
||||
title: 'Favourite',
|
||||
body: `${username(notification.account)} favourited your status`,
|
||||
silent: false
|
||||
} as NotificationConstructorOptions
|
||||
}
|
||||
break
|
||||
case 'follow':
|
||||
if (notifyConfig.follow) {
|
||||
return {
|
||||
title: 'Follow',
|
||||
body: `${username(notification.account)} is now following you`,
|
||||
silent: false
|
||||
} as NotificationConstructorOptions
|
||||
}
|
||||
break
|
||||
case 'mention':
|
||||
if (notifyConfig.reply) {
|
||||
return {
|
||||
title: `${username(notification.status!.account)}`,
|
||||
body: sanitizeHtml(notification.status!.content, {
|
||||
allowedTags: [],
|
||||
allowedAttributes: []
|
||||
}),
|
||||
silent: false
|
||||
} as NotificationConstructorOptions
|
||||
}
|
||||
break
|
||||
case 'reblog':
|
||||
if (notifyConfig.reblog) {
|
||||
return {
|
||||
title: 'Reblog',
|
||||
body: `${username(notification.account)} boosted your status`,
|
||||
silent: false
|
||||
} as NotificationConstructorOptions
|
||||
}
|
||||
break
|
||||
default:
|
||||
break
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
const username = (account: RemoteAccount): string => {
|
||||
if (account.display_name !== '') {
|
||||
return account.display_name
|
||||
} else {
|
||||
return account.username
|
||||
}
|
||||
}
|
||||
|
|
|
@ -56,9 +56,6 @@ export default {
|
|||
created() {
|
||||
this.initialize()
|
||||
},
|
||||
destroyed() {
|
||||
this.$store.dispatch('GlobalHeader/unbindUserStreamings')
|
||||
},
|
||||
methods: {
|
||||
activeRoute() {
|
||||
return this.$route.path
|
||||
|
|
|
@ -1,16 +1,10 @@
|
|||
import sanitizeHtml from 'sanitize-html'
|
||||
import { Account, Notification as NotificationType } from 'megalodon'
|
||||
import { ipcRenderer } from 'electron'
|
||||
import router from '@/router'
|
||||
import { LocalAccount } from '~/src/types/localAccount'
|
||||
import { Module, MutationTree, ActionTree } from 'vuex'
|
||||
import { RootState } from '@/store'
|
||||
import { Notify } from '~/src/types/notify'
|
||||
import { AccountNotification } from '~/src/types/accountNotification'
|
||||
import { StreamingError } from '~src/errors/streamingError'
|
||||
|
||||
declare var Notification: any
|
||||
|
||||
export type GlobalHeaderState = {
|
||||
accounts: Array<LocalAccount>
|
||||
changing: boolean
|
||||
|
@ -55,7 +49,7 @@ const actions: ActionTree<GlobalHeaderState, RootState> = {
|
|||
return accounts
|
||||
},
|
||||
startStreamings: async ({ dispatch }) => {
|
||||
dispatch('bindUserStreamingsForNotify')
|
||||
dispatch('bindNotification')
|
||||
dispatch('startUserStreamings')
|
||||
},
|
||||
listAccounts: ({ dispatch, commit }): Promise<Array<LocalAccount>> => {
|
||||
|
@ -140,24 +134,13 @@ const actions: ActionTree<GlobalHeaderState, RootState> = {
|
|||
stopUserStreamings: () => {
|
||||
ipcRenderer.send('stop-all-user-streamings')
|
||||
},
|
||||
bindUserStreamingsForNotify: ({ rootState }) => {
|
||||
ipcRenderer.on('notification-start-all-user-streamings', (_, accountNotification: AccountNotification) => {
|
||||
const { id, notification } = accountNotification
|
||||
let notify = createNotification(notification, rootState.App.notify as Notify)
|
||||
if (notify) {
|
||||
notify.onclick = () => {
|
||||
bindNotification: () => {
|
||||
ipcRenderer.removeAllListeners('open-notification-tab')
|
||||
ipcRenderer.on('open-notification-tab', (_, id: string) => {
|
||||
router.push(`/${id}/home`)
|
||||
// We have to wait until change el-menu-item
|
||||
setTimeout(() => router.push(`/${id}/notifications`), 1000)
|
||||
}
|
||||
} else {
|
||||
console.log('could not create notify')
|
||||
}
|
||||
setTimeout(() => router.push(`/${id}/notifications`), 500)
|
||||
})
|
||||
},
|
||||
unbindUserStreamings: () => {
|
||||
ipcRenderer.removeAllListeners('notification-start-all-user-streamings')
|
||||
ipcRenderer.removeAllListeners('error-start-all-user-streamings')
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -169,49 +152,3 @@ const GlobalHeader: Module<GlobalHeaderState, RootState> = {
|
|||
}
|
||||
|
||||
export default GlobalHeader
|
||||
|
||||
function createNotification(notification: NotificationType, notifyConfig: Notify): Notification | null {
|
||||
switch (notification.type) {
|
||||
case 'favourite':
|
||||
if (notifyConfig.favourite) {
|
||||
return new Notification('Favourite', {
|
||||
body: `${username(notification.account)} favourited your status`
|
||||
})
|
||||
}
|
||||
break
|
||||
case 'follow':
|
||||
if (notifyConfig.follow) {
|
||||
return new Notification('Follow', {
|
||||
body: `${username(notification.account)} is now following you`
|
||||
})
|
||||
}
|
||||
break
|
||||
case 'mention':
|
||||
if (notifyConfig.reply) {
|
||||
// Clean html tags
|
||||
return new Notification(`${notification.status!.account.display_name}`, {
|
||||
body: sanitizeHtml(notification.status!.content, {
|
||||
allowedTags: [],
|
||||
allowedAttributes: []
|
||||
})
|
||||
})
|
||||
}
|
||||
break
|
||||
case 'reblog':
|
||||
if (notifyConfig.reblog) {
|
||||
return new Notification('Reblog', {
|
||||
body: `${username(notification.account)} boosted your status`
|
||||
})
|
||||
}
|
||||
break
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
function username(account: Account) {
|
||||
if (account.display_name !== '') {
|
||||
return account.display_name
|
||||
} else {
|
||||
return account.username
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue