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 updateRecord: (AccountRecordId, (AccountRecord?) -> (AccountRecord?)) -> Void
public let getCurrent: () -> (AccountRecordId, [AccountRecordAttribute])? public let getCurrent: () -> (AccountRecordId, [AccountRecordAttribute])?
public let setCurrentId: (AccountRecordId) -> Void 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 createRecord: ([AccountRecordAttribute]) -> AccountRecordId
public let getSharedData: (ValueBoxKey) -> AccountSharedData? public let getSharedData: (ValueBoxKey) -> AccountSharedData?
public let updateSharedData: (ValueBoxKey, (AccountSharedData?) -> AccountSharedData?) -> Void 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.recordTable = AccountManagerRecordTable(valueBox: self.valueBox, table: AccountManagerRecordTable.tableSpec(1))
self.sharedDataTable = AccountManagerSharedDataTable(valueBox: self.valueBox, table: AccountManagerSharedDataTable.tableSpec(2)) 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.metadataTable)
self.tables.append(self.recordTable) self.tables.append(self.recordTable)
self.tables.append(self.sharedDataTable) self.tables.append(self.sharedDataTable)
@ -77,6 +82,18 @@ final class AccountManagerImpl {
} }
}, setCurrentId: { id in }, setCurrentId: { id in
self.metadataTable.setCurrentAccountId(id, operations: &self.currentMetadataOperations) 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 }, createRecord: { attributes in
let id = generateAccountRecordId() let id = generateAccountRecordId()
let record = AccountRecord(id: id, attributes: attributes, temporarySessionId: nil) let record = AccountRecord(id: id, attributes: attributes, temporarySessionId: nil)
@ -145,7 +162,7 @@ final class AccountManagerImpl {
private func accountRecordsInternal(transaction: AccountManagerModifier) -> Signal<AccountRecordsView, NoError> { private func accountRecordsInternal(transaction: AccountManagerModifier) -> Signal<AccountRecordsView, NoError> {
let mutableView = MutableAccountRecordsView(getRecords: { let mutableView = MutableAccountRecordsView(getRecords: {
return self.recordTable.getRecords() return self.recordTable.getRecords()
}, currentId: self.metadataTable.getCurrentAccountId()) }, currentId: self.metadataTable.getCurrentAccountId(), currentAuth: self.metadataTable.getCurrentAuthAccount())
let pipe = ValuePipe<AccountRecordsView>() let pipe = ValuePipe<AccountRecordsView>()
let index = self.recordsViews.add((mutableView, pipe)) let index = self.recordsViews.add((mutableView, pipe))

View File

@ -1,11 +1,33 @@
import Foundation 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 { enum AccountManagerMetadataOperation {
case updateCurrentAccountId(AccountRecordId) case updateCurrentAccountId(AccountRecordId)
case updateCurrentAuthAccountRecord(AuthAccountRecord?)
} }
private enum MetadataKey: Int64 { private enum MetadataKey: Int64 {
case currentAccountId = 0 case currentAccountId = 0
case currentAuthAccount = 1
} }
final class AccountManagerMetadataTable: Table { 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)) self.valueBox.set(self.table, key: self.key(.currentAccountId), value: MemoryBuffer(memory: &rawValue, capacity: 8, length: 8, freeWhenDone: false))
operations.append(.updateCurrentAccountId(id)) 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 { final class MutableAccountRecordsView {
fileprivate var records: [AccountRecord] fileprivate var records: [AccountRecord]
fileprivate var currentId: AccountRecordId? fileprivate var currentId: AccountRecordId?
fileprivate var currentAuth: AuthAccountRecord?
init(getRecords: () -> [AccountRecord], currentId: AccountRecordId?) { init(getRecords: () -> [AccountRecord], currentId: AccountRecordId?, currentAuth: AuthAccountRecord?) {
self.records = getRecords() self.records = getRecords()
self.currentId = currentId self.currentId = currentId
self.currentAuth = currentAuth
} }
func replay(operations: [AccountManagerRecordOperation], metadataOperations: [AccountManagerMetadataOperation]) -> Bool { func replay(operations: [AccountManagerRecordOperation], metadataOperations: [AccountManagerMetadataOperation]) -> Bool {
@ -49,6 +51,9 @@ final class MutableAccountRecordsView {
case let .updateCurrentAccountId(id): case let .updateCurrentAccountId(id):
updated = true updated = true
self.currentId = id self.currentId = id
case let .updateCurrentAuthAccountRecord(record):
updated = true
self.currentAuth = record
} }
} }
@ -59,6 +64,7 @@ final class MutableAccountRecordsView {
public final class AccountRecordsView { public final class AccountRecordsView {
public let records: [AccountRecord] public let records: [AccountRecord]
public let currentRecord: AccountRecord? public let currentRecord: AccountRecord?
public let currentAuthAccount: AuthAccountRecord?
init(_ view: MutableAccountRecordsView) { init(_ view: MutableAccountRecordsView) {
self.records = view.records self.records = view.records
@ -74,5 +80,6 @@ public final class AccountRecordsView {
} else { } else {
self.currentRecord = nil self.currentRecord = nil
} }
self.currentAuthAccount = view.currentAuth
} }
} }

View File

@ -2889,9 +2889,12 @@ public final class Postbox {
var additionalChatPeerIds: [PeerId] = [] var additionalChatPeerIds: [PeerId] = []
for peerId in chatPeerIds { for peerId in chatPeerIds {
for associatedId in self.reverseAssociatedPeerTable.get(peerId: peerId) { for associatedId in self.reverseAssociatedPeerTable.get(peerId: peerId) {
let inclusionIndex = self.chatListIndexTable.get(peerId: associatedId)
if inclusionIndex.includedIndex(peerId: associatedId) != nil {
additionalChatPeerIds.append(associatedId) additionalChatPeerIds.append(associatedId)
} }
} }
}
chatPeerIds.append(contentsOf: additionalChatPeerIds) chatPeerIds.append(contentsOf: additionalChatPeerIds)
if let groupId = groupId { if let groupId = groupId {