diff --git a/Signal/src/AppDelegate.m b/Signal/src/AppDelegate.m index 91670eb03..d7ab77bf4 100644 --- a/Signal/src/AppDelegate.m +++ b/Signal/src/AppDelegate.m @@ -1489,7 +1489,7 @@ static NSTimeInterval launchStartedAt; - (LKGroupChat *)lokiPublicChat { - return [[LKGroupChat alloc] initWithServerID:@(LKGroupChatAPI.publicChatServerID).unsignedIntegerValue server:LKGroupChatAPI.publicChatServer displayName:NSLocalizedString(@"Loki Public Chat", @"") isDeletable:true]; + return [[LKGroupChat alloc] initWithServerID:LKGroupChatAPI.publicChatServerID server:LKGroupChatAPI.publicChatServer displayName:NSLocalizedString(@"Loki Public Chat", @"") isDeletable:true]; } - (LKRSSFeed *)lokiNewsFeed diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewItem.m b/Signal/src/ViewControllers/ConversationView/ConversationViewItem.m index 259c87173..76db80e6c 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewItem.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewItem.m @@ -9,6 +9,7 @@ #import "OWSMessageHeaderView.h" #import "OWSSystemMessageCell.h" #import "Session-Swift.h" +#import "AnyPromise.h" #import #import #import @@ -1161,15 +1162,13 @@ NSString *NSStringForOWSMessageCellType(OWSMessageCellType cellType) - (void)deleteAction { - // TODO: Handle deletion differently for public chats + // Delete it optimistically + [self.interaction remove]; + if (self.isGroupThread) { + // If it's RSS then skip TSGroupThread *groupThread = (TSGroupThread *)self.interaction.thread; - - // If it's RSS then just proceed normally - if (groupThread.isRSS) { - [self.interaction remove]; - return; - }; + if (groupThread.isRSS) return; // Only allow deletion on incoming and outgoing messages OWSInteractionType interationType = self.interaction.interactionType; @@ -1179,13 +1178,12 @@ NSString *NSStringForOWSMessageCellType(OWSMessageCellType cellType) TSMessage *message = (TSMessage *)self.interaction; if (!message.isPublicChatMessage) return; - // TODO: Call the group api here to delete the message - - // Just return and don't delete for now - return; + // Delete the message + [[LKGroupChatAPI deleteMessageWithServerID:message.publicChatMessageID forGroup:LKGroupChatAPI.publicChatServerID onServer:LKGroupChatAPI.publicChatServer isOurOwnMessage:interationType == OWSInteractionType_OutgoingMessage].catch(^(NSError *error) { + // If we fail then add the interaction back in + [self.interaction save]; + }) retainUntilComplete]; } - - [self.interaction remove]; } - (BOOL)hasBodyTextActionContent diff --git a/SignalServiceKit/src/Loki/API/LokiGroupChatAPI.swift b/SignalServiceKit/src/Loki/API/LokiGroupChatAPI.swift index ea701bb3b..f1f02b7b0 100644 --- a/SignalServiceKit/src/Loki/API/LokiGroupChatAPI.swift +++ b/SignalServiceKit/src/Loki/API/LokiGroupChatAPI.swift @@ -11,7 +11,7 @@ public final class LokiGroupChatAPI : NSObject { // MARK: Public Chat @objc public static let publicChatServer = "https://chat.lokinet.org" @objc public static let publicChatMessageType = "network.loki.messenger.publicChat" - @objc public static let publicChatServerID = 1 + @objc public static let publicChatServerID: UInt = 1 // MARK: Convenience private static var userDisplayName: String { @@ -205,6 +205,31 @@ public final class LokiGroupChatAPI : NSObject { } } + public static func deleteMessageWithServerID(_ messageServerID: UInt, for group: UInt64, on server: String, isOurOwnMessage: Bool = true) -> Promise { + return getAuthToken(for: server).then { token -> Promise in + let modTag = isOurOwnMessage ? "" : "[Mod]" + print("[Loki]\(modTag) Deleting message with server ID: \(messageServerID) for group chat with ID: \(group) on server: \(server).") + + let endpoint = isOurOwnMessage ? "\(server)/channels/\(group)/messages/\(messageServerID)" : "\(server)/loki/v1/moderation/message/\(messageServerID)" + let url = URL(string: endpoint)! + let request = TSRequest(url: url, method: "DELETE", parameters: [:]) + request.allHTTPHeaderFields = [ "Content-Type" : "application/json", "Authorization" : "Bearer \(token)" ] + return TSNetworkManager.shared().makePromise(request: request).map { result -> Void in + print("[Loki]\(modTag) Deleted message \(messageServerID) on server \(server).") + }.recover { error in + // If we got 404 or 410 then message doesn't exist on the server + if let error = error as? NetworkManagerError, error.statusCode == 404 || error.statusCode == 410 { + print("[Loki]\(modTag) Message \(messageServerID) was already deleted on the server.") + return + } + + print("[Loki]\(modTag) Failed to delete message \(messageServerID) on server \(server).") + throw error + } + } + + } + // MARK: Public API (Obj-C) @objc(getMessagesForGroup:onServer:) public static func objc_getMessages(for group: UInt64, on server: String) -> AnyPromise { @@ -215,4 +240,9 @@ public final class LokiGroupChatAPI : NSObject { public static func objc_sendMessage(_ message: LokiGroupMessage, to group: UInt64, on server: String) -> AnyPromise { return AnyPromise.from(sendMessage(message, to: group, on: server)) } + + @objc (deleteMessageWithServerID:forGroup:onServer:isOurOwnMessage:) + public static func objc_deleteMessageWithServerID(_ messageServerID: UInt, for group: UInt64, on server: String, ourMessage: Bool = true) -> AnyPromise { + return AnyPromise.from(deleteMessageWithServerID(messageServerID, for: group, on: server, isOurOwnMessage: ourMessage)) + } }