forked from zevlg/telega.el
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelega-chat.el
3185 lines (2795 loc) · 125 KB
/
telega-chat.el
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
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; telega-chat.el --- Chat mode for telega -*- lexical-binding:t -*-
;; Copyright (C) 2018-2019 by Zajcev Evgeny.
;; Author: Zajcev Evgeny <zevlg@yandex.ru>
;; Created: Thu Apr 19 19:59:51 2018
;; Keywords:
;; telega is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; telega is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with telega. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
(require 'cl-lib)
(require 'subr-x)
(require 'ring)
(require 'url-util)
(require 'seq)
(require 'telega-core)
(require 'telega-tdlib)
(require 'telega-msg)
(require 'telega-ins)
(require 'telega-voip) ;telega-voip-call
(require 'telega-notifications)
(require 'telega-sticker)
(require 'telega-company)
(eval-when-compile
(require 'rx)
(require 'pcase)) ;`pcase-let*' and `rx'
;; shutup compiler
(declare-function company-complete-common "company")
(declare-function company-begin-backend "company" (backend &optional callback))
(declare-function tracking-add-buffer "tracking" (buffer &optional faces))
(declare-function tracking-remove-buffer "tracking" (buffer))
(declare-function telega-root--chat-update "telega-root"
(chat &optional for-reorder))
(declare-function telega-root--chat-reorder "telega-root" (chat))
(declare-function telega-root--chat-new "telega-root" (chat))
(declare-function telega-status--set "telega-root" (conn-status &optional aux-status raw))
(defvar telega-filters--inhibit-redisplay)
(defvar telega-filters--inhibit-list)
(declare-function telega-filters--redisplay "telega-filter")
(declare-function telega-filter--test "telega-filter" (chat fspec))
(declare-function telega-filter-chats "telega-filter" (filter-spec chats-list))
(declare-function telega-filter-default-p "telega-filter" (&optional filter))
;;; Chatbuf vars
(defvar telega-chatbuf--ewoc nil
"Ewoc for for the current chat buffer.")
(make-variable-buffer-local 'telega-chatbuf--ewoc)
(defvar telega-chatbuf--input-ring nil
"The chat input history ring.")
(make-variable-buffer-local 'telega-chatbuf--input-ring)
(defvar telega-chatbuf--input-idx nil
"The index to the current item in the chat input ring.")
(make-variable-buffer-local 'telega-chatbuf--input-idx)
(defvar telega-chatbuf--input-pending nil
"Non-nil if last input is not yet commited.
Real value is the pending input string.")
(make-variable-buffer-local 'telega-chatbuf--input-pending)
(defvar telega-chatbuf--output-marker nil)
(make-variable-buffer-local 'telega-chatbuf--output-marker)
(defvar telega-chatbuf--aux-button nil
"Button that display reply/edit/fwd message above input.")
(make-variable-buffer-local 'telega-chatbuf--aux-button)
(defvar telega-chatbuf--prompt-button nil "Input prompt button.")
(make-variable-buffer-local 'telega-chatbuf--prompt-button)
(defvar telega-chatbuf--history-loading nil
"Non-nil if history has been requested.
Actual value is `:@extra` value of the call to load history.")
(make-variable-buffer-local 'telega-chatbuf--history-loading)
(defvar telega-chatbuf--voice-msg nil
"Active (playing/paused) voice note message for the current chat.")
(make-variable-buffer-local 'telega-chatbuf--voice-msg)
(defvar telega-chatbuf--my-action nil
"My current action in chat buffer.")
(make-variable-buffer-local 'telega-chatbuf--my-action)
;; Special variable used to set `telega-chatbuf--chat'
;; inside `telega-chat-mode'
(defvar telega-chat--preparing-buffer-for)
(defun telega-chat--set-uaprops (chat uaprops)
"Set CHAT's user application properties to UAPROPS."
(plist-put chat :uaprops uaprops)
(let ((client-data (if uaprops (prin1-to-string uaprops) "")))
(plist-put chat :client_data client-data)
(telega-server--send
(list :@type "setChatClientData"
:chat_id (plist-get chat :id)
:client_data client-data))))
(defun telega-chat-color (chat)
"Return color list associated with CHAT.
If there is no CHAT color, then generate new and assign it to CHAT."
(or (telega-chat-uaprop chat :color)
(setf (telega-chat-uaprop chat :color)
(telega-color-tripple (telega-color-random)))))
(defsubst telega--ordered-chats-insert (chat)
"Insert CHAT into `telega--ordered-chats' according to CHAT's order."
(let ((place telega--ordered-chats))
(if (or (null place)
(> (telega-chat--order chat)
(telega-chat--order (car place))))
(setq telega--ordered-chats (push chat telega--ordered-chats))
(while (and (not (< (telega-chat--order (car place))
(telega-chat--order chat)))
(cdr place)
(not (< (telega-chat--order (cadr place))
(telega-chat--order chat))))
(setq place (cdr place)))
(cl-assert place)
(setcdr place (cons chat (cdr place))))
telega--ordered-chats))
(defsubst telega-chat--ensure (chat)
"Ensure CHAT resides in `telega--chats' and `telega--ordered-chats'.
Return chat from `telega--chats'."
(let ((chat-id (plist-get chat :id)))
(or (gethash chat-id telega--chats)
(prog1
chat
(puthash chat-id chat telega--chats)
;; Place chat in correct place inside `telega--ordered-chats'
(telega--ordered-chats-insert chat)
;; parse :client_data as plist, we use it to store
;; additional chat properties (user application properties)
;; NOTE: plist might contain strings with surropagated
;; pairs, so `telega-tl-str' is used, see
;; https://github.com/zevlg/telega.el/issues/94
(let ((client-data (telega-tl-str chat :client_data)))
(unless (string-empty-p client-data)
(ignore-errors
(plist-put chat :uaprops (car (read-from-string client-data))))))
;; Assign the chat some color (used to draw avatars and
;; highlight users in chat)
;; We use custom chat property, so it is saved between restarts
;; list of three elements - (LIGHT-COLOR COLOR DARK-COLOR)
(unless (telega-chat-uaprop chat :color)
(let ((col (telega-color-random)))
(setf (telega-chat-uaprop chat :color)
(list (telega-color-gradient col 'light)
col
(telega-color-gradient col)))))
))))
(defun telega-chat-get (chat-id &optional offline-p)
"Get chat by its CHAT-ID.
If OFFLINE-P is non-nil then do not request the telegram-server."
(let ((chat (gethash chat-id telega--chats)))
(when (and (not chat) (not offline-p))
(setq chat (telega-server--call
(list :@type "getChat"
:chat_id chat-id)))
(cl-assert chat nil "getChat timed out chat_id=%d" chat-id)
(telega-chat--ensure chat))
chat))
(defun telega-chat-by-username (username)
"Find chat by its USERNAME."
(cl-find username telega--ordered-chats
:test 'string=
:key (lambda (chat)
(telega-tl-str (telega-chat--info chat) :username))))
(defun telega--joinChatByInviteLink (invite-link)
"Return new chat by its INVITE-LINK.
Return nil if can't join the chat."
(let ((chat (telega-server--call
(list :@type "joinChatByInviteLink"
:invite_link invite-link))))
(telega-chat-get (plist-get chat :id))))
(defun telega--checkChatInviteLink (invite-link)
"Check invitation link INVITE-LINK."
(telega-server--call
(list :@type "checkChatInviteLink"
:invite_link invite-link)))
(defun telega--joinChat (chat)
"Adds a new member to a CHAT."
(telega-server--send
(list :@type "joinChat" :chat_id (plist-get chat :id))))
(defun telega--leaveChat (chat)
"Removes current user from CHAT members."
(telega-server--send
(list :@type "leaveChat" :chat_id (plist-get chat :id))))
(defun telega--deleteChatHistory (chat &optional remove-from-list)
"Deletes all messages in the CHAT only for the user.
Cannot be used in channels and public supergroups."
(telega-server--send
(list :@type "deleteChatHistory"
:chat_id (plist-get chat :id)
:remove_from_chat_list (or remove-from-list :false))))
(defun telega--setChatTitle (chat title)
"Changes the CHAT title to TITLE."
(telega-server--send
(list :@type "setChatTitle"
:chat_id (plist-get chat :id)
:title title)))
(defun telega-chat-me ()
"Chat with myself, a.k.a Saved Messages."
;; NOTE: Saved Messages has same id as me user
(telega-chat-get telega--me-id 'offline))
(defun telega-chat--info (chat)
"Return info structure for the CHAT.
It could be user, secretChat, basicGroup or supergroup."
(let ((chat-type (plist-get chat :type)))
(cl-ecase (telega--tl-type chat-type)
(chatTypePrivate
(telega--info 'user (plist-get chat-type :user_id)))
(chatTypeSecret
(telega--info 'secretChat (plist-get chat-type :secret_chat_id)))
(chatTypeBasicGroup
(telega--info 'basicGroup (plist-get chat-type :basic_group_id)))
(chatTypeSupergroup
(telega--info 'supergroup (plist-get chat-type :supergroup_id))))))
(defalias 'telega-chat--user 'telega-chat--info)
(defalias 'telega-chat--secretchat 'telega-chat--info)
(defalias 'telega-chat--basicgroup 'telega-chat--info)
(defalias 'telega-chat--supergroup 'telega-chat--info)
(defun telega-chat--type (chat &optional no-interpret)
"Return type of the CHAT.
Types are: `private', `secret', `bot', `basicgroup', `supergroup' or `channel'.
If NO-INTERPRET is specified, then return only `private',
`secret', `basicgroup' and `supergroup' without interpretation
them to bots or channels."
(let* ((chat-type (plist-get chat :type))
(type-sym (intern (downcase (substring (plist-get chat-type :@type) 8)))))
(cond ((and (not no-interpret)
(eq type-sym 'supergroup)
(plist-get chat-type :is_channel))
'channel)
((and (not no-interpret)
(eq type-sym 'private)
(telega-user-bot-p (telega-chat--user chat)))
'bot)
(t type-sym))))
(defsubst telega-chat-bot-p (chat)
"Return non-nil if CHAT is the chat with bot."
(eq (telega-chat--type chat) 'bot))
(defun telega-chat-private-p (chat &optional include-bots-p)
"Return non-nil if CHAT is private.
If INCLUDE-BOTS-P is non-nil, then return non-nil also for bots."
(eq (telega-chat--type chat include-bots-p) 'private))
(defun telega-chat-secret-p (chat)
"Return non-nil if CHAT is secret."
(eq (telega-chat--type chat 'no-interpret) 'secret))
(defun telega-chat-public-p (chat &optional chat-type)
"Return non-nil if CHAT is public.
Public chats are only chats with non-empty username.
CHAT-TYPE is either `private', `supergroup' or `any'.
`supergroup' type also includes channels.
By default CHAT-TYPE is `any'."
(and (or (eq (or chat-type 'any) 'any)
(eq (telega-chat--type chat 'no-interpret) chat-type))
(telega-chat-username chat)))
(defun telega-chat-muted-p (chat)
"Return non-nil if CHAT is muted."
(> (telega-chat-notification-setting chat :mute_for) 0))
(defun telega-chat-user (chat &optional include-bots-p)
"For private CHAT return corresponding user.
If CHAT is not private, return nil.
If INCLUDE-BOTS-P is non-nil, return corresponding bot user."
(when (telega-chat-private-p chat include-bots-p)
(telega-user--get (telega--tl-get chat :type :user_id))))
(defun telega-chat-title (chat &optional with-username)
"Return title for the CHAT.
If WITH-USERNAME is specified, append trailing username for this chat."
(let ((title (telega-tl-str chat :title)))
(when (string-empty-p title)
(setq title (cl-ecase (telega-chat--type chat)
(private
(telega-user--name (telega-chat--user chat) 'name)))))
(when (and (eq chat (telega-chat-me)) telega-chat-me-custom-title)
(setq title telega-chat-me-custom-title))
(when with-username
(let ((username (telega-chat-username chat)))
(when username
(setq title (concat title " @" username)))))
title))
(defun telega-chat-brackets (chat)
"Return CHAT's brackets from `telega-chat-button-brackets'."
(cdr (seq-find (lambda (bspec)
(telega-filter--test chat (car bspec)))
telega-chat-button-brackets)))
(defun telega-chat--reorder (chat order)
"Reorder CHAT in `telega--ordered-chats' according to ORDER."
;; NOTE: order=nil if reordering with custom ORDER
(when order
(plist-put chat :order order))
;; Reorder CHAT by removing and then adding it again at correct place
(setq telega--ordered-chats (delq chat telega--ordered-chats))
(telega--ordered-chats-insert chat)
(telega-root--chat-reorder chat))
(defun telega--on-updateNewChat (event)
"New chat has been loaded or created."
(let ((chat (plist-get event :chat)))
(telega-chat--ensure chat)
(telega-root--chat-new chat)
(run-hook-with-args 'telega-chat-created-hook chat)))
(defun telega--on-updateChatPhoto (event)
"Chat's photo has been updated."
(let ((chat (telega-chat-get (plist-get event :chat_id)))
(photo (plist-get event :photo)))
(plist-put chat :photo photo)
;; TODO: update chat's :telega-image
))
(defun telega--setChatNotificationSettings (chat &rest settings)
"Set CHAT's notification settings to NOT-CFG."
(declare (indent 1))
(let ((not-cfg (plist-get chat :notification_settings))
(request (list :@type "chatNotificationSettings")))
(cl-loop for (prop-name value) on (append not-cfg settings)
by 'cddr
do (setq request (plist-put request prop-name (or value :false))))
(telega-server--call
(list :@type "setChatNotificationSettings"
:chat_id (plist-get chat :id)
:notification_settings request))))
(defun telega--on-updateChatNotificationSettings (event)
"Notification settings has been changed in chat."
(let ((chat (telega-chat-get (plist-get event :chat_id))))
(plist-put chat :notification_settings
(plist-get event :notification_settings))
(telega-root--chat-update chat)))
(defun telega--on-updateChatTitle (event)
(let ((chat (telega-chat-get (plist-get event :chat_id) 'offline))
(new-title (plist-get event :title)))
(cl-assert chat)
(with-telega-chatbuf chat
(rename-buffer (telega-chatbuf--name chat new-title)))
(plist-put chat :title new-title)
(telega-root--chat-update chat)))
(defun telega--on-updateChatOrder (event)
(let ((chat (telega-chat-get (plist-get event :chat_id) 'offline)))
(cl-assert chat)
(telega-chat--reorder chat (plist-get event :order))
;; NOTE: reorder might affect `telega--filtered-chats' and custom
;; filters, so update the chat
(telega-root--chat-update chat 'for-reorder)))
(defun telega--on-updateChatIsPinned (event)
(let ((chat (telega-chat-get (plist-get event :chat_id) 'offline)))
(cl-assert chat)
(plist-put chat :is_pinned (plist-get event :is_pinned))
(telega-chat--reorder chat (plist-get event :order))
(telega-root--chat-update chat)))
(defun telega--on-updateChatReadInbox (event)
(let ((chat (telega-chat-get (plist-get event :chat_id) 'offline))
(unread-count (plist-get event :unread_count)))
(cl-assert chat)
(plist-put chat :last_read_inbox_message_id
(plist-get event :last_read_inbox_message_id))
(plist-put chat :unread_count unread-count)
;; NOTE: unread_count affects modeline and footer
(with-telega-chatbuf chat
;; NOTE: if all messages are read (in another telegram client) and
;; tracking is enabled, then remove the buffer from tracking
(when (and telega-use-tracking (zerop unread-count))
(tracking-remove-buffer (current-buffer)))
(telega-chatbuf-mode-line-update)
(telega-chatbuf--footer-redisplay))
(telega-root--chat-update chat)))
(defun telega--on-updateChatReadOutbox (event)
(let* ((chat (telega-chat-get (plist-get event :chat_id) 'offline))
(old-read-outbox-msgid (plist-get chat :last_read_outbox_message_id)))
(cl-assert chat)
(plist-put chat :last_read_outbox_message_id
(plist-get event :last_read_outbox_message_id))
(with-telega-chatbuf chat
(telega-chatbuf--read-outbox old-read-outbox-msgid))
(telega-root--chat-update chat)))
(defun telega--on-updateChatUnreadMentionCount (event)
(let ((chat (telega-chat-get (plist-get event :chat_id) 'offline)))
(cl-assert chat)
(plist-put chat :unread_mention_count
(plist-get event :unread_mention_count))
;; NOTE: unread_mention_count affects modeline and footer
(with-telega-chatbuf chat
(telega-chatbuf-mode-line-update)
(telega-chatbuf--footer-redisplay))
(telega-root--chat-update chat)))
(defun telega--on-updateMessageMentionRead (event)
(telega--on-updateChatUnreadMentionCount event)
;; TODO: might be some action needed on `:message_id' as well
)
(defun telega--on-updateChatReplyMarkup (event)
(let ((chat (telega-chat-get (plist-get event :chat_id) 'offline)))
(cl-assert chat)
(plist-put chat :reply_markup_message_id
(plist-get event :reply_markup_message_id))
(telega-root--chat-update chat)))
(defun telega--on-updateChatLastMessage (event)
(let ((chat (telega-chat-get (plist-get event :chat_id) 'offline)))
(cl-assert chat)
(plist-put chat :last_message
(plist-get event :last_message))
(unless (string= (plist-get event :order) (plist-get chat :order))
(telega-chat--reorder chat (plist-get event :order)))
(telega-root--chat-update chat)))
(defun telega--on-updateChatIsMarkedAsUnread (event)
(let ((chat (telega-chat-get (plist-get event :chat_id) 'offline)))
(cl-assert chat)
(plist-put chat :is_marked_as_unread
(plist-get event :is_marked_as_unread))
(telega-root--chat-update chat)))
(defun telega--on-updateChatOnlineMemberCount (event)
"The number of online group members has changed.
NOTE: we store the number as custom chat property, to use it later."
(let ((chat (telega-chat-get (plist-get event :chat_id) 'offline)))
(cl-assert chat)
(plist-put chat :x-online-count
(plist-get event :online_member_count))
;; NOTE: this affects the modeline
(with-telega-chatbuf chat
(telega-chatbuf-mode-line-update))
;; ARGUABLE: root buffer chat inserter might use the counter
; (telega-root--chat-update chat)
))
(defun telega--on-updateChatPinnedMessage (event)
"The chat pinned message was changed."
(let ((chat (telega-chat-get (plist-get event :chat_id) 'offline)))
(cl-assert chat)
(plist-put chat :pinned_message_id
(plist-get event :pinned_message_id))
;; TODO: update chatbuf's view of the pinned message
))
(defun telega-chat--on-getChats (result)
"Ensure chats from RESULT exists, and continue fetching chats."
(let ((chat-ids (plist-get result :chat_ids)))
(telega-debug "on-getChats: %s" chat-ids)
(mapc #'telega-chat--ensure (mapcar #'telega-chat-get chat-ids))
(if (> (length chat-ids) 0)
;; Redisplay the root's custom filters and then
;; Continue fetching chats
(let ((telega-filters--inhibit-redisplay nil))
(telega-filters--redisplay)
(telega--getChats))
;; All chats has been fetched
;; TODO: some chats remains with order="0", i.e. known chats, do
;; not proceed with chats that once was used, such as basic
;; groups upgraded to supergroups, closed secret chats, etc. We
;; might want to remove them from `telega--ordered-chats' list
;; for faster processing, but keep it in chats hash
(setq telega-filters--inhibit-redisplay nil)
(telega-filters--redisplay)
(telega-status--set nil "") ;reset aux status
(run-hooks 'telega-chats-fetched-hook))))
(defun telega--getChats ()
"Retreive all chats from the server in async manner."
;; Do not update filters on every chat fetched, update them at the end
(setq telega-filters--inhibit-redisplay t)
(let* ((last-chat (car (last telega--ordered-chats)))
(offset-order (or (plist-get last-chat :order) "9223372036854775807"))
(offset-chatid (or (plist-get last-chat :id) 0)))
(telega-server--call
(list :@type "getChats"
:offset_order offset-order
:offset_chat_id offset-chatid
:limit 1000)
#'telega-chat--on-getChats)))
(defun telega--getChatPinnedMessage (chat &optional callback)
"Get pinned message for the CHAT, if any."
(unless (zerop (plist-get chat :pinned_message_id))
(telega-server--call
(list :@type "getChatPinnedMessage"
:chat_id (plist-get chat :id))
callback)))
(defsubst telega-chats-list-get (tl-obj-chats)
"Return chats list of TL-OBJ-CHATS represeting `Chats' object."
(mapcar #'telega-chat-get (plist-get tl-obj-chats :chat_ids)))
(defun telega--getCreatedPublicChats ()
"Return list of public chats created by the user."
(telega-chats-list-get
(telega-server--call
(list :@type "getCreatedPublicChats"))))
(defun telega-chats--kill-em-all ()
"Kill all chat buffers."
(dolist (cbuf telega--chat-buffers)
(kill-buffer cbuf)))
(defun telega-chats-top (category)
"Return list of top chats used by CATEGORY.
CATEGORY is one of `Users', `Bots', `Groups', `Channels',
`InlineBots', `Calls'"
(let ((top (assq category telega--top-chats))
(currts (time-to-seconds (current-time))))
(when (> currts (+ (or (cadr top) 0) 60))
;; XXX update only if last fetch is older then 60 seconds
(let* ((cattype (list :@type (concat "topChatCategory"
(symbol-name category))))
(cl (telega-server--call
(list :@type "getTopChats"
:category cattype
:limit 30))))
(setq top (list (time-to-seconds (current-time))
(telega-chats-list-get cl)))
(setf (alist-get category telega--top-chats) top)))
(caddr top)))
(defun telega--sendChatAction (chat action)
"Send ACTION on CHAT."
(telega-server--send
(list :@type "sendChatAction"
:chat_id (plist-get chat :id)
:action action)))
(defun telega--createPrivateChat (user)
"Create private chat with USER.
Return newly created chat."
(telega-chat-get
(plist-get
(telega-server--call
(list :@type "createPrivateChat"
:user_id (plist-get user :id))) :id)))
(defun telega--viewMessages (chat messages &optional force)
"Mark CHAT's MESSAGES as read.
Use non-nil value for FORCE, if messages in closed chats should
be marked as read."
(telega-server--send
(list :@type "viewMessages"
:chat_id (plist-get chat :id)
:message_ids (cl-map 'vector (telega--tl-prop :id) messages)
:force_read (or force :false))))
(defun telega-chatbuf--view-visible-messages (window &optional display-start)
"View all visible messages in chatbuf's window."
(when (or (> (plist-get telega-chatbuf--chat :unread_count) 0)
(< (plist-get telega-chatbuf--chat :last_read_inbox_message_id)
(or (telega--tl-get telega-chatbuf--chat :last_message :id) 0)))
(telega--viewMessages
telega-chatbuf--chat
(telega-chatbuf--visible-messages window display-start))))
(defun telega--toggleChatIsPinned (chat)
"Toggle pin state of the CHAT."
(telega-server--send
(list :@type "toggleChatIsPinned"
:chat_id (plist-get chat :id)
:is_pinned (if (plist-get chat :is_pinned) :false t))))
(defun telega--toggleChatIsMarkedAsUnread (chat)
"Toggle marked as read state of the CHAT."
(telega-server--send
(list :@type "toggleChatIsMarkedAsUnread"
:chat_id (plist-get chat :id)
:is_marked_as_unread
(if (plist-get chat :is_marked_as_unread) :false t))))
(defun telega--readAllChatMentions (chat)
"Read all mentions in CHAT."
(telega-server--send
(list :@type "readAllChatMentions" :chat_id (plist-get chat :id))))
(defun telega--openChat (chat)
"Mark CHAT as opened."
(telega-server--send
(list :@type "openChat"
:chat_id (plist-get chat :id))))
(defun telega--closeChat (chat)
"Mark CHAT as closed."
(telega-server--send
(list :@type "closeChat"
:chat_id (plist-get chat :id))))
(defun telega--searchPublicChat (username &optional callback)
"Search public chat with USERNAME.
If CALLBACK is specified, call it with one argument - CHAT."
(declare (indent 1))
(let ((ret (telega-server--call
(list :@type "searchPublicChat"
:username username)
(when callback
(lambda (reply)
(when reply
(funcall
callback (telega-chat-get (plist-get reply :id)))))))))
(if callback
ret
(when ret
(telega-chat-get (plist-get ret :id))))))
(defun telega--searchPublicChats (query &optional callback)
"Search public chats by looking for specified QUERY.
Return nil if QUERY is less then 5 chars.
If CALLBACK is specified, then do async call and run CALLBACK
with list of chats received."
(unless (< (length query) 5)
(let ((ret (telega-server--call
(list :@type "searchPublicChats"
:query query)
(and callback
`(lambda (reply)
(funcall ',callback (telega-chats-list-get reply)))))))
(if callback
ret
(telega-chats-list-get ret)))))
(defun telega--searchChats (query &optional limit)
"Search already known chats by QUERY."
(telega-chats-list-get
(telega-server--call
(list :@type "searchChats"
:query query
:limit (or limit 200)))))
(defun telega--searchChatsOnServer (query &optional limit)
"Search already known chats on server by QUERY."
(telega-chats-list-get
(telega-server--call
(list :@type "searchChatsOnServer"
:query query
:limit (or limit 200)))))
(defun telega--getGroupsInCommon (with-user &optional limit)
"Return list of common chats WITH-USER.
LIMIT - number of chats to get (default=100)"
(telega-chats-list-get
(telega-server--call
(list :@type "getGroupsInCommon"
:user_id (plist-get with-user :id)
:offset_chat_id 0
:limit (or limit 100)))))
;;; Chat buttons in root buffer
(defvar telega-chat-button-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map button-map)
(define-key map (kbd "i") 'telega-describe-chat)
(define-key map (kbd "h") 'telega-describe-chat)
(define-key map (kbd "a") 'telega-chat-add-member)
(define-key map (kbd "o") 'telega-chat-custom-order)
(define-key map (kbd "r") 'telega-chat-toggle-read)
(define-key map (kbd "d") 'telega-chat-delete)
(define-key map (kbd "L") 'telega-chat-custom-label)
(define-key map (kbd "P") 'telega-chat-pin)
(define-key map (kbd "C") 'telega-chat-call)
(define-key map (kbd "DEL") 'telega-chat-delete)
map)
"The key map for telega chat buttons.")
(define-button-type 'telega-chat
:supertype 'telega
:inserter telega-inserter-for-chat-button
'keymap telega-chat-button-map
'action #'telega-chat-button--action)
(defun telega-chat-button--action (button)
"Action to take when chat BUTTON is pressed."
(telega-chat--pop-to-buffer (button-get button :value)))
(defsubst telega-chat--pp (chat)
"Pretty printer for CHAT button."
(telega-button--insert 'telega-chat chat)
(unless (= (char-before) ?\n)
(insert "\n")))
(defun telega-chat-known--pp (chat)
"Pretty printer for known CHAT button."
;; Insert only visible chat buttons
;; See https://github.com/zevlg/telega.el/issues/3
(let ((visible-p (and (telega-filter-chats nil (list chat))
(if telega-search-query
(memq chat telega--search-chats)
t))))
(when visible-p
(telega-chat--pp chat))))
(defun telega-chat-global--pp (chat)
"Display CHAT found in global public chats search."
(let* ((telega-chat-button-width (+ telega-chat-button-width
(/ telega-chat-button-width 2)))
(telega-filters--inhibit-list '(has-order))
(visible-p (telega-filter-chats nil (list chat))))
(when visible-p
(telega-chat--pp chat))))
(defun telega-chat--pop-to-buffer (chat)
"Pop to CHAT's buffer.
Uses `telega-chat--display-buffer-action' as action in `pop-to-buffer.'"
(pop-to-buffer (telega-chatbuf--get-create chat)
telega-chat--display-buffer-action))
(defun telega-chat-at-point ()
"Return current chat at point."
(let ((button (button-at (point))))
(when (and button (eq (button-type button) 'telega-chat))
(button-get button :value))))
(defun telega-chat-pin (chat)
"Toggle chat's pin state at point."
(interactive (list (telega-chat-at-point)))
(telega--toggleChatIsPinned chat))
(defun telega--addChatMembers (chat users)
"Add new members to the CHAT.
CHAT must be supergroup or channel."
(telega-server--send
(list :@type "addChatMembers"
:chat_id (plist-get chat :id)
:user_ids (cl-map 'vector (telega--tl-prop :id) users))))
(defun telega-chat-add-member (chat user &optional forward-limit)
"Add MEMBER to the CHAT."
(interactive (list (or telega-chatbuf--chat
(telega-chat-at-point))
(telega-completing-read-user "User: ")))
(cl-assert user)
(telega-server--send
(list :@type "addChatMember"
:chat_id (plist-get chat :id)
:user_id (plist-get user :id)
:forward_limit (or forward-limit 300))))
(defun telega-chat-set-title (chat title)
"Set CHAT's title to TITLE."
(interactive (let ((chat (or telega-chatbuf--chat
(telega-chat-at-point))))
(list chat
(read-string "New title: " (telega-chat-title chat)))))
(telega--setChatTitle chat title))
(defun telega-chat-custom-order (chat order)
"For the CHAT (un)set custom ORDER."
(interactive (let ((chat (telega-chat-at-point)))
(list chat
(read-string "Custom Order [empty to unset]: "
(telega-chat--order chat 'as-str)))))
(if (string-empty-p order)
(setq order nil)
(unless (numberp (read order))
(error "Invalid order, must contain only digits")))
(setf (telega-chat-uaprop chat :order) order)
(telega-chat--reorder chat nil)
(telega-root--chat-update chat))
(defun telega-chat-custom-label (chat label)
"For CHAT (un)set custom LABEL."
(interactive (let* ((chat (telega-chat-at-point))
(chat-label (telega-chat-uaprop chat :label)))
;; NOTE: If chat already has label, then use
;; `read-string' to change label, so empty name can
;; be used to unset the label
(list chat
(if chat-label
(read-string "Custom label [empty to unset]: "
chat-label)
(funcall telega-completing-read-function
"Custom label: " (telega-custom-labels))))))
(when (string-empty-p label)
(setq label nil))
(setf (telega-chat-uaprop chat :label) label)
(telega-root--chat-update chat))
(defun telega--setChatMemberStatus (chat user status)
"Change the STATUS of a CHAT USER, needs appropriate privileges.
STATUS is one of: "
(telega-server--send
(list :@type "setChatMemberStatus"
:chat_id (plist-get chat :id)
:user_id (plist-get user :id)
:status status)))
(defun telega-chat-call (chat)
"Call to the user associated with the given private CHAT."
(interactive (list (telega-chat-at-point)))
;; NOTE: If calling to secret chat, then use ordinary private chat
;; for calling
(when (telega-chat-secret-p chat)
(setq chat (telega-chat-get
(plist-get (telega-chat--info chat) :user_id))))
(unless (eq (telega-chat--type chat 'no-interpret) 'private)
(error "Can call only to users"))
(let* ((user (telega-chat--user chat))
(full-info (telega--full-info user)))
(when (plist-get full-info :has_private_calls)
(error "%s can't be called due to their privacy settings"
(telega-user--name user)))
(unless (plist-get full-info :can_be_called)
(error "%s can't be called" (telega-user--name user)))
(telega-voip-call user)))
(defun telega-chat-share-my-contact (chat)
"Share my contact info with CHAT."
(interactive (list telega-chatbuf--chat))
(when chat
(let* ((me (telega-user-me))
(me-contact
(list :@type "Contact"
:phone_number (concat "+" (plist-get me :phone_number))
:first_name (telega-tl-str me :first_name)
:last_name (telega-tl-str me :last_name)
:vcard ""
:user_id (plist-get me :id))))
(telega--sendMessage chat (list :@type "inputMessageContact"
:contact me-contact)))))
(defun telega-describe-chat (chat)
"Show info about chat at point."
(interactive (list (telega-chat-at-point)))
(with-telega-help-win "*Telegram Chat Info*"
(setq telega--chat chat)
(let ((chat-ava (telega-chat-avatar-image chat)))
(telega-ins--image chat-ava 0)
(telega-ins--with-face
(let ((color (telega-chat-color chat))
(lightp (eq (frame-parameter nil 'background-mode) 'light)))
(list :foreground (nth (if lightp 2 0) color)))
(telega-ins (telega-chat-title chat 'with-username)))
(telega-ins "\n")
(telega-ins--image chat-ava 1)
(telega-ins (capitalize (symbol-name (telega-chat--type chat))) " ")
(telega-ins--button "Open"
:value chat
:action 'telega-chat--pop-to-buffer)
(telega-ins "\n"))
(telega-ins-fmt "Id: %d\n" (plist-get chat :id))
(when (telega-chat-public-p chat)
(let ((link (concat (or (plist-get telega--options :t_me_url)
"https://t.me/")
(telega-tl-str (telega-chat--supergroup chat) :username))))
(insert "Link: ")
(apply 'insert-text-button link (telega-link-props 'url link 'link))
(insert "\n")))
(telega-ins "Order")
(when (telega-chat-uaprop chat :order)
(telega-ins " (" (propertize "custom" 'face 'shadow) ")"))
(telega-ins ": " (telega-chat--order chat 'as-str) "\n")
(let* (;; (default-mute-for
;; (telega-chat-notification-setting chat :mute_for 'default))
;; (default-show-preview
;; (telega-chat-notification-setting chat :show_preview 'default))
(not-cfg (plist-get chat :notification_settings))
(unmuted-p (not (telega-chat-muted-p chat)))
(show-preview (telega-chat-notification-setting chat :show_preview)))
(telega-ins "Notifications ("
(propertize (if (plist-get not-cfg :use_default_mute_for)
"default" "custom")
'face 'shadow)
"): ")
(telega-ins--button (if unmuted-p
telega-symbol-heavy-checkmark
" ")
:value chat
:action (lambda (chat)
(telega--setChatNotificationSettings chat
:use_default_mute_for nil
:mute_for (if unmuted-p 599634793 0))
(telega-save-cursor
(telega-describe-chat chat))))
(when unmuted-p
(telega-ins ", Preview ("
(propertize (if (plist-get not-cfg :use_default_show_preview)
"default" "custom")
'face 'shadow)
"): ")
(telega-ins--with-face 'telega-box
(telega-ins (if show-preview
telega-symbol-heavy-checkmark
" "))))
;; Reseting custom notification settings
(unless (and (plist-get not-cfg :use_default_mute_for)
(plist-get not-cfg :use_default_show_preview))
(telega-ins " ")
(telega-ins--button "Reset"
:value chat
:action (lambda (chat)
(telega--setChatNotificationSettings chat
:use_default_mute_for t
:use_default_show_preview t)
(telega-save-cursor
(telega-describe-chat chat)))))
(telega-ins "\n"))
(telega-ins "\n")
(cl-ecase (telega-chat--type chat 'no-interpret)
(private
(telega-ins "--- User Info ---\n")
(telega-info--insert-user
(telega-chat--info chat) chat
(lambda () (telega-describe-chat chat))))
(secret
(telega-ins "--- SecretChat Info ---\n")
(telega-info--insert-secretchat
(telega-chat--info chat) chat))
(basicgroup
(telega-ins "--- BasicGroup Info ---\n")
(telega-info--insert-basicgroup
(telega-chat--info chat) chat))
(supergroup
(telega-ins "--- SuperGroup Info ---\n")
(telega-info--insert-supergroup
(telega-chat--info chat) chat)))
(when telega-debug
(telega-ins "\n---DEBUG---\n")
(telega-ins (propertize "Chat: " 'face 'bold)
(format "%S" chat) "\n")
(telega-ins (propertize "Info: " 'face 'bold)
(format "%S" (telega-chat--info chat))))