diff --git a/DB/Sources/DB/Content/AccountList.swift b/DB/Sources/DB/Content/AccountList.swift deleted file mode 100644 index 1f92a58..0000000 --- a/DB/Sources/DB/Content/AccountList.swift +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright © 2020 Metabolist. All rights reserved. - -import Foundation -import GRDB - -public struct AccountList: ContentDatabaseRecord { - let id: Id - - public init() { - id = Id() - } -} - -public extension AccountList { - typealias Id = UUID -} - -extension AccountList { - static let joins = hasMany(AccountListJoin.self).order(AccountListJoin.Columns.index) - static let accounts = hasMany( - AccountRecord.self, - through: joins, - using: AccountListJoin.account) - - var accounts: QueryInterfaceRequest { - AccountInfo.request(request(for: Self.accounts)) - } -} diff --git a/DB/Sources/DB/Content/AccountListJoin.swift b/DB/Sources/DB/Content/AccountListJoin.swift deleted file mode 100644 index 1445700..0000000 --- a/DB/Sources/DB/Content/AccountListJoin.swift +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright © 2020 Metabolist. All rights reserved. - -import Foundation -import GRDB -import Mastodon - -struct AccountListJoin: ContentDatabaseRecord { - let accountId: Account.Id - let listId: AccountList.Id - let index: Int - - static let account = belongsTo(AccountRecord.self) -} - -extension AccountListJoin { - enum Columns { - static let accountId = Column(CodingKeys.accountId) - static let listId = Column(CodingKeys.listId) - static let index = Column(CodingKeys.index) - } -} diff --git a/DB/Sources/DB/Content/ContentDatabase+Migration.swift b/DB/Sources/DB/Content/ContentDatabase+Migration.swift index 87f2779..e493f56 100644 --- a/DB/Sources/DB/Content/ContentDatabase+Migration.swift +++ b/DB/Sources/DB/Content/ContentDatabase+Migration.swift @@ -221,20 +221,6 @@ extension ContentDatabase { t.primaryKey(["accountId", "statusId"], onConflict: .replace) } - - try db.create(table: "accountList") { t in - t.column("id", .text).primaryKey(onConflict: .replace) - } - - try db.create(table: "accountListJoin") { t in - t.column("accountId", .text).indexed().notNull() - .references("accountRecord", onDelete: .cascade) - t.column("listId", .text).indexed().notNull() - .references("accountList", onDelete: .cascade) - t.column("index", .integer).notNull() - - t.primaryKey(["accountId", "listId"], onConflict: .replace) - } } return migrator diff --git a/DB/Sources/DB/Content/ContentDatabase.swift b/DB/Sources/DB/Content/ContentDatabase.swift index 67ca5e7..f525739 100644 --- a/DB/Sources/DB/Content/ContentDatabase.swift +++ b/DB/Sources/DB/Content/ContentDatabase.swift @@ -257,27 +257,16 @@ public extension ContentDatabase { .eraseToAnyPublisher() } - func append(accounts: [Account], toList list: AccountList) -> AnyPublisher { + func insert(accounts: [Account]) -> AnyPublisher { databaseWriter.writePublisher { - try list.save($0) - - let count = try list.accounts.fetchCount($0) - - for (index, account) in accounts.enumerated() { + for account in accounts { try account.save($0) - try AccountListJoin(accountId: account.id, listId: list.id, index: count + index).save($0) } } .ignoreOutput() .eraseToAnyPublisher() } - func insert(account: Account) -> AnyPublisher { - databaseWriter.writePublisher(updates: account.save) - .ignoreOutput() - .eraseToAnyPublisher() - } - func insert(identityProofs: [IdentityProof], id: Account.Id) -> AnyPublisher { databaseWriter.writePublisher { for identityProof in identityProofs { @@ -488,14 +477,6 @@ public extension ContentDatabase { .eraseToAnyPublisher() } - func accountListPublisher(_ list: AccountList) -> AnyPublisher<[Account], Error> { - ValueObservation.tracking(list.accounts.fetchAll) - .removeDuplicates() - .map { $0.map(Account.init(info:)) } - .publisher(in: databaseWriter) - .eraseToAnyPublisher() - } - func notificationsPublisher() -> AnyPublisher<[[CollectionItem]], Error> { ValueObservation.tracking( NotificationInfo.request( @@ -642,8 +623,6 @@ private extension ContentDatabase { try StatusRecord.filter(!notificationStatusIds.contains(StatusRecord.Columns.id)).deleteAll($0) try AccountRecord.filter(!notificationAccountIds.contains(AccountRecord.Columns.id)).deleteAll($0) } - - try AccountList.deleteAll($0) } } } diff --git a/ServiceLayer/Sources/ServiceLayer/Services/AccountListService.swift b/ServiceLayer/Sources/ServiceLayer/Services/AccountListService.swift index b595d25..0b3e949 100644 --- a/ServiceLayer/Sources/ServiceLayer/Services/AccountListService.swift +++ b/ServiceLayer/Sources/ServiceLayer/Services/AccountListService.swift @@ -11,7 +11,7 @@ public struct AccountListService { public let nextPageMaxId: AnyPublisher public let navigationService: NavigationService - private let list: AccountList + private let accountList = CurrentValueSubject<[Account], Error>([]) private let endpoint: AccountsEndpoint private let mastodonAPIClient: MastodonAPIClient private let contentDatabase: ContentDatabase @@ -22,14 +22,11 @@ public struct AccountListService { mastodonAPIClient: MastodonAPIClient, contentDatabase: ContentDatabase, titleComponents: [String]? = nil) { - list = AccountList() self.endpoint = endpoint self.mastodonAPIClient = mastodonAPIClient self.contentDatabase = contentDatabase self.titleComponents = titleComponents - sections = contentDatabase.accountListPublisher(list) - .map { [$0.map(CollectionItem.account)] } - .eraseToAnyPublisher() + sections = accountList.map { [$0.map(CollectionItem.account)] }.eraseToAnyPublisher() nextPageMaxId = nextPageMaxIdSubject.eraseToAnyPublisher() navigationService = NavigationService(mastodonAPIClient: mastodonAPIClient, contentDatabase: contentDatabase) } @@ -43,7 +40,16 @@ extension AccountListService: CollectionService { nextPageMaxIdSubject.send(maxId) }) - .flatMap { contentDatabase.append(accounts: $0.result, toList: list) } + .flatMap { response in + contentDatabase.insert(accounts: response.result) + .collect() + .map { _ in + let presentIds = Set(accountList.value.map(\.id)) + + accountList.value += response.result.filter { !presentIds.contains($0.id) } + } + } + .ignoreOutput() .eraseToAnyPublisher() } diff --git a/ServiceLayer/Sources/ServiceLayer/Services/ProfileService.swift b/ServiceLayer/Sources/ServiceLayer/Services/ProfileService.swift index d3a7da1..37d3273 100644 --- a/ServiceLayer/Sources/ServiceLayer/Services/ProfileService.swift +++ b/ServiceLayer/Sources/ServiceLayer/Services/ProfileService.swift @@ -67,7 +67,7 @@ public extension ProfileService { func fetchProfile() -> AnyPublisher { Publishers.Merge3( mastodonAPIClient.request(AccountEndpoint.accounts(id: id)) - .flatMap { contentDatabase.insert(account: $0) }, + .flatMap { contentDatabase.insert(accounts: [$0]) }, mastodonAPIClient.request(RelationshipsEndpoint.relationships(ids: [id])) .flatMap { contentDatabase.insert(relationships: $0) }, mastodonAPIClient.request(IdentityProofsEndpoint.identityProofs(id: id))