-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathtelega-chat.el
4172 lines (3716 loc) · 174 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 <[email protected]>
;; 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/>.
;;; ellit-org: commentary
;;
;; Chatbuf is a Emacs buffer showing some Telegram chat. Chatbuf
;; consists of a list of chat messages and an input for your messages
;; to send. Press
;; {{{where-is(telega-describe-message,telega-msg-button-map)}}} to
;; get detailed description of the message at point.
;;; 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)
(require 'telega-i18n)
(require 'telega-tme)
(require 'telega-sort)
(require 'telega-filter)
(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))
;; telega-tdlib-events.el depends on telega-chat.el
(declare-function telega--on-updateDeleteMessages "telega-tdlib-events" (event))
(declare-function telega-chat--update "telega-tdlib-events" (chat &rest events))
;; telega-info.el depends on telega-chat.el
(declare-function telega--info "telega-info" (tlobj-type tlobj-id &optional locally-p))
(declare-function telega--full-info "telega-info" (tlobj &optional offline-p _callback))
;; telega-root.el depends on telega-chat.el
(declare-function telega--check-buffer-switch "telega-root")
;;; 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--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--history-state nil
"State of the history loading.
Could contain `older-loaded' or `newer-loaded' elements.")
(make-variable-buffer-local 'telega-chatbuf--history-state)
(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)
(defvar telega-chatbuf--refresh-point nil
"Non-nil if point needs refreshing on buffer switch in.
Could be either `t', then move point to next unread message.
Or point value to move point to.")
(make-variable-buffer-local 'telega-chatbuf--refresh-point)
(defvar telega-chatbuf--msg-filter nil
"Active messages filter in the chatbuf.
Plist with properties:
- `:title' displayed in chatbuf footer
- `:tdlib-msg-filter' Could be a function or list representing
TDLib SearchMessagesFilter.
- `:query' - query argument to `telega--searchChatMessages'
- `:sender' - sender argument to `telega--searchChatMessages'
- `:total-count' - Number of total messages found.")
(make-variable-buffer-local 'telega-chatbuf--msg-filter)
(defvar telega-chatbuf--thread-msg nil
"MSG that starts a thread in the chatbuf.")
(make-variable-buffer-local 'telega-chatbuf--thread-msg)
(defvar telega-chatbuf--thread-info nil
"Last thread info received by `telega--getMessageThread'.
Used to determine all older thread history has been loaded.")
(make-variable-buffer-local 'telega-chatbuf--thread-info)
(defvar telega-chatbuf--messages-compact-view nil
"Non-nil to use compact view for messages in chatbuf.")
(make-variable-buffer-local 'telega-chatbuf--messages-compact-view)
(defvar telega-chatbuf--inhibit-reset-filter-and-thread nil
"Bind this variable to inhibit
`telega-chatbuf--reset-filter-and-thread' on message goto.")
(defvar telega-chatbuf--messages-pop-ring nil
"History of messages jumps.
Used for `M-g x' command.")
(make-variable-buffer-local 'telega-chatbuf--messages-pop-ring)
;; 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 colors pair associated with CHAT.
If there is no CHAT color, then generate new and assign it to CHAT."
(let ((colors (telega-chat-uaprop chat :color)))
(when (= (length colors) 3)
;; NOTE: Colors from telega<0.6.12, regenerate them
(setq colors nil))
(or colors
(setf (telega-chat-uaprop chat :color)
(or (when-let ((cc (cl-find chat telega-rainbow-color-custom-for
:test #'telega-chat-match-p
:key #'car)))
(list (cdr cc) (cdr cc)))
(let ((cid (telega-chat-title chat " ")))
(list (funcall telega-rainbow-color-function cid 'light)
(funcall telega-rainbow-color-function cid 'dark))))))
))
(defsubst telega--ordered-chats-insert (chat)
"Insert CHAT into `telega--ordered-chats' according active sorter."
(let ((place telega--ordered-chats))
(if (or (null place)
(telega-chat> chat (car place)))
(setq telega--ordered-chats (push chat telega--ordered-chats))
(while (and (telega-chat> (car place) chat)
(cdr place)
(telega-chat> (cadr place) chat))
(setq place (cdr place)))
(cl-assert place)
(setcdr place (cons chat (cdr place))))
telega--ordered-chats))
(defun 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
(when-let ((client-data (telega-tl-str chat :client_data)))
(ignore-errors
(plist-put chat :uaprops (car (read-from-string client-data)))))
))))
(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 telega-server."
(let ((chat (gethash chat-id telega--chats)))
(when (and (not chat) (not offline-p))
(setq chat (telega--getChat 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 #'telega-chat-username))
(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--secretchat 'telega-chat--info)
(defalias 'telega-chat--basicgroup 'telega-chat--info)
(defalias 'telega-chat--supergroup 'telega-chat--info)
;;; ellit-org: chatbuf
;; ** Chat types
;;
;; Every chat has a type. Type is one of:
;; - private :: Private chat with telegram user
;; - secret :: Secret chat with telegram user
;; - bot :: Chat with telegram bot
;; - basicgroup :: Small chat group, could be upgraded to supergroup
;; - supergroup :: Chat group with all the chat possibilities
;; - channel :: Supergroup with unlimited members, where only admins can post messags
(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 'inc-bots)))
'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-channel-p (chat)
"Return non-nil if CHAT is channel."
(eq (telega-chat--type chat) 'channel))
(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-admin-get (chat user)
"Return \"chatAdministrator\" structure for the USER.
Return nil if USER not administrator in the CHAT.
Works only for chats with active chatbuffer and fetched
administrators list."
(with-telega-chatbuf chat
(cl-find (plist-get user :id) telega-chatbuf--administrators
:key (telega--tl-prop :user_id))))
(defun telega-chat-member-my-status (chat)
"Return my status as Chat Member Status for the CHAT.
Only available for basicgroup and supergroup (including channels)."
(when (memq (telega-chat--type chat 'raw) '(basicgroup supergroup))
(plist-get (telega-chat--info chat) :status)))
(defun telega-chat-member-my-permissions (chat)
"Return my member permissions in the CHAT."
(let ((perms (copy-sequence (cddr (plist-get chat :permissions)))))
(when-let ((status (telega-chat-member-my-status chat)))
(cl-case (telega--tl-type status)
((chatMemberStatusCreator chatMemberStatusAdministrator)
;; NOTE: Owner of the chat has all the admins privs except
;; for `:is_anonymous' which is set separately
(let ((owner-p (eq 'chatMemberStatusCreator (telega--tl-type status))))
(dolist (perm-spec telega-chat--admin-permissions)
(plist-put perms (car perm-spec)
(or owner-p (plist-get status (car perm-spec))))))
(plist-put perms :is_anonymous (plist-get status :is_anonymous)))
(chatMemberStatusRestricted
(setq perms (plist-get status :permissions)))))
perms))
(defun telega-chat-title (chat &optional with-username-delim)
"Return title for the CHAT.
If WITH-USERNAME-DELIM is specified, append username to the title
delimiting with WITH-USERNAME-DELIM."
(let* ((telega-emoji-use-images telega-chat-title-emoji-use-images)
(title (or (when (telega-me-p chat)
(telega-i18n "lng_saved_messages"))
(when (telega-replies-p chat)
(telega-i18n "lng_replies_messages"))
(telega-tl-str chat :title)
(progn
(cl-assert (telega-chat-user chat 'inc-bots))
(telega-user-title
(telega-chat-user chat 'inc-bots) 'name)))))
(when with-username-delim
(when-let ((username (telega-chat-username chat)))
(setq title (concat title (if (stringp with-username-delim)
with-username-delim
" ")
"@" username))))
(if-let ((cctfun (cdr (cl-find chat telega-chat-title-custom-for
:test #'telega-chat-match-p
:key #'car))))
(progn
(cl-assert (functionp cctfun))
(funcall cctfun title))
title)))
(defun telega-chat-brackets (chat)
"Return CHAT's brackets from `telega-chat-button-brackets'."
(cdr (seq-find (lambda (bspec)
(telega-chat-match-p chat (car bspec)))
telega-chat-button-brackets)))
(defun telega-chat-title-with-brackets (chat &optional with-username-delim)
"Return CHAT title surrounded with chat brackets."
(let ((brackets (telega-chat-brackets chat)))
(concat (or (car brackets) "[")
(telega-chat-title chat with-username-delim)
(or (cadr brackets) "]"))))
(defun telega-chat-reply-markup-msg (chat &optional callback)
"Return reply markup for the CHAT."
(declare (indent 1))
(let ((reply-markup-msg-id (plist-get chat :reply_markup_message_id)))
(unless (zerop reply-markup-msg-id)
(telega-msg-get chat reply-markup-msg-id callback))))
(defun telega-chatbuf--reply-markup-message-fetch ()
"Asynchronously load reply markup message for CHAT.
Pass non-nil OFFLINE-P argument to avoid any async requests."
(let* ((chat telega-chatbuf--chat)
(reply-markup-msg-id (plist-get chat :reply_markup_message_id)))
(if (zerop reply-markup-msg-id)
(telega-chatbuf--footer-update)
;; Async load reply markup message
(telega-chat-reply-markup-msg chat
(lambda (rm-message &optional offline-p)
(unless offline-p
(telega-msg-cache
(or rm-message
;; deleted message
(list :id reply-markup-msg-id
:chat_id (plist-get chat :id)
:telega-is-deleted-message t))))
(with-telega-chatbuf chat
(telega-chatbuf--footer-update)))))))
(defun telega-chatbuf--admins-fetch ()
"Asynchronously fetch and update `telega-chatbuf--administrators'."
(cl-assert telega-chatbuf--chat)
;; NOTE: fetch admins not frequent as one time in a minute, to avoid
;; admins fetch/supergroup full-info update loop, see
;; https://github.com/tdlib/td/issues/1284
;;
;; We store last update time in `admins' entry in
;; `telega-chatbuf--fetch-alist'
;;
;; Also, admin right is required to to get admins list in channels
(let ((chat telega-chatbuf--chat)
(last-fetch-time (or (alist-get 'admins telega-chatbuf--fetch-alist) 0))
(current-time (time-to-seconds)))
(when (and (> (- current-time last-fetch-time) 60)
(not (telega-chat-private-p chat 'inc-bots))
(not (telega-chat-secret-p chat))
(or (not (telega-chat-channel-p chat))
(telega-chat-match-p chat '(me-is-owner or-admin))))
(setf (alist-get 'admins telega-chatbuf--fetch-alist) current-time)
(telega--getChatAdministrators chat
(lambda (admins)
(with-telega-chatbuf chat
(setq telega-chatbuf--administrators admins)))))
))
(defun telega-chatbuf--pinned-messages-fetch ()
"Asynchronously fetch pinned messages for CHAT.
Pass non-nil OFFLINE-P argument to avoid any async requests.
OLD-PIN-MSG-ID is the id of the previously pinned message."
(let ((chat telega-chatbuf--chat))
(telega--searchChatMessages telega-chatbuf--chat
(list :@type "searchMessagesFilterPinned") "" 0 0 nil nil
(lambda (reply)
(let ((msgs (plist-get reply :messages)))
(plist-put chat :telega-pinned-messages (append msgs nil))
;; Possible clamp index of pinned message displayed in
;; modeline
(let ((pin-idx (plist-get chat :telega-pinned-message-index)))
(unless (and pin-idx (< pin-idx (length msgs)))
(plist-put chat :telega-pinned-message-index 0)))
(with-telega-chatbuf chat
(telega-chatbuf--modeline-update)))))))
(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', `ForwardChats'."
(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
(setq top (list (time-to-seconds (current-time))
(telega--getTopChats (symbol-name category))))
(setf (alist-get category telega--top-chats) top))
(caddr top)))
;;; 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-set-custom-order)
(define-key map (kbd "r") 'telega-chat-toggle-read)
(define-key map (kbd "d") 'telega-chat-delete)
(define-key map (kbd "P") 'telega-chat-toggle-pin)
(define-key map (kbd "^") 'telega-chat-toggle-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
:action #'telega-chat--pop-to-buffer
'keymap telega-chat-button-map)
(defun telega-chat--pop-to-buffer (chat &optional no-history-load)
"Pop to CHAT's buffer.
NO-HISTORY-LOAD passed directly to `telega-chatbuf--get-create'.
Uses `telega-chat--display-buffer-action' as action in `pop-to-buffer.'
Return chatbuf."
(prog1
(pop-to-buffer (telega-chatbuf--get-create chat no-history-load)
telega-chat--display-buffer-action)
;; Force switch-in for non-interactive buffer switching
(telega--check-buffer-switch)))
(defun telega-chat-toggle-pin (chat)
"Toggle chat's pin state at point."
(interactive (list (or telega-chatbuf--chat (telega-chat-at (point)))))
(telega--toggleChatIsPinned chat))
(defun telega-chat-add-member (chat user &optional forward-limit)
"Add USER to the CHAT."
(interactive (list (or telega-chatbuf--chat
telega--chat
(telega-chat-at (point)))
(telega-completing-read-user "Add member: ")))
(cl-assert user)
(telega--addChatMember chat user forward-limit))
(defun telega-chat-remove-member (chat user &optional _ban)
"Remove USER from the CHAT.
Specify non-nil BAN to ban this user in this CHAT."
(interactive
(let ((chat (or telega-chatbuf--chat
telega--chat
(telega-chat-at (point)))))
(list chat
(telega-completing-read-user
"Remove member: "
(telega--searchChatMembers chat ""))
current-prefix-arg)))
;; TODO: ban (chatMemberStatusBanned :banned_until_date)
(telega--setChatMemberStatus
chat user (list :@type "chatMemberStatusLeft")))
(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-set-ttl (chat ttl-seconds)
"Set TTL setting for secret CHAT to TTL-SECONDS."
(interactive
(let ((chat (or telega-chatbuf--chat (telega-chat-at (point)))))
(list chat (ceiling (read-number "TTL (seconds): ")))))
(telega--sendChatSetTtlMessage chat ttl-seconds))
(defun telega-chat-set-custom-order (chat order)
"For the CHAT (un)set custom ORDER."
(interactive
(let ((chat (or telega-chatbuf--chat (telega-chat-at (point)))))
(list chat (read-string "Custom Order [empty to unset]: "
(telega-chat-order chat)))))
(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)
;; NOTE: Update chat with fake event causing chat reorder
(telega-chat--update chat (list :@type "telegaChatReorder")))
(defun telega-chat-call (chat)
"Call to the user associated with the given private CHAT."
(interactive (list (or telega-chatbuf--chat (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 'inc-bots))
(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
(telega--sendMessage chat (list :@type "inputMessageContact"
:contact (telega-user-as-contact
(telega-user-me))))))
(defun telega-chat-unpin-all-messages (chat)
"Unpin all messages in the CHAT."
(interactive (list telega-chatbuf--chat))
(telega--unpinAllChatMessages chat))
(defun telega-describe-chat--inserter (chat)
"Inserter for the CHAT description."
(let ((chat-ava (telega-chat-avatar-image chat)))
(telega-ins--image chat-ava 0
:no-display-if (not telega-chat-show-avatars))
(telega-ins--with-face
(let* ((ccolors (telega-chat-color chat))
(lightp (eq (frame-parameter nil 'background-mode) 'light))
(foreground (nth (if lightp 0 1) ccolors)))
(when foreground
(list :foreground foreground)))
(telega-ins (telega-chat-title chat 'with-username)))
(when (telega-msg-sender-blocked-p chat)
(telega-ins--with-face 'error
(telega-ins " " telega-symbol-blocked "BLOCKED")))
(telega-ins "\n")
(telega-ins--image chat-ava 1
:no-display-if (not telega-chat-show-avatars))
(telega-ins (capitalize (symbol-name (telega-chat--type chat))) " ")
(telega-ins--button "Open"
:value chat
:action 'telega-chat--pop-to-buffer)
(when (telega-me-p chat)
(telega-ins " ")
(telega-ins--button "Set Profile Photo"
'action (lambda (_ignored)
(let ((photo (read-file-name "Profile Photo: " nil nil t)))
(telega--setProfilePhoto photo)))))
(when (telega--tl-get chat :permissions :can_invite_users)
(telega-ins " ")
(telega-ins--button "Add Member"
'action (lambda (_ignored)
(telega-chat-add-member
chat (telega-completing-read-user "Add member: ")))))
(when (telega--tl-get chat :permissions :can_change_info)
(telega-ins " ")
(telega-ins--button "Set Chat Photo"
'action (lambda (_ignored)
(let ((photo (read-file-name "Chat Photo: " nil nil t)))
(telega--setChatPhoto chat photo)))))
;; Archive/Unarchive
(telega-ins " ")
(telega-ins--button (if (telega-chat-match-p chat 'archive)
(telega-i18n "lng_archived_remove")
(telega-i18n "lng_archived_add"))
:value chat
:action #'telega-chat-toggle-archive)
(telega-ins "\n"))
(telega-ins-fmt "Id: %s\n"
(if telega-debug
(format "(telega-chat-get %d)" (plist-get chat :id))
(format "%d" (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-chat-username chat))))
(insert "Public Link: ")
(apply #'insert-text-button link (telega-link-props 'url link 'link))
(insert "\n")))
(telega-ins "Internal Link: ")
(let ((internal-link (telega-tme-internal-link-to chat)))
(apply 'insert-text-button internal-link
(telega-link-props 'url internal-link 'link)))
(telega-ins "\n")
(telega-ins "Order")
(when (telega-chat-uaprop chat :order)
(telega-ins " (" (propertize "custom" 'face 'shadow) ")"))
(telega-ins ": " (telega-chat-order chat) "\n")
(telega-ins "Default Disable Notification: ")
(telega-ins--button (if (plist-get chat :default_disable_notification)
telega-symbol-heavy-checkmark
telega-symbol-blank-button)
:value chat
:action (lambda (chat)
(telega--toggleChatDefaultDisableNotification
chat (not (plist-get chat :default_disable_notification)))))
(telega-ins "\n")
(telega-ins--help-message
(telega-ins "Used when you send a message to the chat.\n"
"Disables message notification on receiver side.\n"
"Use `C-c C-a "
(if (plist-get chat :default_disable_notification)
"enable-notification"
"disable-notification")
" RET' in chatbuf prompt to temporary "
(if (plist-get chat :default_disable_notification)
"enable"
"disable")
" notifications on receiver side at message send time."))
;; Chat notification settings
(let* ((notify-cfg (plist-get chat :notification_settings))
(muted-p (telega-chat-muted-p chat))
(show-preview-p (telega-chat-notification-setting
chat :show_preview))
(disable-pin-msg-p (telega-chat-notification-setting
chat :disable_pinned_message_notifications))
(disable-mentions-p (telega-chat-notification-setting
chat :disable_mention_notifications)))
(telega-ins
(propertize (telega-i18n "lng_settings_section_notify") 'face 'bold))
;; If any custom setting is enabled, then show [Reset] button
(unless (cl-every (apply-partially #'plist-get notify-cfg)
'(:use_default_mute_for
:use_default_sound
:use_default_show_preview
:use_default_disable_pinned_message_notifications
:use_default_disable_mention_notifications))
(telega-ins " ")
(telega-ins--button "Reset"
:value chat
:action (lambda (chat)
(telega--setChatNotificationSettings chat
:use_default_mute_for t
:use_default_sound t
:use_default_show_preview t
:use_default_disable_pinned_message_notifications t
:use_default_disable_mention_notifications t))))
(telega-ins "\n")
(telega-ins--labeled " " nil
(telega-ins--button (if (not muted-p)
telega-symbol-heavy-checkmark
telega-symbol-blank-button)
:value chat
:action #'telega-chat-toggle-muted)
(telega-ins " ")
(telega-ins-fmt "Enabled (%s)"
(propertize (if (plist-get notify-cfg :use_default_mute_for)
"default" "custom")
'face 'shadow))
(unless muted-p
(telega-ins " ")
(telega-ins--button "Mute For"
:value chat
:action (lambda (chat)
(telega-chat-toggle-muted
chat (telega-completing-read-mute-for
"Disable notifications for: ")))))
(telega-ins "\n")
;; Show Preview
(telega-ins--button (if show-preview-p
telega-symbol-heavy-checkmark
telega-symbol-blank-button)
:value chat
:action (lambda (chat)
(telega--setChatNotificationSettings chat
:use_default_show_preview nil
:show_preview (if show-preview-p :false t))))
(telega-ins " ")
(telega-ins-fmt "Show Preview (%s)"
(propertize (if (plist-get notify-cfg :use_default_show_preview)
"default" "custom")
'face 'shadow))
(telega-ins "\n")
(telega-ins--button (if disable-pin-msg-p
telega-symbol-heavy-checkmark
telega-symbol-blank-button)
:value chat
:action (lambda (chat)
(telega--setChatNotificationSettings chat
:use_default_disable_pinned_message_notifications nil
:disable_pinned_message_notifications
(if disable-pin-msg-p :false t))))
(telega-ins " ")
(telega-ins-fmt "Disable Pinned Message Notification (%s)"
(propertize (if (plist-get notify-cfg :use_default_disable_pinned_message_notifications)
"default" "custom")
'face 'shadow))
(telega-ins "\n")
(telega-ins--button (if disable-mentions-p
telega-symbol-heavy-checkmark
telega-symbol-blank-button)
:value chat
:action (lambda (chat)
(telega--setChatNotificationSettings chat
:use_default_disable_mention_notifications nil
:disable_mention_notifications
(if disable-mentions-p :false t))))
(telega-ins " ")
(telega-ins-fmt "Disable Mention Notification (%s)"
(propertize (if (plist-get notify-cfg :use_default_disable_mention_notifications)
"default" "custom")
'face 'shadow))
(telega-ins "\n")
))
;; Permissions for basicgroup and supergroup
(when (telega-chat-match-p chat '(type basicgroup supergroup))
(let ((my-perms (telega-chat-member-my-permissions chat)))
(telega-ins
(propertize (telega-i18n "lng_manage_peer_permissions") 'face 'bold) "\n")
(telega-ins
(propertize (telega-i18n "lng_rights_default_restrictions_header")
'face 'shadow) "\n")
(telega-ins--labeled " " nil
(dolist (perm-spec telega-chat--chat-permisions)
(let ((perm-value (telega--tl-get chat :permissions (car perm-spec))))
(if (plist-get my-perms :can_restrict_members)
(telega-ins--button (if perm-value
telega-symbol-heavy-checkmark
telega-symbol-blank-button)
:value chat
:action (lambda (chat)
(telega--setChatPermissions chat
(car perm-spec) (not perm-value))))
(telega-ins (if perm-value
telega-symbol-ballout-check
telega-symbol-ballout-empty)))
(telega-ins " " (telega-i18n (cdr perm-spec)))
(telega-ins "\n"))))))
(telega-ins "\n")
(let ((info-spec
(assq (telega-chat--type chat 'no-interpret)
'((private "User" telega-info--insert-user)
(secret "SecretChat" telega-info--insert-secretchat)
(basicgroup "BasicGroup" telega-info--insert-basicgroup)
(supergroup "SuperGroup" telega-info--insert-supergroup)))))
(cl-assert info-spec)
(telega-ins--with-face 'bold
(telega-ins (nth 1 info-spec)))
(telega-ins "\n")
(funcall (nth 2 info-spec) (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))))
)
(defun telega-describe-chat (chat)
"Show info about chat at point."
(interactive (list (or telega-chatbuf--chat (telega-chat-at (point)))))
(with-telega-help-win "*Telegram Chat Info*"
(setq telega--chat chat)
(telega-describe-chat--inserter chat)
(setq telega--help-win-param chat)
(setq telega--help-win-inserter #'telega-describe-chat--inserter)
))
(defun telega-describe-chat--maybe-redisplay (chat)
"If CHAT info buffer exists and visible, then redisplay it."
(telega-help-win--maybe-redisplay "*Telegram Chat Info*" chat))
(defun telega-chat-with (chat-or-user)
"Start messaging with CHAT-OR-USER."
(interactive
(let* ((completion-ignore-case t)
(completions (telega-completing-titles))
(title (funcall telega-completing-read-function
"Chat with: " (mapcar #'car completions) nil t)))
(list (cdr (assoc title completions)))))
(when (telega-user-p chat-or-user)
(setq chat-or-user (or (telega-chat-get (plist-get chat-or-user :id))
(telega--createPrivateChat chat-or-user))))
(telega-chat--pop-to-buffer chat-or-user))
(defun telega-chat-join-by-link (link)
"Join chat by invitation LINK."
(interactive "sJoin chat by invite link: ")
(telega-chat--pop-to-buffer (telega--joinChatByInviteLink link)))
(defun telega-chat-toggle-muted (chat &optional muted-for)
"Toggle mute for the CHAT.
If MUTED-FOR is specified, set it as `:mute_for' notification setting."
(interactive (list (or telega-chatbuf--chat (telega-chat-at (point)))))
(telega--setChatNotificationSettings chat
:use_default_mute_for nil
:mute_for (or muted-for (if (telega-chat-muted-p chat)
0
telega-mute-for-ever))))
(defun telega-chat-toggle-archive (chat)
"Archive or Unarchive CHAT."
(interactive (list (or telega-chatbuf--chat (telega-chat-at (point)))))
(telega--addChatToList
chat (list :@type (if (telega-chat-match-p chat 'archive)
"chatListMain"
"chatListArchive"))))
(defun telega-chat-toggle-read (chat)
"Toggle chat as read/unread."
(interactive (list (or telega-chatbuf--chat (telega-chat-at (point)))))
(let ((unread-count (plist-get chat :unread_count))
(unread-mentions-count (plist-get chat :unread_mention_count))
(marked-unread-p (plist-get chat :is_marked_as_unread)))
(if (or (> unread-count 0) (> unread-mentions-count 0) marked-unread-p)
(progn
;; Toggle chat as readed
(when marked-unread-p
(telega--toggleChatIsMarkedAsUnread chat))
(when (> unread-count 0)
(telega--viewMessages
chat (list (plist-get chat :last_message)) 'force))
;; NOTE: reading messages can change mentions count, so
;; force all mentions are read
(telega--readAllChatMentions chat))
;; Toggle chat is unread
(unless marked-unread-p
(telega--toggleChatIsMarkedAsUnread chat))
)))
(defun telega-chats-filtered-kill-chatbuf ()
"Kill chatbuf for all filtered chats."
(interactive)
(let ((filtered-chatbufs
(cl-remove-if-not
(lambda (buf)
(telega-filter-chats (list (telega-chatbuf--chat buf))))
(telega-chat-buffers))))
(unless filtered-chatbufs
(user-error "No chats with chatbuf to kill"))
(when (and (y-or-n-p (telega-i18n "telega_query_kill_chatbufs"
:count (length filtered-chatbufs)))
;; NOTE: If no filter is applied, ask once more time
(or (not (telega-filter-default-p))
(y-or-n-p (telega-i18n "telega_query_kill_anyway"))))
(dolist (buf filtered-chatbufs)
(kill-buffer buf)))))
(defun telega-chats-filtered-toggle-read (&optional _force)
"Apply `telega-chat-toggle-read' to all currently filtered chats."
(interactive
(list (y-or-n-p (telega-i18n "telega_query_read_chats"
:count (length telega--filtered-chats)))))
;; NOTE: If no filter is applied, ask once more time
(when (or (not (telega-filter-default-p))
(y-or-n-p (telega-i18n "telega_query_read_anyway")))
(mapc 'telega-chat-toggle-read telega--filtered-chats)))
(defun telega-chat-leave (chat &optional keep-chatbuf)
"Leave the CHAT."
(interactive
(list (or telega-chatbuf--chat (telega-chat-at (point))) nil))
(cl-case (telega-chat--type chat)
(secret (telega--closeSecretChat (telega-chat--info chat)))
((private bot) 'no-op)
(t (telega--leaveChat chat)))
;; Kill corresponding chat buffer
(unless keep-chatbuf
(with-telega-chatbuf chat
(kill-buffer (current-buffer)))))
(defun telega-chat-delete (chat)
"Delete CHAT.
Query about everything. Leaving CHAT, blocking corresponding
user and delete all the chat history.
Use `telega-chat-leave' to just leave the CHAT."
(interactive (list (or telega-chatbuf--chat (telega-chat-at (point)))))
(when (and chat
(yes-or-no-p
(concat (telega-i18n "telega_action_cant_undone") ".\n"
(telega-i18n "telega_query_delete_chat"
:title (telega-chat-title chat)))))
(telega-chat-leave chat 'keep-chatbuf)
(setq telega-deleted-chats
(cl-pushnew chat telega-deleted-chats))
;; Block corresponding user, so he could not initiate any incoming
;; messages
(when (and (telega-chat-private-p chat 'include-bots)
(not (plist-get chat :is_blocked)))
(when (yes-or-no-p
(concat (telega-i18n "lng_blocked_list_confirm_text"
:name (telega-chat-title chat)) " "))