import PromiseKit @objc(LKPublicChatPoller) public final class PublicChatPoller : NSObject { private let publicChat: OpenGroup private var pollForNewMessagesTimer: Timer? = nil private var pollForDeletedMessagesTimer: Timer? = nil private var pollForModeratorsTimer: Timer? = nil private var pollForDisplayNamesTimer: Timer? = nil private var hasStarted = false private var isPolling = false // MARK: Settings private let pollForNewMessagesInterval: TimeInterval = 4 private let pollForDeletedMessagesInterval: TimeInterval = 60 private let pollForModeratorsInterval: TimeInterval = 10 * 60 private let pollForDisplayNamesInterval: TimeInterval = 60 // MARK: Lifecycle @objc(initForPublicChat:) public init(for publicChat: OpenGroup) { self.publicChat = publicChat super.init() } @objc public func startIfNeeded() { if hasStarted { return } DispatchQueue.main.async { [weak self] in // Timers don't do well on background queues guard let strongSelf = self else { return } strongSelf.pollForNewMessagesTimer = Timer.scheduledTimer(withTimeInterval: strongSelf.pollForNewMessagesInterval, repeats: true) { _ in self?.pollForNewMessages() } strongSelf.pollForDeletedMessagesTimer = Timer.scheduledTimer(withTimeInterval: strongSelf.pollForDeletedMessagesInterval, repeats: true) { _ in self?.pollForDeletedMessages() } strongSelf.pollForModeratorsTimer = Timer.scheduledTimer(withTimeInterval: strongSelf.pollForModeratorsInterval, repeats: true) { _ in self?.pollForModerators() } strongSelf.pollForDisplayNamesTimer = Timer.scheduledTimer(withTimeInterval: strongSelf.pollForDisplayNamesInterval, repeats: true) { _ in self?.pollForDisplayNames() } // Perform initial updates strongSelf.pollForNewMessages() strongSelf.pollForDeletedMessages() strongSelf.pollForModerators() strongSelf.pollForDisplayNames() strongSelf.hasStarted = true } } @objc public func stop() { pollForNewMessagesTimer?.invalidate() pollForDeletedMessagesTimer?.invalidate() pollForModeratorsTimer?.invalidate() pollForDisplayNamesTimer?.invalidate() hasStarted = false } // MARK: Polling @objc(pollForNewMessages) public func objc_pollForNewMessages() -> AnyPromise { AnyPromise.from(pollForNewMessages()) } public func pollForNewMessages() -> Promise { guard !self.isPolling else { return Promise.value(()) } self.isPolling = true let publicChat = self.publicChat let userPublicKey = getUserHexEncodedPublicKey() return OpenGroupAPI.getMessages(for: publicChat.channel, on: publicChat.server).done(on: DispatchQueue.global(qos: .default)) { messages in self.isPolling = false let uniquePublicKeys = Set(messages.map { $0.senderPublicKey }) func proceed() { let storage = OWSPrimaryStorage.shared() /* var newDisplayNameUpdatees: Set = [] storage.dbReadConnection.read { transaction in newDisplayNameUpdatees = Set(uniquePublicKeys.filter { storage.getMasterHexEncodedPublicKey(for: $0, in: transaction) != $0 }.compactMap { storage.getMasterHexEncodedPublicKey(for: $0, in: transaction) }) } if !newDisplayNameUpdatees.isEmpty { let displayNameUpdatees = OpenGroupAPI.displayNameUpdatees[publicChat.id] ?? [] OpenGroupAPI.displayNameUpdatees[publicChat.id] = displayNameUpdatees.union(newDisplayNameUpdatees) } */ // Sorting the messages by timestamp before importing them fixes an issue where messages that quote older messages can't find those older messages messages.sorted { $0.serverTimestamp < $1.serverTimestamp }.forEach { message in var wasSentByCurrentUser = false OWSPrimaryStorage.shared().dbReadConnection.read { transaction in wasSentByCurrentUser = LokiDatabaseUtilities.isUserLinkedDevice(message.senderPublicKey, transaction: transaction) } var masterPublicKey: String? = nil storage.dbReadConnection.read { transaction in masterPublicKey = storage.getMasterHexEncodedPublicKey(for: message.senderPublicKey, in: transaction) } let senderPublicKey = masterPublicKey ?? message.senderPublicKey func generateDisplayName(from rawDisplayName: String) -> String { let endIndex = senderPublicKey.endIndex let cutoffIndex = senderPublicKey.index(endIndex, offsetBy: -8) return "\(rawDisplayName) (...\(senderPublicKey[cutoffIndex.. 0) { SSKEnvironment.shared.profileManager.updateProfileForContact(withID: masterPublicKey!, displayName: message.displayName, with: transaction) } SSKEnvironment.shared.profileManager.updateService(withProfileName: message.displayName, avatarURL: profilePicture.url) SSKEnvironment.shared.profileManager.setProfileKeyData(profilePicture.profileKey, forRecipientId: masterPublicKey!, avatarURL: profilePicture.url) } } } } /* let hexEncodedPublicKeysToUpdate = uniquePublicKeys.filter { hexEncodedPublicKey in let timeSinceLastUpdate: TimeInterval if let lastDeviceLinkUpdate = MultiDeviceProtocol.lastDeviceLinkUpdate[hexEncodedPublicKey] { timeSinceLastUpdate = Date().timeIntervalSince(lastDeviceLinkUpdate) } else { timeSinceLastUpdate = .infinity } return timeSinceLastUpdate > MultiDeviceProtocol.deviceLinkUpdateInterval } if !hexEncodedPublicKeysToUpdate.isEmpty { FileServerAPI.getDeviceLinks(associatedWith: hexEncodedPublicKeysToUpdate).done(on: DispatchQueue.global(qos: .default)) { _ in proceed() hexEncodedPublicKeysToUpdate.forEach { MultiDeviceProtocol.lastDeviceLinkUpdate[$0] = Date() // TODO: Doing this from a global queue seems a bit iffy } }.catch(on: DispatchQueue.global(qos: .default)) { error in if (error as? DotNetAPI.DotNetAPIError) == DotNetAPI.DotNetAPIError.parsingFailed { // Don't immediately re-fetch in case of failure due to a parsing error hexEncodedPublicKeysToUpdate.forEach { MultiDeviceProtocol.lastDeviceLinkUpdate[$0] = Date() // TODO: Doing this from a global queue seems a bit iffy } } proceed() } } else { */ DispatchQueue.global(qos: .default).async { proceed() } /* } */ } } private func pollForDeletedMessages() { let publicChat = self.publicChat let _ = OpenGroupAPI.getDeletedMessageServerIDs(for: publicChat.channel, on: publicChat.server).done(on: DispatchQueue.global(qos: .default)) { deletedMessageServerIDs in Storage.writeSync { transaction in let deletedMessageIDs = deletedMessageServerIDs.compactMap { OWSPrimaryStorage.shared().getIDForMessage(withServerID: UInt($0), in: transaction) } deletedMessageIDs.forEach { messageID in TSMessage.fetch(uniqueId: messageID)?.remove(with: transaction) } } } } private func pollForModerators() { let _ = OpenGroupAPI.getModerators(for: publicChat.channel, on: publicChat.server) } private func pollForDisplayNames() { let _ = OpenGroupAPI.getDisplayNames(for: publicChat.channel, on: publicChat.server) } }