2020-08-18 05:13:37 +00:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
|
|
|
import Combine
|
2020-09-05 02:31:43 +00:00
|
|
|
import Foundation
|
2020-08-18 05:13:37 +00:00
|
|
|
import GRDB
|
2020-09-04 06:12:06 +00:00
|
|
|
import Keychain
|
2020-08-30 23:33:11 +00:00
|
|
|
import Mastodon
|
2020-09-04 06:12:06 +00:00
|
|
|
import Secrets
|
2020-08-18 05:13:37 +00:00
|
|
|
|
2020-09-03 03:28:34 +00:00
|
|
|
public struct ContentDatabase {
|
2020-09-28 03:11:49 +00:00
|
|
|
private let databaseWriter: DatabaseWriter
|
2020-08-18 05:13:37 +00:00
|
|
|
|
2020-09-04 06:12:06 +00:00
|
|
|
public init(identityID: UUID, inMemory: Bool, keychain: Keychain.Type) throws {
|
2020-09-03 03:28:34 +00:00
|
|
|
if inMemory {
|
2020-09-28 03:11:49 +00:00
|
|
|
databaseWriter = DatabaseQueue()
|
2020-08-18 05:13:37 +00:00
|
|
|
} else {
|
2020-09-04 06:12:06 +00:00
|
|
|
let path = try Self.fileURL(identityID: identityID).path
|
|
|
|
var configuration = Configuration()
|
|
|
|
|
2020-09-07 14:25:26 +00:00
|
|
|
configuration.prepareDatabase {
|
2020-09-28 03:11:49 +00:00
|
|
|
try $0.usePassphrase(Secrets.databaseKey(identityID: identityID, keychain: keychain))
|
2020-09-04 06:12:06 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 03:11:49 +00:00
|
|
|
databaseWriter = try DatabasePool(path: path, configuration: configuration)
|
2020-08-18 05:13:37 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 06:05:08 +00:00
|
|
|
try migrator.migrate(databaseWriter)
|
2020-09-29 06:35:11 +00:00
|
|
|
try clean()
|
2020-08-18 05:13:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-03 03:28:34 +00:00
|
|
|
public extension ContentDatabase {
|
2020-09-03 01:14:33 +00:00
|
|
|
static func delete(forIdentityID identityID: UUID) throws {
|
2020-09-15 23:58:04 +00:00
|
|
|
try FileManager.default.removeItem(at: fileURL(identityID: identityID))
|
2020-09-03 01:14:33 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 09:39:44 +00:00
|
|
|
func insert(status: Status) -> AnyPublisher<Never, Error> {
|
2020-09-28 03:11:49 +00:00
|
|
|
databaseWriter.writePublisher(updates: status.save)
|
2020-09-02 09:39:44 +00:00
|
|
|
.ignoreOutput()
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
|
|
|
func insert(statuses: [Status], timeline: Timeline) -> AnyPublisher<Never, Error> {
|
2020-09-28 03:11:49 +00:00
|
|
|
databaseWriter.writePublisher {
|
2020-10-02 07:41:30 +00:00
|
|
|
let timelineRecord = TimelineRecord(timeline: timeline)
|
|
|
|
|
|
|
|
try timelineRecord.save($0)
|
|
|
|
|
|
|
|
let maxIDPresent = try String.fetchOne($0, timelineRecord.statuses.select(max(StatusRecord.Columns.id)))
|
2020-08-18 05:13:37 +00:00
|
|
|
|
|
|
|
for status in statuses {
|
2020-09-02 09:39:44 +00:00
|
|
|
try status.save($0)
|
2020-08-18 05:13:37 +00:00
|
|
|
|
2020-09-02 09:39:44 +00:00
|
|
|
try TimelineStatusJoin(timelineId: timeline.id, statusId: status.id).save($0)
|
2020-09-02 02:39:06 +00:00
|
|
|
}
|
2020-10-02 07:41:30 +00:00
|
|
|
|
|
|
|
if let maxIDPresent = maxIDPresent,
|
|
|
|
let minIDInserted = statuses.map(\.id).min(),
|
|
|
|
minIDInserted > maxIDPresent {
|
2020-10-03 09:19:05 +00:00
|
|
|
try LoadMoreRecord(timelineId: timeline.id, afterStatusId: minIDInserted).save($0)
|
2020-10-02 07:41:30 +00:00
|
|
|
}
|
2020-09-02 02:39:06 +00:00
|
|
|
}
|
|
|
|
.ignoreOutput()
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
|
|
|
func insert(context: Context, parentID: String) -> AnyPublisher<Never, Error> {
|
2020-09-28 03:11:49 +00:00
|
|
|
databaseWriter.writePublisher {
|
2020-10-03 09:19:05 +00:00
|
|
|
for (index, status) in context.ancestors.enumerated() {
|
2020-09-02 09:39:44 +00:00
|
|
|
try status.save($0)
|
2020-10-03 09:19:05 +00:00
|
|
|
try StatusAncestorJoin(parentId: parentID, statusId: status.id, index: index).save($0)
|
2020-09-02 02:39:06 +00:00
|
|
|
}
|
|
|
|
|
2020-10-03 09:19:05 +00:00
|
|
|
for (index, status) in context.descendants.enumerated() {
|
|
|
|
try status.save($0)
|
|
|
|
try StatusDescendantJoin(parentId: parentID, statusId: status.id, index: index).save($0)
|
2020-08-18 05:13:37 +00:00
|
|
|
}
|
2020-10-03 09:19:05 +00:00
|
|
|
|
|
|
|
try StatusAncestorJoin.filter(
|
|
|
|
StatusAncestorJoin.Columns.parentId == parentID
|
|
|
|
&& !context.ancestors.map(\.id).contains(StatusAncestorJoin.Columns.statusId))
|
|
|
|
.deleteAll($0)
|
|
|
|
|
|
|
|
try StatusDescendantJoin.filter(
|
|
|
|
StatusDescendantJoin.Columns.parentId == parentID
|
|
|
|
&& !context.descendants.map(\.id).contains(StatusDescendantJoin.Columns.statusId))
|
|
|
|
.deleteAll($0)
|
2020-08-18 05:13:37 +00:00
|
|
|
}
|
2020-08-26 09:19:38 +00:00
|
|
|
.ignoreOutput()
|
2020-08-18 05:13:37 +00:00
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:16:41 +00:00
|
|
|
func insert(pinnedStatuses: [Status], accountID: String) -> AnyPublisher<Never, Error> {
|
2020-09-28 03:11:49 +00:00
|
|
|
databaseWriter.writePublisher {
|
2020-09-18 00:16:41 +00:00
|
|
|
for (index, status) in pinnedStatuses.enumerated() {
|
|
|
|
try status.save($0)
|
|
|
|
try AccountPinnedStatusJoin(accountId: accountID, statusId: status.id, index: index).save($0)
|
|
|
|
}
|
|
|
|
|
|
|
|
try AccountPinnedStatusJoin.filter(
|
2020-09-29 06:06:25 +00:00
|
|
|
AccountPinnedStatusJoin.Columns.accountId == accountID
|
|
|
|
&& !pinnedStatuses.map(\.id).contains(AccountPinnedStatusJoin.Columns.statusId))
|
2020-09-18 00:16:41 +00:00
|
|
|
.deleteAll($0)
|
|
|
|
}
|
|
|
|
.ignoreOutput()
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
2020-09-28 22:40:03 +00:00
|
|
|
func append(accounts: [Account], toList list: AccountList) -> AnyPublisher<Never, Error> {
|
2020-09-28 03:11:49 +00:00
|
|
|
databaseWriter.writePublisher {
|
2020-09-28 22:40:03 +00:00
|
|
|
try list.save($0)
|
|
|
|
|
|
|
|
let count = try list.accounts.fetchCount($0)
|
|
|
|
|
|
|
|
for (index, account) in accounts.enumerated() {
|
2020-09-22 06:53:11 +00:00
|
|
|
try account.save($0)
|
2020-09-28 22:40:03 +00:00
|
|
|
try AccountListJoin(accountId: account.id, listId: list.id, index: count + index).save($0)
|
2020-09-22 06:53:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.ignoreOutput()
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
2020-09-26 03:28:50 +00:00
|
|
|
func setLists(_ lists: [List]) -> AnyPublisher<Never, Error> {
|
2020-09-28 03:11:49 +00:00
|
|
|
databaseWriter.writePublisher {
|
2020-08-29 03:50:58 +00:00
|
|
|
for list in lists {
|
2020-10-02 07:41:30 +00:00
|
|
|
try TimelineRecord(timeline: Timeline.list(list)).save($0)
|
2020-08-29 03:50:58 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 02:35:06 +00:00
|
|
|
try TimelineRecord
|
|
|
|
.filter(!lists.map(\.id).contains(TimelineRecord.Columns.listId)
|
|
|
|
&& TimelineRecord.Columns.listTitle != nil)
|
2020-09-09 05:40:49 +00:00
|
|
|
.deleteAll($0)
|
2020-08-29 03:50:58 +00:00
|
|
|
}
|
|
|
|
.ignoreOutput()
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
2020-09-26 03:28:50 +00:00
|
|
|
func createList(_ list: List) -> AnyPublisher<Never, Error> {
|
2020-10-02 07:41:30 +00:00
|
|
|
databaseWriter.writePublisher(updates: TimelineRecord(timeline: Timeline.list(list)).save)
|
2020-08-29 03:50:58 +00:00
|
|
|
.ignoreOutput()
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteList(id: String) -> AnyPublisher<Never, Error> {
|
2020-10-01 02:35:06 +00:00
|
|
|
databaseWriter.writePublisher(updates: TimelineRecord.filter(TimelineRecord.Columns.listId == id).deleteAll)
|
2020-08-29 10:26:26 +00:00
|
|
|
.ignoreOutput()
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
|
|
|
func setFilters(_ filters: [Filter]) -> AnyPublisher<Never, Error> {
|
2020-09-28 03:11:49 +00:00
|
|
|
databaseWriter.writePublisher {
|
2020-08-29 10:26:26 +00:00
|
|
|
for filter in filters {
|
|
|
|
try filter.save($0)
|
|
|
|
}
|
|
|
|
|
2020-09-29 06:06:25 +00:00
|
|
|
try Filter.filter(!filters.map(\.id).contains(Filter.Columns.id)).deleteAll($0)
|
2020-08-29 10:26:26 +00:00
|
|
|
}
|
2020-08-29 03:50:58 +00:00
|
|
|
.ignoreOutput()
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
2020-08-29 10:26:26 +00:00
|
|
|
func createFilter(_ filter: Filter) -> AnyPublisher<Never, Error> {
|
2020-09-28 03:11:49 +00:00
|
|
|
databaseWriter.writePublisher(updates: filter.save)
|
2020-08-29 10:26:26 +00:00
|
|
|
.ignoreOutput()
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteFilter(id: String) -> AnyPublisher<Never, Error> {
|
2020-09-29 06:06:25 +00:00
|
|
|
databaseWriter.writePublisher(updates: Filter.filter(Filter.Columns.id == id).deleteAll)
|
2020-08-29 10:26:26 +00:00
|
|
|
.ignoreOutput()
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
2020-10-02 03:19:14 +00:00
|
|
|
func observation(timeline: Timeline) -> AnyPublisher<[[Timeline.Item]], Error> {
|
2020-10-03 09:19:05 +00:00
|
|
|
ValueObservation.tracking { db -> (TimelineItemsInfo?, [Filter]) in
|
|
|
|
(try TimelineItemsInfo.request(
|
|
|
|
TimelineRecord.filter(TimelineRecord.Columns.id == timeline.id)).fetchOne(db),
|
|
|
|
try Filter.active.fetchAll(db))
|
2020-09-02 02:39:06 +00:00
|
|
|
}
|
2020-10-03 09:19:05 +00:00
|
|
|
.map { $0?.items(filters: $1) ?? [] }
|
2020-08-19 22:16:03 +00:00
|
|
|
.removeDuplicates()
|
2020-10-01 02:35:06 +00:00
|
|
|
.publisher(in: databaseWriter)
|
2020-08-19 22:16:03 +00:00
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
2020-08-29 03:50:58 +00:00
|
|
|
|
2020-10-02 03:19:14 +00:00
|
|
|
func contextObservation(parentID: String) -> AnyPublisher<[[Timeline.Item]], Error> {
|
2020-10-03 09:19:05 +00:00
|
|
|
ValueObservation.tracking { db -> (ContextItemsInfo?, [Filter]) in
|
|
|
|
(try ContextItemsInfo.request(StatusRecord.filter(StatusRecord.Columns.id == parentID)).fetchOne(db),
|
|
|
|
try Filter.active.fetchAll(db))
|
2020-09-18 00:16:41 +00:00
|
|
|
}
|
2020-10-03 09:19:05 +00:00
|
|
|
.map { $0?.items(filters: $1) ?? [] }
|
2020-09-18 00:16:41 +00:00
|
|
|
.removeDuplicates()
|
2020-10-01 02:35:06 +00:00
|
|
|
.publisher(in: databaseWriter)
|
2020-09-18 00:16:41 +00:00
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
2020-08-29 03:50:58 +00:00
|
|
|
func listsObservation() -> AnyPublisher<[Timeline], Error> {
|
2020-10-01 02:35:06 +00:00
|
|
|
ValueObservation.tracking(TimelineRecord.filter(TimelineRecord.Columns.listId != nil)
|
|
|
|
.order(TimelineRecord.Columns.listTitle.asc)
|
2020-08-29 03:50:58 +00:00
|
|
|
.fetchAll)
|
2020-08-29 10:26:26 +00:00
|
|
|
.removeDuplicates()
|
2020-09-28 03:11:49 +00:00
|
|
|
.publisher(in: databaseWriter)
|
2020-10-03 09:19:05 +00:00
|
|
|
.tryMap { $0.map(Timeline.init(record:)).compactMap { $0 } }
|
2020-08-29 10:26:26 +00:00
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
2020-10-02 03:19:14 +00:00
|
|
|
func activeFiltersObservation(date: Date) -> AnyPublisher<[Filter], Error> {
|
2020-09-29 06:06:25 +00:00
|
|
|
ValueObservation.tracking(
|
|
|
|
Filter.filter(Filter.Columns.expiresAt == nil || Filter.Columns.expiresAt > date).fetchAll)
|
2020-08-30 00:32:34 +00:00
|
|
|
.removeDuplicates()
|
2020-10-01 02:35:06 +00:00
|
|
|
.publisher(in: databaseWriter)
|
2020-08-30 00:32:34 +00:00
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
|
|
|
func expiredFiltersObservation(date: Date) -> AnyPublisher<[Filter], Error> {
|
2020-09-29 06:06:25 +00:00
|
|
|
ValueObservation.tracking(Filter.filter(Filter.Columns.expiresAt < date).fetchAll)
|
2020-08-30 00:32:34 +00:00
|
|
|
.removeDuplicates()
|
2020-09-28 03:11:49 +00:00
|
|
|
.publisher(in: databaseWriter)
|
2020-08-30 00:32:34 +00:00
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
2020-09-22 06:53:11 +00:00
|
|
|
|
|
|
|
func accountObservation(id: String) -> AnyPublisher<Account?, Error> {
|
2020-09-29 23:56:09 +00:00
|
|
|
ValueObservation.tracking(AccountInfo.request(AccountRecord.filter(AccountRecord.Columns.id == id)).fetchOne)
|
2020-09-22 06:53:11 +00:00
|
|
|
.removeDuplicates()
|
|
|
|
.map {
|
2020-09-29 23:56:09 +00:00
|
|
|
if let info = $0 {
|
|
|
|
return Account(info: info)
|
2020-09-22 06:53:11 +00:00
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2020-10-01 02:35:06 +00:00
|
|
|
.publisher(in: databaseWriter)
|
2020-09-22 06:53:11 +00:00
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
2020-09-28 22:40:03 +00:00
|
|
|
|
|
|
|
func accountListObservation(_ list: AccountList) -> AnyPublisher<[Account], Error> {
|
|
|
|
ValueObservation.tracking(list.accounts.fetchAll)
|
|
|
|
.removeDuplicates()
|
2020-09-29 23:56:09 +00:00
|
|
|
.map { $0.map(Account.init(info:)) }
|
2020-10-01 02:35:06 +00:00
|
|
|
.publisher(in: databaseWriter)
|
2020-09-28 22:40:03 +00:00
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
2020-08-18 05:13:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private extension ContentDatabase {
|
2020-09-03 01:14:33 +00:00
|
|
|
static func fileURL(identityID: UUID) throws -> URL {
|
2020-09-04 06:12:06 +00:00
|
|
|
try FileManager.default.databaseDirectoryURL(name: identityID.uuidString)
|
2020-09-03 01:14:33 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 06:35:11 +00:00
|
|
|
func clean() throws {
|
|
|
|
try databaseWriter.write {
|
2020-10-01 02:35:06 +00:00
|
|
|
try TimelineRecord.deleteAll($0)
|
2020-09-29 06:35:11 +00:00
|
|
|
try StatusRecord.deleteAll($0)
|
|
|
|
try AccountRecord.deleteAll($0)
|
2020-09-28 22:40:03 +00:00
|
|
|
try AccountList.deleteAll($0)
|
2020-09-29 06:35:11 +00:00
|
|
|
}
|
2020-09-17 03:16:32 +00:00
|
|
|
}
|
2020-08-18 05:13:37 +00:00
|
|
|
}
|