Merge pull request #3120 from h3poteto/iss-3119

refs #3119 Create notifications when there are notifications received…
This commit is contained in:
AkiraFukushima 2022-02-12 15:52:56 +09:00 committed by GitHub
commit fb11bee652
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 18 deletions

View File

@ -639,23 +639,8 @@ ipcMain.on('start-all-user-streamings', (event: IpcMainEvent, accounts: Array<Lo
// Cache account // Cache account
await accountCache.insertAccount(id, update.account.acct).catch(err => console.error(err)) await accountCache.insertAccount(id, update.account.acct).catch(err => console.error(err))
}, },
(notification: Entity.Notification) => { async (notification: Entity.Notification) => {
const preferences = new Preferences(preferencesDBPath) await publishNotification(notification, event, id)
preferences.load().then(conf => {
const options = createNotification(notification, conf.notification.notify)
if (options !== null) {
const notify = new Notification(options)
notify.on('click', _ => {
if (!event.sender.isDestroyed()) {
event.sender.send('open-notification-tab', id)
}
})
notify.show()
}
})
if (process.platform === 'darwin') {
app.dock.setBadge('•')
}
// In macOS and Windows, sometimes window is closed (not quit). // In macOS and Windows, sometimes window is closed (not quit).
// But streamings are always running. // But streamings are always running.
@ -978,6 +963,13 @@ ipcMain.on('stop-tag-streaming', () => {
} }
}) })
ipcMain.handle(
'publish-notification',
async (event: IpcMainInvokeEvent, notification: { notification: Entity.Notification; id: string }) => {
await publishNotification(notification.notification, event, notification.id)
}
)
// sounds // sounds
ipcMain.on('fav-rt-action-sound', () => { ipcMain.on('fav-rt-action-sound', () => {
const preferences = new Preferences(preferencesDBPath) const preferences = new Preferences(preferencesDBPath)
@ -1538,6 +1530,24 @@ async function reopenWindow() {
} }
} }
const publishNotification = async (notification: Entity.Notification, event: IpcMainEvent | IpcMainInvokeEvent, id: string) => {
const preferences = new Preferences(preferencesDBPath)
const conf = await preferences.load()
const options = createNotification(notification, conf.notification.notify)
if (options !== null) {
const notify = new Notification(options)
notify.on('click', _ => {
if (!event.sender.isDestroyed()) {
event.sender.send('open-notification-tab', id)
}
})
notify.show()
}
if (process.platform === 'darwin') {
app.dock.setBadge('•')
}
}
const createNotification = (notification: Entity.Notification, notifyConfig: Notify): NotificationConstructorOptions | null => { const createNotification = (notification: Entity.Notification, notifyConfig: Notify): NotificationConstructorOptions | null => {
switch (notification.type) { switch (notification.type) {
case NotificationType.Favourite: case NotificationType.Favourite:

View File

@ -142,9 +142,17 @@ const actions: ActionTree<NotificationsState, RootState> = {
const res = await client.getNotifications({ limit: 30, max_id: localMarker.last_read_id }) const res = await client.getNotifications({ limit: 30, max_id: localMarker.last_read_id })
// Make sure whether new notifications exist or not // Make sure whether new notifications exist or not
const nextResponse = await client.getNotifications({ limit: 1, min_id: lastReadNotification.id }) const nextResponse = await client.getNotifications({ min_id: lastReadNotification.id })
if (nextResponse.data.length > 0) { if (nextResponse.data.length > 0) {
notifications = ([card] as Array<Entity.Notification | LoadingCard>).concat(notifications).concat(res.data) notifications = ([card] as Array<Entity.Notification | LoadingCard>).concat(notifications).concat(res.data)
// Generate notifications received while the app was not running
commit('TimelineSpace/SideMenu/changeUnreadNotifications', true, { root: true })
nextResponse.data.forEach(n => {
win.ipcRenderer.invoke('publish-notification', {
notification: n,
id: rootState.TimelineSpace.account._id
})
})
} else { } else {
notifications = notifications.concat(res.data) notifications = notifications.concat(res.data)
} }