From 1acb3e88d3feb1b5a9a6097650e6aa5768dea81d Mon Sep 17 00:00:00 2001 From: Akash Date: Wed, 5 May 2021 23:30:57 +0530 Subject: [PATCH] topicActions [nfc]: Update `deleteMessagesForTopic` to use `streamId`. 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 --- src/message/messageActionSheet.js | 7 +++++-- src/topics/topicActions.js | 15 +++++++-------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/message/messageActionSheet.js b/src/message/messageActionSheet.js index c9423033819..bb8df3c0edd 100644 --- a/src/message/messageActionSheet.js +++ b/src/message/messageActionSheet.js @@ -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 => new Promise((resolve, reject) => { @@ -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'; diff --git a/src/topics/topicActions.js b/src/topics/topicActions.js index 521410a8f42..04c24aec2a0 100644 --- a/src/topics/topicActions.js +++ b/src/topics/topicActions.js @@ -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); };