diff --git a/Session/Conversations/ConversationVC+Interaction.swift b/Session/Conversations/ConversationVC+Interaction.swift index d184451fb..a8abf2e1e 100644 --- a/Session/Conversations/ConversationVC+Interaction.swift +++ b/Session/Conversations/ConversationVC+Interaction.swift @@ -555,6 +555,10 @@ extension ConversationVC : InputViewDelegate, MessageCellDelegate, ContextMenuAc } func delete(_ viewItem: ConversationViewItem) { + if (!self.isUnsendRequesEnabled) { + viewItem.deleteAction() + return + } func showInputAccessoryView() { UIView.animate(withDuration: 0.25, animations: { diff --git a/Session/Conversations/ConversationVC.swift b/Session/Conversations/ConversationVC.swift index 35a58c6bf..dd9275b59 100644 --- a/Session/Conversations/ConversationVC.swift +++ b/Session/Conversations/ConversationVC.swift @@ -4,7 +4,8 @@ // • Photo rounding (the small corners don't have the correct rounding) // • Remaining search glitchiness -final class ConversationVC : BaseVC, ConversationViewModelDelegate, OWSConversationSettingsViewDelegate, ConversationSearchControllerDelegate, UITableViewDataSource, UITableViewDelegate { +final class ConversationVC : BaseVC, ConversationViewModelDelegate, OWSConversationSettingsViewDelegate, ConversationSearchControllerDelegate, UITableViewDataSource, UITableViewDelegate { + let isUnsendRequesEnabled = false // Switch this to true if unsend request is done on all platforms let thread: TSThread let focusedMessageID: String? // This isn't actually used ATM var unreadViewItems: [ConversationViewItem] = [] diff --git a/Session/Conversations/ConversationViewItem.h b/Session/Conversations/ConversationViewItem.h index 307aaf2a1..8aeaccd01 100644 --- a/Session/Conversations/ConversationViewItem.h +++ b/Session/Conversations/ConversationViewItem.h @@ -136,6 +136,8 @@ NSString *NSStringForOWSMessageCellType(OWSMessageCellType cellType); - (void)deleteLocallyAction; - (void)deleteRemotelyAction; +- (void)deleteAction; // Remove this after the unsend request is enabled + - (BOOL)canCopyMedia; - (BOOL)canSaveMedia; diff --git a/Session/Conversations/ConversationViewItem.m b/Session/Conversations/ConversationViewItem.m index 8fcd4ae95..a60beeab4 100644 --- a/Session/Conversations/ConversationViewItem.m +++ b/Session/Conversations/ConversationViewItem.m @@ -1031,6 +1031,50 @@ NSString *NSStringForOWSMessageCellType(OWSMessageCellType cellType) } +// Remove this after the unsend request is enabled +- (void)deleteAction +{ + [LKStorage writeWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + [self.interaction removeWithTransaction:transaction]; + if (self.interaction.interactionType == OWSInteractionType_OutgoingMessage) { + [LKStorage.shared cancelPendingMessageSendJobIfNeededForMessage:self.interaction.timestamp using:transaction]; + } + }]; + + if (self.isGroupThread) { + TSGroupThread *groupThread = (TSGroupThread *)self.interaction.thread; + + // Only allow deletion on incoming and outgoing messages + OWSInteractionType interationType = self.interaction.interactionType; + if (interationType != OWSInteractionType_IncomingMessage && interationType != OWSInteractionType_OutgoingMessage) return; + + // Make sure it's an open group message + TSMessage *message = (TSMessage *)self.interaction; + if (!message.isOpenGroupMessage) return; + + // Get the open group + SNOpenGroupV2 *openGroupV2 = [LKStorage.shared getV2OpenGroupForThreadID:groupThread.uniqueId]; + if (openGroup == nil && openGroupV2 == nil) return; + + // If it's an incoming message the user must have moderator status + if (self.interaction.interactionType == OWSInteractionType_IncomingMessage) { + NSString *userPublicKey = [LKStorage.shared getUserPublicKey]; + if (openGroupV2 != nil) { + if (![SNOpenGroupAPIV2 isUserModerator:userPublicKey forRoom:openGroupV2.room onServer:openGroupV2.server]) { return; } + } + } + + // Delete the message + BOOL wasSentByUser = (interationType == OWSInteractionType_OutgoingMessage); + if (openGroupV2 != nil) { + [[SNOpenGroupAPIV2 deleteMessageWithServerID:message.openGroupServerMessageID fromRoom:openGroupV2.room onServer:openGroupV2.server].catch(^(NSError *error) { + // Roll back + [self.interaction save]; + }) retainUntilComplete]; + } + } +} + - (BOOL)hasBodyTextActionContent { return self.hasBodyText && self.displayableBodyText.fullText.length > 0;