Skip to content

Commit 0664197

Browse files
committed
Poll improvements
1 parent 9c3cab2 commit 0664197

11 files changed

Lines changed: 112 additions & 52 deletions

File tree

submodules/BrowserUI/Sources/BrowserBookmarksScreen.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,10 @@ public final class BrowserBookmarksScreen: ViewController {
189189
}, requestToggleTodoMessageItem: { _, _, _ in
190190
}, displayTodoToggleUnavailable: { _ in
191191
}, openStarsPurchase: { _ in
192-
}, openRankInfo: { _, _, _ in }, openSetPeerAvatar: {}, automaticMediaDownloadSettings: MediaAutoDownloadSettings.defaultSettings, pollActionState: ChatInterfacePollActionState(), stickerSettings: ChatInterfaceStickerSettings(), presentationContext: ChatPresentationContext(context: context, backgroundNode: nil))
192+
}, openRankInfo: { _, _, _ in
193+
}, openSetPeerAvatar: {
194+
}, displayPollRestrictedToast: { _ in
195+
}, automaticMediaDownloadSettings: MediaAutoDownloadSettings.defaultSettings, pollActionState: ChatInterfacePollActionState(), stickerSettings: ChatInterfaceStickerSettings(), presentationContext: ChatPresentationContext(context: context, backgroundNode: nil))
193196

194197

195198
let tagMask: MessageTags = .webPage

