Skip to content

Commit

Permalink
Show error when editing messages after max edits
Browse files Browse the repository at this point in the history
Co-authored-by: ayumi-signal <143036029+ayumi-signal@users.noreply.github.com>
  • Loading branch information
automated-signal and ayumi-signal committed Nov 8, 2023
1 parent 842332a commit 308270f
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 8 deletions.
8 changes: 8 additions & 0 deletions _locales/en/messages.json
Expand Up @@ -3771,6 +3771,14 @@
"messageformat": "Okay",
"description": "Button to dismiss popup dialog when user-initiated task has gone wrong"
},
"icu:MessageMaxEditsModal__Title": {
"messageformat": "Can't edit message",
"description": "Title in popup dialog when attempting to edit a message too many times."
},
"icu:MessageMaxEditsModal__Description": {
"messageformat": "Only {max, number} edits can be applied to this message.",
"description": "Description text in popup dialog when attempting to edit a message too many times."
},
"icu:unknown-sgnl-link": {
"messageformat": "Sorry, that sgnl:// link didn't make sense!",
"description": "Shown if you click on a sgnl:// link not currently supported by Desktop"
Expand Down
30 changes: 28 additions & 2 deletions ts/state/ducks/conversations.ts
Expand Up @@ -29,10 +29,12 @@ import * as Attachment from '../../types/Attachment';
import { isFileDangerous } from '../../util/isFileDangerous';
import type {
ShowSendAnywayDialogActionType,
ShowErrorModalActionType,
ToggleProfileEditorErrorActionType,
} from './globalModals';
import {
SHOW_SEND_ANYWAY_DIALOG,
SHOW_ERROR_MODAL,
TOGGLE_PROFILE_EDITOR_ERROR,
} from './globalModals';
import {
Expand Down Expand Up @@ -90,6 +92,7 @@ import {
getMe,
getMessagesByConversation,
} from '../selectors/conversations';
import { getIntl } from '../selectors/user';
import type { AvatarDataType, AvatarUpdateType } from '../../types/Avatar';
import { getDefaultAvatars } from '../../types/Avatar';
import { getAvatarData } from '../../util/getAvatarData';
Expand Down Expand Up @@ -164,7 +167,11 @@ import {
} from './composer';
import { ReceiptType } from '../../types/Receipt';
import { Sound, SoundType } from '../../util/Sound';
import { canEditMessage } from '../../util/canEditMessage';
import {
canEditMessage,
isWithinMaxEdits,
MESSAGE_MAX_EDIT_COUNT,
} from '../../util/canEditMessage';
import type { ChangeNavTabActionType } from './nav';
import { CHANGE_NAV_TAB, NavTab, actions as navActions } from './nav';
import { sortByMessageOrder } from '../../types/ForwardDraft';
Expand Down Expand Up @@ -1770,7 +1777,12 @@ function discardEditMessage(
function setMessageToEdit(
conversationId: string,
messageId: string
): ThunkAction<void, RootStateType, unknown, SetFocusActionType> {
): ThunkAction<
void,
RootStateType,
unknown,
SetFocusActionType | ShowErrorModalActionType
> {
return async (dispatch, getState) => {
const conversation = window.ConversationController.get(conversationId);

Expand All @@ -1787,6 +1799,20 @@ function setMessageToEdit(
return;
}

if (!isWithinMaxEdits(message)) {
const i18n = getIntl(getState());
dispatch({
type: SHOW_ERROR_MODAL,
payload: {
title: i18n('icu:MessageMaxEditsModal__Title'),
description: i18n('icu:MessageMaxEditsModal__Description', {
max: MESSAGE_MAX_EDIT_COUNT,
}),
},
});
return;
}

setQuoteByMessageId(conversationId, undefined)(
dispatch,
getState,
Expand Down
4 changes: 2 additions & 2 deletions ts/state/ducks/globalModals.ts
Expand Up @@ -135,7 +135,7 @@ const CLOSE_GV2_MIGRATION_DIALOG = 'globalModals/CLOSE_GV2_MIGRATION_DIALOG';
const SHOW_STICKER_PACK_PREVIEW = 'globalModals/SHOW_STICKER_PACK_PREVIEW';
const CLOSE_STICKER_PACK_PREVIEW = 'globalModals/CLOSE_STICKER_PACK_PREVIEW';
const CLOSE_ERROR_MODAL = 'globalModals/CLOSE_ERROR_MODAL';
const SHOW_ERROR_MODAL = 'globalModals/SHOW_ERROR_MODAL';
export const SHOW_ERROR_MODAL = 'globalModals/SHOW_ERROR_MODAL';
const SHOW_FORMATTING_WARNING_MODAL =
'globalModals/SHOW_FORMATTING_WARNING_MODAL';
const SHOW_SEND_EDIT_WARNING_MODAL =
Expand Down Expand Up @@ -286,7 +286,7 @@ type CloseErrorModalActionType = ReadonlyDeep<{
type: typeof CLOSE_ERROR_MODAL;
}>;

type ShowErrorModalActionType = ReadonlyDeep<{
export type ShowErrorModalActionType = ReadonlyDeep<{
type: typeof SHOW_ERROR_MODAL;
payload: {
description?: string;
Expand Down
1 change: 1 addition & 0 deletions ts/state/selectors/conversations.ts
Expand Up @@ -1215,6 +1215,7 @@ export const getConversationTitle = createSelector(
getConversationTitleForPanelType(i18n, panel?.type)
);

// Note that this doesn't take into account max edit count. See canEditMessage.
export const getLastEditableMessageId = createSelector(
getConversationMessages,
getMessages,
Expand Down
7 changes: 5 additions & 2 deletions ts/util/canEditMessage.ts
Expand Up @@ -8,15 +8,14 @@ import { isMoreRecentThan } from './timestamp';
import { isOutgoing } from '../messages/helpers';
import { isSent, someSendStatus } from '../messages/MessageSendState';

const MAX_EDIT_COUNT = 10;
export const MESSAGE_MAX_EDIT_COUNT = 10;

export function canEditMessage(message: MessageAttributesType): boolean {
const result =
canEditMessages() &&
!message.deletedForEveryone &&
isOutgoing(message) &&
isMoreRecentThan(message.sent_at, DAY) &&
(message.editHistory?.length ?? 0) <= MAX_EDIT_COUNT &&
someSendStatus(message.sendStateByConversationId, isSent) &&
Boolean(message.body);

Expand All @@ -35,3 +34,7 @@ export function canEditMessage(message: MessageAttributesType): boolean {

return false;
}

export function isWithinMaxEdits(message: MessageAttributesType): boolean {
return (message.editHistory?.length ?? 0) <= MESSAGE_MAX_EDIT_COUNT;
}
7 changes: 5 additions & 2 deletions ts/util/sendEditedMessage.ts
Expand Up @@ -15,7 +15,7 @@ import { ErrorWithToast } from '../types/ErrorWithToast';
import { SendStatus } from '../messages/MessageSendState';
import { ToastType } from '../types/Toast';
import type { AciString } from '../types/ServiceId';
import { canEditMessage } from './canEditMessage';
import { canEditMessage, isWithinMaxEdits } from './canEditMessage';
import {
conversationJobQueue,
conversationQueueJobEnum,
Expand Down Expand Up @@ -77,7 +77,10 @@ export async function sendEditedMessage(
return;
}

if (!canEditMessage(targetMessage.attributes)) {
if (
!canEditMessage(targetMessage.attributes) ||
!isWithinMaxEdits(targetMessage.attributes)
) {
throw new ErrorWithToast(
`${idLog}: cannot edit`,
ToastType.CannotEditMessage
Expand Down

0 comments on commit 308270f

Please sign in to comment.