-
Notifications
You must be signed in to change notification settings - Fork 895
/
Copy pathBusinessChatbotController.swift
787 lines (642 loc) · 32.8 KB
/
BusinessChatbotController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
//
// BusinessChatbotController.swift
// Telegram
//
// Created by Mikhail Filimonov on 13.02.2024.
// Copyright © 2024 Telegram. All rights reserved.
//
import Foundation
import TelegramCore
import Postbox
import Cocoa
import TGUIKit
import SwiftSignalKit
private final class BusinessBotRowItem : GeneralRowItem {
let bot: EnginePeer
let context: AccountContext
let titleLayout: TextViewLayout
let statusLayout: TextViewLayout
let selected: Bool
init(_ initialSize: NSSize, stableId: AnyHashable, context: AccountContext, bot: EnginePeer, selected: Bool, viewType: GeneralViewType, action: @escaping()->Void) {
self.bot = bot
self.context = context
self.selected = selected
titleLayout = .init(.initialize(string: bot._asPeer().displayTitle, color: theme.colors.text, font: .medium(.text)), maximumNumberOfLines: 1)
statusLayout = .init(.initialize(string: "@\(bot.addressName ?? "")", color: theme.colors.grayText, font: .normal(.text)), maximumNumberOfLines: 1)
super.init(initialSize, stableId: stableId, viewType: viewType, action: action)
}
override func makeSize(_ width: CGFloat, oldWidth: CGFloat = 0) -> Bool {
_ = super.makeSize(width, oldWidth: oldWidth)
titleLayout.measure(width: blockWidth - 30 - 100)
statusLayout.measure(width: blockWidth - 30 - 100)
return true
}
override var height: CGFloat {
return 44
}
override func viewClass() -> AnyClass {
return BusinessBotRowView.self
}
}
private final class BusinessBotRowView: GeneralContainableRowView {
private let avatar = AvatarControl(font: .avatar(10))
private let titleView = TextView()
private let statusView = TextView()
private var add: TextButton?
private var remove: ImageButton?
required init(frame frameRect: NSRect) {
super.init(frame: frameRect)
avatar.setFrameSize(NSMakeSize(30, 30))
addSubview(avatar)
addSubview(titleView)
addSubview(statusView)
avatar.userInteractionEnabled = false
titleView.userInteractionEnabled = false
titleView.isSelectable = false
statusView.userInteractionEnabled = false
statusView.isSelectable = false
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func set(item: TableRowItem, animated: Bool = false) {
super.set(item: item, animated: animated)
guard let item = item as? BusinessBotRowItem else {
return
}
avatar.setPeer(account: item.context.account, peer: item.bot._asPeer())
titleView.update(item.titleLayout)
statusView.update(item.statusLayout)
if item.selected {
if let view = self.add {
performSubviewRemoval(view, animated: animated)
self.add = nil
}
let current: ImageButton
if let view = self.remove {
current = view
} else {
current = ImageButton()
current.scaleOnClick = true
addSubview(current)
self.remove = current
current.set(handler: { [weak self] _ in
if let item = self?.item as? GeneralRowItem {
item.action()
}
}, for: .Click)
}
current.set(image: theme.icons.stickersRemove, for: .Normal)
current.sizeToFit(.zero, NSMakeSize(24, 24), thatFit: true)
} else {
if let view = self.remove {
performSubviewRemoval(view, animated: animated)
self.remove = nil
}
let current: TextButton
if let view = self.add {
current = view
} else {
current = TextButton()
current.scaleOnClick = true
addSubview(current)
self.add = current
current.set(handler: { [weak self] _ in
if let item = self?.item as? GeneralRowItem {
item.action()
}
}, for: .Click)
}
current.set(font: .medium(.text), for: .Normal)
current.set(color: theme.colors.underSelectedColor, for: .Normal)
current.set(background: theme.colors.accent, for: .Normal)
current.set(text: strings().businessChatbotsAdd, for: .Normal)
current.sizeToFit(NSMakeSize(10, 6))
current.layer?.cornerRadius = current.frame.height / 2
}
needsLayout = true
}
override func layout() {
super.layout()
guard let item = item as? BusinessBotRowItem else {
return
}
avatar.centerY(x: item.viewType.innerInset.left)
titleView.setFrameOrigin(NSMakePoint(avatar.frame.maxX + 10, 7))
statusView.setFrameOrigin(NSMakePoint(avatar.frame.maxX + 10, containerView.frame.height - statusView.frame.height - 5))
if let add {
add.centerY(x: containerView.frame.width - add.frame.width - item.viewType.innerInset.left)
}
if let remove {
remove.centerY(x: containerView.frame.width - remove.frame.width - item.viewType.innerInset.left)
}
}
}
private final class Arguments {
let context: AccountContext
let toggleAccess:(State.Access)->Void
let selectChats:(SelectChatType)->Void
let toggleReplyAccess:()->Void
let removeSelected:(SelectChatType, PeerId)->Void
let setBot:(EnginePeer?)->Void
init(context: AccountContext, toggleAccess:@escaping(State.Access)->Void, selectChats:@escaping(SelectChatType)->Void, toggleReplyAccess:@escaping()->Void, removeSelected:@escaping(SelectChatType, PeerId)->Void, setBot:@escaping(EnginePeer?)->Void) {
self.context = context
self.toggleAccess = toggleAccess
self.selectChats = selectChats
self.toggleReplyAccess = toggleReplyAccess
self.removeSelected = removeSelected
self.setBot = setBot
}
}
private struct State : Equatable {
enum Access : Equatable {
case all
case selected
}
enum BotsResult : Equatable {
case found([EnginePeer])
case loading
}
var username: String?
var access: Access = .all
var botsResult: BotsResult? = nil
var bot: EnginePeer? = nil
var replyAccess: Bool = true
var includeIds: [PeerId] = []
var excludeIds: [PeerId] = []
var disableIds: [PeerId] = []
var includePeers: [EnginePeer] = []
var excludePeers: [EnginePeer] = []
var contacts: Set<PeerId> = Set()
var initialBot: TelegramAccountConnectedBot?
var mapped: TelegramAccountConnectedBot? {
if let bot = bot {
var categories: TelegramBusinessRecipients.Categories = []
let peerIds: Set<PeerId>
let catpeers: Set<PeerId>
switch self.access {
case .all:
peerIds = Set(self.excludeIds.filter {
$0.namespace._internalGetInt32Value() != ChatListFilterPeerCategories.Namespace
})
catpeers = Set(self.excludeIds.filter {
$0.namespace._internalGetInt32Value() == ChatListFilterPeerCategories.Namespace
})
case .selected:
peerIds = Set(self.includeIds.filter {
$0.namespace._internalGetInt32Value() != ChatListFilterPeerCategories.Namespace
})
catpeers = Set(self.includeIds.filter {
$0.namespace._internalGetInt32Value() == ChatListFilterPeerCategories.Namespace
})
}
for peerId in catpeers {
if peerId.id == PeerId.Id._internalFromInt64Value(Int64(ChatListFilterPeerCategories.contacts.rawValue)) {
categories.insert(.contacts)
}
if peerId.id == PeerId.Id._internalFromInt64Value(Int64(ChatListFilterPeerCategories.nonContacts.rawValue)) {
categories.insert(.nonContacts)
}
if peerId.id == PeerId.Id._internalFromInt64Value(Int64(ChatListFilterPeerCategories.newChats.rawValue)) {
categories.insert(.newChats)
}
if peerId.id == PeerId.Id._internalFromInt64Value(Int64(ChatListFilterPeerCategories.existingChats.rawValue)) {
categories.insert(.existingChats)
}
}
return .init(id: bot.id, recipients: .init(categories: categories, additionalPeers: peerIds, excludePeers: Set(excludeIds.filter { $0.namespace._internalGetInt32Value() != ChatListFilterPeerCategories.Namespace }), exclude: self.access == .all), canReply: self.replyAccess)
} else {
return nil
}
}
}
private let _id_header = InputDataIdentifier("_id_header")
private let _id_input = InputDataIdentifier("_id_username")
private let _id_attached_bot = InputDataIdentifier("_id_attached_bot")
private let _id_access_1x1 = InputDataIdentifier("_id_access_1x1")
private let _id_access_selected = InputDataIdentifier("_id_access_selected")
private let _id_include_chats = InputDataIdentifier("_id_include_chats")
private let _id_exclude_chats = InputDataIdentifier("_id_exclude_chats")
private let _id_exclude_users = InputDataIdentifier("_id_exclude_users")
private let _id_reply_to_message = InputDataIdentifier("_id_reply_to_message")
private let _id_remove = InputDataIdentifier("_id_remove")
private let _id_loading = InputDataIdentifier("_id_loading")
private func _id_peer(_ id: PeerId, _ include: Bool) -> InputDataIdentifier {
return InputDataIdentifier("_id_peer_\(id.toInt64())_\(include)")
}
private func _id_bot(_ id: PeerId) -> InputDataIdentifier {
return InputDataIdentifier("_id_bot_\(id.toInt64())")
}
private func entries(_ state: State, arguments: Arguments) -> [InputDataEntry] {
var entries:[InputDataEntry] = []
var sectionId:Int32 = 0
var index: Int32 = 0
entries.append(.sectionId(sectionId, type: .normal))
sectionId += 1
let hText = strings().businessChatbotsHeader
let attr = parseMarkdownIntoAttributedString(hText, attributes: MarkdownAttributes(body: MarkdownAttributeSet(font: .normal(.text), textColor: theme.colors.listGrayText), bold: MarkdownAttributeSet(font: .bold(.text), textColor: theme.colors.listGrayText), link: MarkdownAttributeSet(font: .normal(.text), textColor: theme.colors.link), linkAttribute: { contents in
return (NSAttributedString.Key.link.rawValue, inApp(for: contents.nsstring, context: arguments.context))
}))
entries.append(.custom(sectionId: sectionId, index: 0, value: .none, identifier: _id_header, equatable: nil, comparable: nil, item: { initialSize, stableId in
return AnimatedStickerHeaderItem(initialSize, stableId: stableId, context: arguments.context, sticker: LocalAnimatedSticker.business_chatbot, text: attr)
}))
entries.append(.sectionId(sectionId, type: .normal))
sectionId += 1
if let bot = state.bot {
entries.append(.custom(sectionId: sectionId, index: index, value: .none, identifier: _id_input, equatable: .init(state), comparable: nil, item: { initialSize, stableId in
return GeneralBlockTextRowItem(initialSize, stableId: stableId, viewType: .firstItem, text: "https://t.me/\(bot.addressName ?? "")", font: .normal(.text), color: theme.colors.text)
}))
} else {
entries.append(.input(sectionId: sectionId, index: 0, value: .string(state.username), error: nil, identifier: _id_input, mode: .plain, data: .init(viewType: state.botsResult == nil ? .singleItem : .firstItem, defaultText: ""), placeholder: nil, inputPlaceholder: strings().businessChatbotsPlaceholder, filter: { $0 }, limit: 60))
entries.append(.desc(sectionId: sectionId, index: index, text: .plain(strings().businessChatBotsFooter), data: .init(color: theme.colors.listGrayText, viewType: .textBottomItem)))
index += 1
}
if let result = state.botsResult {
switch result {
case .loading:
entries.append(.custom(sectionId: sectionId, index: index, value: .none, identifier: _id_loading, equatable: nil, comparable: nil, item: { initialSize, stableId in
return GeneralLoadingRowItem(initialSize, stableId: stableId, viewType: .lastItem)
}))
case let .found(peers):
struct Tuple : Equatable {
let peer: EnginePeer
let viewType: GeneralViewType
let selected: Bool
}
var tuples: [Tuple] = []
if peers.isEmpty {
entries.append(.custom(sectionId: sectionId, index: index, value: .none, identifier: _id_bot(.init(0)), equatable: nil, comparable: nil, item: { initialSize, stableId in
return GeneralBlockTextRowItem(initialSize, stableId: stableId, viewType: .lastItem, text: strings().businessChatbotsNotFound, font: .normal(.text), color: theme.colors.grayText, centerViewAlignment: true)
}))
} else {
for (i, peer) in peers.enumerated() {
var viewType: GeneralViewType = bestGeneralViewType(peers, for: i)
if i == 0 {
if i < peers.count - 1 {
viewType = .innerItem
} else {
viewType = .lastItem
}
}
tuples.append(.init(peer: peer, viewType: viewType, selected: state.bot?.id == peer.id))
}
for tuple in tuples {
entries.append(.custom(sectionId: sectionId, index: index, value: .none, identifier: _id_bot(tuple.peer.id), equatable: .init(tuple), comparable: nil, item: { initialSize, stableId in
return BusinessBotRowItem(initialSize, stableId: stableId, context: arguments.context, bot: tuple.peer, selected: tuple.selected, viewType: tuple.viewType, action: {
if tuple.selected {
arguments.setBot(nil)
} else {
arguments.setBot(tuple.peer)
}
})
}))
}
}
}
}
if let _ = state.bot {
entries.append(.sectionId(sectionId, type: .normal))
sectionId += 1
entries.append(.desc(sectionId: sectionId, index: index, text: .plain(strings().businessChatbotsChatTypes), data: .init(color: theme.colors.listGrayText, viewType: .textTopItem)))
index += 1
entries.append(.general(sectionId: sectionId, index: 0, value: .none, error: nil, identifier: _id_access_1x1, data: .init(name: strings().businessMessageRecepientsAll, color: theme.colors.text, type: .selectable(state.access == .all), viewType: .firstItem, action: {
arguments.toggleAccess(.all)
})))
entries.append(.general(sectionId: sectionId, index: 0, value: .none, error: nil, identifier: _id_access_selected, data: .init(name: strings().businessMessageRecepientsSelected, color: theme.colors.text, type: .selectable(state.access == .selected), viewType: .lastItem, action: {
arguments.toggleAccess(.selected)
})))
entries.append(.sectionId(sectionId, type: .normal))
sectionId += 1
struct Tuple : Equatable {
let peer: PeerEquatable
let type: State.Access
let viewType: GeneralViewType
let status: String?
let include: Bool
}
let makePeers:(Bool)->[Peer] = { include in
let ids = !include ? state.excludeIds : state.includeIds
let peers = !include ? state.excludePeers : state.includePeers
var selectedPeers: [Peer] = []
let categories: [PeerId] = ids.filter {
$0.namespace._internalGetInt32Value() == ChatListFilterPeerCategories.Namespace
}
for category in categories {
let cat = ChatListFilterPeerCategories(rawValue: Int32(category.id._internalGetInt64Value()))
selectedPeers.append(TelegramFilterCategory(category: cat))
}
selectedPeers.append(contentsOf: peers.map { $0._asPeer() })
return selectedPeers
}
let insertPeers: (Bool)->Void = { include in
let selectedPeers = makePeers(include)
var tuples: [Tuple] = []
for (i, peer) in selectedPeers.enumerated() {
var viewType: GeneralViewType = bestGeneralViewType(selectedPeers, for: i)
if i == 0 {
if i < selectedPeers.count - 1 {
viewType = .innerItem
} else {
viewType = .lastItem
}
}
let status: String?
if peer is TelegramFilterCategory {
status = nil
} else {
status = state.contacts.contains(peer.id) ? strings().businessMessageContact : strings().businessMessageNonContact
}
tuples.append(.init(peer: .init(peer), type: state.access, viewType: viewType, status: status, include: include))
}
for tuple in tuples {
entries.append(.custom(sectionId: sectionId, index: 0, value: .none, identifier: _id_peer(tuple.peer.id, tuple.include), equatable: .init(tuple), comparable: nil, item: { initialSize, stableId in
return ShortPeerRowItem(initialSize, peer: tuple.peer.peer, account: arguments.context.account, context: arguments.context, stableId: stableId, height: 44, photoSize: NSMakeSize(30, 30), status: tuple.status, inset: NSEdgeInsets(left: 20, right: 20), viewType: tuple.viewType, action: {
//arguments.openInfo(peer.id)
}, contextMenuItems: {
return .single([ContextMenuItem(strings().contextRemove, handler: {
if state.access == .all {
arguments.removeSelected(.exclude, tuple.peer.id)
} else {
arguments.removeSelected(.include, tuple.peer.id)
}
}, itemMode: .destruct, itemImage: MenuAnimation.menu_delete.value)])
}, highlightVerified: true, menuOnAction: true)
}))
}
}
switch state.access {
case .all:
entries.append(.desc(sectionId: sectionId, index: index, text: .plain(strings().businessMessageRecepientsExcludeTitle), data: .init(color: theme.colors.listGrayText, viewType: .textTopItem)))
index += 1
entries.append(.general(sectionId: sectionId, index: 0, value: .none, error: nil, identifier: _id_exclude_chats, data: .init(name: strings().businessMessageRecepientsExclude, color: theme.colors.accent, icon: theme.icons.chat_filter_add, type: .none, viewType: state.excludeIds.isEmpty ? .singleItem : .firstItem, action: {
arguments.selectChats(.exclude)
})))
insertPeers(false)
entries.append(.desc(sectionId: sectionId, index: index, text: .plain(strings().businessMessageRecepientsExcludeInfo), data: .init(color: theme.colors.listGrayText, detectBold: true, viewType: .textBottomItem)))
index += 1
case .selected:
entries.append(.desc(sectionId: sectionId, index: index, text: .plain(strings().businessMessageRecepientsIncludeTitle), data: .init(color: theme.colors.listGrayText, viewType: .textTopItem)))
index += 1
entries.append(.general(sectionId: sectionId, index: 0, value: .none, error: nil, identifier: _id_include_chats, data: .init(name: strings().businessMessageRecepientsInclude, color: theme.colors.accent, icon: theme.icons.chat_filter_add, type: .none, viewType: state.includeIds.isEmpty ? .singleItem : .firstItem, action: {
arguments.selectChats(.include)
})))
insertPeers(true)
entries.append(.desc(sectionId: sectionId, index: index, text: .plain(strings().businessMessageRecepientsIncludeInfo), data: .init(color: theme.colors.listGrayText, detectBold: true, viewType: .textBottomItem)))
index += 1
entries.append(.sectionId(sectionId, type: .normal))
sectionId += 1
entries.append(.desc(sectionId: sectionId, index: index, text: .plain(strings().businessMessageRecepientsExcludeTitle), data: .init(color: theme.colors.listGrayText, viewType: .textTopItem)))
index += 1
entries.append(.general(sectionId: sectionId, index: 0, value: .none, error: nil, identifier: _id_exclude_chats, data: .init(name: strings().businessMessageRecepientsExclude, color: theme.colors.accent, icon: theme.icons.chat_filter_add, type: .none, viewType: state.excludeIds.isEmpty ? .singleItem : .firstItem, action: {
arguments.selectChats(.exclude)
})))
insertPeers(false)
entries.append(.desc(sectionId: sectionId, index: index, text: .plain(strings().businessMessageRecepientsExcludeInfo), data: .init(color: theme.colors.listGrayText, detectBold: true, viewType: .textBottomItem)))
index += 1
}
entries.append(.sectionId(sectionId, type: .normal))
sectionId += 1
entries.append(.desc(sectionId: sectionId, index: index, text: .plain(strings().businessChatbotsPermissionHeader), data: .init(color: theme.colors.listGrayText, viewType: .textTopItem)))
index += 1
entries.append(.general(sectionId: sectionId, index: 0, value: .none, error: nil, identifier: _id_reply_to_message, data: .init(name: strings().businessChatbotsPermission, color: theme.colors.text, type: .switchable(state.replyAccess), viewType: .singleItem, action: arguments.toggleReplyAccess)))
entries.append(.desc(sectionId: sectionId, index: index, text: .plain(strings().businessChatbotsPermissionInfo), data: .init(color: theme.colors.listGrayText, viewType: .textBottomItem)))
index += 1
}
entries.append(.sectionId(sectionId, type: .normal))
sectionId += 1
return entries
}
func BusinessChatbotController(context: AccountContext) -> InputDataController {
let actionsDisposable = DisposableSet()
let initialState = State()
let statePromise = ValuePromise<State>(ignoreRepeated: true)
let stateValue = Atomic(value: initialState)
let updateState: ((State) -> State) -> Void = { f in
statePromise.set(stateValue.modify (f))
}
let includePeers: Signal<[EnginePeer], NoError> = statePromise.get() |> map { $0.includeIds } |> distinctUntilChanged |> mapToSignal { peerIds in
return context.account.postbox.transaction { transaction -> [EnginePeer] in
return peerIds.compactMap {
transaction.getPeer($0)
}.map {
.init($0)
}
}
} |> deliverOnMainQueue
actionsDisposable.add(includePeers.start(next: { peers in
updateState { current in
var current = current
current.includePeers = peers
return current
}
}))
let excludedPeers: Signal<[EnginePeer], NoError> = statePromise.get() |> map { $0.excludeIds } |> distinctUntilChanged |> mapToSignal { peerIds in
return context.account.postbox.transaction { transaction -> [EnginePeer] in
return peerIds.compactMap {
transaction.getPeer($0)
}.map {
.init($0)
}
}
} |> deliverOnMainQueue
actionsDisposable.add(excludedPeers.start(next: { peers in
updateState { current in
var current = current
current.excludePeers = peers
return current
}
}))
let contacts = context.engine.data.get(TelegramEngine.EngineData.Item.Contacts.List(includePresences: false))
actionsDisposable.add(contacts.start(next: { contacts in
updateState { current in
var current = current
current.contacts = Set(contacts.peers.map { $0.id })
return current
}
}))
let chatbot = context.engine.data.get(TelegramEngine.EngineData.Item.Peer.BusinessConnectedBot(id: context.peerId)) |> take(1)
actionsDisposable.add(chatbot.start(next: { connectedBot in
updateState { current in
var current = current
current.initialBot = connectedBot
if let connectedBot = connectedBot {
var categories: [PeerId] = []
if connectedBot.recipients.categories.contains(.nonContacts) {
categories.insert(TelegramFilterCategory(category: .nonContacts).id, at: 0)
}
if connectedBot.recipients.categories.contains(.contacts) {
categories.insert(TelegramFilterCategory(category: .contacts).id, at: 0)
}
if connectedBot.recipients.categories.contains(.newChats) {
categories.insert(TelegramFilterCategory(category: .newChats).id, at: 0)
}
if connectedBot.recipients.categories.contains(.existingChats) {
categories.insert(TelegramFilterCategory(category: .existingChats).id, at: 0)
}
current.access = connectedBot.recipients.exclude ? .all : .selected
switch current.access {
case .all:
current.excludeIds = Array(connectedBot.recipients.additionalPeers)
current.excludeIds.insert(contentsOf: categories, at: 0)
case .selected:
current.includeIds = Array(connectedBot.recipients.additionalPeers)
current.includeIds.insert(contentsOf: categories, at: 0)
current.excludeIds = Array(connectedBot.recipients.excludePeers)
}
current.replyAccess = connectedBot.canReply
current.disableIds = Array(connectedBot.recipients.excludePeers)
}
return current
}
}))
let arguments = Arguments(context: context, toggleAccess: { value in
updateState { current in
var current = current
current.access = value
return current
}
}, selectChats: { type in
let access = stateValue.with { $0.access }
var items: [ShareAdditionItem] = []
if access == .all || type == .include {
switch type {
case .exclude:
items.append(.init(peer: TelegramFilterCategory(category: .existingChats), status: ""))
case .include:
items.append(.init(peer: TelegramFilterCategory(category: .newChats), status: ""))
}
items.append(.init(peer: TelegramFilterCategory(category: .contacts), status: ""))
items.append(.init(peer: TelegramFilterCategory(category: .nonContacts), status: ""))
}
let additionTopItems = ShareAdditionItems(items: items, topSeparator: strings().businessMessageSelectPeersChatTypes, bottomSeparator: strings().businessMessageSelectPeersChats)
let selected: Set<PeerId>
switch type {
case .exclude:
selected = stateValue.with { Set($0.excludeIds) }
case .include:
selected = stateValue.with { Set($0.includeIds) }
}
showModal(with: ShareModalController(BusinessSelectChatsCallbackObject(context, defaultSelectedIds: selected, contacts: stateValue.with { $0.contacts }, additionTopItems: items.isEmpty ? nil : additionTopItems, limit: 100, limitReachedText: strings().businessSelectPeersLimit, callback: { peerIds in
updateState { current in
var current = current
switch type {
case .exclude:
current.excludeIds = Array(peerIds)
case .include:
current.includeIds = Array(peerIds)
}
return current
}
return .complete()
}, excludePeerIds: Set([context.peerId]))), for: context.window)
}, toggleReplyAccess: {
updateState { current in
var current = current
current.replyAccess = !current.replyAccess
return current
}
}, removeSelected: { type, peerId in
updateState { current in
var current = current
switch type {
case .exclude:
current.excludeIds.removeAll(where: { $0 == peerId })
case .include:
current.includeIds.removeAll(where: { $0 == peerId })
}
return current
}
}, setBot: { bot in
if let user = bot?._asPeer() as? TelegramUser, let botInfo = user.botInfo {
if !botInfo.flags.contains(.isBusiness) {
alert(for: context.window, info: strings().businessChatBotsBotNotSupported)
return
}
}
updateState { current in
var current = current
current.bot = bot
current.username = nil
if let bot = bot {
current.botsResult = .found([bot])
} else {
current.botsResult = nil
}
return current
}
})
let signal = statePromise.get() |> deliverOnPrepareQueue |> map { state in
return InputDataSignalValue(entries: entries(state, arguments: arguments))
}
let controller = InputDataController(dataSignal: signal, title: strings().businessChatbotsTitle, removeAfterDisappear: false, hasDone: true, identifier: "business_chatbot")
controller.updateDatas = { datas in
updateState { current in
var current = current
current.username = datas[_id_input]?.stringValue
return current
}
return .none
}
controller.validateData = { data in
let state = stateValue.with { $0 }
if state.initialBot != state.mapped {
_ = context.engine.accountData.setAccountConnectedBot(bot: state.mapped).start()
showModalText(for: context.window, text: strings().businessUpdated)
return .success(.navigationBack)
}
return .none
}
controller.updateDoneValue = { data in
return { f in
let isEnabled = stateValue.with { $0.initialBot != $0.mapped }
if isEnabled {
f(.enabled(strings().navigationDone))
} else {
f(.disabled(strings().navigationDone))
}
}
}
let usernameUpdate: Signal<State.BotsResult?, NoError> = statePromise.get() |> filter { $0.bot == nil }
|> map { $0.username }
|> distinctUntilChanged
|> mapToSignal { username in
if let username = username, !username.isEmpty {
return .single(.loading) |> then(context.engine.contacts.searchRemotePeers(query: username) |> map {
return .found(($0.0 + $0.1).prefix(5).filter { $0.peer.isBot }.map { EnginePeer($0.peer) })
})
} else {
return .single(nil)
}
}
let initialIdUpdate: Signal<EnginePeer?, NoError> = statePromise.get() |> filter { $0.bot == nil }
|> map { $0.initialBot?.id }
|> distinctUntilChanged
|> mapToSignal { peerId in
if let peerId = peerId {
return context.engine.data.get(TelegramEngine.EngineData.Item.Peer.Peer(id: peerId))
} else {
return .never()
}
}
actionsDisposable.add(usernameUpdate.startStandalone(next: { result in
updateState { current in
var current = current
current.botsResult = result
return current
}
}))
actionsDisposable.add(initialIdUpdate.start(next: { result in
updateState { current in
var current = current
current.bot = result
if let result {
current.botsResult = .found([result])
}
return current
}
}))
controller.onDeinit = {
actionsDisposable.dispose()
}
return controller
}