some additional errors

This commit is contained in:
overtake
2019-05-30 14:12:57 +02:00
parent 1b22eefde1
commit bf68f97340
2 changed files with 34 additions and 35 deletions

View File

@@ -6,18 +6,31 @@
import SwiftSignalKit
#endif
public func updateChannelHistoryAvailabilitySettingsInteractively(postbox: Postbox, network: Network, accountStateManager: AccountStateManager, peerId: PeerId, historyAvailableForNewMembers: Bool) -> Signal<Void, NoError> {
return postbox.transaction { transaction -> Signal<Void, NoError> in
if let peer = transaction.getPeer(peerId), let inputChannel = apiInputChannel(peer) {
return network.request(Api.functions.channels.togglePreHistoryHidden(channel: inputChannel, enabled: historyAvailableForNewMembers ? .boolFalse : .boolTrue))
|> map(Optional.init)
|> `catch` { _ -> Signal<Api.Updates?, NoError> in
return .single(nil)
}
|> mapToSignal { updates -> Signal<Void, NoError> in
if let updates = updates {
accountStateManager.addUpdates(updates)
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 {
@@ -32,11 +45,8 @@ public func updateChannelHistoryAvailabilitySettingsInteractively(postbox: Postb
return currentData
}
})
}
}
} else {
return .complete()
} |> introduceError(ChannelHistoryAvailabilityError.self)
}
}
|> switchToLatest
}

View File

@@ -35,28 +35,11 @@ public func availableGroupsForChannelDiscussion(postbox: Postbox, network: Netwo
}
}
public func availableChannelsForGroupDiscussion(network: Network) -> Signal<[Peer], AvailableChannelDiscussionGroupError> {
return network.request(Api.functions.channels.getBroadcastsForDiscussion()) |> mapError { _ in
return .generic
} |> map { result in
let chats:[Api.Chat]
switch result {
case let .chats(c):
chats = c
case let .chatsSlice(_, c):
chats = c
}
let peers = chats.compactMap {
return parseTelegramGroupOrChannel(chat: $0)
}
return peers
}
}
public enum ChannelDiscussionGroupError {
case generic
case preHistoryHidden
case hasNotPermissions
}
public func updateGroupDiscussionForChannel(network: Network, postbox: Postbox, channelId: PeerId, groupId: PeerId?) -> Signal<Bool, ChannelDiscussionGroupError> {
@@ -89,6 +72,12 @@ public func updateGroupDiscussionForChannel(network: Network, postbox: Postbox,
if error.errorDescription == "LINK_NOT_MODIFIED" {
return .single(true)
}
if error.errorDescription == "MEGAGROUP_PREHISTORY_HIDDEN" {
return .fail(.preHistoryHidden)
}
if error.errorDescription == "CHAT_ADMIN_REQUIRED" {
return .fail(.hasNotPermissions)
}
return .fail(.generic)
}
}