Skip to content

Commit

Permalink
fix: delete locally only allowed for failed sogs message
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilb committed Mar 27, 2024
1 parent a52ed64 commit e6e4e94
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ import React from 'react';
import { useDispatch, useSelector } from 'react-redux';
import useKey from 'react-use/lib/useKey';

import {
deleteMessagesById,
deleteMessagesByIdForEveryone,
deleteMessagesForX,
} from '../../../interactions/conversations/unsendingInteractions';
import { deleteMessagesForX } from '../../../interactions/conversations/unsendingInteractions';
import { resetSelectedMessageIds } from '../../../state/ducks/conversations';
import { getSelectedMessageIds } from '../../../state/selectors/conversations';
import {
Expand All @@ -22,15 +18,6 @@ import {
} from '../../basic/SessionButton';
import { SessionIconButton } from '../../icon';

function onDeleteSelectedMessagesForEveryone(
selectedConversationKey: string,
selectedMessageIds: Array<string>
) {
if (selectedConversationKey) {
void deleteMessagesByIdForEveryone(selectedMessageIds, selectedConversationKey);
}
}

export const SelectionOverlay = () => {
const selectedMessageIds = useSelector(getSelectedMessageIds);
const selectedConversationKey = useSelectedConversationKey();
Expand Down Expand Up @@ -73,7 +60,11 @@ export const SelectionOverlay = () => {
}
);

const isOnlyServerDeletable = isPublic;
// `enforceDeleteServerSide` should check for message statuses too, but when we have multiple selected,
// some might be sent and some in an error state. We default to trying to delete all of them server side first,
// which might fail. If that fails, the user will need to do a delete for all the ones sent already, and a manual delete
// for each ones which is in an error state.
const enforceDeleteServerSide = isPublic;

const classNameAndId = 'message-selection-overlay';

Expand All @@ -90,16 +81,13 @@ export const SelectionOverlay = () => {
buttonShape={SessionButtonShape.Square}
buttonType={SessionButtonType.Solid}
text={window.i18n('delete')}
onClick={() => {
onClick={async () => {
if (selectedConversationKey) {
if (isOnlyServerDeletable) {
void onDeleteSelectedMessagesForEveryone(
selectedConversationKey,
selectedMessageIds
);
} else {
void deleteMessagesById(selectedMessageIds, selectedConversationKey);
}
await deleteMessagesForX(
selectedMessageIds,
selectedConversationKey,
enforceDeleteServerSide
);
}
}}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,15 @@ const DeleteItem = ({ messageId }: { messageId: string }) => {

const isDeletable = useMessageIsDeletable(messageId);
const isDeletableForEveryone = useMessageIsDeletableForEveryone(messageId);
const messageStatus = useMessageStatus(messageId);

const enforceDeleteServerSide = isPublic && messageStatus !== 'error';

const onDelete = useCallback(() => {
if (convoId) {
void deleteMessagesForX([messageId], convoId, isPublic);
void deleteMessagesForX([messageId], convoId, enforceDeleteServerSide);
}
}, [convoId, isPublic, messageId]);
}, [convoId, enforceDeleteServerSide, messageId]);

if (!convoId || (isPublic && !isDeletableForEveryone) || (!isPublic && !isDeletable)) {
return null;
Expand Down
13 changes: 6 additions & 7 deletions ts/interactions/conversations/unsendingInteractions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ const doDeleteSelectedMessages = async ({
}

const isAllOurs = selectedMessages.every(message => ourDevicePubkey === message.getSource());
if (conversation.isPublic()) {
if (conversation.isPublic() && deleteForEveryone) {
await doDeleteSelectedMessagesInSOGS(selectedMessages, conversation, isAllOurs);
return;
}
Expand Down Expand Up @@ -341,14 +341,13 @@ const doDeleteSelectedMessages = async ({
export async function deleteMessagesForX(
messageIds: Array<string>,
conversationId: string,
isPublic: boolean
enforceDeleteServerSide: boolean // should only be enforced for messages successfully sent on communities
) {
if (conversationId) {
if (!isPublic) {
void deleteMessagesById(messageIds, conversationId);
}
if (isPublic) {
void deleteMessagesByIdForEveryone(messageIds, conversationId);
if (enforceDeleteServerSide) {
await deleteMessagesByIdForEveryone(messageIds, conversationId);
} else {
await deleteMessagesById(messageIds, conversationId);
}
}
}
Expand Down

0 comments on commit e6e4e94

Please sign in to comment.