Skip to content

Commit 45cee80

Browse files
committed
ActionSheet [nfc]: Drop streamName from signature.
Also involves necessary changes at call sites, and some actionsheet functions.
1 parent 62678a1 commit 45cee80

File tree

5 files changed

+31
-39
lines changed

5 files changed

+31
-39
lines changed

src/message/__tests__/messageActionSheet-test.js

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// @flow strict-local
22
import deepFreeze from 'deep-freeze';
33
import { HOME_NARROW } from '../../utils/narrow';
4-
import { streamNameOfStreamMessage } from '../../utils/recipient';
54

65
import * as eg from '../../__tests__/lib/exampleData';
76
import { constructMessageActionButtons, constructTopicActionButtons } from '../messageActionSheet';
@@ -46,10 +45,11 @@ describe('constructActionButtons', () => {
4645
});
4746

4847
describe('constructTopicActionButtons', () => {
49-
const streamMessage = eg.streamMessage();
50-
const streamName = streamNameOfStreamMessage(streamMessage);
48+
const stream = eg.makeStream();
49+
const streamMessage = eg.streamMessage({ stream });
5150
const topic = streamMessage.subject;
5251
const streamId = streamMessage.stream_id;
52+
const streams = deepFreeze(new Map([[stream.stream_id, stream]]));
5353

5454
const baseState = (() => {
5555
const r = (state, action) => reducer(state, action, eg.plusReduxState);
@@ -61,8 +61,7 @@ describe('constructTopicActionButtons', () => {
6161
test('show mark as read if topic is unread', () => {
6262
const unread = baseState;
6363
const buttons = constructTopicActionButtons({
64-
backgroundData: { ...eg.backgroundData, unread },
65-
streamName,
64+
backgroundData: { ...eg.backgroundData, streams, unread },
6665
streamId,
6766
topic,
6867
});
@@ -71,19 +70,17 @@ describe('constructTopicActionButtons', () => {
7170

7271
test('do not show mark as read if topic is read', () => {
7372
const buttons = constructTopicActionButtons({
74-
backgroundData: eg.backgroundData,
75-
streamName,
73+
backgroundData: { ...eg.backgroundData, streams },
7674
streamId,
7775
topic,
7876
});
7977
expect(buttonTitles(buttons)).not.toContain('Mark topic as read');
8078
});
8179

8280
test('show Unmute topic option if topic is muted', () => {
83-
const mute = deepFreeze([[streamName, topic]]);
81+
const mute = deepFreeze([[stream.name, topic]]);
8482
const buttons = constructTopicActionButtons({
85-
backgroundData: { ...eg.backgroundData, mute },
86-
streamName,
83+
backgroundData: { ...eg.backgroundData, streams, mute },
8784
streamId,
8885
topic,
8986
});
@@ -92,30 +89,27 @@ describe('constructTopicActionButtons', () => {
9289

9390
test('show mute topic option if topic is not muted', () => {
9491
const buttons = constructTopicActionButtons({
95-
backgroundData: { ...eg.backgroundData, mute: [] },
96-
streamName,
92+
backgroundData: { ...eg.backgroundData, streams, mute: [] },
9793
streamId,
9894
topic,
9995
});
10096
expect(buttonTitles(buttons)).toContain('Mute topic');
10197
});
10298

10399
test('show Unmute stream option if stream is not in home view', () => {
104-
const subscriptions = [{ ...eg.subscription, in_home_view: false }];
100+
const subscriptions = [{ ...eg.subscription, in_home_view: false, ...stream }];
105101
const buttons = constructTopicActionButtons({
106-
backgroundData: { ...eg.backgroundData, subscriptions },
107-
streamName,
102+
backgroundData: { ...eg.backgroundData, subscriptions, streams },
108103
streamId,
109104
topic,
110105
});
111106
expect(buttonTitles(buttons)).toContain('Unmute stream');
112107
});
113108

114109
test('show mute stream option if stream is in home view', () => {
115-
const subscriptions = [{ ...eg.subscription, in_home_view: true }];
110+
const subscriptions = [{ ...eg.subscription, in_home_view: true, ...stream }];
116111
const buttons = constructTopicActionButtons({
117-
backgroundData: { ...eg.backgroundData, subscriptions },
118-
streamName,
112+
backgroundData: { ...eg.backgroundData, subscriptions, streams },
119113
streamId,
120114
topic,
121115
});
@@ -125,8 +119,7 @@ describe('constructTopicActionButtons', () => {
125119
test('show delete topic option if current user is an admin', () => {
126120
const ownUser = { ...eg.selfUser, is_admin: true };
127121
const buttons = constructTopicActionButtons({
128-
backgroundData: { ...eg.backgroundData, ownUser },
129-
streamName,
122+
backgroundData: { ...eg.backgroundData, ownUser, streams },
130123
streamId,
131124
topic,
132125
});
@@ -135,8 +128,7 @@ describe('constructTopicActionButtons', () => {
135128

136129
test('do not show delete topic option if current user is not an admin', () => {
137130
const buttons = constructTopicActionButtons({
138-
backgroundData: eg.backgroundData,
139-
streamName,
131+
backgroundData: { ...eg.backgroundData, streams },
140132
streamId,
141133
topic,
142134
});

src/message/messageActionSheet.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* @flow strict-local */
22
import { Clipboard, Share, Alert } from 'react-native';
3+
import invariant from 'invariant';
34

45
import * as NavigationService from '../nav/NavigationService';
56
import type {
@@ -40,10 +41,10 @@ export type ShowActionSheetWithOptions = (
4041

4142
type TopicArgs = {
4243
auth: Auth,
43-
streamName: string,
4444
streamId: number,
4545
topic: string,
4646
subscriptions: Subscription[],
47+
streams: Map<number, Stream>,
4748
dispatch: Dispatch,
4849
_: GetText,
4950
...
@@ -128,14 +129,18 @@ const markTopicAsRead = async ({ auth, streamId, topic }) => {
128129
markTopicAsRead.title = 'Mark topic as read';
129130
markTopicAsRead.errorMessage = 'Failed to mark topic as read';
130131

131-
const unmuteTopic = async ({ auth, streamName, topic }) => {
132-
await api.setTopicMute(auth, streamName, topic, false);
132+
const unmuteTopic = async ({ auth, streamId, topic, streams }) => {
133+
const stream = streams.get(streamId);
134+
invariant(stream !== undefined, 'Stream with provided streamId must exist.');
135+
await api.setTopicMute(auth, stream.name, topic, false);
133136
};
134137
unmuteTopic.title = 'Unmute topic';
135138
unmuteTopic.errorMessage = 'Failed to unmute topic';
136139

137-
const muteTopic = async ({ auth, streamName, topic }) => {
138-
await api.setTopicMute(auth, streamName, topic, true);
140+
const muteTopic = async ({ auth, streamId, topic, streams }) => {
141+
const stream = streams.get(streamId);
142+
invariant(stream !== undefined, 'Stream with provided streamId must exist.');
143+
await api.setTopicMute(auth, stream.name, topic, true);
139144
};
140145
muteTopic.title = 'Mute topic';
141146
muteTopic.errorMessage = 'Failed to mute topic';
@@ -229,7 +234,6 @@ cancel.errorMessage = 'Failed to hide menu';
229234

230235
export const constructTopicActionButtons = ({
231236
backgroundData: { mute, ownUser, streams, subscriptions, unread },
232-
streamName,
233237
streamId,
234238
topic,
235239
}: {|
@@ -241,7 +245,6 @@ export const constructTopicActionButtons = ({
241245
ownUser: User,
242246
...
243247
}>,
244-
streamName: string,
245248
streamId: number,
246249
topic: string,
247250
|}): Button<TopicArgs>[] => {
@@ -253,12 +256,14 @@ export const constructTopicActionButtons = ({
253256
if (unreadCount > 0) {
254257
buttons.push(markTopicAsRead);
255258
}
256-
if (isTopicMuted(streamName, topic, mute)) {
259+
const stream = streams.get(streamId);
260+
invariant(stream !== undefined, 'Stream with provided streamId not found.');
261+
if (isTopicMuted(stream.name, topic, mute)) {
257262
buttons.push(unmuteTopic);
258263
} else {
259264
buttons.push(muteTopic);
260265
}
261-
const sub = subscriptions.find(x => x.name === streamName);
266+
const sub = subscriptions.find(x => x.stream_id === streamId);
262267
if (sub && !sub.in_home_view) {
263268
buttons.push(unmuteStream);
264269
} else {
@@ -403,7 +408,6 @@ export const showTopicActionSheet = ({
403408
callbacks,
404409
backgroundData,
405410
topic,
406-
streamName,
407411
streamId,
408412
}: {|
409413
showActionSheetWithOptions: ShowActionSheetWithOptions,
@@ -421,26 +425,25 @@ export const showTopicActionSheet = ({
421425
flags: FlagsState,
422426
...
423427
}>,
424-
streamName: string,
425428
streamId: number,
426429
topic: string,
427430
|}): void => {
428431
const buttonList = constructTopicActionButtons({
429432
backgroundData,
430-
streamName,
431433
streamId,
432434
topic,
433435
});
436+
const stream = backgroundData.streams.get(streamId);
437+
invariant(stream !== undefined, 'Stream with provided streamId not found.');
434438
showActionSheetWithOptions(
435439
{
436-
title: `#${streamName} > ${topic}`,
440+
title: `#${stream.name} > ${topic}`,
437441
options: buttonList.map(button => callbacks._(button.title)),
438442
cancelButtonIndex: buttonList.length - 1,
439443
},
440444
makeButtonCallback(buttonList, {
441445
...backgroundData,
442446
...callbacks,
443-
streamName,
444447
streamId,
445448
topic,
446449
}),

src/streams/TopicItem.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ export default function TopicItem(props: Props) {
7474
showActionSheetWithOptions,
7575
callbacks: { dispatch, _ },
7676
backgroundData,
77-
streamName,
7877
streamId: stream.stream_id,
7978
topic: name,
8079
});

src/title/TitleStream.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ const TitleStream = (props: Props) => {
8282
showActionSheetWithOptions,
8383
callbacks: { dispatch, _ },
8484
backgroundData,
85-
streamName: stream.name,
8685
streamId: stream.stream_id,
8786
topic: topicOfNarrow(narrow),
8887
});

src/webview/handleOutboundEvents.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,6 @@ const handleLongPress = (
220220
showActionSheetWithOptions,
221221
callbacks: { dispatch, _ },
222222
backgroundData,
223-
streamName,
224223
streamId: stream.stream_id,
225224
topic: message.subject,
226225
});

0 commit comments

Comments
 (0)