Added dictionary for adding more servers.

Updated group chat poller to take an array of groups.
pull/41/head
Mikunj 6 years ago
parent 201127f6c2
commit 3930891a3d

@ -56,6 +56,13 @@ static NSString *const kInitialViewControllerIdentifier = @"UserInitialViewContr
static NSString *const kURLSchemeSGNLKey = @"sgnl"; static NSString *const kURLSchemeSGNLKey = @"sgnl";
static NSString *const kURLHostVerifyPrefix = @"verify"; static NSString *const kURLHostVerifyPrefix = @"verify";
static NSString *const kChatID = @"PublicChatID";
static NSString *const kChatType = @"PublicChatType";
static NSString *const kChatServerURL = @"PublicChatServerURL";
static NSString *const kChatName = @"PublicChatName";
static NSString *const kChatClosable = @"PublicChatClosable";
static NSString *const kChatChannelID = @"PublicChatChannelID";
static NSTimeInterval launchStartedAt; static NSTimeInterval launchStartedAt;
@interface AppDelegate () <UNUserNotificationCenterDelegate> @interface AppDelegate () <UNUserNotificationCenterDelegate>
@ -1485,28 +1492,93 @@ static NSTimeInterval launchStartedAt;
#pragma mark - Loki #pragma mark - Loki
- (NSArray *)publicChats
{
return @[
@{
kChatID: @"chat.lokinet.org.1",
kChatType: @"publicChat",
kChatServerURL: LKGroupChatAPI.publicChatServer,
kChatName: NSLocalizedString(@"Loki Public Chat", @""),
kChatClosable: @true,
kChatChannelID: @(LKGroupChatAPI.publicChatID),
},
@{
kChatID: @"rss://loki.network/feed/",
kChatType: @"rss",
kChatServerURL: @"https://loki.network/feed/",
kChatName: NSLocalizedString(@"Loki.network News", @""),
kChatClosable: @true,
kChatChannelID: @1,
},
@{
kChatID: @"rss://loki.network/category/messenger-updates/feed/",
kChatType: @"rss",
kChatServerURL: @"https://loki.network/category/messenger-updates/feed/",
kChatName: NSLocalizedString(@"Messenger updates", @""),
kChatClosable: @false,
kChatChannelID: @1,
}
];
}
- (void)setupPublicChatGroupsIfNeeded
{
NSArray *chats = [self publicChats];
NSString *ourPublicKey = OWSIdentityManager.sharedManager.identityKeyPair.hexEncodedPublicKey;
for (NSDictionary *chat in chats) {
NSString *chatID = [chat objectForKey:kChatID];
BOOL closable = [[chat objectForKey:kChatClosable] boolValue];
NSString *setupKey = [@"setup-" stringByAppendingString:chatID];
if (closable) {
BOOL isChatSetup = [NSUserDefaults.standardUserDefaults boolForKey:setupKey];
if (isChatSetup) { continue; }
}
NSString *title = [chat objectForKey:kChatName];
NSString *serverURL = [chat objectForKey:kChatServerURL];
// Create the group threads
TSGroupModel *group = [[TSGroupModel alloc] initWithTitle:title memberIds:@[ ourPublicKey, serverURL ] image:nil groupId:[chatID dataUsingEncoding:NSUTF8StringEncoding]];
__block TSGroupThread *thread;
[OWSPrimaryStorage.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
thread = [TSGroupThread getOrCreateThreadWithGroupModel:group transaction:transaction];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
NSCalendar *calendar = NSCalendar.currentCalendar;
[calendar setTimeZone:timeZone];
NSDateComponents *dateComponents = [NSDateComponents new];
[dateComponents setYear:999];
NSDate *date = [calendar dateByAddingComponents:dateComponents toDate:[NSDate new] options:0];
[thread updateWithMutedUntilDate:date transaction:transaction];
}];
[OWSProfileManager.sharedManager addThreadToProfileWhitelist:thread];
if (closable) {
[NSUserDefaults.standardUserDefaults setBool:YES forKey:setupKey];
}
}
}
- (void)setUpPublicChatIfNeeded - (void)setUpPublicChatIfNeeded
{ {
if (self.lokiPublicChatPoller != nil) { return; } static dispatch_once_t groupChatSetup;
self.lokiPublicChatPoller = [[LKGroupChatPoller alloc] initForGroup:(NSUInteger)LKGroupChatAPI.publicChatID onServer:LKGroupChatAPI.publicChatServer]; dispatch_once(&groupChatSetup, ^{
BOOL isPublicChatSetUp = [NSUserDefaults.standardUserDefaults boolForKey:@"isPublicChatSetUp"]; [self setupPublicChatGroupsIfNeeded];
if (isPublicChatSetUp) { return; } });
NSString *title = NSLocalizedString(@"Loki Public Chat", @""); [self setupPublicChatPollersIfNeeded];
NSData *groupID = [[[LKGroupChatAPI.publicChatServer stringByAppendingString:@"."] stringByAppendingString:@(LKGroupChatAPI.publicChatID).stringValue] dataUsingEncoding:NSUTF8StringEncoding]; }
TSGroupModel *group = [[TSGroupModel alloc] initWithTitle:title memberIds:@[ OWSIdentityManager.sharedManager.identityKeyPair.hexEncodedPublicKey, LKGroupChatAPI.publicChatServer ] image:nil groupId:groupID];
__block TSGroupThread *thread; - (void)setupPublicChatPollersIfNeeded
[OWSPrimaryStorage.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { {
thread = [TSGroupThread getOrCreateThreadWithGroupModel:group transaction:transaction]; if (self.lokiPublicChatPoller == nil) {
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"]; NSArray *publicChats = [[self publicChats] filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id object, NSDictionary* bindings) {
NSCalendar *calendar = NSCalendar.currentCalendar; NSDictionary *group = (NSDictionary *)object;
[calendar setTimeZone:timeZone]; NSString *chatType = [group objectForKey:kChatType];
NSDateComponents *dateComponents = [NSDateComponents new]; return [chatType isEqualToString:@"publicChat"];
[dateComponents setYear:999]; }]];
NSDate *date = [calendar dateByAddingComponents:dateComponents toDate:[NSDate new] options:0]; self.lokiPublicChatPoller = [[LKGroupChatPoller alloc] initWithGroups:publicChats];
[thread updateWithMutedUntilDate:date transaction:transaction]; }
}];
[OWSProfileManager.sharedManager addThreadToProfileWhitelist:thread];
[NSUserDefaults.standardUserDefaults setBool:YES forKey:@"isPublicChatSetUp"];
} }
- (void)startPublicChatPollingIfNeeded - (void)startPublicChatPollingIfNeeded

