From f811d2a6475398acafae51f8b562b65d486b8bfd Mon Sep 17 00:00:00 2001 From: Peter Iakovlev Date: Fri, 25 Jan 2019 17:47:19 +0400 Subject: [PATCH] no message --- Postbox/AccountManager.swift | 19 +++++++++- Postbox/AccountManagerMetadataTable.swift | 43 +++++++++++++++++++++++ Postbox/AccountRecordsView.swift | 9 ++++- Postbox/Postbox.swift | 5 ++- 4 files changed, 73 insertions(+), 3 deletions(-) diff --git a/Postbox/AccountManager.swift b/Postbox/AccountManager.swift index 24d3ba0e45..f2bcf3f43f 100644 --- a/Postbox/AccountManager.swift +++ b/Postbox/AccountManager.swift @@ -10,6 +10,9 @@ public struct AccountManagerModifier { public let updateRecord: (AccountRecordId, (AccountRecord?) -> (AccountRecord?)) -> Void public let getCurrent: () -> (AccountRecordId, [AccountRecordAttribute])? public let setCurrentId: (AccountRecordId) -> Void + public let getCurrentAuth: () -> AuthAccountRecord? + public let createAuth: ([AccountRecordAttribute]) -> AuthAccountRecord? + public let removeAuth: () -> Void public let createRecord: ([AccountRecordAttribute]) -> AccountRecordId public let getSharedData: (ValueBoxKey) -> AccountSharedData? public let updateSharedData: (ValueBoxKey, (AccountSharedData?) -> AccountSharedData?) -> Void @@ -46,6 +49,8 @@ final class AccountManagerImpl { self.recordTable = AccountManagerRecordTable(valueBox: self.valueBox, table: AccountManagerRecordTable.tableSpec(1)) self.sharedDataTable = AccountManagerSharedDataTable(valueBox: self.valueBox, table: AccountManagerSharedDataTable.tableSpec(2)) + postboxLog("AccountManager: currentAccountId = \(String(describing: self.metadataTable.getCurrentAccountId()))") + self.tables.append(self.metadataTable) self.tables.append(self.recordTable) self.tables.append(self.sharedDataTable) @@ -77,6 +82,18 @@ final class AccountManagerImpl { } }, setCurrentId: { id in self.metadataTable.setCurrentAccountId(id, operations: &self.currentMetadataOperations) + }, getCurrentAuth: { + if let id = self.metadataTable.getCurrentAuthAccount() { + return id + } else { + return nil + } + }, createAuth: { attributes in + let record = AuthAccountRecord(id: generateAccountRecordId(), attributes: attributes) + self.metadataTable.setCurrentAuthAccount(record, operations: &self.currentMetadataOperations) + return record + }, removeAuth: { + self.metadataTable.setCurrentAuthAccount(nil, operations: &self.currentMetadataOperations) }, createRecord: { attributes in let id = generateAccountRecordId() let record = AccountRecord(id: id, attributes: attributes, temporarySessionId: nil) @@ -145,7 +162,7 @@ final class AccountManagerImpl { private func accountRecordsInternal(transaction: AccountManagerModifier) -> Signal { let mutableView = MutableAccountRecordsView(getRecords: { return self.recordTable.getRecords() - }, currentId: self.metadataTable.getCurrentAccountId()) + }, currentId: self.metadataTable.getCurrentAccountId(), currentAuth: self.metadataTable.getCurrentAuthAccount()) let pipe = ValuePipe() let index = self.recordsViews.add((mutableView, pipe)) diff --git a/Postbox/AccountManagerMetadataTable.swift b/Postbox/AccountManagerMetadataTable.swift index 3e31283f42..9d4aa123f3 100644 --- a/Postbox/AccountManagerMetadataTable.swift +++ b/Postbox/AccountManagerMetadataTable.swift @@ -1,11 +1,33 @@ import Foundation +public struct AuthAccountRecord: PostboxCoding { + public let id: AccountRecordId + public let attributes: [AccountRecordAttribute] + + init(id: AccountRecordId, attributes: [AccountRecordAttribute]) { + self.id = id + self.attributes = attributes + } + + public init(decoder: PostboxDecoder) { + self.id = AccountRecordId(rawValue: decoder.decodeOptionalInt64ForKey("id")!) + self.attributes = decoder.decodeObjectArrayForKey("attributes").compactMap({ $0 as? AccountRecordAttribute }) + } + + public func encode(_ encoder: PostboxEncoder) { + encoder.encodeInt64(self.id.rawValue, forKey: "id") + encoder.encodeGenericObjectArray(self.attributes.map { $0 as PostboxCoding }, forKey: "attributes") + } +} + enum AccountManagerMetadataOperation { case updateCurrentAccountId(AccountRecordId) + case updateCurrentAuthAccountRecord(AuthAccountRecord?) } private enum MetadataKey: Int64 { case currentAccountId = 0 + case currentAuthAccount = 1 } final class AccountManagerMetadataTable: Table { @@ -34,4 +56,25 @@ final class AccountManagerMetadataTable: Table { self.valueBox.set(self.table, key: self.key(.currentAccountId), value: MemoryBuffer(memory: &rawValue, capacity: 8, length: 8, freeWhenDone: false)) operations.append(.updateCurrentAccountId(id)) } + + func getCurrentAuthAccount() -> AuthAccountRecord? { + if let value = self.valueBox.get(self.table, key: self.key(.currentAuthAccount)), let object = PostboxDecoder(buffer: value).decodeRootObject() as? AuthAccountRecord { + return object + } else { + return nil + } + } + + func setCurrentAuthAccount(_ record: AuthAccountRecord?, operations: inout [AccountManagerMetadataOperation]) { + if let record = record { + let encoder = PostboxEncoder() + encoder.encodeRootObject(record) + withExtendedLifetime(encoder, { + self.valueBox.set(self.table, key: self.key(.currentAuthAccount), value: encoder.readBufferNoCopy()) + }) + } else { + self.valueBox.remove(self.table, key: self.key(.currentAuthAccount)) + } + operations.append(.updateCurrentAuthAccountRecord(record)) + } } diff --git a/Postbox/AccountRecordsView.swift b/Postbox/AccountRecordsView.swift index 44dcb79b05..3a86acaf50 100644 --- a/Postbox/AccountRecordsView.swift +++ b/Postbox/AccountRecordsView.swift @@ -3,10 +3,12 @@ import Foundation final class MutableAccountRecordsView { fileprivate var records: [AccountRecord] fileprivate var currentId: AccountRecordId? + fileprivate var currentAuth: AuthAccountRecord? - init(getRecords: () -> [AccountRecord], currentId: AccountRecordId?) { + init(getRecords: () -> [AccountRecord], currentId: AccountRecordId?, currentAuth: AuthAccountRecord?) { self.records = getRecords() self.currentId = currentId + self.currentAuth = currentAuth } func replay(operations: [AccountManagerRecordOperation], metadataOperations: [AccountManagerMetadataOperation]) -> Bool { @@ -49,6 +51,9 @@ final class MutableAccountRecordsView { case let .updateCurrentAccountId(id): updated = true self.currentId = id + case let .updateCurrentAuthAccountRecord(record): + updated = true + self.currentAuth = record } } @@ -59,6 +64,7 @@ final class MutableAccountRecordsView { public final class AccountRecordsView { public let records: [AccountRecord] public let currentRecord: AccountRecord? + public let currentAuthAccount: AuthAccountRecord? init(_ view: MutableAccountRecordsView) { self.records = view.records @@ -74,5 +80,6 @@ public final class AccountRecordsView { } else { self.currentRecord = nil } + self.currentAuthAccount = view.currentAuth } } diff --git a/Postbox/Postbox.swift b/Postbox/Postbox.swift index 07058ec309..15b3703228 100644 --- a/Postbox/Postbox.swift +++ b/Postbox/Postbox.swift @@ -2889,7 +2889,10 @@ public final class Postbox { var additionalChatPeerIds: [PeerId] = [] for peerId in chatPeerIds { for associatedId in self.reverseAssociatedPeerTable.get(peerId: peerId) { - additionalChatPeerIds.append(associatedId) + let inclusionIndex = self.chatListIndexTable.get(peerId: associatedId) + if inclusionIndex.includedIndex(peerId: associatedId) != nil { + additionalChatPeerIds.append(associatedId) + } } } chatPeerIds.append(contentsOf: additionalChatPeerIds)