pull/41/head
Niels Andriesse 6 years ago
parent 06dba7ddd9
commit ba0da149ad

@ -8,6 +8,7 @@ extern NSString *const AppDelegateStoryboardMain;
@interface AppDelegate : UIResponder <UIApplicationDelegate> @interface AppDelegate : UIResponder <UIApplicationDelegate>
- (void)startPublicChatPollingIfNeeded; - (void)createGroupChatsIfNeeded;
- (void)startGroupChatPollersIfNeeded;
@end @end

@ -65,6 +65,8 @@ static NSTimeInterval launchStartedAt;
@property (nonatomic) BOOL didAppLaunchFail; @property (nonatomic) BOOL didAppLaunchFail;
@property (nonatomic) LKP2PServer *lokiP2PServer; @property (nonatomic) LKP2PServer *lokiP2PServer;
@property (nonatomic) LKGroupChatPoller *lokiPublicChatPoller; @property (nonatomic) LKGroupChatPoller *lokiPublicChatPoller;
@property (nonatomic) LKGroupChatPoller *lokiNewsPoller;
@property (nonatomic) LKGroupChatPoller *lokiMessengerUpdatesPoller;
@end @end
@ -1485,26 +1487,30 @@ static NSTimeInterval launchStartedAt;
#pragma mark - Loki #pragma mark - Loki
- (NSArray *)groupChats - (LKGroupChat *)lokiPublicChat
{ {
return @[ return [[LKGroupChat alloc] initWithKindAsString:@"publicChat" id:@(LKGroupChatAPI.publicChatID).stringValue server:LKGroupChatAPI.publicChatServer displayName:NSLocalizedString(@"Loki Public Chat", @"") isDeletable:true];
[[LKGroupChat alloc] initWithKindAsString:@"publicChat" id:@(LKGroupChatAPI.publicChatID).stringValue server:LKGroupChatAPI.publicChatServer displayName:NSLocalizedString(@"Loki Public Chat", @"") isDeletable:true], }
[[LKGroupChat alloc] initWithKindAsString:@"rss" id:@"loki.network.feed" server:@"https://loki.network/feed/" displayName:NSLocalizedString(@"Loki News", @"") isDeletable:true],
[[LKGroupChat alloc] initWithKindAsString:@"rss" id:@"loki.network.messenger-update" server:@"https://loki.network/category/messenger-updates/feed/" displayName:NSLocalizedString(@"Messenger Updates", @"") isDeletable:false], - (LKGroupChat *)lokiNews
]; {
return [[LKGroupChat alloc] initWithKindAsString:@"rss" id:@"loki.network.feed" server:@"https://loki.network/feed/" displayName:NSLocalizedString(@"Loki News", @"") isDeletable:true];
}
- (LKGroupChat *)lokiMessengerUpdates
{
return [[LKGroupChat alloc] initWithKindAsString:@"rss" id:@"loki.network.messenger-update" server:@"https://loki.network/category/messenger-updates/feed/" displayName:NSLocalizedString(@"Loki Messenger Updates", @"") isDeletable:false];
} }
- (void)setUpGroupChatsIfNeeded - (void)createGroupChatsIfNeeded
{ {
NSArray *groupChats = self.groupChats; NSArray *allGroupChats = @[ self.lokiPublicChat, self.lokiNews, self.lokiMessengerUpdates ];
NSString *userHexEncodedPublicKey = OWSIdentityManager.sharedManager.identityKeyPair.hexEncodedPublicKey; NSString *userHexEncodedPublicKey = OWSIdentityManager.sharedManager.identityKeyPair.hexEncodedPublicKey;
for (LKGroupChat *chat in groupChats) { for (LKGroupChat *chat in allGroupChats) {
NSString *userDefaultsKey = [@"isGroupChatSetUp." stringByAppendingString:chat.id]; NSString *userDefaultsKey = [@"isSetUp." stringByAppendingString:chat.id];
if (chat.isDeletable) { BOOL isChatSetUp = [NSUserDefaults.standardUserDefaults boolForKey:userDefaultsKey];
BOOL isChatSetUp = [NSUserDefaults.standardUserDefaults boolForKey:setupKey]; if (chat.isDeletable && isChatSetUp) { continue; }
if (isChatSetUp) { continue; } TSGroupModel *group = [[TSGroupModel alloc] initWithTitle:chat.displayName memberIds:@[ userHexEncodedPublicKey, chat.server ] image:nil groupId:[chat.id dataUsingEncoding:NSUTF8StringEncoding]];
}
TSGroupModel *group = [[TSGroupModel alloc] initWithTitle:chat.displayName memberIds:@[ ourPublicKey, chat.server ] image:nil groupId:[chat.id dataUsingEncoding:NSUTF8StringEncoding]];
__block TSGroupThread *thread; __block TSGroupThread *thread;
[OWSPrimaryStorage.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [OWSPrimaryStorage.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
thread = [TSGroupThread getOrCreateThreadWithGroupModel:group transaction:transaction]; thread = [TSGroupThread getOrCreateThreadWithGroupModel:group transaction:transaction];
@ -1517,26 +1523,20 @@ static NSTimeInterval launchStartedAt;
[thread updateWithMutedUntilDate:date transaction:transaction]; [thread updateWithMutedUntilDate:date transaction:transaction];
}]; }];
[OWSProfileManager.sharedManager addThreadToProfileWhitelist:thread]; [OWSProfileManager.sharedManager addThreadToProfileWhitelist:thread];
if (chat.isDeletable) { [NSUserDefaults.standardUserDefaults setBool:YES forKey:userDefaultsKey];
[NSUserDefaults.standardUserDefaults setBool:YES forKey:userDefaultsKey];
}
} }
} }
- (void)setUpGroupChatPollerIfNeeded - (void)createGroupChatPollersIfNeeded
{ {
if (self.lokiGroupChatPoller == nil) { if (self.lokiPublicChatPoller == nil) { self.lokiPublicChatPoller = [[LKGroupChatPoller alloc] initForGroup:self.lokiPublicChat]; }
NSArray *groupChats = [self.groupChats filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id object, NSDictionary* bindings) { if (self.lokiNewsPoller == nil) { self.lokiNewsPoller = [[LKGroupChatPoller alloc] initForGroup:self.lokiNews]; }
LKGroupChat *group = (LKGroupChat *)object; if (self.lokiMessengerUpdatesPoller == nil) { self.lokiMessengerUpdatesPoller = [[LKGroupChatPoller alloc] initForGroup:self.lokiMessengerUpdates]; }
return group.isPublicChat;
}]];
self.lokiPublicChatPoller = [[LKGroupChatPoller alloc] initWithGroups:publicChats];
}
} }
- (void)startPublicChatPollingIfNeeded - (void)startGroupChatPollersIfNeeded
{ {
[self setUpPublicChatIfNeeded]; [self createGroupChatPollersIfNeeded];
[self.lokiPublicChatPoller startIfNeeded]; [self.lokiPublicChatPoller startIfNeeded];
} }

