Skip to content

Commit

Permalink
action sheet: Add delete topic feature.
Browse files Browse the repository at this point in the history
The webapp offers the feature to delete all messages within a
topic for a stream. This feature is missing in the mobile app.

Create action `deleteMessagesForTopic()`. Create API function
'deleteTopic'. Add 'Delete topic' action in message action sheet.
  • Loading branch information
agrawal-d committed Feb 8, 2020
1 parent 4ef9a13 commit b366b10
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import markAllAsRead from './mark_as_read/markAllAsRead';
import markStreamAsRead from './mark_as_read/markStreamAsRead';
import markTopicAsRead from './mark_as_read/markTopicAsRead';
import deleteMessage from './messages/deleteMessage';
import deleteTopic from './messages/deleteTopic';
import getRawMessageContent from './messages/getRawMessageContent';
import getMessages from './messages/getMessages';
import getMessageHistory from './messages/getMessageHistory';
Expand Down Expand Up @@ -72,6 +73,7 @@ export {
markStreamAsRead,
markTopicAsRead,
deleteMessage,
deleteTopic,
getRawMessageContent,
getMessages,
getMessageHistory,
Expand Down
11 changes: 11 additions & 0 deletions src/api/messages/deleteTopic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* @flow strict-local */
import type { ApiResponse, Auth } from '../transportTypes';
import { apiPost } from '../apiFetch';

/**
* Delete all messages in a stream for a given topic.
*/
export default async (auth: Auth, streamId: number, topicName: string): Promise<ApiResponse> =>
apiPost(auth, `streams/${streamId}/delete_topic`, {
topic_name: topicName,
});
35 changes: 34 additions & 1 deletion src/message/messageActionSheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as api from '../api';
import { showToast } from '../utils/info';
import { doNarrow, startEditMessage, deleteOutboxMessage, navigateToEmojiPicker } from '../actions';
import { navigateToMessageReactionScreen } from '../nav/navActions';
import { deleteMessagesForTopic } from '../topics/topicActions';

// TODO really this belongs in a libdef.
export type ShowActionSheetWithOptions = (
Expand Down Expand Up @@ -90,6 +91,34 @@ const muteTopic = async ({ auth, message }) => {
muteTopic.title = 'Mute topic';
muteTopic.errorMessage = 'Failed to mute topic';

const deleteTopic = async ({ auth, message, dispatch, ownEmail, _ }) => {
Alert.alert(
`Are you sure you want to delete the topic '${message.subject}' ?`,
'This will also delete all messages in the topic.',
[
{
text: 'Delete topic',
onPress: async () => {
dispatch(deleteMessagesForTopic(getNarrowFromMessage(message, ownEmail), message)).catch(
err => {
Alert.alert(`${_(deleteTopic.errorMessage)} '${message.subject}'`, err.message);
},
);
},
style: 'destructive',
},
{
text: 'Cancel',
onPress: () => {},
style: 'cancel',
},
],
{ cancelable: true },
);
};
deleteTopic.title = 'Delete topic';
deleteTopic.errorMessage = 'Failed to delete topic';

const unmuteStream = async ({ auth, message, subscriptions }) => {
const sub = subscriptions.find(x => x.name === message.display_recipient);
if (sub) {
Expand Down Expand Up @@ -159,6 +188,7 @@ const allButtonsRaw = {
// For headers
unmuteTopic,
muteTopic,
deleteTopic,
muteStream,
unmuteStream,

Expand All @@ -181,11 +211,14 @@ type ConstructSheetParams = {|
|};

export const constructHeaderActionButtons = ({
backgroundData: { mute, subscriptions },
backgroundData: { mute, subscriptions, ownUser },
message,
}: ConstructSheetParams): ButtonCode[] => {
const buttons: ButtonCode[] = [];
if (message.type === 'stream') {
if (ownUser.is_admin) {
buttons.push('deleteTopic');
}
if (isTopicMuted(message.display_recipient, message.subject, mute)) {
buttons.push('unmuteTopic');
} else {
Expand Down
28 changes: 27 additions & 1 deletion src/topics/topicActions.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/* @flow strict-local */
import type { GetState, Dispatch, Narrow, Topic, Action } from '../types';
import type { GetState, Dispatch, Message, Narrow, Topic, Action, Outbox, Stream } from '../types';
import * as api from '../api';
import { INIT_TOPICS } from '../actionConstants';
import { isStreamNarrow } from '../utils/narrow';
import { getAuth, getStreams } from '../selectors';
import { deleteOutboxMessage } from '../actions';
import { getOutbox } from '../directSelectors';

export const initTopics = (topics: Topic[], streamId: number): Action => ({
type: INIT_TOPICS,
Expand Down Expand Up @@ -34,3 +36,27 @@ export const fetchTopicsForActiveStream = (narrow: Narrow) => async (
}
dispatch(fetchTopics(stream.stream_id));
};

export const deleteMessagesForTopic = (narrow: Narrow, message: Message | Outbox) => async (
dispatch: Dispatch,
getState: GetState,
) => {
const state = getState();
const outbox = getOutbox(state);
outbox.forEach((outboxMessage: Outbox) => {
if (
outboxMessage.display_recipient === narrow[0].operand
&& outboxMessage.subject === narrow[1].operand
) {
dispatch(deleteOutboxMessage(outboxMessage.id));
}
});
const currentStream: Stream | void = getStreams(state).find(
(stream: Stream) => stream.name === message.display_recipient,
);
if (currentStream) {
await api.deleteTopic(getAuth(state), currentStream.stream_id, narrow[1].operand).catch(err => {
throw err;
});
}
};
2 changes: 2 additions & 0 deletions static/translations/messages_en.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"Copy to clipboard": "Copy to clipboard",
"Link copied to clipboard": "Link copied to clipboard",
"Mute topic": "Mute topic",
"Delete topic": "Delete topic",
"Unmute topic": "Unmute topic",
"Mute stream": "Mute stream",
"Unmute stream": "Unmute stream",
Expand Down Expand Up @@ -135,6 +136,7 @@
"Failed to mute topic": "Failed to mute topic",
"Failed to mute stream": "Failed to mute stream",
"Failed to unmute stream": "Failed to unmute stream",
"Failed to delete topic": "Failed to delete topic",
"show": "show",
"hide": "hide",
"Debug": "Debug",
Expand Down

0 comments on commit b366b10

Please sign in to comment.