-
-
Notifications
You must be signed in to change notification settings - Fork 171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add webxdc notification handler #4400
Merged
+159
−36
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
164d35a
Handle webxdc notifications
nicodh 91ef68c
Add test-results to .prettierignore
nicodh 3edca1f
Add doc comments in notifications
nicodh a0a45f9
Expose sendUpdateInterval & sendUpdateMaxSize
nicodh beb8061
Update CHANGELOG
nicodh 3beb3ef
Remove isWebxdcInfo from DcNotification as it is not needed any more
nicodh c9760ef
fix: webxdc notifications in browser version
WofWca c86c3ab
Pass eventText to showNotification
nicodh d12a326
Add queue to garantuee eventhandler order
nicodh 333f816
Simplify event handling
nicodh c173f6a
Remove chatId condition
nicodh 01383fc
Update comment in runtime
nicodh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,13 +10,38 @@ import type { T } from '@deltachat/jsonrpc-client' | |
|
||
const log = getLogger('renderer/notifications') | ||
|
||
/** | ||
* Notification handling: | ||
* | ||
* - listens for incoming notifications | ||
* - reflects notification settings | ||
* - prepares notification data (DcNotification) | ||
* - queues notifications if needed to avoid "mass" notifications | ||
* - sends notifications to runtime (which invokes ipcBackend) | ||
*/ | ||
|
||
export function initNotifications() { | ||
BackendRemote.on('IncomingMsg', (accountId, { chatId, msgId }) => { | ||
incomingMessageHandler(accountId, chatId, msgId, false) | ||
}) | ||
BackendRemote.on('IncomingWebxdcNotify', (accountId, { msgId, text }) => { | ||
// we don't have the chatId yet, but it will be fetched in flushNotifications | ||
incomingMessageHandler(accountId, -1, msgId, true, text) | ||
}) | ||
BackendRemote.on('IncomingMsgBunch', accountId => { | ||
flushNotifications(accountId) | ||
}) | ||
} | ||
|
||
function isMuted(accountId: number, chatId: number) { | ||
return BackendRemote.rpc.isChatMuted(accountId, chatId) | ||
} | ||
|
||
type queuedNotification = { | ||
chatId: number | ||
messageId: number | ||
isWebxdcInfo: boolean | ||
eventText?: string | ||
} | ||
|
||
let queuedNotifications: { | ||
|
@@ -26,7 +51,9 @@ let queuedNotifications: { | |
function incomingMessageHandler( | ||
accountId: number, | ||
chatId: number, | ||
messageId: number | ||
messageId: number, | ||
isWebxdcInfo: boolean, | ||
eventText?: string | ||
) { | ||
log.debug('incomingMessageHandler: ', { chatId, messageId }) | ||
|
||
|
@@ -58,13 +85,20 @@ function incomingMessageHandler( | |
if (typeof queuedNotifications[accountId] === 'undefined') { | ||
queuedNotifications[accountId] = [] | ||
} | ||
queuedNotifications[accountId].push({ chatId, messageId }) | ||
queuedNotifications[accountId].push({ | ||
chatId, | ||
messageId, | ||
isWebxdcInfo, | ||
eventText, | ||
}) | ||
} | ||
|
||
async function showNotification( | ||
accountId: number, | ||
chatId: number, | ||
messageId: number | ||
messageId: number, | ||
isWebxdcInfo: boolean, | ||
eventText?: string | ||
) { | ||
const tx = window.static_translate | ||
|
||
|
@@ -81,11 +115,50 @@ async function showNotification( | |
try { | ||
const notificationInfo = | ||
await BackendRemote.rpc.getMessageNotificationInfo(accountId, messageId) | ||
const { chatName, summaryPrefix, summaryText } = notificationInfo | ||
let summaryPrefix = notificationInfo.summaryPrefix | ||
const summaryText = eventText ?? notificationInfo.summaryText | ||
const chatName = notificationInfo.chatName | ||
let icon = getNotificationIcon(notificationInfo) | ||
if (isWebxdcInfo && chatId === -1) { | ||
WofWca marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* messageId may refer to a webxdc message OR a wexdc-info-message! | ||
* | ||
* a notification might be sent even when no webxdc-info-message was | ||
* added to the chat; in that case the msg_id refers to the webxdc instance | ||
*/ | ||
let message = await BackendRemote.rpc.getMessage(accountId, messageId) | ||
if ( | ||
message.systemMessageType === 'WebxdcInfoMessage' && | ||
message.parentId | ||
) { | ||
// we have to get the parent message | ||
// (the webxdc message which holds the webxdcInfo) | ||
message = await BackendRemote.rpc.getMessage( | ||
accountId, | ||
message.parentId | ||
) | ||
} | ||
if (message.webxdcInfo) { | ||
summaryPrefix = `${message.webxdcInfo.name}` | ||
nicodh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (message.webxdcInfo.icon) { | ||
const iconName = message.webxdcInfo.icon | ||
const iconBlob = await BackendRemote.rpc.getWebxdcBlob( | ||
accountId, | ||
message.id, | ||
iconName | ||
) | ||
// needed for valid dataUrl | ||
const imageExtension = iconName.split('.').pop() | ||
icon = `data:image/${imageExtension};base64,${iconBlob}` | ||
} | ||
} else { | ||
throw new Error(`no webxdcInfo in message with id ${message.id}`) | ||
} | ||
} | ||
runtime.showNotification({ | ||
title: chatName, | ||
body: summaryPrefix ? `${summaryPrefix}: ${summaryText}` : summaryText, | ||
icon: getNotificationIcon(notificationInfo), | ||
icon, | ||
chatId, | ||
messageId, | ||
accountId, | ||
|
@@ -174,6 +247,14 @@ async function flushNotifications(accountId: number) { | |
let notifications = [...queuedNotifications[accountId]] | ||
queuedNotifications = [] | ||
|
||
for await (const n of notifications) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if (n.isWebxdcInfo) { | ||
// get real chatId of the webxdc message | ||
const message = await BackendRemote.rpc.getMessage(accountId, n.messageId) | ||
n.chatId = message.chatId | ||
} | ||
} | ||
|
||
// filter out muted chats: | ||
const uniqueChats = [...new Set(notifications.map(n => n.chatId))] | ||
const mutedChats = ( | ||
|
@@ -202,8 +283,19 @@ async function flushNotifications(accountId: number) { | |
if (notifications.length > notificationLimit) { | ||
showGroupedNotification(accountId, notifications) | ||
} else { | ||
for (const { chatId, messageId } of notifications) { | ||
await showNotification(accountId, chatId, messageId) | ||
for (const { | ||
chatId, | ||
messageId, | ||
isWebxdcInfo, | ||
eventText, | ||
} of notifications) { | ||
await showNotification( | ||
accountId, | ||
chatId, | ||
messageId, | ||
isWebxdcInfo ?? false, | ||
eventText | ||
) | ||
} | ||
} | ||
notificationLimit = NORMAL_LIMIT | ||
|
@@ -231,12 +323,3 @@ function getNotificationIcon( | |
return null | ||
} | ||
} | ||
|
||
export function initNotifications() { | ||
BackendRemote.on('IncomingMsg', (accountId, { chatId, msgId }) => { | ||
incomingMessageHandler(accountId, chatId, msgId) | ||
}) | ||
BackendRemote.on('IncomingMsgBunch', accountId => { | ||
flushNotifications(accountId) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is chatID
-1
here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah because it isn't there yet.... I'd rather use
null
orundefined
to represent that state.Or add the chat id to the
IncomingWebxdcNotify
event in core.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thinking about it more it makes sense to add it to the event, because It already has contact id.
https://github.com/deltachat/deltachat-core-rust/blob/191eb7efdd2765af0b9440a1316e850ed321b2b2/src/events/payload.rs#L111-L123