conversations-classic-ios/ConversationsClassic/AppData/Store/ConversationStore.swift

289 lines
9.7 KiB
Swift
Raw Normal View History

2024-08-17 11:22:47 +00:00
import AVFoundation
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-17 11:22:47 +00:00
import Photos
2024-08-13 08:40:27 +00:00
@MainActor
final class ConversationStore: 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-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
}
}
extension ConversationStore {
2024-08-15 15:15:49 +00:00
func sendMessage(_ message: String) async {
2024-08-17 22:12:23 +00:00
var msg = Message.blank
msg.from = roster.bareJid
msg.to = roster.contactBareJid
msg.body = message
2024-08-15 11:37:21 +00:00
2024-08-15 15:15:49 +00:00
// store as pending on db, and send
do {
2024-08-17 22:12:23 +00:00
try await msg.save()
try await client.sendMessage(msg)
try await msg.setStatus(.sent)
2024-08-17 08:06:28 +00:00
} catch {
2024-08-17 22:12:23 +00:00
try? await msg.setStatus(.error)
2024-08-17 08:06:28 +00:00
}
2024-08-14 13:48:30 +00:00
}
}
2024-08-17 16:15:05 +00:00
extension ConversationStore {
var attachmentsStore: AttachmentsStore {
AttachmentsStore()
}
func sendMedia(_ items: [GalleryItem]) async {
2024-08-17 23:21:15 +00:00
for item in items {
Task {
var message = Message.blank
message.from = roster.bareJid
message.to = roster.contactBareJid
switch item.type {
case .photo:
guard let asset = PHAsset.fetchAssets(withLocalIdentifiers: [item.id], options: nil).firstObject else { return }
guard let photo = try? await PHImageManager.default().getPhoto(for: asset) else { return }
guard let data = photo.jpegData(compressionQuality: 1.0) else { return }
let localName = "\(message.id)_\(UUID().uuidString).jpg"
let localUrl = Const.fileFolder.appendingPathComponent(localName)
try? data.write(to: localUrl)
message.contentType = .attachment(
Attachment(
type: .image,
localName: localName,
thumbnailName: nil,
remotePath: nil
)
)
try? await message.save()
case .video:
guard let asset = PHAsset.fetchAssets(withLocalIdentifiers: [item.id], options: nil).firstObject else { return }
guard let video = try? await PHImageManager.default().getVideo(for: asset) else { return }
// swiftlint:disable:next force_cast
let assetURL = video as! AVURLAsset
let url = assetURL.url
let localName = "\(message.id)_\(UUID().uuidString).mov"
let localUrl = Const.fileFolder.appendingPathComponent(localName)
try? FileManager.default.copyItem(at: url, to: localUrl)
message.contentType = .attachment(
Attachment(
type: .video,
localName: localName,
thumbnailName: nil,
remotePath: nil
)
)
try? await message.save()
}
await upload(message)
}
}
2024-08-17 16:15:05 +00:00
}
2024-08-17 16:25:35 +00:00
func sendCaptured(_ data: Data, _ type: GalleryMediaType) async {
2024-08-17 21:12:39 +00:00
// save locally and make message
2024-08-17 22:12:23 +00:00
var message = Message.blank
message.from = roster.bareJid
message.to = roster.contactBareJid
2024-08-17 21:33:14 +00:00
let localName: String
let msgType: AttachmentType
2024-08-17 21:12:39 +00:00
do {
2024-08-17 22:05:18 +00:00
(localName, msgType) = try await Task {
// local name
let fileId = UUID().uuidString
let localName: String
let msgType: AttachmentType
switch type {
case .photo:
2024-08-17 22:12:23 +00:00
localName = "\(message.id)_\(fileId).jpg"
2024-08-17 22:05:18 +00:00
msgType = .image
case .video:
2024-08-17 22:12:23 +00:00
localName = "\(message.id)_\(fileId).mov"
2024-08-17 22:05:18 +00:00
msgType = .video
}
// save
let localUrl = Const.fileFolder.appendingPathComponent(localName)
try data.write(to: localUrl)
return (localName, msgType)
}.value
2024-08-17 21:12:39 +00:00
} catch {
logIt(.error, "Can't save file for uploading: \(error)")
2024-08-17 21:33:14 +00:00
return
}
// save message
2024-08-17 22:12:23 +00:00
message.contentType = .attachment(
Attachment(
type: msgType,
localName: localName,
thumbnailName: nil,
remotePath: nil
)
2024-08-17 21:33:14 +00:00
)
do {
try await message.save()
} catch {
logIt(.error, "Can't save message: \(error)")
return
2024-08-17 21:12:39 +00:00
}
// upload and save
2024-08-17 21:33:14 +00:00
await upload(message)
2024-08-17 16:56:04 +00:00
}
func sendDocuments(_ data: [Data], _ extensions: [String]) async {
2024-08-17 23:21:15 +00:00
for (index, data) in data.enumerated() {
Task {
let newMessageId = UUID().uuidString
let fileId = UUID().uuidString
let localName = "\(newMessageId)_\(fileId).\(extensions[index])"
let localUrl = Const.fileFolder.appendingPathComponent(localName)
do {
try data.write(to: localUrl)
} catch {
print("FileProcessing: Error writing document: \(error)")
return
}
var message = Message.blank
message.from = roster.bareJid
message.to = roster.contactBareJid
message.contentType = .attachment(
Attachment(
type: localName.attachmentType,
localName: localName,
thumbnailName: nil,
remotePath: nil
)
)
do {
try await message.save()
await upload(message)
} catch {
print("FileProcessing: Error saving document: \(error)")
}
}
}
2024-08-17 16:56:04 +00:00
}
func sendContact(_ jidStr: String) async {
2024-08-17 17:37:19 +00:00
await sendMessage("contact:\(jidStr)")
2024-08-17 16:56:04 +00:00
}
2024-08-17 17:33:54 +00:00
func sendLocation(_ lat: Double, _ lon: Double) async {
2024-08-17 17:37:19 +00:00
await sendMessage("geo:\(lat),\(lon)")
2024-08-17 17:33:54 +00:00
}
2024-08-17 21:12:39 +00:00
2024-08-17 21:33:14 +00:00
private func upload(_ message: Message) async {
2024-08-17 21:12:39 +00:00
do {
2024-08-17 21:33:14 +00:00
try await message.setStatus(.pending)
var message = message
guard case .attachment(let attachment) = message.contentType else {
throw ClientStoreError.invalidContentType
}
2024-08-17 22:05:18 +00:00
guard let localName = attachment.localPath else {
2024-08-17 21:33:14 +00:00
throw ClientStoreError.invalidLocalName
}
let remotePath = try await client.uploadFile(localName)
2024-08-17 21:12:39 +00:00
message.contentType = .attachment(
Attachment(
2024-08-17 21:33:14 +00:00
type: attachment.type,
localName: attachment.localName,
2024-08-17 21:12:39 +00:00
thumbnailName: nil,
remotePath: remotePath
)
)
2024-08-17 22:05:18 +00:00
message.body = remotePath
message.oobUrl = remotePath
2024-08-17 21:12:39 +00:00
try await message.save()
2024-08-17 21:33:14 +00:00
try await client.sendMessage(message)
try await message.setStatus(.sent)
2024-08-17 21:12:39 +00:00
} catch {
2024-08-17 21:33:14 +00:00
try? await message.setStatus(.error)
2024-08-17 21:12:39 +00:00
}
}
2024-08-17 23:21:15 +00:00
func downloadAttachment(_ message: Message) async {
guard case .attachment(let attachment) = message.contentType else {
return
}
guard let remotePath = attachment.remotePath, let remoteUrl = URL(string: remotePath) else {
return
}
do {
let localName = "\(message.id)_\(UUID().uuidString).\(remoteUrl.lastPathComponent)"
let localUrl = Const.fileFolder.appendingPathComponent(localName)
// Download the file
let (tempUrl, _) = try await URLSession.shared.download(from: remoteUrl)
try FileManager.default.moveItem(at: tempUrl, to: localUrl)
var message = message
message.contentType = .attachment(
Attachment(
type: attachment.type,
localName: localName,
thumbnailName: attachment.thumbnailName,
remotePath: remotePath
)
)
try await message.save()
} catch {
logIt(.error, "Can't download attachment: \(error)")
}
}
2024-08-17 16:56:04 +00:00
}
extension ConversationStore {
var contacts: [Roster] {
get async {
do {
let rosters = try await Database.shared.dbQueue.read { db in
try Roster
.filter(Column("locallyDeleted") == false)
.fetchAll(db)
}
return rosters
} catch {
return []
}
}
2024-08-17 16:25:35 +00:00
}
2024-08-17 16:15:05 +00:00
}
2024-08-15 11:37:21 +00:00
private extension ConversationStore {
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
self?.messages = messages
2024-08-15 11:37:21 +00:00
}
2024-08-13 08:40:27 +00:00
}
}