@ -1,16 +1,23 @@
@objc(LKGroupChatPoller) @objc(LKGroupChatPoller)
public final class LokiGroupChatPoller : NSObject { public final class LokiGroupChatPoller : NSObject {
private let groups: [LokiGroupChat] private let group: LokiGroupChat
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
private let pollForNewMessagesInterval: TimeInterval = 4 private lazy var pollForNewMessagesInterval: TimeInterval = {
switch group.kind {
case .publicChat(_): return 4
case .rss(_): return 8 * 60
}
}()
private let pollForDeletedMessagesInterval: TimeInterval = 32 * 60 private let pollForDeletedMessagesInterval: TimeInterval = 32 * 60
@objc public init(groups: [LokiGroupChat]) { @objc(initForGroup:)
self.groups = groups public init(for group: LokiGroupChat) {
self.group = group
super.init() super.init()
} }
@ -28,40 +35,34 @@ public final class LokiGroupChatPoller : NSObject {
} }
private func pollForNewMessages() { private func pollForNewMessages() {
for group in groups { let group = self.group
guard case let LokiGroupChat.Kind.publicChat(channelID) = group.kind else { switch group.kind {
Logger.info("[Loki] Trying to poll RSS group chat: \(group)") case .publicChat(let id):
return let _ = LokiGroupChatAPI.getMessages(for: id, on: group.server).done { messages in
} messages.reversed().forEach { message in
let id = group.id.data(using: String.Encoding.utf8)!
LokiGroupChatAPI.getMessages(for: channelID, on: group.server).map { [weak self] messages in let x1 = SSKProtoGroupContext.builder(id: id, type: .deliver)
self?.handleMessages(messages: messages, group: group) x1.setName(group.displayName)
} let x2 = SSKProtoDataMessage.builder()
} x2.setTimestamp(message.timestamp)
} x2.setGroup(try! x1.build())
x2.setBody(message.body)
private func handleMessages(messages: [LokiGroupMessage], group: LokiGroupChat) -> Void { let x3 = SSKProtoContent.builder()
messages.reversed().forEach { message in x3.setDataMessage(try! x2.build())
let id = group.id.data(using: String.Encoding.utf8)! let x4 = SSKProtoEnvelope.builder(type: .ciphertext, timestamp: message.timestamp)
let x1 = SSKProtoGroupContext.builder(id: id, type: .deliver) let senderHexEncodedPublicKey = message.hexEncodedPublicKey
x1.setName(group.displayName) let endIndex = senderHexEncodedPublicKey.endIndex
let x2 = SSKProtoDataMessage.builder() let cutoffIndex = senderHexEncodedPublicKey.index(endIndex, offsetBy: -8)
x2.setTimestamp(message.timestamp) let senderDisplayName = "\(message.displayName) (...\(senderHexEncodedPublicKey[cutoffIndex..<endIndex]))"
x2.setGroup(try! x1.build()) x4.setSource(senderDisplayName)
x2.setBody(message.body) x4.setSourceDevice(OWSDevicePrimaryDeviceId)
let x3 = SSKProtoContent.builder() x4.setContent(try! x3.build().serializedData())
x3.setDataMessage(try! x2.build()) OWSPrimaryStorage.shared().dbReadWriteConnection.readWrite { transaction in
let x4 = SSKProtoEnvelope.builder(type: .ciphertext, timestamp: message.timestamp) SSKEnvironment.shared.messageManager.throws_processEnvelope(try! x4.build(), plaintextData: try! x3.build().serializedData(), wasReceivedByUD: false, transaction: transaction)
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)
} }
case .rss(let customID): break // TODO: Implement
} }
} }

@ -692,7 +692,8 @@ typedef NS_ENUM(NSInteger, HomeViewControllerSection) {
} }
if (OWSIdentityManager.sharedManager.identityKeyPair != nil) { if (OWSIdentityManager.sharedManager.identityKeyPair != nil) {
AppDelegate *appDelegate = (AppDelegate *)UIApplication.sharedApplication.delegate; AppDelegate *appDelegate = (AppDelegate *)UIApplication.sharedApplication.delegate;
[appDelegate startPublicChatPollingIfNeeded]; [appDelegate createGroupChatsIfNeeded];
[appDelegate startGroupChatPollersIfNeeded];
} }
} }

