mv-experiment #1
|
@ -4,75 +4,88 @@ import GRDB
|
|||
import Martin
|
||||
|
||||
final class ClientMartinMessagesManager {
|
||||
private var cancellable: AnyCancellable?
|
||||
private var cancellables: Set<AnyCancellable> = []
|
||||
|
||||
init(_ xmppConnection: XMPPClient) {
|
||||
cancellable = xmppConnection.module(MessageModule.self).messagesPublisher
|
||||
// subscribe to client messages
|
||||
xmppConnection.module(MessageModule.self).messagesPublisher
|
||||
.sink { [weak self] message in
|
||||
self?.handleClientMessage(message)
|
||||
self?.handleMessage(message.message)
|
||||
}
|
||||
.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)
|
||||
}
|
||||
|
||||
private func handleClientMessage(_ received: MessageModule.MessageReceived) {
|
||||
let message = received.message
|
||||
let chat = received.chat
|
||||
private func handleMessage(_ received: Martin.Message) {
|
||||
#if DEBUG
|
||||
print("---")
|
||||
print("Message received: \(message)")
|
||||
print("In chat: \(chat)")
|
||||
print("Message received: \(received)")
|
||||
print("---")
|
||||
#endif
|
||||
|
||||
// Check that the message type is supported
|
||||
let chatTypes: [StanzaType] = [.chat, .groupchat]
|
||||
guard let mType = message.type, chatTypes.contains(mType) else {
|
||||
guard let mType = received.type, chatTypes.contains(mType) else {
|
||||
#if DEBUG
|
||||
print("Unsupported message type: \(message.type?.rawValue ?? "nil")")
|
||||
print("Unsupported received type: \(received.type?.rawValue ?? "nil")")
|
||||
#endif
|
||||
return
|
||||
}
|
||||
|
||||
// Type
|
||||
let type = MessageType(rawValue: message.type?.rawValue ?? "") ?? .chat
|
||||
let type = MessageType(rawValue: received.type?.rawValue ?? "") ?? .chat
|
||||
|
||||
// Content type
|
||||
var contentType: MessageContentType = .text
|
||||
if let oob = message.oob {
|
||||
if let oob = received.oob {
|
||||
contentType = .attachment(.init(
|
||||
type: oob.attachmentType,
|
||||
localName: nil,
|
||||
thumbnailName: nil,
|
||||
remotePath: oob
|
||||
))
|
||||
} else if message.hints.contains(.noStore) {
|
||||
} else if received.hints.contains(.noStore) {
|
||||
contentType = .typing
|
||||
// skip for now
|
||||
return
|
||||
}
|
||||
|
||||
// From/To
|
||||
let from = message.from?.bareJid.stringValue ?? ""
|
||||
let to = message.to?.bareJid.stringValue
|
||||
let from = received.from?.bareJid.stringValue ?? ""
|
||||
let to = received.to?.bareJid.stringValue
|
||||
|
||||
// Extract date or set current
|
||||
var date = Date()
|
||||
if let timestampStr = message.attribute("archived_date"), let timeInterval = TimeInterval(timestampStr) {
|
||||
if let timestampStr = received.attribute("archived_date"), let timeInterval = TimeInterval(timestampStr) {
|
||||
date = Date(timeIntervalSince1970: timeInterval)
|
||||
}
|
||||
|
||||
// Msg
|
||||
let msg = Message(
|
||||
id: message.id ?? UUID().uuidString,
|
||||
id: received.id ?? UUID().uuidString,
|
||||
type: type,
|
||||
date: date,
|
||||
contentType: contentType,
|
||||
status: .sent,
|
||||
from: from,
|
||||
to: to,
|
||||
body: message.body,
|
||||
subject: message.subject,
|
||||
thread: message.thread,
|
||||
oobUrl: message.oob
|
||||
body: received.body,
|
||||
subject: received.subject,
|
||||
thread: received.thread,
|
||||
oobUrl: received.oob
|
||||
)
|
||||
|
||||
// Save message
|
||||
|
|
|
@ -96,6 +96,7 @@ extension Client {
|
|||
}
|
||||
|
||||
let msg = chat.createMessage(text: message.body ?? "??", id: message.id)
|
||||
msg.oob = message.oobUrl
|
||||
try await chat.send(message: msg)
|
||||
}
|
||||
|
||||
|
@ -128,7 +129,7 @@ extension Client {
|
|||
switch response {
|
||||
case let httpResponse as HTTPURLResponse where httpResponse.statusCode == 201:
|
||||
return slot.getUri.absoluteString
|
||||
|
||||
|
||||
default:
|
||||
throw URLError(.badServerResponse)
|
||||
}
|
||||
|
@ -164,7 +165,7 @@ private extension Client {
|
|||
client.modulesManager.register(MessageModule(chatManager: chat))
|
||||
// client.modulesManager.register(MessageArchiveManagementModule())
|
||||
|
||||
// client.modulesManager.register(MessageCarbonsModule())
|
||||
client.modulesManager.register(MessageCarbonsModule())
|
||||
|
||||
// file transfer modules
|
||||
client.modulesManager.register(HttpFileUploadModule())
|
||||
|
|
Loading…
Reference in a new issue