mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-07-25 12:40:36 +00:00

git-subtree-dir: submodules/TelegramCore git-subtree-mainline: 971273e8f8f49a47f14b251d2f35e3445a61fc3f git-subtree-split: 9561227540acef69894e6546395ab223a6233600
53 lines
2.2 KiB
Swift
53 lines
2.2 KiB
Swift
#if os(macOS)
|
|
import PostboxMac
|
|
import SwiftSignalKitMac
|
|
#else
|
|
import Postbox
|
|
import SwiftSignalKit
|
|
#endif
|
|
|
|
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)
|
|
}
|
|
|> introduceError(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
|
|
}
|
|
})
|
|
} |> introduceError(ChannelHistoryAvailabilityError.self)
|
|
}
|
|
|
|
}
|
|
}
|