another.im-ios/ConversationsClassic/AppData/Store/MessagesStore.swift

235 lines
8 KiB
Swift
Raw Normal View History

2024-08-14 13:48:30 +00:00
import Combine
2024-08-13 08:40:27 +00:00
import Foundation
2024-08-14 13:48:30 +00:00
import GRDB
2024-08-19 01:25:27 +00:00
import Martin
2024-08-13 08:40:27 +00:00
@MainActor
2024-08-18 16:27:18 +00:00
final class MessagesStore: ObservableObject {
2024-08-15 15:15:49 +00:00
@Published private(set) var messages: [Message] = []
@Published var replyText = ""
2024-08-13 08:40:27 +00:00
2024-08-17 11:22:47 +00:00
private(set) var roster: Roster
2024-08-13 08:40:27 +00:00
private let client: Client
2024-08-14 13:48:30 +00:00
2024-08-15 11:37:21 +00:00
private var messagesCancellable: AnyCancellable?
2024-08-19 05:22:54 +00:00
private let archiver = ArchiveMessageFetcher()
2024-08-13 08:40:27 +00:00
init(roster: Roster, client: Client) {
self.client = client
self.roster = roster
2024-08-15 15:15:49 +00:00
subscribe()
2024-08-14 13:48:30 +00:00
}
}
2024-08-19 01:25:27 +00:00
// MARK: - Send message
2024-08-18 16:27:18 +00:00
extension MessagesStore {
2024-08-18 10:26:54 +00:00
func sendMessage(_ message: String) {
Task {
var msg = Message.blank
msg.from = roster.bareJid
msg.to = roster.contactBareJid
msg.body = message
// store as pending on db, and send
do {
try await msg.save()
try await client.sendMessage(msg)
try await msg.setStatus(.sent)
} catch {
try? await msg.setStatus(.error)
2024-08-17 21:33:14 +00:00
}
2024-08-17 21:12:39 +00:00
}
}
2024-08-17 23:21:15 +00:00
2024-08-18 10:26:54 +00:00
func sendContact(_ jidStr: String) {
sendMessage("contact:\(jidStr)")
}
2024-08-17 23:21:15 +00:00
2024-08-18 10:26:54 +00:00
func sendLocation(_ lat: Double, _ lon: Double) {
sendMessage("geo:\(lat),\(lon)")
2024-08-17 23:21:15 +00:00
}
2024-08-17 16:56:04 +00:00
}
2024-08-19 01:25:27 +00:00
// MARK: - Subscriptions
2024-08-18 16:27:18 +00:00
private extension MessagesStore {
2024-08-15 15:15:49 +00:00
func subscribe() {
messagesCancellable = ValueObservation.tracking(Message
.filter(
(Column("to") == roster.bareJid && Column("from") == roster.contactBareJid) ||
(Column("from") == roster.bareJid && Column("to") == roster.contactBareJid)
2024-08-15 11:37:21 +00:00
)
2024-08-15 15:15:49 +00:00
.order(Column("date").desc)
.fetchAll
)
.publisher(in: Database.shared.dbQueue, scheduling: .immediate)
.receive(on: DispatchQueue.main)
.sink { _ in
} receiveValue: { [weak self] messages in
2024-08-19 05:22:54 +00:00
guard let self else { return }
self.messages = messages
Task {
await self.archiver.initialFetch(messages, self.roster, self.client)
2024-08-19 01:25:27 +00:00
}
}
}
}
2024-08-19 05:22:54 +00:00
// MARK: - Fetch archived messages
2024-08-19 01:25:27 +00:00
extension MessagesStore {
2024-08-19 05:22:54 +00:00
func fetchForward() {
2024-08-19 01:25:27 +00:00
Task {
2024-08-19 05:22:54 +00:00
await archiver.fetchForward(roster, client)
2024-08-15 11:37:21 +00:00
}
2024-08-13 08:40:27 +00:00
}
2024-08-19 01:25:27 +00:00
2024-08-19 05:22:54 +00:00
func fetchBackward() {
2024-08-19 01:25:27 +00:00
Task {
2024-08-19 05:22:54 +00:00
await archiver.fetchBackward(roster, client)
2024-08-19 01:25:27 +00:00
}
}
}
private actor ArchiveMessageFetcher {
2024-08-19 05:22:54 +00:00
private var initFetchStarted = false
private var forwardRsm: RSM.Query?
private var backwardRsm: RSM.Query?
2024-08-19 01:25:27 +00:00
2024-08-19 05:22:54 +00:00
func initialFetch(_ messages: [Message], _ roster: Roster, _ client: Client) async {
if initFetchStarted { return }
initFetchStarted = true
2024-08-19 01:25:27 +00:00
do {
2024-08-19 05:22:54 +00:00
if let firstExistId = messages.first?.id {
let result = try await client.fetchArchiveMessages(for: roster, query: .init(before: firstExistId, max: Const.mamRequestLimit))
result.complete ? forwardRsm = nil : (forwardRsm = .init(after: result.rsm?.last, max: Const.mamRequestLimit))
result.complete ? backwardRsm = nil : (backwardRsm = .init(before: result.rsm?.first, max: Const.mamRequestLimit))
} else {
let result = try await client.fetchArchiveMessages(for: roster, query: .init(lastItems: Const.mamRequestLimit))
result.complete ? backwardRsm = nil : (backwardRsm = .init(before: result.rsm?.first, max: Const.mamRequestLimit))
2024-08-19 01:25:27 +00:00
}
} catch {
logIt(.error, "Error requesting archived messages: \(error)")
2024-08-19 05:22:54 +00:00
initFetchStarted = false
2024-08-19 01:25:27 +00:00
}
}
2024-08-19 05:22:54 +00:00
func fetchForward(_ roster: Roster, _ client: Client) {
guard let rsm = forwardRsm else { return }
Task {
let result = try await client.fetchArchiveMessages(for: roster, query: rsm)
result.complete ? (forwardRsm = nil) : (forwardRsm = .init(after: result.rsm?.last, max: Const.mamRequestLimit))
2024-08-19 01:25:27 +00:00
}
2024-08-19 05:22:54 +00:00
}
2024-08-19 01:25:27 +00:00
2024-08-19 05:22:54 +00:00
func fetchBackward(_ roster: Roster, _ client: Client) {
guard let rsm = backwardRsm else { return }
Task {
let result = try await client.fetchArchiveMessages(for: roster, query: rsm)
result.complete ? (backwardRsm = nil) : (backwardRsm = .init(before: result.rsm?.first, max: Const.mamRequestLimit))
2024-08-19 01:25:27 +00:00
}
}
2024-08-19 05:22:54 +00:00
// func fetchBackward(_ roster: Roster, _ client: Client) {
// guard let rsm = backwardRsm else { return }
// Task {
// let result = try await client.fetchArchiveMessages(for: roster, query: rsm)
// result.complete ? (backwardRsm = nil) : (backwardRsm = .init(before: result.rsm?.first, max: Const.mamRequestLimit))
// }
// }
2024-08-13 08:40:27 +00:00
}
2024-08-19 05:22:54 +00:00
// MARK: - Archived messages
// extension MessagesStore {
// func requestEarliestArchivedMessages() {
// guard let beforeId = messages.first?.id else { return }
// Task {
// await archiveMessageFetcher.fetchAfterMessages(roster, client, afterId: beforeId)
// // await archiveMessageFetcher.fetchBeforeMessages(roster, client, beforeId: beforeId)
// }
// }
//
// func requestLatestArchivedMessages() {
// guard let afterId = messages.last?.id else { return }
// Task {
// await archiveMessageFetcher.fetchBeforeMessages(roster, client, beforeId: afterId)
// // await archiveMessageFetcher.fetchAfterMessages(roster, client, afterId: afterId)
// }
// }
//
// private func requestLastArchivedMessages() {
// Task {
// await archiveMessageFetcher.fetchLastMessages(roster, client)
// }
// }
// }
// private actor ArchiveMessageFetcher {
// private var afterAvailable = true
// private var beforeAvailable = true
// private var isFetching = false
// private var fetchingIsPossinle = true
//
// func fetchLastMessages(_ roster: Roster, _ client: Client) async {
// if !fetchingIsPossinle { return }
// while isFetching {
// await Task.yield()
// }
// isFetching = true
//
// let query: RSM.Query = .init(lastItems: Const.mamRequestLimit)
// do {
// _ = try await client.fetchArchiveMessages(for: roster, query: query)
// } catch AppError.featureNotSupported {
// fetchingIsPossinle = false
// } catch {
// logIt(.error, "Error requesting archived messages: \(error)")
// }
//
// isFetching = false
// }
//
// func fetchBeforeMessages(_ roster: Roster, _ client: Client, beforeId: String) async {
// if !fetchingIsPossinle || !beforeAvailable { return }
// while isFetching {
// await Task.yield()
// }
// isFetching = true
//
// let query: RSM.Query = .init(before: beforeId, max: Const.mamRequestLimit)
// do {
// let result = try await client.fetchArchiveMessages(for: roster, query: query)
// if result.complete {
// beforeAvailable = false
// }
// } catch AppError.featureNotSupported {
// fetchingIsPossinle = false
// } catch {
// logIt(.error, "Error requesting archived messages: \(error)")
// }
//
// isFetching = false
// }
//
// func fetchAfterMessages(_ roster: Roster, _ client: Client, afterId: String) async {
// if !fetchingIsPossinle || !afterAvailable { return }
// while isFetching {
// await Task.yield()
// }
// isFetching = true
//
// let query: RSM.Query = .init(after: afterId, max: Const.mamRequestLimit)
// do {
// let result = try await client.fetchArchiveMessages(for: roster, query: query)
// if result.complete {
// afterAvailable = false
// }
// } catch AppError.featureNotSupported {
// fetchingIsPossinle = false
// } catch {
// logIt(.error, "Error requesting archived messages: \(error)")
// }
//
// isFetching = false
// }
// }