@ -1,8 +1,12 @@
private let kChatID = "PublicChatID"
private let kChatChannelID = "PublicChatChannelID"
private let kChatName = "PublicChatName"
private let kServerURL = "PublicChatServerURL"
@objc(LKGroupChatPoller) @objc(LKGroupChatPoller)
public final class LokiGroupChatPoller : NSObject { public final class LokiGroupChatPoller : NSObject {
private let group: UInt private let groups: [[String: Any]]
private let server: String
private var pollForNewMessagesTimer: Timer? = nil private var pollForNewMessagesTimer: Timer? = nil
private var pollForDeletedMessagesTimer: Timer? = nil private var pollForDeletedMessagesTimer: Timer? = nil
private var hasStarted = false private var hasStarted = false
@ -10,10 +14,8 @@ public final class LokiGroupChatPoller : NSObject {
private let pollForNewMessagesInterval: TimeInterval = 4 private let pollForNewMessagesInterval: TimeInterval = 4
private let pollForDeletedMessagesInterval: TimeInterval = 120 private let pollForDeletedMessagesInterval: TimeInterval = 120
@objc(initForGroup:onServer:) @objc public init(groups: [[String: Any]]) {
public init(for group: UInt, on server: String) { self.groups = groups
self.group = group
self.server = server
super.init() super.init()
} }
@ -31,30 +33,44 @@ public final class LokiGroupChatPoller : NSObject {
} }
private func pollForNewMessages() { private func pollForNewMessages() {
let group = self.group for group in groups {
let server = self.server guard let channelID = group[kChatChannelID] as? UInt, let server = group[kServerURL] as? String else {
let _ = LokiGroupChatAPI.getMessages(for: group, on: server).map { messages in Logger.info("[Loki] Failed to get channel id or server url from group: \(group)")
messages.reversed().map { message in return
let id = "\(server).\(group)".data(using: String.Encoding.utf8)! }
let x1 = SSKProtoGroupContext.builder(id: id, type: .deliver)
x1.setName(NSLocalizedString("Loki Public Chat", comment: "")) LokiGroupChatAPI.getMessages(for: channelID, on: server).map { [weak self] messages in
let x2 = SSKProtoDataMessage.builder() self?.handleMessages(messages: messages, group: group)
x2.setTimestamp(message.timestamp) }
x2.setGroup(try! x1.build()) }
x2.setBody(message.body) }
let x3 = SSKProtoContent.builder()
x3.setDataMessage(try! x2.build()) private func handleMessages(messages: [LokiGroupMessage], group: [String: Any]) -> Void {
let x4 = SSKProtoEnvelope.builder(type: .ciphertext, timestamp: message.timestamp) guard let groupID = group[kChatID] as? String, let groupName = group[kChatName] as? String else {
let senderHexEncodedPublicKey = message.hexEncodedPublicKey Logger.info("[Loki] Failed to handle messages for group: \(group))")
let endIndex = senderHexEncodedPublicKey.endIndex return
let cutoffIndex = senderHexEncodedPublicKey.index(endIndex, offsetBy: -8) }
let senderDisplayName = "\(message.displayName) (...\(senderHexEncodedPublicKey[cutoffIndex..<endIndex]))"
x4.setSource(senderDisplayName) messages.reversed().forEach { message in
x4.setSourceDevice(OWSDevicePrimaryDeviceId) let id = groupID.data(using: String.Encoding.utf8)!
x4.setContent(try! x3.build().serializedData()) let x1 = SSKProtoGroupContext.builder(id: id, type: .deliver)
OWSPrimaryStorage.shared().dbReadWriteConnection.readWrite { transaction in x1.setName(groupName)
SSKEnvironment.shared.messageManager.throws_processEnvelope(try! x4.build(), plaintextData: try! x3.build().serializedData(), wasReceivedByUD: false, transaction: transaction) let x2 = SSKProtoDataMessage.builder()
} x2.setTimestamp(message.timestamp)
x2.setGroup(try! x1.build())
x2.setBody(message.body)
let x3 = SSKProtoContent.builder()
x3.setDataMessage(try! x2.build())
let x4 = SSKProtoEnvelope.builder(type: .ciphertext, timestamp: message.timestamp)
let senderHexEncodedPublicKey = message.hexEncodedPublicKey
let endIndex = senderHexEncodedPublicKey.endIndex
let cutoffIndex = senderHexEncodedPublicKey.index(endIndex, offsetBy: -8)
let senderDisplayName = "\(message.displayName) (...\(senderHexEncodedPublicKey[cutoffIndex..<endIndex]))"
x4.setSource(senderDisplayName)
x4.setSourceDevice(OWSDevicePrimaryDeviceId)
x4.setContent(try! x3.build().serializedData())
OWSPrimaryStorage.shared().dbReadWriteConnection.readWrite { transaction in
SSKEnvironment.shared.messageManager.throws_processEnvelope(try! x4.build(), plaintextData: try! x3.build().serializedData(), wasReceivedByUD: false, transaction: transaction)
} }
} }
} }

Loading…
Cancel
Save