Added mod polling

This commit is contained in:
Mikunj 2019-08-29 12:49:06 +10:00
parent bf09a2014b
commit 679b2f7742
7 changed files with 102 additions and 6 deletions

View File

@ -7,6 +7,7 @@
objects = {
/* Begin PBXBuildFile section */
24684784231772CD00600430 /* LokiGroupChatModPoller.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24684783231772CD00600430 /* LokiGroupChatModPoller.swift */; };
24A830A22293CD0100F4CAC0 /* LokiP2PServer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24A830A12293CD0100F4CAC0 /* LokiP2PServer.swift */; };
2AE2882E4C2B96BFFF9EE27C /* Pods_SignalShareExtension.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0F94C85CB0B235DA37F68ED0 /* Pods_SignalShareExtension.framework */; };
3403B95D20EA9527001A1F44 /* OWSContactShareButtonsView.m in Sources */ = {isa = PBXBuildFile; fileRef = 3403B95B20EA9526001A1F44 /* OWSContactShareButtonsView.m */; };
@ -660,6 +661,7 @@
0F94C85CB0B235DA37F68ED0 /* Pods_SignalShareExtension.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SignalShareExtension.framework; sourceTree = BUILT_PRODUCTS_DIR; };
1C93CF3971B64E8B6C1F9AC1 /* Pods-SignalShareExtension.test.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SignalShareExtension.test.xcconfig"; path = "Pods/Target Support Files/Pods-SignalShareExtension/Pods-SignalShareExtension.test.xcconfig"; sourceTree = "<group>"; };
1CE3CD5C23334683BDD3D78C /* Pods-Signal.test.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Signal.test.xcconfig"; path = "Pods/Target Support Files/Pods-Signal/Pods-Signal.test.xcconfig"; sourceTree = "<group>"; };
24684783231772CD00600430 /* LokiGroupChatModPoller.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LokiGroupChatModPoller.swift; sourceTree = "<group>"; };
24A830A12293CD0100F4CAC0 /* LokiP2PServer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LokiP2PServer.swift; sourceTree = "<group>"; };
264242150E87D10A357DB07B /* Pods_SignalMessaging.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SignalMessaging.framework; sourceTree = BUILT_PRODUCTS_DIR; };
3403B95B20EA9526001A1F44 /* OWSContactShareButtonsView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OWSContactShareButtonsView.m; sourceTree = "<group>"; };
@ -2615,17 +2617,18 @@
B8439518228510E9000563FE /* Loki */ = {
isa = PBXGroup;
children = (
B821F2F72272CED3002C88C0 /* AccountDetailsViewController.swift */,
B8162F0222891AD600D46544 /* FriendRequestView.swift */,
B8162F0422892C5F00D46544 /* FriendRequestViewDelegate.swift */,
B89841E222B7579F00B1BDC6 /* NewConversationViewController.swift */,
B821F2F72272CED3002C88C0 /* AccountDetailsViewController.swift */,
B821F2F92272CEEE002C88C0 /* SeedViewController.swift */,
24A830A12293CD0100F4CAC0 /* LokiP2PServer.swift */,
24684783231772CD00600430 /* LokiGroupChatModPoller.swift */,
B845B4D3230CD09000D759F0 /* LokiGroupChatPoller.swift */,
24A830A12293CD0100F4CAC0 /* LokiP2PServer.swift */,
B825849F2315024B001B41CB /* LokiRSSFeedPoller.swift */,
B89841E222B7579F00B1BDC6 /* NewConversationViewController.swift */,
B825848A230F94FE001B41CB /* QRCodeViewController.swift */,
B8258491230FA5DA001B41CB /* ScanQRCodeViewController.h */,
B8258492230FA5E9001B41CB /* ScanQRCodeViewController.m */,
B821F2F92272CEEE002C88C0 /* SeedViewController.swift */,
);
path = Loki;
sourceTree = "<group>";
@ -3627,6 +3630,7 @@
341F2C0F1F2B8AE700D07D6B /* DebugUIMisc.m in Sources */,
340FC8AF204DAC8D007AEB0F /* OWSLinkDeviceViewController.m in Sources */,
34E3EF0D1EFC235B007F6822 /* DebugUIDiskUsage.m in Sources */,
24684784231772CD00600430 /* LokiGroupChatModPoller.swift in Sources */,
454A84042059C787008B8C75 /* MediaTileViewController.swift in Sources */,
340FC8B4204DAC8D007AEB0F /* OWSBackupSettingsViewController.m in Sources */,
34D1F0871F8678AA0066283D /* ConversationViewItem.m in Sources */,

View File

@ -67,6 +67,7 @@ static NSTimeInterval launchStartedAt;
@property (nonatomic) LKGroupChatPoller *lokiPublicChatPoller;
@property (nonatomic) LKRSSFeedPoller *lokiNewsFeedPoller;
@property (nonatomic) LKRSSFeedPoller *lokiMessengerUpdatesFeedPoller;
@property (nonatomic) LKGroupChatModPoller *lokiModPoller;
@end
@ -1555,6 +1556,7 @@ static NSTimeInterval launchStartedAt;
- (void)createGroupChatPollersIfNeeded
{
if (self.lokiPublicChatPoller == nil) { self.lokiPublicChatPoller = [[LKGroupChatPoller alloc] initForGroup:self.lokiPublicChat]; }
if (self.lokiModPoller == nil) { self.lokiModPoller = [[LKGroupChatModPoller alloc] initForGroup:self.lokiPublicChat]; }
}
- (void)createRSSFeedPollersIfNeeded
@ -1567,6 +1569,7 @@ static NSTimeInterval launchStartedAt;
{
[self createGroupChatPollersIfNeeded];
[self.lokiPublicChatPoller startIfNeeded];
[self.lokiModPoller startIfNeeded];
}
- (void)startRSSFeedPollersIfNeeded

View File

@ -0,0 +1,40 @@
@objc(LKGroupChatModPoller)
public final class LokiGroupChatModPoller : NSObject {
private let group: LokiGroupChat
private var timer: Timer? = nil
private var hasStarted = false
private let interval: TimeInterval = 10 * 60
private let storage = OWSPrimaryStorage.shared()
private let ourHexEncodedPubKey = OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey
@objc(initForGroup:)
public init(for group: LokiGroupChat) {
self.group = group
super.init()
}
@objc public func startIfNeeded() {
if hasStarted { return }
timer = Timer.scheduledTimer(withTimeInterval: interval, repeats: true) { [weak self] _ in self?.pollForModeratorStatus() }
pollForModeratorStatus() // Perform initial update
hasStarted = true
}
@objc public func stop() {
timer?.invalidate()
hasStarted = false
}
private func pollForModeratorStatus() {
let group = self.group
let _ = LokiGroupChatAPI.isCurrentUserMod(on: group.server).done { [weak self] isModerator in
guard let self = self else { return }
self.storage.dbReadWriteConnection.readWrite { transaction in
self.storage.setIsModerator(isModerator, for: group.server, transaction: transaction)
}
}
}
}

View File

@ -15,6 +15,8 @@
#import <SignalServiceKit/NSString+SSK.h>
#import <SignalServiceKit/OWSContact.h>
#import <SignalServiceKit/TSInteraction.h>
#import <SignalServiceKit/SSKEnvironment.h>
#import <SignalServiceKit/SignalServiceKit-Swift.h>
NS_ASSUME_NONNULL_BEGIN
@ -199,6 +201,13 @@ NSString *NSStringForOWSMessageCellType(OWSMessageCellType cellType)
}
}
- (OWSPrimaryStorage *)primaryStorage
{
OWSAssertDebug(SSKEnvironment.shared.primaryStorage);
return SSKEnvironment.shared.primaryStorage;
}
- (NSString *)itemId
{
return self.interaction.uniqueId;
@ -1242,7 +1251,14 @@ NSString *NSStringForOWSMessageCellType(OWSMessageCellType cellType)
TSMessage *message = (TSMessage *)self.interaction;
if (!message.isPublicChatMessage) return false;
// TODO: Disable deletion of incoming messages if we're not moderators
// Don't allow deletion if we're not mods on incoming messages
if (interationType == OWSInteractionType_IncomingMessage) {
__block BOOL isModerator;
[[self primaryStorage].dbReadWriteConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
isModerator = [[self primaryStorage] getIsModeratorForServer:LKGroupChatAPI.publicChatServer transaction:transaction];
}];
if (!isModerator) return false;
}
return true;
}

