33
44import { createLogger } from '../logging/log.std.js' ;
55
6- import { isIncoming , isOutgoing } from './helpers.std.js' ;
6+ import { isOutgoing } from './helpers.std.js' ;
77import { getAuthor } from './sources.preload.js' ;
88
99import type { ConversationModel } from '../models/conversations.preload.js' ;
@@ -17,6 +17,10 @@ import { notificationService } from '../services/notifications.preload.js';
1717import { getNotificationTextForMessage } from '../util/getNotificationTextForMessage.preload.js' ;
1818import type { MessageAttributesType } from '../model-types.d.ts' ;
1919import type { ReactionAttributesType } from '../messageModifiers/Reactions.preload.js' ;
20+ import {
21+ type PollVoteAttributesType ,
22+ PollSource ,
23+ } from '../messageModifiers/Polls.preload.js' ;
2024import { shouldStoryReplyNotifyUser } from '../util/shouldStoryReplyNotifyUser.preload.js' ;
2125import { ReactionSource } from '../reactions/ReactionSource.std.js' ;
2226
@@ -29,24 +33,53 @@ type MaybeNotifyArgs = {
2933 reaction : Readonly < ReactionAttributesType > ;
3034 targetMessage : Readonly < MessageAttributesType > ;
3135 }
32- | { message : Readonly < MessageAttributesType > ; reaction ?: never }
36+ | {
37+ pollVote : Readonly < PollVoteAttributesType > ;
38+ targetMessage : Readonly < MessageAttributesType > ;
39+ }
40+ | {
41+ message : Readonly < MessageAttributesType > ;
42+ reaction ?: never ;
43+ pollVote ?: never ;
44+ }
3345) ;
3446
47+ function isMention ( args : MaybeNotifyArgs ) : boolean {
48+ if ( 'reaction' in args || 'pollVote' in args ) {
49+ return false ;
50+ }
51+ return Boolean ( args . message . mentionsMe ) ;
52+ }
53+
3554export async function maybeNotify ( args : MaybeNotifyArgs ) : Promise < void > {
3655 if ( ! notificationService . isEnabled ) {
3756 return ;
3857 }
3958
4059 const { i18n } = window . SignalContext ;
4160
42- const { conversation, reaction } = args ;
61+ const { conversation } = args ;
62+ const reaction = 'reaction' in args ? args . reaction : undefined ;
63+ const pollVote = 'pollVote' in args ? args . pollVote : undefined ;
4364
4465 let warrantsNotification : boolean ;
45- if ( reaction ) {
46- warrantsNotification = doesReactionWarrantNotification ( args ) ;
66+ if ( 'reaction' in args && 'targetMessage' in args ) {
67+ warrantsNotification = doesReactionWarrantNotification ( {
68+ reaction : args . reaction ,
69+ targetMessage : args . targetMessage ,
70+ } ) ;
71+ } else if ( 'pollVote' in args && 'targetMessage' in args ) {
72+ warrantsNotification = doesPollVoteWarrantNotification ( {
73+ pollVote : args . pollVote ,
74+ targetMessage : args . targetMessage ,
75+ } ) ;
4776 } else {
48- warrantsNotification = await doesMessageWarrantNotification ( args ) ;
77+ warrantsNotification = await doesMessageWarrantNotification ( {
78+ message : args . message ,
79+ conversation,
80+ } ) ;
4981 }
82+
5083 if ( ! warrantsNotification ) {
5184 return ;
5285 }
@@ -56,29 +89,34 @@ export async function maybeNotify(args: MaybeNotifyArgs): Promise<void> {
5689 }
5790
5891 const activeProfile = getActiveProfile ( window . reduxStore . getState ( ) ) ;
92+
5993 if (
6094 ! shouldNotifyDuringNotificationProfile ( {
6195 activeProfile,
6296 conversationId : conversation . id ,
6397 isCall : false ,
64- isMention : args . reaction ? false : Boolean ( args . message . mentionsMe ) ,
98+ isMention : isMention ( args ) ,
6599 } )
66100 ) {
67101 log . info ( 'Would notify for message, but notification profile prevented it' ) ;
68102 return ;
69103 }
70104
71105 const conversationId = conversation . get ( 'id' ) ;
72- const messageForNotification = args . reaction
73- ? args . targetMessage
74- : args . message ;
106+ const messageForNotification =
107+ 'targetMessage' in args ? args . targetMessage : args . message ;
75108 const isMessageInDirectConversation = isDirectConversation (
76109 conversation . attributes
77110 ) ;
78111
79- const sender = reaction
80- ? window . ConversationController . get ( reaction . fromId )
81- : getAuthor ( args . message ) ;
112+ let sender : ConversationModel | undefined ;
113+ if ( reaction ) {
114+ sender = window . ConversationController . get ( reaction . fromId ) ;
115+ } else if ( pollVote ) {
116+ sender = window . ConversationController . get ( pollVote . fromConversationId ) ;
117+ } else if ( 'message' in args ) {
118+ sender = getAuthor ( args . message ) ;
119+ }
82120 const senderName = sender ? sender . getTitle ( ) : i18n ( 'icu:unknownContact' ) ;
83121 const senderTitle = isMessageInDirectConversation
84122 ? senderName
@@ -110,6 +148,13 @@ export async function maybeNotify(args: MaybeNotifyArgs): Promise<void> {
110148 targetTimestamp : reaction . targetTimestamp ,
111149 }
112150 : undefined ,
151+ pollVote : pollVote
152+ ? {
153+ voterConversationId : pollVote . fromConversationId ,
154+ targetAuthorAci : pollVote . targetAuthorAci ,
155+ targetTimestamp : pollVote . targetTimestamp ,
156+ }
157+ : undefined ,
113158 sentAt : messageForNotification . timestamp ,
114159 type : reaction ? NotificationType . Reaction : NotificationType . Message ,
115160 } ) ;
@@ -128,14 +173,26 @@ function doesReactionWarrantNotification({
128173 ) ;
129174}
130175
176+ function doesPollVoteWarrantNotification ( {
177+ pollVote,
178+ targetMessage,
179+ } : {
180+ targetMessage : MessageAttributesType ;
181+ pollVote : PollVoteAttributesType ;
182+ } ) : boolean {
183+ return (
184+ pollVote . source === PollSource . FromSomeoneElse && isOutgoing ( targetMessage )
185+ ) ;
186+ }
187+
131188async function doesMessageWarrantNotification ( {
132189 message,
133190 conversation,
134191} : {
135192 message : MessageAttributesType ;
136193 conversation : ConversationModel ;
137194} ) : Promise < boolean > {
138- if ( ! isIncoming ( message ) ) {
195+ if ( ! ( message . type === 'incoming' || message . type === 'poll-terminate' ) ) {
139196 return false ;
140197 }
141198
@@ -154,19 +211,15 @@ async function doesMessageWarrantNotification({
154211}
155212
156213function isAllowedByConversation ( args : MaybeNotifyArgs ) : boolean {
157- const { conversation, reaction } = args ;
214+ const { conversation } = args ;
158215
159216 if ( ! conversation . isMuted ( ) ) {
160217 return true ;
161218 }
162219
163- if ( reaction ) {
164- return false ;
165- }
166-
167220 if ( conversation . get ( 'dontNotifyForMentionsIfMuted' ) ) {
168221 return false ;
169222 }
170223
171- return args . message . mentionsMe === true ;
224+ return isMention ( args ) ;
172225}
0 commit comments