forked from zevlg/telega.el
-
Notifications
You must be signed in to change notification settings - Fork 0
/
telega-chat.el
7169 lines (6405 loc) · 308 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.
;;
;; ~visual-line-mode~ and ~visual-fill-column-mode~ are enabled by
;; default in chat buffers, to word-wrap and fill message content. You
;; might want to tune ~visual-fill-column-extra-text-width~ custom
;; option if message's header does not fit into
;; ~telega-chat-fill-column~ for some reason.
;;
;; Avoid setting ~truncate-lines~ to non-nil value in the chatbufs
;; (and using modes that does so), unless you know what you are doing,
;; you will get confusing results.
;;
;; Note for
;; [[https://en.wikipedia.org/wiki/Right-to-left_script][RTL]] users:
;; unlike rootbuf, chatbufs disables bidirectional display reordering
;; by default, so RTL text will look reversed in chatbufs. To enable
;; bidi in chatbufs customize your
;; ~telega-chat-bidi-display-reordering~ user option.
;;
;; Important customizable options:
;; - {{{user-option(telega-chat-fill-column, 2)}}}
;; - {{{user-option(telega-chat-use-date-breaks, 2)}}}
;;; Code:
(require 'cl-lib)
(require 'subr-x)
(require 'ring)
(require 'url-util)
(require 'seq)
(require 'dired) ; dired-get-marked-files
(require 'mailcap) ; mailcap-extension-to-mime
(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)
(require 'telega-modes)
(require 'visual-fill-column)
;; shutup compiler
(defvar company-backends)
(declare-function company-complete "company")
(declare-function company-begin-backend "company" (backend &optional callback))
(declare-function company-call-backend "company" (&rest args))
;; 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 _callback))
;; telega-root.el depends on telega-chat.el
(declare-function telega--check-buffer-switch "telega-root")
(declare-function telega-root--keep-cursor-at-chat "telega-root" (chat))
(declare-function telega-root--global-chat-pp "telega-root" (chat))
;;; 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--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--vvnote-msg nil
"Active (playing/paused) voice/video note message for the chatbuf.")
(make-variable-buffer-local 'telega-chatbuf--vvnote-msg)
(defvar telega-chatbuf--my-action nil
"My current action in chat buffer.")
(make-variable-buffer-local 'telega-chatbuf--my-action)
(defvar telega-chatbuf--msg-filter nil
"Active messages filter in the chatbuf.
Plist with properties:
- `:title' displayed in chatbuf footer. Might be a function returning
string.
- `:tdlib-msg-filter' Could be a function or list representing
TDLib SearchMessagesFilter.
- `:msg-temex' - Custom message temex to match against filter.
- `:saved-messages-topic-id' Topic id in the Saved Messages
- `:saved-messages-tag' Tag в Saved Messages
- `:query' - query argument to `telega--searchChatMessages'
- `:sender' - sender argument to `telega--searchChatMessages'
- `:msg-position' - Message's at point position across filtered messages.
- `:total-count' - Number of total messages found.")
(make-variable-buffer-local 'telega-chatbuf--msg-filter)
(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--messages-pop-ring nil
"History of messages jumps.
Used for `M-g x' command.
Bind it to nil to avoid pushing message to the ring when jumping
across messages.")
(make-variable-buffer-local 'telega-chatbuf--messages-pop-ring)
(defun telega-chatbuf--messages-pop-ring-last-p (msg)
"Return non-nil if most recently inserted message is not a MSG.
To avoid multiple same messages in the pop ring."
(and (not (ring-empty-p telega-chatbuf--messages-pop-ring))
(telega-msg-id= (ring-ref telega-chatbuf--messages-pop-ring 0)
msg)))
;; 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--ordered-chats-insert (chat)
"Insert CHAT into `telega--ordered-chats' according active sorter."
(if (or (null telega--ordered-chats)
(telega-chat> chat (car telega--ordered-chats)))
;; Insert at the head ?
(setq telega--ordered-chats (cons chat telega--ordered-chats))
;; NOTE: Binary search the place where to insert, starting from
;; the very last item in the `telega--ordered-chats'
(cl-assert (not (null telega--ordered-chats)))
(let ((idx (1- (length telega--ordered-chats)))
(place (last telega--ordered-chats 1)))
(unless (telega-chat> (car place) chat)
;; Need binary search
(setq place telega--ordered-chats)
(while (not (zerop idx))
(let* ((middle-idx (/ idx 2))
(middle-place (nthcdr middle-idx place)))
(when (telega-chat> (car middle-place) chat)
(setq place middle-place))
(setq idx middle-idx))))
(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)
;; 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)))))
;; Place chat in correct place inside `telega--ordered-chats'
;; NOTE: `:uaprops' might have order for the chat, thats why
;; we put it in ordered list *after* constructing `:uaprops'
(telega--ordered-chats-insert chat)
))))
(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 &optional locally-p)
"Return info structure for the CHAT.
It could be user, secretChat, basicGroup or supergroup.
If LOCALLY-P is non-nil, then return nil if chat info is not available
without request to the server."
(let ((chat-type (plist-get chat :type)))
(cl-ecase (telega--tl-type chat-type)
(chatTypePrivate
(telega--info 'user (plist-get chat-type :user_id) locally-p))
(chatTypeSecret
(telega--info 'secretChat (plist-get chat-type :secret_chat_id) locally-p))
(chatTypeBasicGroup
(telega--info 'basicGroup (plist-get chat-type :basic_group_id) locally-p))
(chatTypeSupergroup
(telega--info 'supergroup (plist-get chat-type :supergroup_id) locally-p)))))
(defalias 'telega-chat--secretchat 'telega-chat--info)
(defalias 'telega-chat--basicgroup 'telega-chat--info)
(defalias 'telega-chat--supergroup 'telega-chat--info)
(defun telega-chat--supergroup-locally (chat)
(telega-chat--supergroup chat 'locally))
(defun telega-chat--type (chat)
"Return type of the CHAT.
Types are: `private', `secret', `bot', `basicgroup', `supergroup' or `channel'."
(or (plist-get chat :telega-chat-type)
(let* ((chat-type (plist-get chat :type))
(type-sym (pcase (plist-get chat-type :@type)
("chatTypePrivate" 'private)
("chatTypeBasicGroup" 'basicgroup)
("chatTypeSupergroup" 'supergroup)
("chatTypeSecret" 'secret)))
(tc-type (cond ((and (eq type-sym 'supergroup)
(plist-get chat-type :is_channel))
'channel)
((and (eq type-sym 'private)
(telega-user-bot-p (telega-chat-user chat)))
'bot)
(t type-sym))))
;; NOTE: chat type won't change, so we can cache calculated
;; chat type
(plist-put chat :telega-chat-type tc-type)
tc-type)))
(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)
"Return non-nil if CHAT is private.
Chats with bots are also considered private, use `telega-chat-bot-p'
to check chat with bots."
(memq (telega-chat--type chat) '(private bot)))
(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) 'secret))
(defun telega-chat-muted-p (chat)
"Return non-nil if CHAT is muted."
(telega-chat-match-p chat 'muted))
(defun telega-chat-user (chat)
"For private CHAT return corresponding user."
(when-let ((user-id (telega--tl-get chat :type :user_id)))
(telega-user-get user-id)))
(defun telega-chat-admin-get (chat user)
"Return \"chatAdministrator\" TDLib object 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) '(basicgroup supergroup channel))
(plist-get (telega-chat--info chat) :status)))
(defun telega-chat-member-my-permissions (chat)
"Return my member permissions in the CHAT.
Combines chat permissions and admin/owner permissions."
(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
;; NOTE: Owner of the chat has all the admins privs except
;; for `:is_anonymous' which is set separately
(dolist (perm-spec telega-chat--admin-permissions)
(plist-put perms (car perm-spec) t))
(plist-put perms :is_anonymous (plist-get status :is_anonymous)))
(chatMemberStatusAdministrator
(telega--tl-dolist ((pname pval) (plist-get status :rights))
(plist-put perms pname pval)))
(chatMemberStatusRestricted
(setq perms (plist-get status :permissions)))
;; NOTE: In comments group me might be non-member, but still
;; able to send messages
;; (chatMemberStatusLeft
;; (setq perms nil))
))
perms))
(defun telega-chat-title (chat &optional no-badges)
"Return title for the CHAT.
If NO-BADGES is specified, then do not attach any chat badges at the
end of the title."
(let* ((info (telega-chat--info chat 'offline))
(raw-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)))
(chat-user (unless raw-title (telega-chat-user chat)))
(title0 (if chat-user
(telega-user-title chat-user 'full-name 'no-badges)
;; NOTE: Channels we are banned in can have empty title
(or raw-title (format "CHAT-%d" (plist-get chat :id)))))
(title
(if no-badges
title0
(concat title0
;; Badges
(cond ((plist-get chat :emoji_status)
(telega-ins--as-string
(telega-ins--chat-emoji-status chat)))
((plist-get info :is_premium)
(telega-symbol 'premium)))
(when (plist-get info :is_scam)
(propertize (telega-i18n "lng_scam_badge") 'face 'error))
(when (plist-get info :is_fake)
(propertize (telega-i18n "lng_fake_badge") 'face 'error))
(when (plist-get info :is_verified)
(telega-symbol 'verified))
(when (telega-chat-match-p chat 'is-blocked)
(telega-symbol 'blocked))
))))
(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-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))
(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 chatbuf."
(let ((chat telega-chatbuf--chat))
(telega--searchChatMessages telega-chatbuf--chat
'(:@type "searchMessagesFilterPinned") 0 0
:callback
(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--chat-update "pinned-messages")))))))
(defun telega-chatbuf--active-stories-fetch ()
"Asynchronously fetch active stories for the chatbuf."
(when (and (telega-chatbuf-match-p 'has-active-stories)
(telega-chatbuf-match-p telega-story-show-active-stories-for)
(not (telega-chat--active-stories telega-chatbuf--chat)))
(let ((chat telega-chatbuf--chat))
(telega--getChatActiveStories chat
(lambda (active-stories)
(setf (telega-chat--active-stories chat) active-stories)
(with-telega-chatbuf chat
(telega-chatbuf--chat-update "active-stories")))))))
(defun telega-chatbuf--pinned-stories-fetch ()
"Asynchronously fetch pinned stories for the chatbuf."
(when (telega-chatbuf-match-p telega-story-show-pinned-stories-for)
(cond ((telega-chatbuf-match-p 'has-pinned-stories)
(telega--getChatPostedToChatPageStories telega-chatbuf--chat nil nil
(lambda-with-current-buffer (stories)
(let* ((pinned-ids (plist-get stories :pinned_story_ids))
(pinned-stories
(seq-filter (lambda (story)
(seq-contains-p
pinned-ids (plist-get story :id)))
(plist-get stories :stories))))
(plist-put telega-chatbuf--chat :telega-pinned-stories
pinned-stories)
(when pinned-stories
(telega-chatbuf--chat-update "pinned-stories"))))))
((plist-get telega-chatbuf--chat :telega-pinned-stories)
(plist-put telega-chatbuf--chat :telega-pinned-stories nil)
(telega-chatbuf--chat-update "pinned-stories")))))
(defun telega-chatbuf--sponsored-messages-fetch ()
"Asynchronously fetch sponsored messages for the chatbuf."
(let* ((chat telega-chatbuf--chat)
(tsm-orig (plist-get chat :telega-sponsored-messages)))
(plist-put chat :telega-sponsored-messages nil)
(when (telega-chat-match-p chat '(type channel))
(telega--getChatSponsoredMessages telega-chatbuf--chat
(lambda (reply)
(plist-put chat :telega-sponsored-messages reply)
(unless (equal tsm-orig reply)
(with-telega-chatbuf chat
(telega-chatbuf--chat-update "sponsored-messages"))))))))
(defun telega-chat-group-call (&optional chat)
"Return group call for the chatbuf's voice chat.
If CHAT is specified, return group call for the CHAT."
(telega-group-call-get (telega--tl-get (or chat telega-chatbuf--chat)
:video_chat :group_call_id)))
(defun telega-chatbuf--video-chat-fetch ()
"Asynchronously fetch video chat state for the chatbuf."
(let* ((chat telega-chatbuf--chat)
(group-call-id (telega--tl-get chat :video_chat :group_call_id)))
(if (zerop group-call-id)
(telega-chatbuf--chat-update "group-call")
(telega-group-call-get group-call-id
(lambda (_group-call_ignored)
(with-telega-chatbuf chat
(telega-chatbuf--chat-update "group-call")))))))
(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-menu-map
(let ((menu-map (make-sparse-keymap "Telega Chat")))
(bindings--define-key menu-map [similar-chats]
'(menu-item (telega-i18n "lng_similar_channels_title")
telega-view-similar-channels
:help "Show similar channels"
:visible (telega-chat-match-p (telega-chat-at-down-mouse-3)
'(type channel))))
(bindings--define-key menu-map [s1] menu-bar-separator)
(bindings--define-key menu-map [describe]
'(menu-item (telega-i18n "lng_info_about_label")
telega-describe-chat
:help "Describe the message"))
;; TODO: more items
menu-map))
(defvar telega-chat-button-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map button-map)
(define-key map [remap self-insert-command] #'undefined)
(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 "DEL") 'telega-chat-delete)
(define-key map (kbd "TAB") 'telega-chat-button-toggle-view)
;; Menu for right mouse on a chat
(define-key map [down-mouse-3] telega-chat-button-menu-map)
(define-key map [mouse-3] #'ignore)
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 is 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-message-ttl (chat)
"Interactively set CHAT's message TTL setting."
(interactive (list (or telega-chatbuf--chat (telega-chat-at (point)))))
(let* ((ttl-table `((,(telega-i18n "lng_ttl_about_duration1") . 86400)
(,(telega-i18n "lng_ttl_about_duration2") . 604800)
(,(telega-i18n "lng_ttl_about_duration3") . 2678400)
(,(telega-i18n "lng_settings_ttl_after_off") . 0)
(,(telega-i18n "lng_settings_ttl_after_custom") . -1)))
(title (concat (telega-chat-title chat) " "
(telega-i18n "lng_settings_ttl_title")))
(ttl (if (telega-chat-secret-p chat)
(ceiling (read-number
(concat title
" (" (substring (telega-i18n "lng_seconds"
:count 2)
2 ))))
(cdr (assoc (completing-read (concat title ": ")
(mapcar #'car ttl-table) nil t)
ttl-table)))))
(when (< ttl 0)
;; Custom message ttl setting, see
;; `telega--setChatMessageAutoDeleteTime' for ttl value
;; restrictions
(setq ttl (* 86400 (read-number
(concat title " (0-"
(telega-i18n "lng_settings_ttl_after_days"
:count 365)
"): ")))))
(telega--setChatMessageAutoDeleteTime chat ttl)))
(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 forcing reorder
(telega-chat--update chat 'reorder))
(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 (and (telega-chat-private-p chat)
(not (telega-chat-bot-p chat)))
(error "Can call only to users"))
(let* ((telega-full-info-offline-p nil)
(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-msg-sender-title user
:with-avatar-p t
:with-username-p t)))
(unless (plist-get full-info :can_be_called)
(error "%s can't be called"
(telega-msg-sender-title user
:with-avatar-p t
:with-username-p t)))
(telega-voip-call user)))
(defun telega-chat-share-my-contact (chat)
"Share my contact info with CHAT."
(interactive (list telega-chatbuf--chat))
(unless chat
(user-error "`telega-chat-share-my-contact' available only in chatbuf"))
(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-msg-sender-avatar-image-three-lines chat)))
(telega-ins--image chat-ava 0
:no-display-if (not telega-chat-show-avatars))
(telega-ins--msg-sender chat :with-username-p 'telega-username)
(when (telega-chat-match-p chat 'is-blocked)
(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--with-face 'telega-shadow
(telega-ins (if (telega-chat-match-p chat 'is-broadcast-group)
(telega-i18n "lng_rights_gigagroup_title")
(capitalize (symbol-name (telega-chat--type chat))))))
(telega-ins "\n")
(telega-ins--image chat-ava 2
:no-display-if (not telega-chat-show-avatars))
(telega-ins--box-button (telega-i18n "lng_open_link")
:value chat
:action #'telega-chat--pop-to-buffer)
(when (telega-me-p chat)
(telega-ins " ")
;; [Set Profile Photo] button
(telega-ins--box-button (telega-i18n "lng_settings_upload")
'action (lambda (_button)
(telega--setProfilePhoto
(telega-read-file-name "Profile Photo: " nil nil t)))))
(when (telega-chat-match-p chat '(my-permission :can_invite_users))
(telega-ins " ")
(telega-ins--box-button (telega-i18n "lng_profile_add_participant")
'action (lambda (_button)
(call-interactively #'telega-chat-add-member))))
(when (telega-chat-match-p chat '(my-permission :can_change_info))
(telega-ins " ")
(telega-ins--box-button (telega-i18n "lng_profile_set_group_photo")
:value chat
:action (lambda (for-chat)
(let ((photo (telega-read-file-name "Chat Photo: " nil nil t)))
;; NOTE: create a local copy of the file in case
;; docker is used, see https://t.me/emacs_telega/42209
(telega--setChatPhoto
for-chat (telega-file-local-copy photo))))))
;; Archive/Unarchive
(telega-ins " ")
(telega-ins--box-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"))
(when (telega-chat-match-p chat 'is-broadcast-group)
(telega-ins--help-message
(telega-ins-i18n "lng_rights_gigagroup_about")))
(telega-ins "\n")
(telega-ins-describe-item "Id"
(telega-ins-fmt (if telega-debug
"(telega-chat-get %d)"
"%d")
(plist-get chat :id)))
(telega-ins-describe-item
(concat "Order"
(when (telega-chat-uaprop chat :order)
(concat " (" (propertize "custom" 'face 'telega-shadow) ")")))
(telega-ins (telega-chat-order chat)))
;; Stories
(when-let* ((active-stories
(telega-chat--active-stories chat))
(stories (plist-get active-stories :stories)))
(unless (seq-empty-p stories)
(telega-ins-describe-item (telega-i18n "lng_stories_row_count"
:count (seq-length stories))
(seq-doseq (story-info stories)
(if-let* ((chat-id (plist-get chat :id))
(story-id (plist-get story-info :story_id))
(story (telega-story-get chat-id story-id 'offline)))
(telega-ins--button-story-one-line-no-caption story)
;; NOTE: need to fetch story structure to display it
(telega-help-win--add-tdlib-callback
(telega--getStory chat-id story-id nil
(telega--gen-ins-continuation-callback nil
(lambda (story)
(telega-ins--button-story-one-line-no-caption story))))))
(telega-ins " ")))))
(when (telega-chat-match-p chat 'is-public)
(telega-ins-describe-item (telega-i18n "lng_group_invite_permanent")
(let ((link (concat (or (plist-get telega--options :t_me_url)
"https://t.me/")
(telega-chat-username chat))))
(telega-ins--raw-button (telega-link-props 'url link 'face 'link)
(telega-ins link)))))
(telega-ins-describe-item "Telega Link"
(let ((internal-link (telega-tme-internal-link-to chat)))
(telega-ins--raw-button (telega-link-props 'url internal-link 'face 'link)
(telega-ins internal-link))))
(when-let ((folders (telega-chat-folders chat)))
(telega-ins-describe-item (telega-i18n "lng_filters_title")
(telega-ins (mapconcat #'identity folders ", "))))
;; Messages TTL setting for the chat
(let ((ttl (or (telega-chat-match-p chat 'has-message-ttl) 0))
(can-change-ttl-p (telega-chat-match-p chat
'(or (type private secret)
(my-permission :can_change_info)))))
(when (or (> ttl 0) can-change-ttl-p)
(telega-ins-describe-item (telega-i18n "lng_settings_ttl_title")
(telega-ins (if (> ttl 0)
(telega-duration-human-readable ttl 2)
(telega-i18n "lng_manage_messages_ttl_never")))
(when can-change-ttl-p
(telega-ins " ")
(telega-ins--box-button
(telega-i18n "lng_manage_messages_ttl_after_custom")
:value chat
:action #'telega-chat-set-message-ttl))
(telega-ins "\n")
(telega-ins--help-message
(cond ((telega-chat-match-p chat '(type private secret))
(telega-ins-i18n "lng_ttl_edit_about"
:user (telega-chat-title chat)))
((telega-chat-match-p chat '(type channel))
(telega-ins-i18n "lng_ttl_edit_about_channel"))
(t
(telega-ins-i18n "lng_ttl_edit_about_group")))
;; NOTE: No trailing newline
nil))))
;; NOTIFICATIONS Section
(telega-ins-describe-item (telega-i18n "lng_settings_section_notify")
(let ((notify-cfg (plist-get chat :notification_settings)))
;; 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_mute_stories
:use_default_story_sound
:use_default_disable_pinned_message_notifications
:use_default_disable_mention_notifications))
(telega-ins--with-face 'telega-shadow
(telega-ins "custom "))
(telega-ins--box-button (telega-i18n "lng_signin_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_mute_stories t
:use_default_story_sound t
:use_default_disable_pinned_message_notifications t
:use_default_disable_mention_notifications t))))
(telega-ins "\n")
(telega-ins--line-wrap-prefix " "
(let ((muted-p (telega-chat-muted-p chat)))
(telega-ins--text-button (if (not muted-p)
(telega-symbol 'checkbox-on)
(telega-symbol 'checkbox-off))
'face 'telega-link
:value chat
:action #'telega-chat-toggle-muted)
(telega-ins " ")
(telega-ins-fmt "%s (%s)"
(telega-i18n "lng_settings_desktop_notify")
(propertize (if (plist-get notify-cfg :use_default_mute_for)
"default" "custom")
'face 'telega-shadow))
(unless muted-p
(telega-ins " ")
(telega-ins--box-button (telega-i18n "lng_mute_menu_duration")
:value chat
:action (lambda (chat)
(telega-chat-toggle-muted
chat (telega-completing-read-mute-for
(telega-i18n "lng_mute_menu_duration_any"
:duration ": "))))))
(telega-ins "\n"))
;; Show Preview
(let ((show-preview-p (telega-chat-notification-setting
chat :show_preview)))
(telega-ins--text-button (if show-preview-p
(telega-symbol 'checkbox-on)
(telega-symbol 'checkbox-off))
'face 'telega-link
'action (lambda (_button)
(telega--setChatNotificationSettings chat
:use_default_show_preview nil
:show_preview (if show-preview-p :false t))))
(telega-ins " ")
(telega-ins-fmt "%s (%s)"
(telega-i18n "lng_settings_show_preview")
(propertize (if (plist-get notify-cfg :use_default_show_preview)
"default" "custom")
'face 'telega-shadow))
(telega-ins "\n"))
;; Stories is a private/channels things
(let ((mute-stories-p (telega-chat-notification-setting
chat :mute_stories)))
(telega-ins--text-button (if mute-stories-p
(telega-symbol 'checkbox-on)
(telega-symbol 'checkbox-off))
'face 'telega-link
'action (lambda (_button)
(telega--setChatNotificationSettings chat
:use_default_mute_stories nil
:mute_stories (if mute-stories-p :false t))))
(telega-ins " ")
(telega-ins-fmt "Mute Stories (%s)"
(propertize (if (plist-get notify-cfg :use_default_mute_stories)
"default" "custom")
'face 'telega-shadow))
(telega-ins "\n"))
(let ((disable-pin-msg-p (telega-chat-notification-setting
chat :disable_pinned_message_notifications)))
(telega-ins--text-button (if disable-pin-msg-p
(telega-symbol 'checkbox-on)
(telega-symbol 'checkbox-off))
'face 'telega-link
'action (lambda (_button)
(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 "Disable Pinned Message Notification (")
(telega-ins--with-face 'telega-shadow
(telega-ins
(if (plist-get notify-cfg :use_default_disable_pinned_message_notifications)
"default"
"custom")))
(telega-ins ")")
(telega-ins "\n"))
(let ((disable-mentions-p (telega-chat-notification-setting
chat :disable_mention_notifications)))
(telega-ins--text-button (if disable-mentions-p
(telega-symbol 'checkbox-on)
(telega-symbol 'checkbox-off))
'face 'telega-link
'action (lambda (_button)
(telega--setChatNotificationSettings chat
:use_default_disable_mention_notifications nil
:disable_mention_notifications
(if disable-mentions-p :false t))))
(telega-ins " ")