Skip to content

Commit

Permalink
topicActions [nfc]: Update deleteMessagesForTopic to use streamId.
Browse files Browse the repository at this point in the history
This also involved changing the call to `deleteMessagesForTopic`
appropriately.
The change is done to facilitate migration to `stream_id` instead of
stream name for identifying streams.

Related: #3918
  • Loading branch information
AkashDhiman committed May 12, 2021
1 parent 2e9e6b0 commit 1acb3e8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
7 changes: 5 additions & 2 deletions src/message/messageActionSheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ const muteTopic = async ({ auth, stream, topic, subscriptions }) => {
muteTopic.title = 'Mute topic';
muteTopic.errorMessage = 'Failed to mute topic';

const deleteTopic = async ({ auth, stream, topic, dispatch, _ }) => {
const deleteTopic = async ({ auth, stream, topic, subscriptions, dispatch, _ }) => {
const alertTitle = _('Are you sure you want to delete the topic “{topic}”?', { topic });
const AsyncAlert = async (): Promise<boolean> =>
new Promise((resolve, reject) => {
Expand All @@ -173,7 +173,10 @@ const deleteTopic = async ({ auth, stream, topic, dispatch, _ }) => {
);
});
if (await AsyncAlert()) {
await dispatch(deleteMessagesForTopic(stream, topic));
const sub = subscriptions.find(x => x.name === stream);
if (sub) {
await dispatch(deleteMessagesForTopic(sub.stream_id, topic));
}
}
};
deleteTopic.title = 'Delete topic';
Expand Down
15 changes: 7 additions & 8 deletions src/topics/topicActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,24 @@ export const fetchTopicsForStream = (narrow: Narrow) => async (
dispatch(fetchTopics(stream.stream_id));
};

export const deleteMessagesForTopic = (streamName: string, topic: string) => async (
export const deleteMessagesForTopic = (streamId: number, topic: string) => async (
dispatch: Dispatch,
getState: GetState,
) => {
const state = getState();
const outbox = getOutbox(state);
const currentStream: Stream | void = getStreams(state).find(
(stream: Stream) => stream.stream_id === streamId,
);

outbox.forEach((outboxMessage: Outbox) => {
if (
outboxMessage.type === 'stream'
&& streamNameOfStreamMessage(outboxMessage) === streamName
&& streamNameOfStreamMessage(outboxMessage) === currentStream?.name
&& outboxMessage.subject === topic
) {
dispatch(deleteOutboxMessage(outboxMessage.id));
}
});
const currentStream: Stream | void = getStreams(state).find(
(stream: Stream) => stream.name === streamName,
);
if (currentStream) {
await api.deleteTopic(getAuth(state), currentStream.stream_id, topic);
}
await api.deleteTopic(getAuth(state), streamId, topic);
};

0 comments on commit 1acb3e8

Please sign in to comment.