Recent apps

This commit is contained in:
Isaac
2024-07-16 13:33:46 +08:00
parent 61578961ce
commit d15818649b
20 changed files with 1140 additions and 601 deletions

View File

@@ -12,6 +12,10 @@ func cachedRecentPeersEntryId() -> ItemCacheEntryId {
return ItemCacheEntryId(collectionId: 101, key: CachedRecentPeers.cacheKey())
}
func cachedRecentAppsEntryId() -> ItemCacheEntryId {
return ItemCacheEntryId(collectionId: 102, key: CachedRecentPeers.cacheKey())
}
public func _internal_recentPeers(accountPeerId: EnginePeer.Id, postbox: Postbox) -> Signal<RecentPeers, NoError> {
let key = PostboxViewKey.cachedItem(cachedRecentPeersEntryId())
return postbox.combinedView(keys: [key])
@@ -248,3 +252,57 @@ func _internal_removeRecentlyUsedInlineBot(account: Account, peerId: PeerId) ->
}
} |> switchToLatest
}
public func _internal_recentApps(accountPeerId: PeerId, postbox: Postbox) -> Signal<[EnginePeer.Id], NoError> {
let key = PostboxViewKey.cachedItem(cachedRecentAppsEntryId())
return postbox.combinedView(keys: [key])
|> mapToSignal { views -> Signal<[EnginePeer.Id], NoError> in
if let value = (views.views[key] as? CachedItemView)?.value?.get(CachedRecentPeers.self) {
return .single(value.ids)
} else {
return .single([])
}
}
}
public func _internal_managedUpdatedRecentApps(accountPeerId: PeerId, postbox: Postbox, network: Network) -> Signal<Void, NoError> {
let key = PostboxViewKey.cachedItem(cachedRecentAppsEntryId())
let peersEnabled = postbox.combinedView(keys: [key])
|> map { views -> Bool in
if let value = (views.views[key] as? CachedItemView)?.value?.get(CachedRecentPeers.self) {
return value.enabled
} else {
return true
}
}
|> distinctUntilChanged
let updateOnce =
network.request(Api.functions.contacts.getTopPeers(flags: 1 << 16, offset: 0, limit: 50, hash: 0))
|> `catch` { _ -> Signal<Api.contacts.TopPeers, NoError> in
return .complete()
}
|> mapToSignal { result -> Signal<Void, NoError> in
return postbox.transaction { transaction -> Void in
switch result {
case let .topPeers(_, _, users):
let parsedPeers = AccumulatedPeers(users: users)
updatePeers(transaction: transaction, accountPeerId: accountPeerId, peers: parsedPeers)
if let entry = CodableEntry(CachedRecentPeers(enabled: true, ids: users.map { $0.peerId })) {
transaction.putItemCacheEntry(id: cachedRecentAppsEntryId(), entry: entry)
}
case .topPeersNotModified:
break
case .topPeersDisabled:
if let entry = CodableEntry(CachedRecentPeers(enabled: false, ids: [])) {
transaction.putItemCacheEntry(id: cachedRecentAppsEntryId(), entry: entry)
}
}
}
}
return peersEnabled |> mapToSignal { _ -> Signal<Void, NoError> in
return updateOnce
}
}