@@ -38,8 +38,8 @@ export type ShowActionSheetWithOptions = (
38
38
39
39
type TopicArgs = {
40
40
auth : Auth ,
41
- stream : string ,
42
41
topic : string ,
42
+ streamId : number ,
43
43
subscriptions : Subscription [ ] ,
44
44
dispatch : Dispatch ,
45
45
_ : GetText ,
@@ -119,34 +119,25 @@ const deleteMessage = async ({ auth, message, dispatch }) => {
119
119
deleteMessage . title = 'Delete message' ;
120
120
deleteMessage . errorMessage = 'Failed to delete message' ;
121
121
122
- const markTopicAsRead = async ( { auth, stream, topic, subscriptions } ) => {
123
- const sub = subscriptions . find ( x => x . name === stream ) ;
124
- if ( sub ) {
125
- await api . markTopicAsRead ( auth , sub . stream_id , topic ) ;
126
- }
122
+ const markTopicAsRead = async ( { auth, streamId, topic } ) => {
123
+ await api . markTopicAsRead ( auth , streamId , topic ) ;
127
124
} ;
128
125
markTopicAsRead . title = 'Mark topic as read' ;
129
126
markTopicAsRead . errorMessage = 'Failed to mark topic as read' ;
130
127
131
- const unmuteTopic = async ( { auth, stream, topic, subscriptions } ) => {
132
- const sub = subscriptions . find ( x => x . name === stream ) ;
133
- if ( sub ) {
134
- await api . unmuteTopic ( auth , sub . stream_id , topic ) ;
135
- }
128
+ const unmuteTopic = async ( { auth, streamId, topic } ) => {
129
+ await api . unmuteTopic ( auth , streamId , topic ) ;
136
130
} ;
137
131
unmuteTopic . title = 'Unmute topic' ;
138
132
unmuteTopic . errorMessage = 'Failed to unmute topic' ;
139
133
140
- const muteTopic = async ( { auth, stream, topic, subscriptions } ) => {
141
- const sub = subscriptions . find ( x => x . name === stream ) ;
142
- if ( sub ) {
143
- await api . muteTopic ( auth , sub . stream_id , topic ) ;
144
- }
134
+ const muteTopic = async ( { auth, streamId, topic } ) => {
135
+ await api . muteTopic ( auth , streamId , topic ) ;
145
136
} ;
146
137
muteTopic . title = 'Mute topic' ;
147
138
muteTopic . errorMessage = 'Failed to mute topic' ;
148
139
149
- const deleteTopic = async ( { auth, stream , topic, subscriptions , dispatch, _ } ) => {
140
+ const deleteTopic = async ( { auth, streamId , topic, dispatch, _ } ) => {
150
141
const alertTitle = _ ( 'Are you sure you want to delete the topic “{topic}”?' , { topic } ) ;
151
142
const AsyncAlert = async ( ) : Promise < boolean > =>
152
143
new Promise ( ( resolve , reject ) => {
@@ -173,38 +164,26 @@ const deleteTopic = async ({ auth, stream, topic, subscriptions, dispatch, _ })
173
164
) ;
174
165
} ) ;
175
166
if ( await AsyncAlert ( ) ) {
176
- const sub = subscriptions . find ( x => x . name === stream ) ;
177
- if ( sub ) {
178
- await dispatch ( deleteMessagesForTopic ( sub . stream_id , topic ) ) ;
179
- }
167
+ await dispatch ( deleteMessagesForTopic ( streamId , topic ) ) ;
180
168
}
181
169
} ;
182
170
deleteTopic . title = 'Delete topic' ;
183
171
deleteTopic . errorMessage = 'Failed to delete topic' ;
184
172
185
- const unmuteStream = async ( { auth, stream, subscriptions } ) => {
186
- const sub = subscriptions . find ( x => x . name === stream ) ;
187
- if ( sub ) {
188
- await api . toggleMuteStream ( auth , sub . stream_id , false ) ;
189
- }
173
+ const unmuteStream = async ( { auth, streamId } ) => {
174
+ await api . toggleMuteStream ( auth , streamId , false ) ;
190
175
} ;
191
176
unmuteStream . title = 'Unmute stream' ;
192
177
unmuteStream . errorMessage = 'Failed to unmute stream' ;
193
178
194
- const muteStream = async ( { auth, stream, subscriptions } ) => {
195
- const sub = subscriptions . find ( x => x . name === stream ) ;
196
- if ( sub ) {
197
- await api . toggleMuteStream ( auth , sub . stream_id , true ) ;
198
- }
179
+ const muteStream = async ( { auth, streamId } ) => {
180
+ await api . toggleMuteStream ( auth , streamId , true ) ;
199
181
} ;
200
182
muteStream . title = 'Mute stream' ;
201
183
muteStream . errorMessage = 'Failed to mute stream' ;
202
184
203
- const showStreamSettings = ( { stream, subscriptions } ) => {
204
- const sub = subscriptions . find ( x => x . name === stream ) ;
205
- if ( sub ) {
206
- NavigationService . dispatch ( navigateToStream ( sub . stream_id ) ) ;
207
- }
185
+ const showStreamSettings = ( { streamId } ) => {
186
+ NavigationService . dispatch ( navigateToStream ( streamId ) ) ;
208
187
} ;
209
188
showStreamSettings . title = 'Stream settings' ;
210
189
showStreamSettings . errorMessage = 'Failed to show stream settings' ;
@@ -247,8 +226,8 @@ cancel.errorMessage = 'Failed to hide menu';
247
226
248
227
export const constructTopicActionButtons = ( {
249
228
backgroundData : { mute, subscriptions, ownUser, unreadStreams } ,
250
- stream,
251
229
topic,
230
+ streamId,
252
231
} : { |
253
232
backgroundData : $ReadOnly < {
254
233
mute : MuteState ,
@@ -257,29 +236,27 @@ export const constructTopicActionButtons = ({
257
236
unreadStreams : UnreadStreamsState ,
258
237
...
259
238
} > ,
260
- stream : string ,
239
+ streamId : number ,
261
240
topic : string ,
262
241
| } ) : Button < TopicArgs > [ ] = > {
242
+ const sub = subscriptions . find ( x => x . stream_id === streamId ) ;
263
243
const buttons = [ ] ;
264
244
if ( ownUser . is_admin ) {
265
245
buttons . push ( deleteTopic ) ;
266
246
}
267
- if ( isTopicMuted ( stream , topic , mute ) ) {
247
+ if ( sub && isTopicMuted ( sub . name , topic , mute ) ) {
268
248
buttons . push ( unmuteTopic ) ;
269
249
} else {
270
250
buttons . push ( muteTopic ) ;
271
251
}
272
- const sub = subscriptions . find ( x => x . name === stream ) ;
273
- if ( sub ) {
274
- const unreadCount = unreadStreams . get ( sub . stream_id ) ?. get ( topic ) ?. size ;
275
- if ( unreadCount !== undefined && unreadCount > 0 ) {
276
- buttons . push ( markTopicAsRead ) ;
277
- }
278
- if ( ! sub . in_home_view ) {
279
- buttons . push ( unmuteStream ) ;
280
- } else {
281
- buttons . push ( muteStream ) ;
282
- }
252
+ const unreadCount = unreadStreams . get ( streamId ) ?. get ( topic ) ?. size ;
253
+ if ( unreadCount !== undefined && unreadCount > 0 ) {
254
+ buttons . push ( markTopicAsRead ) ;
255
+ }
256
+ if ( sub && ! sub . in_home_view ) {
257
+ buttons . push ( unmuteStream ) ;
258
+ } else {
259
+ buttons . push ( muteStream ) ;
283
260
}
284
261
buttons . push ( showStreamSettings ) ;
285
262
buttons . push ( cancel ) ;
@@ -420,7 +397,7 @@ export const showTopicActionSheet = ({
420
397
callbacks ,
421
398
backgroundData ,
422
399
topic ,
423
- stream ,
400
+ streamId ,
424
401
} : { |
425
402
showActionSheetWithOptions : ShowActionSheetWithOptions ,
426
403
callbacks : { |
@@ -436,12 +413,22 @@ export const showTopicActionSheet = ({
436
413
unreadStreams : UnreadStreamsState ,
437
414
...
438
415
} > ,
439
- stream : string ,
416
+ streamId : number ,
440
417
topic : string ,
441
418
| } ): void => {
419
+ const sub = backgroundData . subscriptions . find ( x => x . stream_id === streamId ) ;
420
+ try {
421
+ if ( ! sub ) {
422
+ throw Error ( 'Stream id does not exist.' ) ;
423
+ }
424
+ } catch ( err ) {
425
+ logging . error ( err ) ;
426
+ return ;
427
+ }
428
+ const stream = sub.name;
442
429
const buttonList = constructTopicActionButtons({
443
430
backgroundData ,
444
- stream ,
431
+ streamId ,
445
432
topic ,
446
433
} );
447
434
showActionSheetWithOptions(
@@ -453,7 +440,7 @@ export const showTopicActionSheet = ({
453
440
makeButtonCallback ( buttonList , {
454
441
...backgroundData ,
455
442
...callbacks ,
456
- stream ,
443
+ streamId ,
457
444
topic,
458
445
} ) ,
459
446
) ;
0 commit comments