2024-06-19 15:15:27 +00:00
|
|
|
import Foundation
|
|
|
|
import GRDB
|
|
|
|
import Martin
|
|
|
|
|
2024-08-11 00:28:01 +00:00
|
|
|
final class RosterManager: Martin.RosterManager {
|
2024-06-19 15:15:27 +00:00
|
|
|
func clear(for context: Martin.Context) {
|
|
|
|
do {
|
2024-08-11 00:28:01 +00:00
|
|
|
try Database.shared.dbQueue.write { db in
|
2024-06-19 15:15:27 +00:00
|
|
|
try Roster
|
|
|
|
.filter(Column("bareJid") == context.userBareJid.stringValue)
|
|
|
|
.deleteAll(db)
|
|
|
|
|
|
|
|
try RosterVersion
|
|
|
|
.filter(Column("bareJid") == context.userBareJid.stringValue)
|
|
|
|
.deleteAll(db)
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
logIt(.error, "Error clearing roster: \(error.localizedDescription)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func items(for context: Martin.Context) -> [any Martin.RosterItemProtocol] {
|
|
|
|
do {
|
2024-08-11 00:28:01 +00:00
|
|
|
let rosters: [Roster] = try Database.shared.dbQueue.read { db in
|
2024-06-19 15:15:27 +00:00
|
|
|
try Roster.filter(Column("bareJid") == context.userBareJid.stringValue).fetchAll(db)
|
|
|
|
}
|
|
|
|
return rosters.map { roster in
|
|
|
|
RosterItemBase(
|
|
|
|
jid: JID(roster.bareJid),
|
|
|
|
name: roster.name,
|
|
|
|
subscription: RosterItemSubscription(rawValue: roster.subscription) ?? .none,
|
|
|
|
groups: roster.data.groups,
|
|
|
|
ask: roster.ask,
|
|
|
|
annotations: roster.data.annotations
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
logIt(.error, "Error fetching roster items: \(error.localizedDescription)")
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func item(for context: Martin.Context, jid: Martin.JID) -> (any Martin.RosterItemProtocol)? {
|
|
|
|
do {
|
2024-08-11 00:28:01 +00:00
|
|
|
let roster: Roster? = try Database.shared.dbQueue.read { db in
|
2024-06-19 15:15:27 +00:00
|
|
|
try Roster
|
|
|
|
.filter(Column("bareJid") == context.userBareJid.stringValue)
|
|
|
|
.filter(Column("contactBareJid") == jid.stringValue)
|
|
|
|
.fetchOne(db)
|
|
|
|
}
|
|
|
|
if let roster {
|
|
|
|
return RosterItemBase(
|
|
|
|
jid: JID(roster.bareJid),
|
|
|
|
name: roster.name,
|
|
|
|
subscription: RosterItemSubscription(rawValue: roster.subscription) ?? .none,
|
|
|
|
groups: roster.data.groups,
|
|
|
|
ask: roster.ask,
|
|
|
|
annotations: roster.data.annotations
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
logIt(.error, "Error fetching roster item: \(error.localizedDescription)")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateItem(for context: Martin.Context, jid: Martin.JID, name: String?, subscription: Martin.RosterItemSubscription, groups: [String], ask: Bool, annotations: [Martin.RosterItemAnnotation]) -> (any Martin.RosterItemProtocol)? {
|
|
|
|
do {
|
|
|
|
let roster = Roster(
|
|
|
|
bareJid: context.userBareJid.stringValue,
|
|
|
|
contactBareJid: jid.stringValue,
|
|
|
|
name: name,
|
|
|
|
subscription: subscription.rawValue,
|
|
|
|
ask: ask,
|
|
|
|
data: DBRosterData(
|
|
|
|
groups: groups,
|
|
|
|
annotations: annotations
|
|
|
|
)
|
|
|
|
)
|
2024-08-11 00:28:01 +00:00
|
|
|
try Database.shared.dbQueue.write { db in
|
2024-06-19 15:15:27 +00:00
|
|
|
try roster.save(db)
|
|
|
|
}
|
|
|
|
return RosterItemBase(jid: jid, name: name, subscription: subscription, groups: groups, ask: ask, annotations: annotations)
|
|
|
|
} catch {
|
|
|
|
logIt(.error, "Error updating roster item: \(error.localizedDescription)")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteItem(for context: Martin.Context, jid: Martin.JID) -> (any Martin.RosterItemProtocol)? {
|
|
|
|
do {
|
2024-08-11 00:28:01 +00:00
|
|
|
let roster: Roster? = try Database.shared.dbQueue.read { db in
|
2024-06-19 15:15:27 +00:00
|
|
|
try Roster
|
|
|
|
.filter(Column("bareJid") == context.userBareJid.stringValue)
|
|
|
|
.filter(Column("contactBareJid") == jid.stringValue)
|
|
|
|
.fetchOne(db)
|
|
|
|
}
|
|
|
|
if let roster {
|
2024-08-11 00:28:01 +00:00
|
|
|
_ = try Database.shared.dbQueue.write { db in
|
2024-06-19 15:15:27 +00:00
|
|
|
try roster.delete(db)
|
|
|
|
}
|
|
|
|
return RosterItemBase(
|
|
|
|
jid: JID(roster.bareJid),
|
|
|
|
name: roster.name,
|
|
|
|
subscription: RosterItemSubscription(rawValue: roster.subscription) ?? .none,
|
|
|
|
groups: roster.data.groups,
|
|
|
|
ask: roster.ask,
|
|
|
|
annotations: roster.data.annotations
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
logIt(.error, "Error fetching roster version: \(error.localizedDescription)")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func version(for context: Martin.Context) -> String? {
|
|
|
|
do {
|
2024-08-11 00:28:01 +00:00
|
|
|
let version: RosterVersion? = try Database.shared.dbQueue.read { db in
|
2024-06-19 15:15:27 +00:00
|
|
|
try RosterVersion
|
|
|
|
.filter(Column("bareJid") == context.userBareJid.stringValue)
|
|
|
|
.fetchOne(db)
|
|
|
|
}
|
|
|
|
return version?.version
|
|
|
|
} catch {
|
|
|
|
logIt(.error, "Error fetching roster version: \(error.localizedDescription)")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func set(version: String?, for context: Martin.Context) {
|
|
|
|
guard let version else { return }
|
|
|
|
do {
|
2024-08-11 00:28:01 +00:00
|
|
|
try Database.shared.dbQueue.write { db in
|
2024-06-19 15:15:27 +00:00
|
|
|
let rosterVersion = RosterVersion(
|
|
|
|
bareJid: context.userBareJid.stringValue,
|
|
|
|
version: version
|
|
|
|
)
|
|
|
|
try rosterVersion.save(db)
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
logIt(.error, "Error setting roster version: \(error.localizedDescription)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func initialize(context _: Martin.Context) {}
|
|
|
|
func deinitialize(context _: Martin.Context) {}
|
2024-07-29 16:36:22 +00:00
|
|
|
}
|