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.

Add 'Delete topic' button in message action sheet.

Fixes #3806.
  • Loading branch information
agrawal-d committed Feb 26, 2020
1 parent 2c6a7d6 commit d19a024
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/message/__tests__/messageActionSheet-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,32 @@ describe('constructHeaderActionButtons', () => {

expect(buttons).toContain('muteStream');
});

test('show delete topic option if current user is an admin', () => {
const backgroundData = deepFreeze({
...eg.backgroundData,
ownUser: {
...eg.backgroundData.ownUser,
is_admin: true,
},
});

const buttons = constructHeaderActionButtons({
backgroundData,
message: eg.streamMessage(),
narrow: eg.narrow,
});

expect(buttons).toContain('deleteTopic');
});

test('do not show delete topic option if current user is not an admin', () => {
const buttons = constructHeaderActionButtons({
backgroundData: eg.backgroundData,
message: eg.streamMessage(),
narrow: eg.narrow,
});

expect(buttons).not.toContain('deleteTopic');
});
});
46 changes: 45 additions & 1 deletion src/message/messageActionSheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { showToast } from '../utils/info';
import { doNarrow, startEditMessage, deleteOutboxMessage, navigateToEmojiPicker } from '../actions';
import { navigateToMessageReactionScreen } from '../nav/navActions';
import filteredRecipientsForPM from '../pm-conversations/filteredRecipientsForPM';
import { deleteMessagesForTopic } from '../topics/topicActions';

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

const deleteTopic = async ({ auth, message, dispatch, ownEmail, _ }) => {
const alertTitle = _.intl.formatMessage(
{
id: "Are you sure you want to delete the topic '{topic}'?",
defaultMessage: "Are you sure you want to delete the topic '{topic}'?",
},
{ topic: message.subject },
);
const AsyncAlert = async (): Promise<boolean> =>
new Promise((resolve, reject) => {
Alert.alert(
alertTitle,
_('This will also delete all messages in the topic.'),
[
{
text: _('Delete topic'),
onPress: () => {
resolve(true);
},
style: 'destructive',
},
{
text: _('Cancel'),
onPress: () => {
resolve(false);
},
style: 'cancel',
},
],
{ cancelable: true },
);
});
if (await AsyncAlert()) {
await dispatch(deleteMessagesForTopic(getNarrowFromMessage(message, ownEmail), message));
}
};
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 @@ -168,6 +208,7 @@ const allButtonsRaw = {
// For headers
unmuteTopic,
muteTopic,
deleteTopic,
muteStream,
unmuteStream,

Expand All @@ -190,11 +231,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
4 changes: 4 additions & 0 deletions static/translations/messages_en.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
"Copy to clipboard": "Copy to clipboard",
"Link copied to clipboard": "Link copied to clipboard",
"Mute topic": "Mute topic",
"Delete topic": "Delete topic",
"Are you sure you want to delete the topic '{topic}'?": "Are you sure you want to delete the topic '{topic}'?",
"This will also delete all messages in the topic.": "This will also delete all messages in the topic.",
"Unmute topic": "Unmute topic",
"Mute stream": "Mute stream",
"Unmute stream": "Unmute stream",
Expand Down Expand Up @@ -153,6 +156,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 d19a024

Please sign in to comment.