Hookup api.

This commit is contained in:
Mikunj 2019-08-28 16:07:14 +10:00
parent 74f0b8ec7d
commit bf09a2014b
3 changed files with 43 additions and 15 deletions

View file

@ -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

View file

@ -9,6 +9,7 @@
#import "OWSMessageHeaderView.h"
#import "OWSSystemMessageCell.h"
#import "Session-Swift.h"
#import "AnyPromise.h"
#import <SignalMessaging/OWSUnreadIndicator.h>
#import <SignalServiceKit/NSData+Image.h>
#import <SignalServiceKit/NSString+SSK.h>
@ -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

View file

@ -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<Void> {
return getAuthToken(for: server).then { token -> Promise<Void> 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))
}
}