another.im-ios/ConversationsClassic/AppData/Client/Client+MartinMessages.swift

101 lines
3 KiB
Swift
Raw Normal View History

2024-08-14 09:29:51 +00:00
import Combine
2024-08-14 13:48:30 +00:00
import Foundation
2024-08-14 09:29:51 +00:00
import GRDB
import Martin
final class ClientMartinMessagesManager {
2024-08-18 11:26:05 +00:00
private var cancellables: Set<AnyCancellable> = []
2024-08-14 09:29:51 +00:00
init(_ xmppConnection: XMPPClient) {
2024-08-18 11:26:05 +00:00
// subscribe to client messages
xmppConnection.module(MessageModule.self).messagesPublisher
2024-08-14 09:29:51 +00:00
.sink { [weak self] message in
2024-08-18 11:26:05 +00:00
self?.handleMessage(message.message)
2024-08-14 09:29:51 +00:00
}
2024-08-18 11:26:05 +00:00
.store(in: &cancellables)
// subscribe to carbons
xmppConnection.module(MessageCarbonsModule.self).carbonsPublisher
.sink { [weak self] carbon in
self?.handleMessage(carbon.message)
}
.store(in: &cancellables)
// enable carbons if available
xmppConnection.module(.messageCarbons).$isAvailable.filter { $0 }
.sink(receiveValue: { [weak xmppConnection] _ in
xmppConnection?.module(.messageCarbons).enable()
})
.store(in: &cancellables)
2024-08-14 09:29:51 +00:00
}
2024-08-18 11:26:05 +00:00
private func handleMessage(_ received: Martin.Message) {
2024-08-14 09:29:51 +00:00
#if DEBUG
print("---")
2024-08-18 11:26:05 +00:00
print("Message received: \(received)")
2024-08-14 09:29:51 +00:00
print("---")
#endif
2024-08-14 13:48:30 +00:00
// Check that the message type is supported
let chatTypes: [StanzaType] = [.chat, .groupchat]
2024-08-18 11:26:05 +00:00
guard let mType = received.type, chatTypes.contains(mType) else {
2024-08-14 13:48:30 +00:00
#if DEBUG
2024-08-18 11:26:05 +00:00
print("Unsupported received type: \(received.type?.rawValue ?? "nil")")
2024-08-14 13:48:30 +00:00
#endif
return
}
// Type
2024-08-18 11:26:05 +00:00
let type = MessageType(rawValue: received.type?.rawValue ?? "") ?? .chat
2024-08-14 13:48:30 +00:00
// Content type
var contentType: MessageContentType = .text
2024-08-18 11:26:05 +00:00
if let oob = received.oob {
2024-08-17 23:21:15 +00:00
contentType = .attachment(.init(
type: oob.attachmentType,
localName: nil,
thumbnailName: nil,
remotePath: oob
))
2024-08-18 11:26:05 +00:00
} else if received.hints.contains(.noStore) {
2024-08-14 13:48:30 +00:00
contentType = .typing
2024-08-17 23:21:15 +00:00
// skip for now
2024-08-15 11:37:21 +00:00
return
2024-08-14 13:48:30 +00:00
}
// From/To
2024-08-18 11:26:05 +00:00
let from = received.from?.bareJid.stringValue ?? ""
let to = received.to?.bareJid.stringValue
2024-08-14 13:48:30 +00:00
// Extract date or set current
var date = Date()
2024-08-18 11:26:05 +00:00
if let timestampStr = received.attribute("archived_date"), let timeInterval = TimeInterval(timestampStr) {
2024-08-14 13:48:30 +00:00
date = Date(timeIntervalSince1970: timeInterval)
}
// Msg
let msg = Message(
2024-08-18 11:26:05 +00:00
id: received.id ?? UUID().uuidString,
2024-08-14 13:48:30 +00:00
type: type,
date: date,
contentType: contentType,
status: .sent,
from: from,
to: to,
2024-08-18 11:26:05 +00:00
body: received.body,
subject: received.subject,
thread: received.thread,
oobUrl: received.oob
2024-08-14 13:48:30 +00:00
)
// Save message
Task {
do {
try await msg.save()
} catch {
logIt(.error, "Error saving message: \(error)")
}
}
2024-08-14 09:29:51 +00:00
}
}