pull/57/head
Niels Andriesse 5 years ago
parent b4af9d16d5
commit 8e5e113ecf

@ -565,7 +565,7 @@
B821F2F82272CED3002C88C0 /* DisplayNameVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = B821F2F72272CED3002C88C0 /* DisplayNameVC.swift */; };
B821F2FA2272CEEE002C88C0 /* SeedVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = B821F2F92272CEEE002C88C0 /* SeedVC.swift */; };
B8258493230FA5E9001B41CB /* ScanQRCodeVC.m in Sources */ = {isa = PBXBuildFile; fileRef = B8258492230FA5E9001B41CB /* ScanQRCodeVC.m */; };
B82584A02315024B001B41CB /* RSSFeedPoller.swift in Sources */ = {isa = PBXBuildFile; fileRef = B825849F2315024B001B41CB /* RSSFeedPoller.swift */; };
B82584A02315024B001B41CB /* LokiRSSFeedPoller.swift in Sources */ = {isa = PBXBuildFile; fileRef = B825849F2315024B001B41CB /* LokiRSSFeedPoller.swift */; };
B846365B22B7418B00AF1514 /* Identicon+ObjC.swift in Sources */ = {isa = PBXBuildFile; fileRef = B846365A22B7418B00AF1514 /* Identicon+ObjC.swift */; };
B84664F5235022F30083A1CD /* MentionUtilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = B84664F4235022F30083A1CD /* MentionUtilities.swift */; };
B86BD08123399883000F5AE3 /* QRCodeModal.swift in Sources */ = {isa = PBXBuildFile; fileRef = B86BD08023399883000F5AE3 /* QRCodeModal.swift */; };
@ -1377,7 +1377,7 @@
B821F2F92272CEEE002C88C0 /* SeedVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SeedVC.swift; sourceTree = "<group>"; };
B8258491230FA5DA001B41CB /* ScanQRCodeVC.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ScanQRCodeVC.h; sourceTree = "<group>"; };
B8258492230FA5E9001B41CB /* ScanQRCodeVC.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ScanQRCodeVC.m; sourceTree = "<group>"; };
B825849F2315024B001B41CB /* RSSFeedPoller.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RSSFeedPoller.swift; sourceTree = "<group>"; };
B825849F2315024B001B41CB /* LokiRSSFeedPoller.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LokiRSSFeedPoller.swift; sourceTree = "<group>"; };
B846365A22B7418B00AF1514 /* Identicon+ObjC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Identicon+ObjC.swift"; sourceTree = "<group>"; };
B84664F4235022F30083A1CD /* MentionUtilities.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MentionUtilities.swift; sourceTree = "<group>"; };
B86BD08023399883000F5AE3 /* QRCodeModal.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QRCodeModal.swift; sourceTree = "<group>"; };
@ -2647,7 +2647,6 @@
B8439518228510E9000563FE /* Loki */ = {
isa = PBXGroup;
children = (
B86BD0892339A278000F5AE3 /* Group Chat */,
B86BD0872339A1ED000F5AE3 /* Onboarding */,
B86BD08223399ABF000F5AE3 /* Settings */,
B86BD0882339A253000F5AE3 /* Utilities */,
@ -2655,6 +2654,7 @@
B8162F0422892C5F00D46544 /* FriendRequestViewDelegate.swift */,
24A830A12293CD0100F4CAC0 /* LokiP2PServer.swift */,
B89841E222B7579F00B1BDC6 /* NewConversationVC.swift */,
B825849F2315024B001B41CB /* LokiRSSFeedPoller.swift */,
B8258491230FA5DA001B41CB /* ScanQRCodeVC.h */,
B8258492230FA5E9001B41CB /* ScanQRCodeVC.m */,
24BD2608234DA2050008EB0A /* NewPublicChatVC.swift */,
@ -2706,14 +2706,6 @@
path = Utilities;
sourceTree = "<group>";
};
B86BD0892339A278000F5AE3 /* Group Chat */ = {
isa = PBXGroup;
children = (
B825849F2315024B001B41CB /* RSSFeedPoller.swift */,
);
path = "Group Chat";
sourceTree = "<group>";
};
D221A07E169C9E5E00537ABF = {
isa = PBXGroup;
children = (
@ -3786,7 +3778,7 @@
4C4AEC4520EC343B0020E72B /* DismissableTextField.swift in Sources */,
4CB5F26720F6E1E2004D1B42 /* MenuActionsViewController.swift in Sources */,
3496955E219B605E00DCFE74 /* PhotoLibrary.swift in Sources */,
B82584A02315024B001B41CB /* RSSFeedPoller.swift in Sources */,
B82584A02315024B001B41CB /* LokiRSSFeedPoller.swift in Sources */,
24A830A22293CD0100F4CAC0 /* LokiP2PServer.swift in Sources */,
349ED990221B0194008045B0 /* Onboarding2FAViewController.swift in Sources */,
45D231771DC7E8F10034FA89 /* SessionResetJob.swift in Sources */,

@ -10,9 +10,8 @@ extern NSString *const AppDelegateStoryboardMain;
- (void)startLongPollerIfNeeded;
- (void)stopLongPollerIfNeeded;
- (void)createGroupChatsIfNeeded;
- (void)setUpDefaultPublicChatsIfNeeded;
- (void)createRSSFeedsIfNeeded;
- (void)startGroupChatPollersIfNeeded;
- (void)startRSSFeedPollersIfNeeded;
@end

@ -1534,11 +1534,10 @@ static NSTimeInterval launchStartedAt;
return [[LKRSSFeed alloc] initWithId:@"loki.network.messenger-updates.feed" server:@"https://loki.network/category/messenger-updates/feed/" displayName:NSLocalizedString(@"Loki Messenger Updates", @"") isDeletable:false];
}
- (void)createGroupChatsIfNeeded
- (void)setUpDefaultPublicChatsIfNeeded
{
// Setup our default public chats
for (LKPublicChat *chat in LKPublicChat.defaultChats) {
NSString *userDefaultsKey = [@"isGroupChatSetUp." stringByAppendingString:chat.id];
for (LKPublicChat *chat in LKPublicChatAPI.defaultChats) {
NSString *userDefaultsKey = [@"isGroupChatSetUp." stringByAppendingString:chat.id]; // Should ideally be isPublicChatSetUp
BOOL isChatSetUp = [NSUserDefaults.standardUserDefaults boolForKey:userDefaultsKey];
if (!isChatSetUp || !chat.isDeletable) {
[LKPublicChatManager.shared addChatWithServer:chat.server channel:chat.channel name:chat.displayName];
@ -1593,11 +1592,6 @@ static NSTimeInterval launchStartedAt;
}
}
- (void)startGroupChatPollersIfNeeded
{
[LKPublicChatManager.shared startPollersIfNeeded];
}
- (void)startRSSFeedPollersIfNeeded
{
[self createRSSFeedPollersIfNeeded];

@ -1,7 +1,7 @@
import FeedKit
@objc(LKRSSFeedPoller)
public final class RSSFeedPoller : NSObject {
public final class LokiRSSFeedPoller : NSObject {
private let feed: LokiRSSFeed
private var timer: Timer? = nil
private var hasStarted = false

@ -5,13 +5,13 @@
final class MentionCandidateSelectionView : UIView, UITableViewDataSource, UITableViewDelegate {
@objc var mentionCandidates: [Mention] = [] { didSet { tableView.reloadData() } }
@objc var publicChatServer: String?
var publicChatServerID: UInt64?
var publicChatChannel: UInt64?
@objc var delegate: MentionCandidateSelectionViewDelegate?
// MARK: Convenience
@objc(setPublicChatServerID:)
func setPublicChatServerID(to publicChatServerID: UInt64) {
self.publicChatServerID = publicChatServerID != 0 ? publicChatServerID : nil
@objc(setPublicChatChannel:)
func setPublicChatChannel(to publicChatChannel: UInt64) {
self.publicChatChannel = publicChatChannel != 0 ? publicChatChannel : nil
}
// MARK: Components
@ -52,7 +52,7 @@ final class MentionCandidateSelectionView : UIView, UITableViewDataSource, UITab
let mentionCandidate = mentionCandidates[indexPath.row]
cell.mentionCandidate = mentionCandidate
cell.publicChatServer = publicChatServer
cell.publicChatServerID = publicChatServerID
cell.publicChatChannel = publicChatChannel
return cell
}
@ -70,7 +70,7 @@ private extension MentionCandidateSelectionView {
final class Cell : UITableViewCell {
var mentionCandidate = Mention(hexEncodedPublicKey: "", displayName: "") { didSet { update() } }
var publicChatServer: String?
var publicChatServerID: UInt64?
var publicChatChannel: UInt64?
// MARK: Components
private lazy var profilePictureImageView = AvatarImageView()
@ -130,8 +130,8 @@ private extension MentionCandidateSelectionView {
displayNameLabel.text = mentionCandidate.displayName
let profilePicture = OWSContactAvatarBuilder(signalId: mentionCandidate.hexEncodedPublicKey, colorName: .blue, diameter: 36).build()
profilePictureImageView.image = profilePicture
if let publicChatServer = publicChatServer, let publicChatServerID = publicChatServerID {
let isUserModerator = LokiPublicChatAPI.isUserModerator(mentionCandidate.hexEncodedPublicKey, for: publicChatServerID, on: publicChatServer)
if let server = publicChatServer, let channel = publicChatChannel {
let isUserModerator = LokiPublicChatAPI.isUserModerator(mentionCandidate.hexEncodedPublicKey, for: channel, on: server)
moderatorIconImageView.isHidden = !isUserModerator
} else {
moderatorIconImageView.isHidden = true

@ -64,6 +64,11 @@ final class DisplayNameVC : OnboardingBaseViewController {
TSAccountManager.sharedInstance().didRegister()
UserDefaults.standard.set(true, forKey: "didUpdateForMainnet")
onboardingController.verificationDidComplete(fromView: self)
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.setUpDefaultPublicChatsIfNeeded()
appDelegate.createRSSFeedsIfNeeded()
LokiPublicChatManager.shared.startPollersIfNeeded()
appDelegate.startRSSFeedPollersIfNeeded()
OWSProfileManager.shared().updateLocalProfileName(displayName, avatarImage: nil, success: { }, failure: { }) // Try to save the user name but ignore the result
}
}

@ -1099,7 +1099,7 @@ const CGFloat kMaxTextViewHeight = 98;
}];
if (publicChat != nil) {
self.mentionCandidateSelectionView.publicChatServer = publicChat.server;
[self.mentionCandidateSelectionView setPublicChatServerID:publicChat.channel];
[self.mentionCandidateSelectionView setPublicChatChannel:publicChat.channel];
}
self.mentionCandidateSelectionView.mentionCandidates = mentionCandidates;
self.mentionCandidateSelectionViewSizeConstraint.constant = 6 + MIN(mentionCandidates.count, 4) * 52;

@ -693,9 +693,9 @@ typedef NS_ENUM(NSInteger, HomeViewControllerSection) {
}
if (OWSIdentityManager.sharedManager.identityKeyPair != nil) {
AppDelegate *appDelegate = (AppDelegate *)UIApplication.sharedApplication.delegate;
[appDelegate createGroupChatsIfNeeded];
[appDelegate setUpDefaultPublicChatsIfNeeded];
[appDelegate createRSSFeedsIfNeeded];
[appDelegate startGroupChatPollersIfNeeded];
[LKPublicChatManager.shared startPollersIfNeeded];
[appDelegate startRSSFeedPollersIfNeeded];
}
}

@ -169,11 +169,11 @@ NS_ASSUME_NONNULL_BEGIN
# pragma mark Loki - Upgrading to Public Chat Manager
// Versions less than or equal to 1.2.0 didn't store group chat mappings
// Versions less than or equal to 1.2.0 didn't store public chat mappings
+ (void)updatePublicChatMapping
{
[OWSPrimaryStorage.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction * _Nonnull transaction) {
for (LKPublicChat *chat in LKPublicChat.defaultChats) {
for (LKPublicChat *chat in LKPublicChatAPI.defaultChats) {
TSGroupThread *thread = [TSGroupThread threadWithGroupId:chat.idAsData transaction:transaction];
if (thread != nil) {
[LKDatabaseUtilities setPublicChat:chat threadID:thread.uniqueId transaction:transaction];

@ -1,7 +1,7 @@
import PromiseKit
@objc(LKLongPoller)
public final class LongPoller : NSObject {
public final class LokiLongPoller : NSObject {
private let onMessagesReceived: ([SSKProtoEnvelope]) -> Void
private let storage = OWSPrimaryStorage.shared()
private var hasStarted = false

@ -8,16 +8,6 @@ public final class LokiPublicChat : NSObject, NSCoding {
@objc public let displayName: String
@objc public let isDeletable: Bool
@objc public static var defaultChats: [LokiPublicChat] {
var chats = [LokiPublicChat(channel: UInt64(1), server: "https://chat.lokinet.org", displayName: NSLocalizedString("Loki Public Chat", comment: ""), isDeletable: true)!]
#if DEBUG
chats.append(LokiPublicChat(channel: UInt64(1), server: "https://chat-dev.lokinet.org", displayName: "Loki Dev Chat", isDeletable: true)!)
#endif
return chats
}
@objc public init?(channel: UInt64, server: String, displayName: String, isDeletable: Bool) {
let id = "\(server).\(channel)"
self.id = id

@ -12,6 +12,15 @@ public final class LokiPublicChatAPI : LokiDotNetAPI {
@objc private static let channelInfoType = "net.patter-app.settings"
@objc public static let publicChatMessageType = "network.loki.messenger.publicChat"
@objc public static let defaultChats: [LokiPublicChat] = {
var result: [LokiPublicChat] = []
result.append(LokiPublicChat(channel: 1, server: "https://chat.lokinet.org", displayName: NSLocalizedString("Loki Public Chat", comment: ""), isDeletable: true)!)
#if DEBUG
result.append(LokiPublicChat(channel: 1, server: "https://chat-dev.lokinet.org", displayName: "Loki Dev Chat", isDeletable: true)!)
#endif
return result
}()
// MARK: Convenience
private static var userDisplayName: String {
return SSKEnvironment.shared.contactsManager.displayName(forPhoneIdentifier: userHexEncodedPublicKey) ?? "Anonymous"
@ -240,9 +249,9 @@ public final class LokiPublicChatAPI : LokiDotNetAPI {
}
}
public static func resetLastMessageCache(for group: UInt64, on server: String) {
removeLastMessageServerID(for: group, on: server)
removeLastDeletionServerID(for: group, on: server)
public static func clearCaches(for channel: UInt64, on server: String) {
removeLastMessageServerID(for: channel, on: server)
removeLastDeletionServerID(for: channel, on: server)
}
// MARK: Public API (Obj-C)

@ -12,7 +12,7 @@ public final class LokiPublicChatManager: NSObject {
@objc public static let shared = LokiPublicChatManager()
private var chats: [String: LokiPublicChat] = [:]
private var pollers: [String: GroupChatPoller] = [:]
private var pollers: [String: LokiPublicChatPoller] = [:]
private var isPolling = false
private let storage = OWSPrimaryStorage.shared()
@ -33,7 +33,7 @@ public final class LokiPublicChatManager: NSObject {
if let poller = pollers[threadID] {
poller.startIfNeeded()
} else {
let poller = GroupChatPoller(for: publicChat)
let poller = LokiPublicChatPoller(for: publicChat)
poller.startIfNeeded()
pollers[threadID] = poller
}
@ -124,7 +124,7 @@ public final class LokiPublicChatManager: NSObject {
// Reset the last message cache
if let chat = self.chats[threadId] {
LokiPublicChatAPI.resetLastMessageCache(for: chat.channel, on: chat.server)
LokiPublicChatAPI.clearCaches(for: chat.channel, on: chat.server)
}
// Remove the chat from the db

@ -1,7 +1,7 @@
@objc(LKPublicChatPoller)
public final class GroupChatPoller : NSObject {
private let group: LokiPublicChat
public final class LokiPublicChatPoller : NSObject {
private let publicChat: LokiPublicChat
private var pollForNewMessagesTimer: Timer? = nil
private var pollForDeletedMessagesTimer: Timer? = nil
private var pollForModeratorsTimer: Timer? = nil
@ -14,9 +14,9 @@ public final class GroupChatPoller : NSObject {
private let pollForModeratorsInterval: TimeInterval = 10 * 60
// MARK: Lifecycle
@objc(initForGroup:)
public init(for group: LokiPublicChat) {
self.group = group
@objc(initForPublicChat:)
public init(for publicChat: LokiPublicChat) {
self.publicChat = publicChat
super.init()
}
@ -42,7 +42,7 @@ public final class GroupChatPoller : NSObject {
// MARK: Polling
private func pollForNewMessages() {
// Prepare
let group = self.group
let publicChat = self.publicChat
let userHexEncodedPublicKey = self.userHexEncodedPublicKey
// Processing logic for incoming messages
func processIncomingMessage(_ message: LokiPublicChatMessage) {
@ -50,9 +50,9 @@ public final class GroupChatPoller : NSObject {
let endIndex = senderHexEncodedPublicKey.endIndex
let cutoffIndex = senderHexEncodedPublicKey.index(endIndex, offsetBy: -8)
let senderDisplayName = "\(message.displayName) (...\(senderHexEncodedPublicKey[cutoffIndex..<endIndex]))"
let id = group.idAsData
let id = publicChat.idAsData
let groupContext = SSKProtoGroupContext.builder(id: id, type: .deliver)
groupContext.setName(group.displayName)
groupContext.setName(publicChat.displayName)
let dataMessage = SSKProtoDataMessage.builder()
dataMessage.setTimestamp(message.timestamp)
dataMessage.setGroup(try! groupContext.build())
@ -75,7 +75,7 @@ public final class GroupChatPoller : NSObject {
envelope.setContent(try! content.build().serializedData())
let storage = OWSPrimaryStorage.shared()
storage.dbReadWriteConnection.readWrite { transaction in
transaction.setObject(senderDisplayName, forKey: senderHexEncodedPublicKey, inCollection: group.id)
transaction.setObject(senderDisplayName, forKey: senderHexEncodedPublicKey, inCollection: publicChat.id)
SSKEnvironment.shared.messageManager.throws_processEnvelope(try! envelope.build(), plaintextData: try! content.build().serializedData(), wasReceivedByUD: false, transaction: transaction)
}
}
@ -89,7 +89,7 @@ public final class GroupChatPoller : NSObject {
isDuplicate = id != nil
}
guard !isDuplicate else { return }
let groupID = group.idAsData
let groupID = publicChat.idAsData
let thread = TSGroupThread.getOrCreateThread(withGroupId: groupID)
let signalQuote: TSQuotedMessage?
if let quote = message.quote {
@ -100,7 +100,7 @@ public final class GroupChatPoller : NSObject {
let message = TSOutgoingMessage(outgoingMessageWithTimestamp: message.timestamp, in: thread, messageBody: message.body, attachmentIds: [], expiresInSeconds: 0,
expireStartedAt: 0, isVoiceMessage: false, groupMetaMessage: .deliver, quotedMessage: signalQuote, contactShare: nil, linkPreview: nil)
storage.dbReadWriteConnection.readWrite { transaction in
message.update(withSentRecipient: group.server, wasSentByUD: false, transaction: transaction)
message.update(withSentRecipient: publicChat.server, wasSentByUD: false, transaction: transaction)
message.saveGroupChatServerID(messageServerID, in: transaction)
guard let messageID = message.uniqueId else { return print("[Loki] Failed to save public chat message.") }
storage.setIDForMessageWithServerID(UInt(messageServerID), to: messageID, in: transaction)
@ -110,7 +110,7 @@ public final class GroupChatPoller : NSObject {
}
}
// Poll
let _ = LokiPublicChatAPI.getMessages(for: group.channel, on: group.server).done(on: .main) { messages in
let _ = LokiPublicChatAPI.getMessages(for: publicChat.channel, on: publicChat.server).done(on: .main) { messages in
messages.forEach { message in
if message.hexEncodedPublicKey != userHexEncodedPublicKey {
processIncomingMessage(message)
@ -122,8 +122,8 @@ public final class GroupChatPoller : NSObject {
}
private func pollForDeletedMessages() {
let group = self.group
let _ = LokiPublicChatAPI.getDeletedMessageServerIDs(for: group.channel, on: group.server).done { deletedMessageServerIDs in
let publicChat = self.publicChat
let _ = LokiPublicChatAPI.getDeletedMessageServerIDs(for: publicChat.channel, on: publicChat.server).done { deletedMessageServerIDs in
let storage = OWSPrimaryStorage.shared()
storage.dbReadWriteConnection.readWrite { transaction in
let deletedMessageIDs = deletedMessageServerIDs.compactMap { storage.getIDForMessage(withServerID: UInt($0), in: transaction) }
@ -135,6 +135,6 @@ public final class GroupChatPoller : NSObject {
}
private func pollForModerators() {
let _ = LokiPublicChatAPI.getModerators(for: group.channel, on: group.server)
let _ = LokiPublicChatAPI.getModerators(for: publicChat.channel, on: publicChat.server)
}
}
Loading…
Cancel
Save