mv-experiment #1

Merged
fmodf merged 88 commits from mv-experiment into develop 2024-09-03 15:13:59 +00:00
2 changed files with 37 additions and 23 deletions
Showing only changes of commit 57ecd115d9 - Show all commits

View file

@ -4,75 +4,88 @@ import GRDB
import Martin import Martin
final class ClientMartinMessagesManager { final class ClientMartinMessagesManager {
private var cancellable: AnyCancellable? private var cancellables: Set<AnyCancellable> = []
init(_ xmppConnection: XMPPClient) { init(_ xmppConnection: XMPPClient) {
cancellable = xmppConnection.module(MessageModule.self).messagesPublisher // subscribe to client messages
xmppConnection.module(MessageModule.self).messagesPublisher
.sink { [weak self] message in .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) { private func handleMessage(_ received: Martin.Message) {
let message = received.message
let chat = received.chat
#if DEBUG #if DEBUG
print("---") print("---")
print("Message received: \(message)") print("Message received: \(received)")
print("In chat: \(chat)")
print("---") print("---")
#endif #endif
// Check that the message type is supported // Check that the message type is supported
let chatTypes: [StanzaType] = [.chat, .groupchat] 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 #if DEBUG
print("Unsupported message type: \(message.type?.rawValue ?? "nil")") print("Unsupported received type: \(received.type?.rawValue ?? "nil")")
#endif #endif
return return
} }
// Type // Type
let type = MessageType(rawValue: message.type?.rawValue ?? "") ?? .chat let type = MessageType(rawValue: received.type?.rawValue ?? "") ?? .chat
// Content type // Content type
var contentType: MessageContentType = .text var contentType: MessageContentType = .text
if let oob = message.oob { if let oob = received.oob {
contentType = .attachment(.init( contentType = .attachment(.init(
type: oob.attachmentType, type: oob.attachmentType,
localName: nil, localName: nil,
thumbnailName: nil, thumbnailName: nil,
remotePath: oob remotePath: oob
)) ))
} else if message.hints.contains(.noStore) { } else if received.hints.contains(.noStore) {
contentType = .typing contentType = .typing
// skip for now // skip for now
return return
} }
// From/To // From/To
let from = message.from?.bareJid.stringValue ?? "" let from = received.from?.bareJid.stringValue ?? ""
let to = message.to?.bareJid.stringValue let to = received.to?.bareJid.stringValue
// Extract date or set current // Extract date or set current
var date = Date() 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) date = Date(timeIntervalSince1970: timeInterval)
} }
// Msg // Msg
let msg = Message( let msg = Message(
id: message.id ?? UUID().uuidString, id: received.id ?? UUID().uuidString,
type: type, type: type,
date: date, date: date,
contentType: contentType, contentType: contentType,
status: .sent, status: .sent,
from: from, from: from,
to: to, to: to,
body: message.body, body: received.body,
subject: message.subject, subject: received.subject,
thread: message.thread, thread: received.thread,
oobUrl: message.oob oobUrl: received.oob
) )
// Save message // Save message

View file

@ -96,6 +96,7 @@ extension Client {
} }
let msg = chat.createMessage(text: message.body ?? "??", id: message.id) let msg = chat.createMessage(text: message.body ?? "??", id: message.id)
msg.oob = message.oobUrl
try await chat.send(message: msg) try await chat.send(message: msg)
} }
@ -128,7 +129,7 @@ extension Client {
switch response { switch response {
case let httpResponse as HTTPURLResponse where httpResponse.statusCode == 201: case let httpResponse as HTTPURLResponse where httpResponse.statusCode == 201:
return slot.getUri.absoluteString return slot.getUri.absoluteString
default: default:
throw URLError(.badServerResponse) throw URLError(.badServerResponse)
} }
@ -164,7 +165,7 @@ private extension Client {
client.modulesManager.register(MessageModule(chatManager: chat)) client.modulesManager.register(MessageModule(chatManager: chat))
// client.modulesManager.register(MessageArchiveManagementModule()) // client.modulesManager.register(MessageArchiveManagementModule())
// client.modulesManager.register(MessageCarbonsModule()) client.modulesManager.register(MessageCarbonsModule())
// file transfer modules // file transfer modules
client.modulesManager.register(HttpFileUploadModule()) client.modulesManager.register(HttpFileUploadModule())