Merge branch 'voicechats'

This commit is contained in:
Ilya Laktyushin
2021-03-04 22:07:31 +04:00
94 changed files with 6884 additions and 5396 deletions

View File

@@ -1697,19 +1697,16 @@ public func editGroupCallTitle(account: Account, callId: Int64, accessHash: Int6
}
public func groupCallDisplayAsAvailablePeers(network: Network, postbox: Postbox) -> Signal<[FoundPeer], NoError> {
return network.request(Api.functions.channels.getAdminedPublicChannels(flags: 1 << 2))
|> retryRequest
|> mapToSignal { result in
let chats:[Api.Chat]
|> retryRequest
|> mapToSignal { result in
let chats: [Api.Chat]
switch result {
case let .chatsSlice(_, c):
chats = c
case let .chats(c):
chats = c
case let .chatsSlice(_, c):
chats = c
case let .chats(c):
chats = c
}
var subscribers: [PeerId: Int32] = [:]
let peers = chats.compactMap(parseTelegramGroupOrChannel)
@@ -1720,16 +1717,13 @@ public func groupCallDisplayAsAvailablePeers(network: Network, postbox: Postbox)
if let participantsCount = channel.participantsCount {
subscribers[groupOrChannel.id] = participantsCount
}
case let .chat(chat):
subscribers[groupOrChannel.id] = chat.participantsCount
default:
break
case let .chat(chat):
subscribers[groupOrChannel.id] = chat.participantsCount
default:
break
}
}
}
return postbox.transaction { transaction -> [Peer] in
updatePeers(transaction: transaction, peers: peers, update: { _, updated in
return updated
@@ -1738,7 +1732,59 @@ public func groupCallDisplayAsAvailablePeers(network: Network, postbox: Postbox)
} |> map { peers -> [FoundPeer] in
return peers.map { FoundPeer(peer: $0, subscribers: subscribers[$0.id]) }
}
}
}
public final class CachedDisplayAsPeers: PostboxCoding {
public let peerIds: [PeerId]
public let timestamp: Int32
public init(peerIds: [PeerId], timestamp: Int32) {
self.peerIds = peerIds
self.timestamp = timestamp
}
public init(decoder: PostboxDecoder) {
self.peerIds = decoder.decodeInt64ArrayForKey("peerIds").map { PeerId($0) }
self.timestamp = decoder.decodeInt32ForKey("timestamp", orElse: 0)
}
public func encode(_ encoder: PostboxEncoder) {
encoder.encodeInt64Array(self.peerIds.map { $0.toInt64() }, forKey: "peerIds")
encoder.encodeInt32(self.timestamp, forKey: "timestamp")
}
}
public func cachedGroupCallDisplayAsAvailablePeers(account: Account) -> Signal<[FoundPeer], NoError> {
let key = ValueBoxKey(length: 0)
return account.postbox.transaction { transaction -> ([FoundPeer], Int32)? in
let cached = transaction.retrieveItemCacheEntry(id: ItemCacheEntryId(collectionId: Namespaces.CachedItemCollection.cachedGroupCallDisplayAsPeers, key: key)) as? CachedDisplayAsPeers
if let cached = cached {
var peers: [FoundPeer] = []
for peerId in cached.peerIds {
if let peer = transaction.getPeer(peerId) {
peers.append(FoundPeer(peer: peer, subscribers: nil))
}
}
return (peers, cached.timestamp)
} else {
return nil
}
}
|> mapToSignal { cachedPeersAndTimestamp -> Signal<[FoundPeer], NoError> in
let currentTimestamp = Int32(CFAbsoluteTimeGetCurrent() + kCFAbsoluteTimeIntervalSince1970)
if let (cachedPeers, timestamp) = cachedPeersAndTimestamp, currentTimestamp - timestamp < 60 * 5 {
return .single(cachedPeers)
} else {
return groupCallDisplayAsAvailablePeers(network: account.network, postbox: account.postbox)
|> mapToSignal { peers -> Signal<[FoundPeer], NoError> in
return account.postbox.transaction { transaction -> [FoundPeer] in
let currentTimestamp = Int32(CFAbsoluteTimeGetCurrent() + kCFAbsoluteTimeIntervalSince1970)
transaction.putItemCacheEntry(id: ItemCacheEntryId(collectionId: Namespaces.CachedItemCollection.cachedGroupCallDisplayAsPeers, key: key), entry: CachedDisplayAsPeers(peerIds: peers.map { $0.peer.id }, timestamp: currentTimestamp), collectionSpec: ItemCacheCollectionSpec(lowWaterItemCount: 1, highWaterItemCount: 1))
return peers
}
}
}
}
}