mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-06-16 05:55:20 +00:00
51 lines
2.2 KiB
Swift
51 lines
2.2 KiB
Swift
import Postbox
|
|
import TelegramApi
|
|
import SwiftSignalKit
|
|
|
|
import SyncCore
|
|
|
|
public enum ChannelHistoryAvailabilityError {
|
|
case generic
|
|
case hasNotPermissions
|
|
}
|
|
|
|
public func updateChannelHistoryAvailabilitySettingsInteractively(postbox: Postbox, network: Network, accountStateManager: AccountStateManager, peerId: PeerId, historyAvailableForNewMembers: Bool) -> Signal<Void, ChannelHistoryAvailabilityError> {
|
|
return postbox.transaction { transaction -> Peer? in
|
|
return transaction.getPeer(peerId)
|
|
}
|
|
|> castError(ChannelHistoryAvailabilityError.self)
|
|
|> mapToSignal { peer in
|
|
|
|
guard let peer = peer, let inputChannel = apiInputChannel(peer) else {
|
|
return .fail(.generic)
|
|
}
|
|
|
|
return network.request(Api.functions.channels.togglePreHistoryHidden(channel: inputChannel, enabled: historyAvailableForNewMembers ? .boolFalse : .boolTrue))
|
|
|> `catch` { error -> Signal<Api.Updates, ChannelHistoryAvailabilityError> in
|
|
if error.errorDescription == "CHAT_ADMIN_REQUIRED" {
|
|
return .fail(.hasNotPermissions)
|
|
}
|
|
return .fail(.generic)
|
|
}
|
|
|> mapToSignal { updates -> Signal<Void, ChannelHistoryAvailabilityError> in
|
|
accountStateManager.addUpdates(updates)
|
|
return postbox.transaction { transaction -> Void in
|
|
transaction.updatePeerCachedData(peerIds: [peerId], update: { peerId, currentData in
|
|
if let currentData = currentData as? CachedChannelData {
|
|
var flags = currentData.flags
|
|
if historyAvailableForNewMembers {
|
|
flags.insert(.preHistoryEnabled)
|
|
} else {
|
|
flags.remove(.preHistoryEnabled)
|
|
}
|
|
return currentData.withUpdatedFlags(flags)
|
|
} else {
|
|
return currentData
|
|
}
|
|
})
|
|
} |> castError(ChannelHistoryAvailabilityError.self)
|
|
}
|
|
|
|
}
|
|
}
|