|
|
|
@ -48,135 +48,8 @@ public final class LokiPublicChatPoller : NSObject {
|
|
|
|
|
|
|
|
|
|
// MARK: Polling
|
|
|
|
|
private func pollForNewMessages() {
|
|
|
|
|
// Prepare
|
|
|
|
|
let publicChat = self.publicChat
|
|
|
|
|
let userHexEncodedPublicKey = self.userHexEncodedPublicKey
|
|
|
|
|
// Processing logic for incoming messages
|
|
|
|
|
func processIncomingMessage(_ message: LokiPublicChatMessage) {
|
|
|
|
|
let storage = OWSPrimaryStorage.shared()
|
|
|
|
|
var masterHexEncodedPublicKey: String? = nil
|
|
|
|
|
storage.dbReadConnection.read { transaction in
|
|
|
|
|
masterHexEncodedPublicKey = storage.getMasterHexEncodedPublicKey(for: message.hexEncodedPublicKey, in: transaction)
|
|
|
|
|
}
|
|
|
|
|
let senderHexEncodedPublicKey = masterHexEncodedPublicKey ?? message.hexEncodedPublicKey
|
|
|
|
|
func generateDisplayName(from rawDisplayName: String) -> String {
|
|
|
|
|
let endIndex = senderHexEncodedPublicKey.endIndex
|
|
|
|
|
let cutoffIndex = senderHexEncodedPublicKey.index(endIndex, offsetBy: -8)
|
|
|
|
|
return "\(rawDisplayName) (...\(senderHexEncodedPublicKey[cutoffIndex..<endIndex]))"
|
|
|
|
|
}
|
|
|
|
|
var senderDisplayName = ""
|
|
|
|
|
if let masterHexEncodedPublicKey = masterHexEncodedPublicKey {
|
|
|
|
|
senderDisplayName = DisplayNameUtilities.getPublicChatDisplayName(for: senderHexEncodedPublicKey, in: publicChat.channel, on: publicChat.server) ?? generateDisplayName(from: NSLocalizedString("Anonymous", comment: ""))
|
|
|
|
|
} else {
|
|
|
|
|
senderDisplayName = generateDisplayName(from: message.displayName)
|
|
|
|
|
}
|
|
|
|
|
let id = publicChat.idAsData
|
|
|
|
|
let groupContext = SSKProtoGroupContext.builder(id: id, type: .deliver)
|
|
|
|
|
groupContext.setName(publicChat.displayName)
|
|
|
|
|
let dataMessage = SSKProtoDataMessage.builder()
|
|
|
|
|
let attachments: [SSKProtoAttachmentPointer] = message.attachments.compactMap { attachment in
|
|
|
|
|
guard attachment.kind == .attachment else { return nil }
|
|
|
|
|
let result = SSKProtoAttachmentPointer.builder(id: attachment.serverID)
|
|
|
|
|
result.setContentType(attachment.contentType)
|
|
|
|
|
result.setSize(UInt32(attachment.size))
|
|
|
|
|
result.setFileName(attachment.fileName)
|
|
|
|
|
result.setFlags(UInt32(attachment.flags))
|
|
|
|
|
result.setWidth(UInt32(attachment.width))
|
|
|
|
|
result.setHeight(UInt32(attachment.height))
|
|
|
|
|
if let caption = attachment.caption {
|
|
|
|
|
result.setCaption(caption)
|
|
|
|
|
}
|
|
|
|
|
result.setUrl(attachment.url)
|
|
|
|
|
return try! result.build()
|
|
|
|
|
}
|
|
|
|
|
dataMessage.setAttachments(attachments)
|
|
|
|
|
if let linkPreview = message.attachments.first(where: { $0.kind == .linkPreview }) {
|
|
|
|
|
let signalLinkPreview = SSKProtoDataMessagePreview.builder(url: linkPreview.linkPreviewURL!)
|
|
|
|
|
signalLinkPreview.setTitle(linkPreview.linkPreviewTitle!)
|
|
|
|
|
let attachment = SSKProtoAttachmentPointer.builder(id: linkPreview.serverID)
|
|
|
|
|
attachment.setContentType(linkPreview.contentType)
|
|
|
|
|
attachment.setSize(UInt32(linkPreview.size))
|
|
|
|
|
attachment.setFileName(linkPreview.fileName)
|
|
|
|
|
attachment.setFlags(UInt32(linkPreview.flags))
|
|
|
|
|
attachment.setWidth(UInt32(linkPreview.width))
|
|
|
|
|
attachment.setHeight(UInt32(linkPreview.height))
|
|
|
|
|
if let caption = linkPreview.caption {
|
|
|
|
|
attachment.setCaption(caption)
|
|
|
|
|
}
|
|
|
|
|
attachment.setUrl(linkPreview.url)
|
|
|
|
|
signalLinkPreview.setImage(try! attachment.build())
|
|
|
|
|
dataMessage.setPreview([ try! signalLinkPreview.build() ])
|
|
|
|
|
}
|
|
|
|
|
dataMessage.setTimestamp(message.timestamp)
|
|
|
|
|
dataMessage.setGroup(try! groupContext.build())
|
|
|
|
|
if let quote = message.quote {
|
|
|
|
|
let signalQuote = SSKProtoDataMessageQuote.builder(id: quote.quotedMessageTimestamp, author: quote.quoteeHexEncodedPublicKey)
|
|
|
|
|
signalQuote.setText(quote.quotedMessageBody)
|
|
|
|
|
dataMessage.setQuote(try! signalQuote.build())
|
|
|
|
|
}
|
|
|
|
|
let body = (message.body == message.timestamp.description) ? "" : message.body // Workaround for the fact that the back-end doesn't accept messages without a body
|
|
|
|
|
dataMessage.setBody(body)
|
|
|
|
|
if let messageServerID = message.serverID {
|
|
|
|
|
let publicChatInfo = SSKProtoPublicChatInfo.builder()
|
|
|
|
|
publicChatInfo.setServerID(messageServerID)
|
|
|
|
|
dataMessage.setPublicChatInfo(try! publicChatInfo.build())
|
|
|
|
|
}
|
|
|
|
|
let content = SSKProtoContent.builder()
|
|
|
|
|
content.setDataMessage(try! dataMessage.build())
|
|
|
|
|
let envelope = SSKProtoEnvelope.builder(type: .ciphertext, timestamp: message.timestamp)
|
|
|
|
|
envelope.setSource(senderHexEncodedPublicKey)
|
|
|
|
|
envelope.setSourceDevice(OWSDevicePrimaryDeviceId)
|
|
|
|
|
envelope.setContent(try! content.build().serializedData())
|
|
|
|
|
storage.dbReadWriteConnection.readWrite { transaction in
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Processing logic for outgoing messages
|
|
|
|
|
func processOutgoingMessage(_ message: LokiPublicChatMessage) {
|
|
|
|
|
guard let messageServerID = message.serverID else { return }
|
|
|
|
|
let storage = OWSPrimaryStorage.shared()
|
|
|
|
|
var isDuplicate = false
|
|
|
|
|
storage.dbReadConnection.read { transaction in
|
|
|
|
|
let id = storage.getIDForMessage(withServerID: UInt(messageServerID), in: transaction)
|
|
|
|
|
isDuplicate = id != nil
|
|
|
|
|
}
|
|
|
|
|
guard !isDuplicate else { return }
|
|
|
|
|
let groupID = publicChat.idAsData
|
|
|
|
|
let thread = TSGroupThread.getOrCreateThread(withGroupId: groupID)
|
|
|
|
|
let signalQuote: TSQuotedMessage?
|
|
|
|
|
if let quote = message.quote {
|
|
|
|
|
signalQuote = TSQuotedMessage(timestamp: quote.quotedMessageTimestamp, authorId: quote.quoteeHexEncodedPublicKey, body: quote.quotedMessageBody, quotedAttachmentsForSending: [])
|
|
|
|
|
} else {
|
|
|
|
|
signalQuote = nil
|
|
|
|
|
}
|
|
|
|
|
var attachmentIDs: [String] = []
|
|
|
|
|
// TODO: Restore attachments
|
|
|
|
|
let signalLinkPreview: OWSLinkPreview?
|
|
|
|
|
if let linkPreview = message.attachments.first(where: { $0.kind == .linkPreview }) {
|
|
|
|
|
let attachment = TSAttachmentPointer(serverId: linkPreview.serverID, encryptionKey: nil, byteCount: UInt32(linkPreview.size), contentType: linkPreview.contentType, sourceFilename: linkPreview.fileName, caption: linkPreview.caption, albumMessageId: nil)
|
|
|
|
|
attachment.save()
|
|
|
|
|
signalLinkPreview = OWSLinkPreview(urlString: linkPreview.linkPreviewURL!, title: linkPreview.linkPreviewTitle!, imageAttachmentId: attachment.uniqueId!, isDirectAttachment: false)
|
|
|
|
|
} else {
|
|
|
|
|
signalLinkPreview = nil
|
|
|
|
|
}
|
|
|
|
|
let body = (message.body == message.timestamp.description) ? "" : message.body // Workaround for the fact that the back-end doesn't accept messages without a body
|
|
|
|
|
let signalMessage = TSOutgoingMessage(outgoingMessageWithTimestamp: message.timestamp, in: thread, messageBody: body, attachmentIds: NSMutableArray(array: attachmentIDs), expiresInSeconds: 0,
|
|
|
|
|
expireStartedAt: 0, isVoiceMessage: false, groupMetaMessage: .deliver, quotedMessage: signalQuote, contactShare: nil, linkPreview: signalLinkPreview)
|
|
|
|
|
signalMessage.actualSenderHexEncodedPublicKey = message.hexEncodedPublicKey
|
|
|
|
|
storage.dbReadWriteConnection.readWrite { transaction in
|
|
|
|
|
signalMessage.update(withSentRecipient: publicChat.server, wasSentByUD: false, transaction: transaction)
|
|
|
|
|
signalMessage.saveGroupChatServerID(messageServerID, in: transaction)
|
|
|
|
|
guard let messageID = signalMessage.uniqueId else { return print("[Loki] Failed to save public chat message.") }
|
|
|
|
|
storage.setIDForMessageWithServerID(UInt(messageServerID), to: messageID, in: transaction)
|
|
|
|
|
}
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
if let linkPreviewURL = OWSLinkPreview.previewUrl(forMessageBodyText: message.body, selectedRange: nil) {
|
|
|
|
|
signalMessage.generateLinkPreviewIfNeeded(fromURL: linkPreviewURL)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Poll
|
|
|
|
|
let _ = LokiPublicChatAPI.getMessages(for: publicChat.channel, on: publicChat.server).done(on: DispatchQueue.global()) { messages in
|
|
|
|
|
let uniqueHexEncodedPublicKeys = Set(messages.map { $0.hexEncodedPublicKey })
|
|
|
|
|
func proceed() {
|
|
|
|
@ -194,10 +67,97 @@ public final class LokiPublicChatPoller : NSObject {
|
|
|
|
|
OWSPrimaryStorage.shared().dbReadConnection.read { transaction in
|
|
|
|
|
wasSentByCurrentUser = LokiDatabaseUtilities.isUserLinkedDevice(message.hexEncodedPublicKey, transaction: transaction)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var masterHexEncodedPublicKey: String? = nil
|
|
|
|
|
storage.dbReadConnection.read { transaction in
|
|
|
|
|
masterHexEncodedPublicKey = storage.getMasterHexEncodedPublicKey(for: message.hexEncodedPublicKey, in: transaction)
|
|
|
|
|
}
|
|
|
|
|
let senderHexEncodedPublicKey = masterHexEncodedPublicKey ?? message.hexEncodedPublicKey
|
|
|
|
|
func generateDisplayName(from rawDisplayName: String) -> String {
|
|
|
|
|
let endIndex = senderHexEncodedPublicKey.endIndex
|
|
|
|
|
let cutoffIndex = senderHexEncodedPublicKey.index(endIndex, offsetBy: -8)
|
|
|
|
|
return "\(rawDisplayName) (...\(senderHexEncodedPublicKey[cutoffIndex..<endIndex]))"
|
|
|
|
|
}
|
|
|
|
|
var senderDisplayName = ""
|
|
|
|
|
if let masterHexEncodedPublicKey = masterHexEncodedPublicKey {
|
|
|
|
|
senderDisplayName = DisplayNameUtilities.getPublicChatDisplayName(for: senderHexEncodedPublicKey, in: publicChat.channel, on: publicChat.server) ?? generateDisplayName(from: NSLocalizedString("Anonymous", comment: ""))
|
|
|
|
|
} else {
|
|
|
|
|
senderDisplayName = generateDisplayName(from: message.displayName)
|
|
|
|
|
}
|
|
|
|
|
let id = publicChat.idAsData
|
|
|
|
|
let groupContext = SSKProtoGroupContext.builder(id: id, type: .deliver)
|
|
|
|
|
groupContext.setName(publicChat.displayName)
|
|
|
|
|
let dataMessage = SSKProtoDataMessage.builder()
|
|
|
|
|
let attachments: [SSKProtoAttachmentPointer] = message.attachments.compactMap { attachment in
|
|
|
|
|
guard attachment.kind == .attachment else { return nil }
|
|
|
|
|
let result = SSKProtoAttachmentPointer.builder(id: attachment.serverID)
|
|
|
|
|
result.setContentType(attachment.contentType)
|
|
|
|
|
result.setSize(UInt32(attachment.size))
|
|
|
|
|
result.setFileName(attachment.fileName)
|
|
|
|
|
result.setFlags(UInt32(attachment.flags))
|
|
|
|
|
result.setWidth(UInt32(attachment.width))
|
|
|
|
|
result.setHeight(UInt32(attachment.height))
|
|
|
|
|
if let caption = attachment.caption {
|
|
|
|
|
result.setCaption(caption)
|
|
|
|
|
}
|
|
|
|
|
result.setUrl(attachment.url)
|
|
|
|
|
return try! result.build()
|
|
|
|
|
}
|
|
|
|
|
dataMessage.setAttachments(attachments)
|
|
|
|
|
if let linkPreview = message.attachments.first(where: { $0.kind == .linkPreview }) {
|
|
|
|
|
let signalLinkPreview = SSKProtoDataMessagePreview.builder(url: linkPreview.linkPreviewURL!)
|
|
|
|
|
signalLinkPreview.setTitle(linkPreview.linkPreviewTitle!)
|
|
|
|
|
let attachment = SSKProtoAttachmentPointer.builder(id: linkPreview.serverID)
|
|
|
|
|
attachment.setContentType(linkPreview.contentType)
|
|
|
|
|
attachment.setSize(UInt32(linkPreview.size))
|
|
|
|
|
attachment.setFileName(linkPreview.fileName)
|
|
|
|
|
attachment.setFlags(UInt32(linkPreview.flags))
|
|
|
|
|
attachment.setWidth(UInt32(linkPreview.width))
|
|
|
|
|
attachment.setHeight(UInt32(linkPreview.height))
|
|
|
|
|
if let caption = linkPreview.caption {
|
|
|
|
|
attachment.setCaption(caption)
|
|
|
|
|
}
|
|
|
|
|
attachment.setUrl(linkPreview.url)
|
|
|
|
|
signalLinkPreview.setImage(try! attachment.build())
|
|
|
|
|
dataMessage.setPreview([ try! signalLinkPreview.build() ])
|
|
|
|
|
}
|
|
|
|
|
dataMessage.setTimestamp(message.timestamp)
|
|
|
|
|
dataMessage.setGroup(try! groupContext.build())
|
|
|
|
|
if let quote = message.quote {
|
|
|
|
|
let signalQuote = SSKProtoDataMessageQuote.builder(id: quote.quotedMessageTimestamp, author: quote.quoteeHexEncodedPublicKey)
|
|
|
|
|
signalQuote.setText(quote.quotedMessageBody)
|
|
|
|
|
dataMessage.setQuote(try! signalQuote.build())
|
|
|
|
|
}
|
|
|
|
|
let body = (message.body == message.timestamp.description) ? "" : message.body // Workaround for the fact that the back-end doesn't accept messages without a body
|
|
|
|
|
dataMessage.setBody(body)
|
|
|
|
|
if let messageServerID = message.serverID {
|
|
|
|
|
let publicChatInfo = SSKProtoPublicChatInfo.builder()
|
|
|
|
|
publicChatInfo.setServerID(messageServerID)
|
|
|
|
|
dataMessage.setPublicChatInfo(try! publicChatInfo.build())
|
|
|
|
|
}
|
|
|
|
|
let content = SSKProtoContent.builder()
|
|
|
|
|
if !wasSentByCurrentUser {
|
|
|
|
|
processIncomingMessage(message)
|
|
|
|
|
content.setDataMessage(try! dataMessage.build())
|
|
|
|
|
} else {
|
|
|
|
|
processOutgoingMessage(message)
|
|
|
|
|
let syncMessageSentBuilder = SSKProtoSyncMessageSent.builder()
|
|
|
|
|
syncMessageSentBuilder.setMessage(try! dataMessage.build())
|
|
|
|
|
syncMessageSentBuilder.setDestination(userHexEncodedPublicKey)
|
|
|
|
|
syncMessageSentBuilder.setTimestamp(message.timestamp)
|
|
|
|
|
let syncMessageSent = try! syncMessageSentBuilder.build()
|
|
|
|
|
let syncMessageBuilder = SSKProtoSyncMessage.builder()
|
|
|
|
|
syncMessageBuilder.setSent(syncMessageSent)
|
|
|
|
|
content.setSyncMessage(try! syncMessageBuilder.build())
|
|
|
|
|
}
|
|
|
|
|
let envelope = SSKProtoEnvelope.builder(type: .ciphertext, timestamp: message.timestamp)
|
|
|
|
|
envelope.setSource(senderHexEncodedPublicKey)
|
|
|
|
|
envelope.setSourceDevice(OWSDevicePrimaryDeviceId)
|
|
|
|
|
envelope.setContent(try! content.build().serializedData())
|
|
|
|
|
storage.dbReadWriteConnection.readWrite { transaction in
|
|
|
|
|
transaction.setObject(senderDisplayName, forKey: senderHexEncodedPublicKey, inCollection: publicChat.id)
|
|
|
|
|
let messageServerID = message.serverID
|
|
|
|
|
SSKEnvironment.shared.messageManager.throws_processEnvelope(try! envelope.build(), plaintextData: try! content.build().serializedData(), wasReceivedByUD: false, transaction: transaction, serverID: messageServerID ?? 0)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|