mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-08-17 19:09:56 +00:00
55 lines
2.8 KiB
Swift
55 lines
2.8 KiB
Swift
import Foundation
|
|
#if os(macOS)
|
|
import PostboxMac
|
|
import SwiftSignalKitMac
|
|
#else
|
|
import Postbox
|
|
import SwiftSignalKit
|
|
#endif
|
|
|
|
func fetchAndUpdateCachedParticipants(peerId: PeerId, network: Network, postbox: Postbox) -> Signal<Void, NoError> {
|
|
return postbox.loadedPeerWithId(peerId)
|
|
|> mapToSignal { peer -> Signal<Void, NoError> in
|
|
if let inputChannel = apiInputChannel(peer) {
|
|
return network.request(Api.functions.channels.getParticipants(channel: inputChannel, filter: .channelParticipantsRecent, offset: 0, limit: 200, hash: 0))
|
|
|> retryRequest
|
|
|> mapToSignal { result -> Signal<Void, NoError> in
|
|
return postbox.transaction { transaction -> Void in
|
|
switch result {
|
|
case let .channelParticipants(count, participants, users):
|
|
var peers: [Peer] = []
|
|
var peerPresences: [PeerId: PeerPresence] = [:]
|
|
for user in users {
|
|
let telegramUser = TelegramUser(user: user)
|
|
peers.append(telegramUser)
|
|
if let presence = TelegramUserPresence(apiUser: user) {
|
|
peerPresences[telegramUser.id] = presence
|
|
}
|
|
}
|
|
|
|
updatePeers(transaction: transaction, peers: peers, update: { _, updated -> Peer in
|
|
return updated
|
|
})
|
|
|
|
transaction.updatePeerPresences(peerPresences)
|
|
|
|
let parsedParticipants = CachedChannelParticipants(apiParticipants: participants)
|
|
|
|
transaction.updatePeerCachedData(peerIds: [peerId], update: { peerId, currentData in
|
|
if let currentData = currentData as? CachedChannelData {
|
|
return currentData.withUpdatedTopParticipants(parsedParticipants)
|
|
} else {
|
|
return currentData
|
|
}
|
|
})
|
|
case .channelParticipantsNotModified:
|
|
break
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
return .complete()
|
|
}
|
|
}
|
|
}
|