submodules/TelegramCore/Sources/TelegramEngine/Messages/Polls.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func pollCloudMediaToInputMedia(_ media: Media) -> Api.InputMedia? {
3131

3232
public enum RequestMessageSelectPollOptionError {
3333
case generic
34+
case restrictedToSubscribers
3435
}
3536

3637
func _internal_requestMessageSelectPollOption(account: Account, messageId: MessageId, opaqueIdentifiers: [Data]) -> Signal<TelegramMediaPoll?, RequestMessageSelectPollOptionError> {
@@ -40,7 +41,10 @@ func _internal_requestMessageSelectPollOption(account: Account, messageId: Messa
4041
|> mapToSignal { peer in
4142
if let inputPeer = apiInputPeer(peer) {
4243
return account.network.request(Api.functions.messages.sendVote(peer: inputPeer, msgId: messageId.id, options: opaqueIdentifiers.map { Buffer(data: $0) }))
43-
|> mapError { _ -> RequestMessageSelectPollOptionError in
44+
|> mapError { error -> RequestMessageSelectPollOptionError in
45+
if error.errorDescription == "POLL_MEMBER_RESTRICTED" {
46+
return .restrictedToSubscribers
47+
}
4448
return .generic
4549
}
4650
|> mapToSignal { result -> Signal<TelegramMediaPoll?, RequestMessageSelectPollOptionError> in

submodules/TelegramUI/Components/Chat/ChatMessagePollBubbleContentNode/Sources/ChatMessagePollBubbleContentNode.swift

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2918,49 +2918,7 @@ public class ChatMessagePollBubbleContentNode: ChatMessageBubbleContentNode {
29182918
if case .public = poll.publicity {
29192919
item.controllerInteraction.openMessagePollResults(item.message.id, option.opaqueIdentifier)
29202920
} else if isRestricted {
2921-
let text: String
2922-
2923-
let peerName = item.message.peers[item.message.id.peerId].flatMap(EnginePeer.init)?.compactDisplayTitle ?? ""
2924-
if !poll.countries.isEmpty {
2925-
let locale = localeWithStrings(item.presentationData.strings)
2926-
let countryNames = poll.countries.map { id in
2927-
if id == "FT" {
2928-
return "Fragment"
2929-
} else if let countryName = locale.localizedString(forRegionCode: id) {
2930-
return countryName
2931-
} else {
2932-
return id
2933-
}
2934-
}
2935-
var countries: String = ""
2936-
if countryNames.count == 1, let country = countryNames.first {
2937-
countries = "**\(country)**"
2938-
} else {
2939-
for i in 0 ..< countryNames.count {
2940-
countries.append("**\(countryNames[i])**")
2941-
if i == countryNames.count - 2 {
2942-
countries.append(item.presentationData.strings.Chat_Poll_Restriction_Country_CountriesLastDelimiter)
2943-
} else if i < countryNames.count - 2 {
2944-
countries.append(item.presentationData.strings.Chat_Poll_Restriction_Country_CountriesDelimiter)
2945-
}
2946-
}
2947-
}
2948-
if poll.restrictToSubscribers {
2949-
text = item.presentationData.strings.Chat_Poll_Restriction_SubscribersCountry(peerName, countries).string
2950-
} else {
2951-
text = item.presentationData.strings.Chat_Poll_Restriction_Country(countries).string
2952-
}
2953-
} else {
2954-
text = item.presentationData.strings.Chat_Poll_Restriction_Subscribers(peerName).string
2955-
}
2956-
let controller = UndoOverlayController(
2957-
presentationData: item.context.sharedContext.currentPresentationData.with { $0 },
2958-
content: .banned(text: text),
2959-
elevatedLayout: true,
2960-
position: .bottom,
2961-
action: { _ in return true }
2962-
)
2963-
item.controllerInteraction.presentController(controller, nil)
2921+
item.controllerInteraction.displayPollRestrictedToast(item.message.id)
29642922
}
29652923
}
29662924
}

submodules/TelegramUI/Components/Chat/ChatRecentActionsController/Sources/ChatRecentActionsControllerNode.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,10 @@ final class ChatRecentActionsControllerNode: ViewControllerTracingNode {
666666
}, requestToggleTodoMessageItem: { _, _, _ in
667667
}, displayTodoToggleUnavailable: { _ in
668668
}, openStarsPurchase: { _ in
669-
}, openRankInfo: { _, _, _ in }, openSetPeerAvatar: {}, automaticMediaDownloadSettings: self.automaticMediaDownloadSettings,
669+
}, openRankInfo: { _, _, _ in
670+
}, openSetPeerAvatar: {
671+
}, displayPollRestrictedToast: { _ in
672+
}, automaticMediaDownloadSettings: self.automaticMediaDownloadSettings,
670673
pollActionState: ChatInterfacePollActionState(), stickerSettings: ChatInterfaceStickerSettings(), presentationContext: ChatPresentationContext(context: context, backgroundNode: self.backgroundNode))
671674
self.controllerInteraction = controllerInteraction
672675

submodules/TelegramUI/Components/Chat/ChatSendAudioMessageContextPreview/Sources/ChatSendAudioMessageContextPreview.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,10 @@ public final class ChatSendGroupMediaMessageContextPreview: UIView, ChatSendMess
513513
}, requestToggleTodoMessageItem: { _, _, _ in
514514
}, displayTodoToggleUnavailable: { _ in
515515
}, openStarsPurchase: { _ in
516-
}, openRankInfo: { _, _, _ in }, openSetPeerAvatar: {}, automaticMediaDownloadSettings: MediaAutoDownloadSettings.defaultSettings,
516+
}, openRankInfo: { _, _, _ in
517+
}, openSetPeerAvatar: {
518+
}, displayPollRestrictedToast: { _ in
519+
}, automaticMediaDownloadSettings: MediaAutoDownloadSettings.defaultSettings,
517520
pollActionState: ChatInterfacePollActionState(), stickerSettings: ChatInterfaceStickerSettings(), presentationContext: ChatPresentationContext(context: self.context, backgroundNode: self.wallpaperBackgroundNode))
518521

519522
let associatedData = ChatMessageItemAssociatedData(

submodules/TelegramUI/Components/ChatControllerInteraction/Sources/ChatControllerInteraction.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ public final class ChatControllerInteraction: ChatControllerInteractionProtocol
303303
public let openStarsPurchase: (Int64?) -> Void
304304
public let openRankInfo: (EnginePeer, ChatRankInfoScreenRole, String) -> Void
305305
public let openSetPeerAvatar: () -> Void
306+
public let displayPollRestrictedToast: (EngineMessage.Id) -> Void
306307

307308
public var canPlayMedia: Bool = false
308309
public var hiddenMedia: [MessageId: [Media]] = [:]
@@ -480,6 +481,7 @@ public final class ChatControllerInteraction: ChatControllerInteractionProtocol
480481
openStarsPurchase: @escaping (Int64?) -> Void,
481482
openRankInfo: @escaping (EnginePeer, ChatRankInfoScreenRole, String) -> Void,
482483
openSetPeerAvatar: @escaping () -> Void,
484+
displayPollRestrictedToast: @escaping (EngineMessage.Id) -> Void,
483485
automaticMediaDownloadSettings: MediaAutoDownloadSettings,
484486
pollActionState: ChatInterfacePollActionState,
485487
stickerSettings: ChatInterfaceStickerSettings,
@@ -610,6 +612,7 @@ public final class ChatControllerInteraction: ChatControllerInteractionProtocol
610612
self.openStarsPurchase = openStarsPurchase
611613
self.openRankInfo = openRankInfo
612614
self.openSetPeerAvatar = openSetPeerAvatar
615+
self.displayPollRestrictedToast = displayPollRestrictedToast
613616

614617
self.automaticMediaDownloadSettings = automaticMediaDownloadSettings
615618

submodules/TelegramUI/Components/PeerInfo/PeerInfoScreen/Sources/PeerInfoScreen.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1284,7 +1284,10 @@ final class PeerInfoScreenNode: ViewControllerTracingNode, PeerInfoScreenNodePro
12841284
}, requestToggleTodoMessageItem: { _, _, _ in
12851285
}, displayTodoToggleUnavailable: { _ in
12861286
}, openStarsPurchase: { _ in
1287-
}, openRankInfo: { _, _, _ in }, openSetPeerAvatar: {}, automaticMediaDownloadSettings: MediaAutoDownloadSettings.defaultSettings,
1287+
}, openRankInfo: { _, _, _ in
1288+
}, openSetPeerAvatar: {
1289+
}, displayPollRestrictedToast: { _ in
1290+
}, automaticMediaDownloadSettings: MediaAutoDownloadSettings.defaultSettings,
12881291
pollActionState: ChatInterfacePollActionState(), stickerSettings: ChatInterfaceStickerSettings(), presentationContext: ChatPresentationContext(context: context, backgroundNode: nil))
12891292
self.hiddenMediaDisposable = context.sharedContext.mediaManager.galleryHiddenMediaManager.hiddenIds().startStrict(next: { [weak self] ids in
12901293
guard let strongSelf = self else {

submodules/TelegramUI/Sources/Chat/ChatControllerToasts.swift

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Display
88
import UndoUI
99
import AccountContext
1010
import ChatControllerInteraction
11+
import TelegramStringFormatting
1112

1213
extension ChatControllerImpl {
1314
func displaySendReactionRestrictedToast() {
@@ -20,6 +21,71 @@ extension ChatControllerImpl {
2021
action: { _ in return true }
2122
), nil)
2223
}
24+
25+
func displayPollRestrictedToast(messageId: EngineMessage.Id) {
26+
let _ = (self.context.engine.data.get(
27+
TelegramEngine.EngineData.Item.Messages.Message(id: messageId),
28+
TelegramEngine.EngineData.Item.Peer.Peer(id: messageId.peerId)
29+
)
30+
|> deliverOnMainQueue).startStandalone(next: { [weak self] message, peer in
31+
guard let self, let message, let peer else {
32+
return
33+
}
34+
let peerName = peer.compactDisplayTitle
35+
guard let poll = message.media.first(where: { $0 is TelegramMediaPoll }) as? TelegramMediaPoll else {
36+
return
37+
}
38+
39+
var accountCountry = ""
40+
if let data = self.context.currentAppConfiguration.with({ $0 }).data, let country = data["phone_country_iso2"] as? String {
41+
accountCountry = (country)
42+
}
43+
var text = ""
44+
if !poll.countries.isEmpty && !poll.countries.contains(accountCountry) {
45+
let locale = localeWithStrings(self.presentationData.strings)
46+
let countryNames = poll.countries.map { id in
47+
if id == "FT" {
48+
return "Fragment"
49+
} else if let countryName = locale.localizedString(forRegionCode: id) {
50+
return countryName
51+
} else {
52+
return id
53+
}
54+
}
55+
var countries: String = ""
56+
if countryNames.count == 1, let country = countryNames.first {
57+
countries = "**\(country)**"
58+
} else {
59+
for i in 0 ..< countryNames.count {
60+
countries.append("**\(countryNames[i])**")
61+
if i == countryNames.count - 2 {
62+
countries.append(self.presentationData.strings.Chat_Poll_Restriction_Country_CountriesLastDelimiter)
63+
} else if i < countryNames.count - 2 {
64+
countries.append(self.presentationData.strings.Chat_Poll_Restriction_Country_CountriesDelimiter)
65+
}
66+
}
67+
}
68+
if poll.restrictToSubscribers {
69+
text = self.presentationData.strings.Chat_Poll_Restriction_SubscribersCountry(peerName, countries).string
70+
} else {
71+
text = self.presentationData.strings.Chat_Poll_Restriction_Country(countries).string
72+
}
73+
} else {
74+
if case let .channel(channel) = peer, case .member = channel.participationStatus {
75+
text = self.presentationData.strings.Chat_Poll_Restriction_Subscribers_TimeLimit
76+
} else {
77+
text = self.presentationData.strings.Chat_Poll_Restriction_Subscribers(peerName).string
78+
}
79+
}
80+
let controller = UndoOverlayController(
81+
presentationData: self.presentationData,
82+
content: .banned(text: text),
83+
position: .bottom,
84+
action: { _ in return true }
85+
)
86+
self.controllerInteraction?.presentControllerInCurrent(controller, nil)
87+
})
88+
}
2389

2490
func displayPostedScheduledMessagesToast(ids: [EngineMessage.Id]) {
2591
let timestamp = CFAbsoluteTimeGetCurrent()

submodules/TelegramUI/Sources/ChatController.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3896,20 +3896,26 @@ public final class ChatControllerImpl: TelegramBaseController, ChatController, G
38963896
)
38973897
strongSelf.present(controller, in: .current)
38983898
}
3899-
}, error: { _ in
3899+
}, error: { error in
39003900
guard let strongSelf = self, let controllerInteraction = strongSelf.controllerInteraction else {
39013901
return
39023902
}
39033903
if controllerInteraction.pollActionState.pollMessageIdsInProgress.removeValue(forKey: id) != nil {
39043904
strongSelf.chatDisplayNode.historyNode.requestMessageUpdate(id)
39053905
}
3906+
3907+
switch error {
3908+
case .restrictedToSubscribers:
3909+
strongSelf.displayPollRestrictedToast(messageId: id)
3910+
default:
3911+
break
3912+
}
39063913
}, completed: {
39073914
guard let strongSelf = self, let controllerInteraction = strongSelf.controllerInteraction else {
39083915
return
39093916
}
39103917
if controllerInteraction.pollActionState.pollMessageIdsInProgress.removeValue(forKey: id) != nil {
39113918
Queue.mainQueue().after(1.0, {
3912-
39133919
strongSelf.chatDisplayNode.historyNode.requestMessageUpdate(id)
39143920
})
39153921
}
@@ -5664,6 +5670,11 @@ public final class ChatControllerImpl: TelegramBaseController, ChatController, G
56645670
return
56655671
}
56665672
self.interfaceInteraction?.openSetPeerAvatar()
5673+
}, displayPollRestrictedToast: { [weak self] messageId in
5674+
guard let self else {
5675+
return
5676+
}
5677+
self.displayPollRestrictedToast(messageId: messageId)
56675678
}, automaticMediaDownloadSettings: self.automaticMediaDownloadSettings, pollActionState: ChatInterfacePollActionState(), stickerSettings: self.stickerSettings, presentationContext: ChatPresentationContext(context: context, backgroundNode: self.chatBackgroundNode))
56685679
controllerInteraction.enableFullTranslucency = context.sharedContext.energyUsageSettings.fullTranslucency
56695680

