no message

This commit is contained in:
Peter Iakovlev 2019-01-25 17:47:19 +04:00
parent acbebf2342
commit f811d2a647
4 changed files with 73 additions and 3 deletions

View File

@ -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<AccountRecordsView, NoError> {
let mutableView = MutableAccountRecordsView(getRecords: {
return self.recordTable.getRecords()
}, currentId: self.metadataTable.getCurrentAccountId())
}, currentId: self.metadataTable.getCurrentAccountId(), currentAuth: self.metadataTable.getCurrentAuthAccount())
let pipe = ValuePipe<AccountRecordsView>()
let index = self.recordsViews.add((mutableView, pipe))

View File

@ -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))
}
}

View File

@ -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
}
}

View File

@ -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)