@ -2608,7 +2608,7 @@
"This version of Loki Messenger is no longer supported. Please press OK to reset your account and migrate to the latest version." = "This version of Loki Messenger is no longer supported. Please press OK to reset your account and migrate to the latest version."; "This version of Loki Messenger is no longer supported. Please press OK to reset your account and migrate to the latest version." = "This version of Loki Messenger is no longer supported. Please press OK to reset your account and migrate to the latest version.";
"Loki Public Chat" = "Loki Public Chat"; "Loki Public Chat" = "Loki Public Chat";
"Loki News" = "Loki News"; "Loki News" = "Loki News";
"Messenger Updates" = "Messenger Updates"; "Loki Messenger Updates" = "Loki Messenger Updates";
"Show QR Code" = "Show QR Code"; "Show QR Code" = "Show QR Code";
"This is your personal QR code. Other people can scan it to start a secure conversation with you." = "This is your personal QR code. Other people can scan it to start a secure conversation with you."; "This is your personal QR code. Other people can scan it to start a secure conversation with you." = "This is your personal QR code. Other people can scan it to start a secure conversation with you.";
"Scan a QR Code Instead" = "Scan a QR Code Instead"; "Scan a QR Code Instead" = "Scan a QR Code Instead";

Loading…
Cancel
Save