submodules/TelegramUI/Sources/OverlayAudioPlayerControllerNode.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,10 @@ final class OverlayAudioPlayerControllerNode: ViewControllerTracingNode, ASGestu
258258
}, requestToggleTodoMessageItem: { _, _, _ in
259259
}, displayTodoToggleUnavailable: { _ in
260260
}, openStarsPurchase: { _ in
261-
}, openRankInfo: { _, _, _ in }, openSetPeerAvatar: {}, automaticMediaDownloadSettings: MediaAutoDownloadSettings.defaultSettings, pollActionState: ChatInterfacePollActionState(), stickerSettings: ChatInterfaceStickerSettings(), presentationContext: ChatPresentationContext(context: context, backgroundNode: nil))
261+
}, openRankInfo: { _, _, _ in
262+
}, openSetPeerAvatar: {
263+
}, displayPollRestrictedToast: { _ in
264+
}, automaticMediaDownloadSettings: MediaAutoDownloadSettings.defaultSettings, pollActionState: ChatInterfacePollActionState(), stickerSettings: ChatInterfaceStickerSettings(), presentationContext: ChatPresentationContext(context: context, backgroundNode: nil))
262265

263266
self.dimNode = ASDisplayNode()
264267
self.dimNode.backgroundColor = UIColor(white: 0.0, alpha: 0.5)

0 commit comments

Comments
 (0)