View File

@ -28,7 +28,7 @@ public final class LokiGroupChatAPI : NSObject {
// MARK: Error
public enum Error : Swift.Error {
case tokenParsingFailed, tokenDecryptionFailed, messageParsingFailed
case tokenParsingFailed, tokenDecryptionFailed, messageParsingFailed, jsonParsingFailed
}
// MARK: Database
@ -230,6 +230,23 @@ public final class LokiGroupChatAPI : NSObject {
}
public static func isCurrentUserMod(on server: String) -> Promise<Bool> {
return getAuthToken(for: server).then { token -> Promise<Bool> in
let url = URL(string: "\(server)/loki/v1/user_info")!
let request = TSRequest(url: url)
request.allHTTPHeaderFields = [ "Content-Type" : "application/json", "Authorization" : "Bearer \(token)" ]
return TSNetworkManager.shared().makePromise(request: request).map { $0.responseObject }.map { rawResponse in
guard let json = rawResponse as? JSON, let data = json["data"] as? JSON else {
print("[Loki] Couldn't parse json for user info.")
throw Error.jsonParsingFailed
}
// moderator_status is not set for users that are not mods
return data["moderator_status"] as? Bool ?? false
}
}
}
// MARK: Public API (Obj-C)
@objc(getMessagesForGroup:onServer:)
public static func objc_getMessages(for group: UInt64, on server: String) -> AnyPromise {

View File

@ -95,9 +95,16 @@ NS_ASSUME_NONNULL_BEGIN
*/
- (void)setLastMessageHashForServiceNode:(NSString *)serviceNode hash:(NSString *)hash expiresAt:(u_int64_t)expiresAt transaction:(YapDatabaseReadWriteTransaction *)transaction NS_SWIFT_NAME(setLastMessageHash(forServiceNode:hash:expiresAt:transaction:));
# pragma mark - Public chat
- (void)setIDForMessageWithServerID:(NSUInteger)serverID to:(NSString *)messageID in:(YapDatabaseReadWriteTransaction *)transaction;
- (NSString *_Nullable)getIDForMessageWithServerID:(NSUInteger)serverID in:(YapDatabaseReadTransaction *)transaction;
- (void)setIsModerator:(BOOL)isModerator forServer:(NSString *)server transaction:(YapDatabaseReadWriteTransaction *)transaction
NS_SWIFT_NAME(setIsModerator(_:for:transaction:));
- (BOOL)getIsModeratorForServer:(NSString *)server transaction:(YapDatabaseReadTransaction *)transaction
NS_SWIFT_NAME(getIsModerator(forServer:transaction:));
@end
NS_ASSUME_NONNULL_END

View File

@ -19,6 +19,7 @@
#define LKReceivedMessageHashesKey @"LKReceivedMessageHashesKey"
#define LKReceivedMessageHashesCollection @"LKReceivedMessageHashesCollection"
#define LKMessageIDCollection @"LKMessageIDCollection"
#define LKModeratorCollection @"LKModerationCollection"
@implementation OWSPrimaryStorage (Loki)
@ -173,4 +174,12 @@
return [transaction objectForKey:key inCollection:LKMessageIDCollection];
}
- (void)setIsModerator:(BOOL)isModerator forServer:(NSString *)server transaction:(YapDatabaseReadWriteTransaction *)transaction {
[transaction setBool:isModerator forKey:server inCollection:LKModeratorCollection];
}
- (BOOL)getIsModeratorForServer:(NSString *)server transaction:(YapDatabaseReadTransaction *)transaction {
return [transaction boolForKey:server inCollection:LKModeratorCollection defaultValue:false];
}
@end