-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathtelega-tdlib-events.el
1636 lines (1353 loc) · 63.3 KB
/
telega-tdlib-events.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-tdlib-events.el --- Handle events from TDLib -*- lexical-binding: t -*-
;; Copyright (C) 2020 by Zajcev Evgeny.
;; Author: Zajcev Evgeny <[email protected]>
;; Created: Sun May 24 20:36:43 2020
;; 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 'telega-tdlib)
(require 'telega-root)
(require 'telega-chat)
(require 'telega-inline)
(require 'telega-topic)
(require 'telega-story)
(defvar tracking-buffers nil)
(declare-function telega--authorization-ready "telega")
(defvar telega-tdlib-min-version)
(defvar telega-tdlib-max-version)
(defun telega-chat--update (chat &rest dirtiness)
"Something changed in CHAT, button needs to be updated.
DIRTINESS specifies additional CHAT dirtiness."
(let ((chat-dirtiness (nconc dirtiness (plist-get chat :telega-dirtiness))))
(telega-debug "IN: `telega-chat--update': %s dirtiness: %S"
(telega-chat-title chat) chat-dirtiness)
(when dirtiness
(plist-put chat :telega-dirtiness chat-dirtiness)
(when (memq 'reorder dirtiness)
(setq telega--ordered-chats (delq chat telega--ordered-chats))
(telega--ordered-chats-insert chat))))
;; Update root ewocs, filters and chatbuf
(telega-root-view--update :on-chat-update chat)
(telega-filters--chat-update chat)
(with-telega-chatbuf chat
(telega-chatbuf--chat-update))
(telega-describe-chat--maybe-redisplay chat)
(run-hook-with-args 'telega-chat-update-hook chat)
;; Finally chat has been updated
(plist-put chat :telega-dirtiness nil))
(defun telega-chat--mark-dirty (chat &optional event)
"Mark CHAT as dirty by EVENT."
(unless (memq chat telega--dirty-chats)
(setq telega--dirty-chats (cons chat telega--dirty-chats)))
(plist-put chat :telega-dirtiness
(cons (plist-get event :@type)
(plist-get chat :telega-dirtiness)))
;; If there are more then 50 chats are dirty, then force update
(when (> (length telega--dirty-chats) 50)
(telega-chats-dirty--update)
(telega-filters--redisplay)
;; NOTE: Timers are not run when Emacs is under heavy load, so
;; force status animations as well if timer is running
(when telega-status--timer
(telega-status--animate))
))
(defun telega-chats-dirty--update ()
"Update dirty chats."
(let* ((dirty-chats (prog1 telega--dirty-chats
(setq telega--dirty-chats nil)))
(reorder-chats
(cl-remove-if-not #'telega-chat-order-dirty-p dirty-chats)))
;; NOTE: To reorder REORDER-CHATS we fist remove all of them from
;; the ordered chat list and then insert them one by one, so
;; chat's dirty orders won't affect insertion point
(dolist (rc reorder-chats)
(setq telega--ordered-chats (delq rc telega--ordered-chats)))
(dolist (rc reorder-chats)
(telega--ordered-chats-insert rc))
(dolist (dc dirty-chats)
(telega-chat--update dc))))
(defun telega--on-ok (_event)
"On ok result from command function call."
;; no-op
)
;; User updates
(defun telega-user--update (user event)
"USER has been updated, do something about this."
(telega-root-view--update :on-user-update user)
(telega-describe-user--maybe-redisplay (plist-get user :id))
(telega-describe-contact--maybe-redisplay (plist-get user :id))
;; Update corresponding private chat as well
(when-let ((chat (telega-chat-get (plist-get user :id) 'offline)))
(telega-chat--mark-dirty chat event))
(run-hook-with-args 'telega-user-update-hook user))
(defun telega--on-updateUser (event)
"Some user info has has been changed."
(let ((user (plist-get event :user)))
(telega--info-update user)
;; NOTE: Updating user might affect his palette, so delete cached
;; palette
(telega-plist-del user :telega-palette)
(telega-user--update user event)))
(defun telega--on-updateUserStatus (event)
"User status has been changed."
(let* ((user-id (plist-get event :user_id))
(user (telega-user-get user-id))
(status (plist-get event :status)))
(plist-put user :status status)
;; NOTE: For online status, set special USER property with value
;; of time last seen online
(when (eq (telega--tl-type status) 'userStatusOnline)
(plist-put user :telega-last-online (telega-time-seconds)))
;; NOTE: do not track online status changes on me
(unless (telega-me-p user)
(telega-user--update user event))
))
(defun telega--on-updateChatAction (event)
"Some message sender has actions on chat."
(let* ((chat-id (plist-get event :chat_id))
(msg-thread-id (plist-get event :message_thread_id))
(action-id (cons chat-id msg-thread-id))
(chat-actions (gethash action-id telega--actions))
(sender (plist-get event :sender_id))
(user-action (assoc sender chat-actions))
(action (plist-get event :action))
(cancel-p (eq (telega--tl-type action) 'chatActionCancel)))
(cond (cancel-p
(let ((new-chat-actions (assoc-delete-all sender chat-actions)))
(if new-chat-actions
(puthash action-id new-chat-actions telega--actions)
(remhash action-id telega--actions))))
(user-action
(setcdr user-action action))
(t (puthash action-id (cons (cons sender action) chat-actions)
telega--actions)))
(let ((chat (telega-chat-get chat-id)))
(telega-chat--mark-dirty chat event)
(with-telega-chatbuf chat
;; If action by me, update `telega-chatbuf--my-action' as well
(when (telega-me-p (telega-msg-sender sender))
(setq telega-chatbuf--my-action (unless cancel-p action)))))
))
(defun telega--on-updateUserFullInfo (event)
(let ((user-id (plist-get event :user_id))
(ufi (cdr (assq 'user telega--full-info))))
(puthash user-id (plist-get event :user_full_info) ufi)
;; Possibly update `telega--blocked-user-ids-alist', keeping
;; `telega--blocked-user-ids-alist' in sync with update events
;; see https://github.com/tdlib/td/issues/1669
;; 1. First remove user from all block lists
(dolist (blocked-user-list telega--blocked-user-ids-alist)
(when (memq user-id (cdr blocked-user-list))
(setcdr blocked-user-list
(delq user-id (cdr blocked-user-list)))))
;; 2. Then add it to the block list from event
(when-let* ((block-list (telega--tl-get event :user_full_info :block_list))
(blocked-user-list
(assoc block-list telega--blocked-user-ids-alist)))
(unless (memq user-id (cdr blocked-user-list))
(setcdr blocked-user-list
(append (cdr blocked-user-list) (list user-id)))))
(telega-user--update (telega-user-get user-id) event)))
;; Chat updates
(defun telega--on-updateNewChat (event)
"New chat has been loaded or created."
(let ((chat (telega-chat--ensure (plist-get event :chat))))
(telega-chat--mark-dirty chat event)
;; Asynchronously fetch all topics
(when (telega-chat-match-p chat 'is-forum)
(telega-chat--topics-fetch 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)
;; XXX remove cached avatars
(plist-put chat :telega-image nil)
(plist-put chat :telega-avatar-1 nil)
(plist-put chat :telega-avatar-3 nil)
(plist-put chat :telega-avatar-vc-1 nil)
(plist-put chat :telega-avatar-vc-speaking-1 nil)
(telega-chat--mark-dirty chat event)
))
(defun telega--on-updateChatPermissions (event)
"Chat's permissions was changed."
(let ((chat (telega-chat-get (plist-get event :chat_id))))
(plist-put chat :permissions (plist-get event :permissions))
(telega-chat--mark-dirty chat event)
))
(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-chat--mark-dirty chat event)
(telega-root-view--update :on-notifications-update)
(telega-describe-notifications--maybe-redisplay)
))
(defun telega--on-updateChatTitle (event)
"EVENT arrives when title of a chat was changed."
(let ((chat (telega-chat-get (plist-get event :chat_id) 'offline)))
(cl-assert chat)
(plist-put chat :title (plist-get event :title))
;; NOTE: Updating chat's title might affect its palette, so delete
;; cached palette
(telega-plist-del chat :telega-palette)
(telega-chat--mark-dirty chat event)
(with-telega-chatbuf chat
(rename-buffer (telega-chatbuf--name chat)))
))
(defun telega--on-updateChatPosition (event)
(let* ((chat (telega-chat-get (plist-get event :chat_id) 'offline))
(new-pos (plist-get event :position))
(chat-positions
(cl-delete (plist-get new-pos :list) (plist-get chat :positions)
:key (telega--tl-prop :list) :test #'equal)))
(plist-put chat :positions (vconcat chat-positions (list new-pos)))
(telega-chat--mark-dirty chat event)))
(defun telega--on-updateChatMessageSender (event)
(let ((chat (telega-chat-get (plist-get event :chat_id) 'offline)))
(plist-put chat :message_sender_id
(plist-get event :message_sender_id))
(telega-chat--mark-dirty chat event)))
(defun telega--on-updateChatHasProtectedContent (event)
(let ((chat (telega-chat-get (plist-get event :chat_id) 'offline)))
(plist-put chat :has_protected_content
(plist-get event :has_protected_content))
(telega-chat--mark-dirty chat event)))
(defun telega--on-updateChatIsTranslatable (event)
(let ((chat (telega-chat-get (plist-get event :chat_id) 'offline)))
(plist-put chat :is_translatable
(plist-get event :is_translatable))
(telega-chat--mark-dirty chat event)))
(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)
(telega-chat--mark-dirty chat event)
(with-telega-chatbuf chat
(when (zerop unread-count)
;; Remove unread messages bar if all messages are read
(telega-chatbuf--manage-unread-messages-bar)
;; NOTE: if all messages are read (in another telegram
;; client), then remove the chatbuf from tracking
(when (member (buffer-name) tracking-buffers)
(tracking-remove-buffer (current-buffer)))))))
(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))
(telega-chat--mark-dirty chat event)
(with-telega-chatbuf chat
(telega-chatbuf--read-outbox old-read-outbox-msgid))))
(defun telega--on-updateChatUnreadMentionCount (event &optional chat)
(unless chat
(setq 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))
(telega-chat--mark-dirty chat event))
(defmacro with-telega--msg-update-event (event bindings &rest body)
(declare (indent 2))
(let ((chat-id-sym (gensym "chat-id"))
(msg-id-sym (gensym "msg-id")))
`(let* ((,chat-id-sym (plist-get ,event :chat_id))
(,msg-id-sym (plist-get ,event :message_id))
(,(nth 0 bindings) (telega-chat-get ,chat-id-sym 'offline))
(,(nth 2 bindings) (with-telega-chatbuf ,(nth 0 bindings)
(telega-chatbuf--node-by-msg-id ,msg-id-sym)))
(,(nth 1 bindings) (if ,(nth 2 bindings)
(ewoc-data ,(nth 2 bindings))
(gethash (cons ,chat-id-sym ,msg-id-sym)
telega--cached-messages))))
,@body)))
(defun telega--on-updateMessageMentionRead (event)
(with-telega--msg-update-event event (chat msg node)
(cl-assert chat)
(telega--on-updateChatUnreadMentionCount event chat)
;; Update message's `:contains_unread_mention' as well
;; This requires message redisplay, since message could outline
;; unread mention
(plist-put msg :contains_unread_mention nil)
(when node
(with-telega-chatbuf chat
(telega-chatbuf--redisplay-node node)))))
(defun telega--on-updateChatDefaultDisableNotification (event)
(let ((chat (telega-chat-get (plist-get event :chat_id) 'offline)))
(cl-assert chat)
(plist-put chat :default_disable_notification
(plist-get event :default_disable_notification))
(telega-chat--mark-dirty chat event)))
(defun telega--on-updateChatLastMessage (event)
(let* ((chat-id (plist-get event :chat_id))
(chat (telega-chat-get chat-id 'offline))
(event-last-msg (plist-get event :last_message)))
(cl-assert chat)
;; NOTE: If chat's last message is already in the message's
;; cache, use it, because various logic (such as ignoring
;; predicated) might be already applied to the message
(when event-last-msg
(when-let ((cached-last-msg
(gethash (cons chat-id (plist-get event-last-msg :id))
telega--cached-messages)))
(setq event-last-msg cached-last-msg)))
(plist-put chat :last_message event-last-msg)
(plist-put chat :positions (plist-get event :positions))
;; NOTE: `:last_message' is unset when gap is created in the chat
;; This case is handled in the `telega-chatbuf--last-msg-loaded-p'
;; See https://github.com/tdlib/td/issues/896
;;
;; Gap can be also created if last message in the chat is deleted.
;; TDLib might take some time to update chat's last message.
(with-telega-chatbuf chat
(telega-chatbuf--history-state-delete :newer-loaded))
(telega-chat--mark-dirty chat event)))
(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-chat--mark-dirty chat event)))
(defun telega--on-updateChatBlockList (event)
"Chat/User has been blocked/unblocked."
(let ((chat (telega-chat-get (plist-get event :chat_id) 'offline)))
(cl-assert chat)
(plist-put chat :block_list
(plist-get event :block_list))
(telega-chat--mark-dirty chat event)))
(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))
(telega-chat--mark-dirty chat event)))
(defun telega--on-updateForumTopicInfo (event)
(let* ((new-info (plist-get event :info))
(chat (telega-chat-get (plist-get event :chat_id) 'offline))
(topic (telega-topic-get chat (plist-get new-info :message_thread_id))))
(plist-put topic :info new-info)
(telega-chat--mark-dirty chat event)))
(defun telega--on-updateChatDraftMessage (event)
(let ((chat (telega-chat-get (plist-get event :chat_id) 'offline))
(draft-msg (plist-get event :draft_message)))
(cl-assert chat)
(plist-put chat :draft_message draft-msg)
;; Generate fake events with position updates
(seq-doseq (pos (plist-get event :positions))
(telega--on-updateChatPosition (list :chat_id (plist-get event :chat_id)
:position pos)))
(telega-chat--mark-dirty chat event)
;; Update chat's input to the text in DRAFT-MSG
(with-telega-chatbuf chat
(telega-chatbuf--input-draft-update))
))
(defun telega--on-updateChatHasScheduledMessages (event)
(let ((chat (telega-chat-get (plist-get event :chat_id) 'offline)))
(cl-assert chat)
(plist-put chat :has_scheduled_messages
(plist-get event :has_scheduled_messages))
(telega-chat--mark-dirty chat event)
;; NOTE: `telega-chatbuf--name' uses `:has_scheduled_messages', so
;; rename the buffer
(with-telega-chatbuf chat
(rename-buffer (telega-chatbuf--name chat)))))
(defun telega--on-updateChatActionBar (event)
(let ((chat (telega-chat-get (plist-get event :chat_id) 'offline)))
(cl-assert chat)
(plist-put chat :action_bar (plist-get event :action_bar))
(telega-chat--mark-dirty chat event)))
(defun telega--on-updateChatTheme (event)
(let ((chat (telega-chat-get (plist-get event :chat_id) 'offline)))
(cl-assert chat)
(plist-put chat :theme_name (plist-get event :theme_name))
(telega-chat--mark-dirty chat event)))
(defun telega--on-updateChatMessageAutoDeleteTime (event)
(let ((chat (telega-chat-get (plist-get event :chat_id))))
(cl-assert chat)
(plist-put chat :message_auto_delete_time
(plist-get event :message_auto_delete_time))
(telega-chat--mark-dirty chat event)))
(defun telega--on-updateSecretChat (event)
(let ((secretchat (plist-get event :secret_chat)))
(telega--info-update secretchat)
;; update corresponding secret chat button
(when-let ((chat (cl-find secretchat
(telega-filter-chats
telega--ordered-chats '(type secret))
:test 'eq :key #'telega-chat--info)))
(telega-chat--mark-dirty chat event))))
(defun telega--on-blocked-senders-load (reply)
;; NOTE:
;; - first element of the REPLY is a block-list
;; - second element of the REPLY is a total number of blocked
;; senders in a block-lis
;; - rest is the message senders ids
(let* ((block-list (car reply))
(_total-senders (cadr reply))
(senders (cddr reply))
(blocked-user-ids
(alist-get block-list telega--blocked-user-ids-alist)))
(when senders
(cl-incf (car blocked-user-ids) (length senders))
(setcdr blocked-user-ids
(mapcar (telega--tl-prop :id)
(cl-remove-if-not #'telega-user-p senders)))
;; Continue fetching blocked users
(telega--getBlockedMessageSenders block-list (car blocked-user-ids)
#'telega--on-blocked-senders-load)
)))
(defun telega--on-initial-chats-load (tl-ok)
"Process initially loaded chats, or continue loading chats."
(if (not (telega--tl-error-p tl-ok))
(progn
;; Continue fetching chats
(telega--loadChats (list :@type "chatListMain")
#'telega--on-initial-chats-load))
;; All chats has been fetched
(telega-status--set nil "")
;; Check `:last_message' of initially fetched chats for client
;; side messages ignoring. Also trigger reordering, since
;; ignoring last message might affect chat order, see
;; `contrib/telega-adblock.el'
(dolist (chat telega--ordered-chats)
(when-let ((last-message (plist-get chat :last_message)))
(when (telega-msg-run-ignore-predicates last-message 'last-msg)
(telega-chat--update chat 'reorder))
;; Also fetch dependend message and custom emojis for the last
;; message, because last message might be displayed in the
;; rootbuf
(when (telega-msg-match-p last-message
'(type PinMessage GameScore PaymentSuccessful
ForumTopicEdited ForumTopicIsClosedToggled))
(telega-msg--replied-message-fetch last-message))
(when telega-use-images
(telega-msg--custom-emojis-fetch last-message))
))
(run-hooks 'telega-chats-fetched-hook)))
(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))
(with-telega-chatbuf chat
(telega-chatbuf--reply-markup-message-fetch))))
(defun telega--on-updateChatVideoChat (event)
(let ((chat (telega-chat-get (plist-get event :chat_id) 'offline)))
(cl-assert chat)
(plist-put chat :video_chat (plist-get event :video_chat))
(telega-chat--mark-dirty chat event)
(with-telega-chatbuf chat
(telega-chatbuf--video-chat-fetch))))
(defun telega--on-updateGroupCall (event)
(let ((new-group-call (plist-get event :group_call)))
(telega-group-call--ensure new-group-call)
(when-let ((chat (telega-group-call-get-chat (plist-get new-group-call :id))))
(telega-chat--mark-dirty chat event))
))
(defun telega--on-updateGroupCallParticipant (event)
(let ((group-call-id (plist-get event :group_call_id))
(_call-user (plist-get event :participant)))
(when-let ((chat (telega-group-call-get-chat group-call-id)))
(with-telega-chatbuf chat
;; TODO: video-chats
;; update `telega-chatbuf--group-call-users'
)
)))
;; Chat filters
(defun telega--on-updateChatFolders (event)
"List of chat filters has been updated."
(setq telega-tdlib--chat-folder-tags-p
(plist-get event :are_tags_enabled))
;; NOTE: collect folders with changed names and update all chats in
;; that folders. Because folder name might be displayed along the
;; side with chat's title in the rootbuf
(let* ((new-tdlib-folders (append (plist-get event :chat_folders) nil))
(new-names (seq-difference (telega-folder-names new-tdlib-folders)
(telega-folder-names))))
(setq telega-tdlib--chat-folders new-tdlib-folders)
(dolist (folder-name new-names)
(dolist (fchat (telega-filter-chats
telega--ordered-chats `(folder ,folder-name)))
(telega-chat--mark-dirty fchat)))
(telega-chats-dirty--update)
;; Update custom filters ewoc
(with-telega-root-buffer
(telega-save-cursor
(telega-filters--refresh))
;; If folders view is active, then redisplay it, see
;; https://t.me/emacs_telega/36581
(when (eq (car telega-root--view) #'telega-view-folders)
(telega-view-folders))
(run-hooks 'telega-root-update-hook))
))
;; Messages updates
(defun telega-message--update (msg)
"Message MSG has been updated."
(when (plist-get msg :is_pinned)
(when-let ((chat (telega-msg-chat msg 'offline)))
(plist-put chat :telega-pinned-message
(cons msg
(cl-remove msg (plist-get chat :telega-pinned-messages)
:test #'telega-msg-id=)))))
(telega-root-view--update :on-message-update msg)
)
(defalias 'telega--on-message 'ignore)
(defun telega--on-updateNewMessage (event)
"A new message was received; can also be an outgoing message."
(let* ((new-msg (plist-get event :message))
(chat (telega-msg-chat new-msg)))
;; NOTE: We always set `:ignored-p' property to not trigger
;; `telega-msg-run-ignore-predicates' once again when this message
;; is inserted into chatbuf
(if (telega-msg-run-ignore-predicates new-msg 'last-msg)
(progn
;; NOTE: In case ignored message contains mention, we mark
;; all chat mentions as read if there is no other mentions.
;; See https://github.com/zevlg/telega.el/issues/314
(when (and (plist-get new-msg :contains_unread_mention)
(eq 1 (plist-get chat :unread_mention_count)))
(telega--readAllChatMentions chat))
;; NOTE: If all messages in the chat are read and ignored
;; message arives, automatically read it
;; See https://github.com/zevlg/telega.el/issues/381
(when-let ((last-message (plist-get chat :last_message)))
(when (<= (plist-get last-message :id)
(plist-get chat :last_read_inbox_message_id))
(telega--viewMessages chat (list new-msg)
:source '(:@type "messageSourceChatHistory")
:force t))))
(plist-put new-msg :ignored-p nil))
(run-hook-with-args 'telega-chat-pre-message-hook new-msg)
(with-telega-chatbuf chat
(telega-msg-cache new-msg)
;; NOTE: `:last_message' could be already updated in the chat
;; with the id of the NEW-MSG, so check for it
(when (telega-chatbuf--append-new-message-p new-msg)
(when-let ((node (telega-chatbuf--insert-messages
(list new-msg) 'append-new)))
(when (and (telega-chatbuf-match-p telega-use-tracking-for)
(not (telega-msg-match-p new-msg 'ignored))
(not (plist-get new-msg :is_outgoing))
(not (telega-msg-seen-p new-msg telega-chatbuf--chat)))
(tracking-add-buffer (current-buffer) '(telega-tracking)))
;; If message is visible in some window, then mark it as read
;; see https://github.com/zevlg/telega.el/issues/4
(when (telega-chatbuf--msg-observable-p new-msg node)
(telega-chatbuf--msg-view new-msg))
)))
;; NOTE: Trigger `telega-chat-post-message-hook' for outgoing
;; messages, only when message is successfully sent
;; See https://t.me/emacs_telega/25615
(unless (plist-get new-msg :sending_state)
(run-hook-with-args 'telega-chat-post-message-hook new-msg))))
(defun telega--on-updateMessageSendSucceeded (event)
"Message has been successfully sent to server.
Message id could be updated on this update."
(let* ((new-msg (plist-get event :message))
(chat-id (plist-get new-msg :chat_id))
(new-id (plist-get new-msg :id))
(old-id (plist-get event :old_message_id)))
;; Actualize cached message
(remhash (cons chat-id old-id) telega--cached-messages)
(puthash (cons chat-id new-id) new-msg telega--cached-messages)
(with-telega-chatbuf (telega-msg-chat new-msg)
;; NOTE: Actualize message position according to NEW-ID
;; Optimization: search old message's node from last node
(let ((node (ewoc-nth telega-chatbuf--ewoc -1)))
(while (and node (not (= old-id (plist-get (ewoc-data node) :id))))
(setq node (ewoc-prev telega-chatbuf--ewoc node)))
(when node
(let ((before-node (ewoc-next telega-chatbuf--ewoc node)))
;; Search the node to insert new message before
(while (and before-node
(> new-id (plist-get (ewoc-data before-node) :id)))
(setq before-node (ewoc-next telega-chatbuf--ewoc before-node)))
(run-hook-with-args 'telega-chatbuf-pre-msg-insert-hook new-msg)
(ewoc-set-data node new-msg)
(telega-ewoc--move-node telega-chatbuf--ewoc node before-node
'save-point)
(when before-node
;; NOTE: need to redisplay next to newly created
;; node, in case `telega-chat-group-messages-for' is
;; used, see https://github.com/zevlg/telega.el/issues/159
(ewoc-invalidate telega-chatbuf--ewoc before-node))
;; Automatically view all outgoing messages
(when (plist-get new-msg :is_outgoing)
(telega-chatbuf--msg-view new-msg))
(run-hook-with-args 'telega-chatbuf-post-msg-insert-hook new-msg)
))))
(unless (plist-get new-msg :sending_state)
;; NOTE: In case outgoing message is done via bot, we need to
;; update recently used inline bots list
(when-let ((outgoing-p (plist-get new-msg :is_outgoing))
(via-bot-id (plist-get new-msg :via_bot_user_id)))
(unless (zerop via-bot-id)
(telega--recent-inline-bots-fetch)))
;; NOTE: Handle update for the favorite messages storage creation
(when (and (eq chat-id (plist-get telega--favorite-messages-storage-message :chat_id))
(eq old-id (plist-get telega--favorite-messages-storage-message :id)))
(setq telega--favorite-messages-storage-message new-msg)
(message "telega: Storage for the favorite messages has been created"))
(run-hook-with-args 'telega-chat-post-message-hook new-msg))))
(defun telega--on-updateMessageSendFailed (event)
"Message failed to send."
;; NOTE: Triggered for example if trying to send bad picture.
;; `telega--on-updateMessageSendSucceeded' updates the message
;; content with new(failed) state
(telega--on-updateMessageSendSucceeded event)
(let ((err (plist-get event :error)))
(message "telega: Failed to send message: %d %s"
(plist-get err :code) (telega-tl-str err :message))
))
(defun telega--on-updateMessageContent (event)
"Content of the message has been changed."
(let ((new-content (plist-get event :new_content)))
;; NOTE: for "messagePoll" update check if there any option with
;; `:is_being_chosen' set to non-nil.
;; If so, then just ignore this update, waiting for real update
;; chosing some poll option may fail with "REVOTE_NOT_ALLOWED" error
(unless (and (eq (telega--tl-type new-content) 'messagePoll)
(cl-some (telega--tl-prop :is_being_chosen)
(telega--tl-get new-content :poll :options)))
(with-telega--msg-update-event event (chat msg node)
(plist-put msg :content new-content)
(when node
(with-telega-chatbuf chat
(run-hook-with-args 'telega-chatbuf-pre-msg-update-hook msg)
(telega-chatbuf--redisplay-node node)
(run-hook-with-args 'telega-chatbuf-post-msg-update-hook msg)
))))))
(defun telega--on-updateMessageEdited (event)
"Edited date of the message specified by EVENT has been changed."
(with-telega--msg-update-event event (chat msg node)
(plist-put msg :edit_date (plist-get event :edit_date))
(plist-put msg :reply_markup (plist-get event :reply_markup))
(with-telega-chatbuf chat
(when node
(telega-chatbuf--redisplay-node node))
;; In case user has active aux with a message from EVENT,
;; then redisplay aux as well.
;; See https://t.me/emacs_telega/7243
(when-let ((aux-msg (plist-get telega-chatbuf--aux-plist :aux-msg)))
(when (telega-msg-id= msg aux-msg)
(plist-put telega-chatbuf--aux-plist :aux-msg msg)
(telega-chatbuf--chat-update "aux-plist")))
)))
(defun telega--on-updateMessageIsPinned (event)
"Message has ben pinned or unpinned."
(with-telega--msg-update-event event (chat msg node)
(plist-put msg :is_pinned (plist-get event :is_pinned))
(with-telega-chatbuf chat
(when node
(telega-chatbuf--redisplay-node node))
(telega-chatbuf--pinned-messages-fetch))
;; TODO:
;(telega-chat--update-pinned-message chat nil old-pinned-message-id)
(telega-chat--mark-dirty chat event)
))
(defun telega--on-updateMessageInteractionInfo (event)
"Message interaction info has been changed."
(with-telega--msg-update-event event (chat msg node)
(plist-put msg :interaction_info (plist-get event :interaction_info))
(with-telega-chatbuf chat
(when node
(telega-chatbuf--redisplay-node node)
;; NOTE: reactions might be updated and custom emojis needs to
;; be downloaded.
(telega-msg--custom-emojis-fetch msg))
(when-let ((thread-msg (telega-chatbuf--thread-msg)))
(when (= (plist-get event :message_id) (plist-get thread-msg :id))
(telega-chatbuf--chat-update "thread")))
)))
(defun telega--on-updateMessageContentOpened (event)
"The message content was opened.
Updates voice note messages to \"listened\", video note messages
to \"viewed\" and starts the TTL timer for self-destructing
messages."
(with-telega--msg-update-event event (chat msg node)
(when-let ((content (plist-get msg :content)))
(cl-case (telega--tl-type content)
(messageVoiceNote
(plist-put content :is_listened t))
(messageVideoNote
(plist-put content :is_viewed t))
;; TODO: Check self-destruct type and probably start self-destruct
;; timer
;; ((messagePhoto messageVideo)
(t
;; Nothing to update
(setq node nil))))
(when node
(with-telega-chatbuf chat
(telega-chatbuf--redisplay-node node)))))
(defun telega--on-updateDeleteMessages (event)
"Some messages has been deleted from chat."
(let ((chat-id (plist-get event :chat_id)))
;; NOTE: Always delete message from `telega--cached-messages' even
;; if `:from_cache' is nil. Both `:is_permanent' and
;; `:from_cache' could be nil for some private channels we are not
;; member of
(seq-doseq (msg-id (plist-get event :message_ids))
(remhash (cons chat-id msg-id) telega--cached-messages))
(when (plist-get event :is_permanent)
(with-telega-chatbuf (telega-chat-get chat-id)
(seq-doseq (msg-id (plist-get event :message_ids))
(when-let ((node (telega-chatbuf--node-by-msg-id msg-id))
(msg (ewoc--node-data node)))
(plist-put msg :telega-is-deleted-message t)
(if (telega-chat-match-p (telega-msg-chat msg)
telega-chat-show-deleted-messages-for)
(telega-chatbuf--redisplay-node node)
;; NOTE: need to redisplay next to deleted node, in case
;; `telega-chat-group-messages-for' is used
;; See https://github.com/zevlg/telega.el/issues/159
(let ((next-node (ewoc-next telega-chatbuf--ewoc node)))
(ewoc-delete telega-chatbuf--ewoc node)
(when next-node
(telega-chatbuf--redisplay-node next-node))))
;; TODO: 1.7.0 pinned
(when (plist-get msg :is_pinned)
(telega-chatbuf--pinned-messages-fetch))
))))))
;; Call updates
(defun telega--on-updateCall (event)
"Called when some call data has been updated."
(let* ((call (plist-get event :call))
(state (plist-get call :state))
(call-id (plist-get call :id))
(old-call (telega-voip--by-id call-id)))
(setf (alist-get call-id telega-voip--alist) call)
;; Update active call value
(when (eq call-id (plist-get telega-voip--active-call :id))
(setq telega-voip--active-call call))
(cl-case (telega--tl-type state)
(callStatePending
(unless old-call
(if (plist-get call :is_outgoing)
(run-hook-with-args 'telega-call-outgoing-hook call)
(run-hook-with-args 'telega-call-incoming-hook call)))
;; * If no active calls and CALL is outgoing, then make it
;; active
;; * If there is active call and `telega-voip-busy-if-active' is
;; non-nil then discard all other incoming calls
(if (plist-get call :is_outgoing)
(unless telega-voip--active-call
(setq telega-voip--active-call call))
(when (and telega-voip-busy-if-active
telega-voip--active-call
(not (eq call telega-voip--active-call)))
(telega--discardCall call-id)))
(when (and telega-voip-help-echo
(not telega-voip--active-call)
(eq call (telega-voip--incoming-call)))
(let ((prefix (when (eq (telega-root--buffer) (window-buffer))
"\\<telega-root-mode-map>")))
(message "telega: Press `%s' to answer, `%s' to decline"
(substitute-command-keys
(concat prefix "\\[telega-voip-accept]"))
(substitute-command-keys
(concat prefix "\\[telega-voip-discard]"))))))
(callStateReady
(unless (eq call telega-voip--active-call)
(error "Another call became Ready, while having active call"))
(run-hook-with-args 'telega-call-ready-hook call)
(let ((start
(list :@command "start"
:server_config (plist-get state :config)
:is_outgoing (or (plist-get call :is_outgoing) :false)
:encryption_key (plist-get state :encryption_key)
:allow_p2p (or telega-voip-allow-p2p :false)
:max_layer (telega--tl-get state :protocol :max_layer)
:endpoints (plist-get state :connections))))
(when telega-voip-logfile
(telega-server--send
(list :@command "config"
:log-file-path telega-voip-logfile) "voip"))
(telega-server--send start "voip"))
(when telega-voip-help-echo
(message "telega: Press `%s' to hang up"
(substitute-command-keys
(concat (when (eq (telega-root--buffer) (window-buffer))
"\\<telega-root-mode-map>")
"\\[telega-voip-discard]")))))
(callStateError
(let ((err (plist-get state :error))
(user (telega-user-get (plist-get call :user_id))))
(message "Error[%d] calling %s: %s" (plist-get err :code)
(telega-msg-sender-title user
:with-avatar-p t
:with-username-p t)
(plist-get err :message))))
(callStateDiscarded
(let ((discard (plist-get state :reason))
(user (telega-user-get (plist-get call :user_id))))
(message "Call %s discarded: %s" (telega-msg-sender-title user
:with-avatar-p t
:with-username-p t)
(substring (plist-get discard :@type) 17))))
)
;; Delete call from the list, if call is ended
(when (memq (telega--tl-type state) '(callStateError callStateDiscarded))
(unwind-protect
(run-hook-with-args 'telega-call-end-hook call)
(when (eq telega-voip--active-call call)
(telega-server--send (list :@command "stop") "voip")
(setq telega-voip--active-call nil))
(setq telega-voip--alist (assq-delete-all call-id telega-voip--alist))))
;; Update user
(telega-user--update (telega-user-get (plist-get call :user_id)) event)
;; Update aux status
(telega-root-aux-redisplay 'telega-ins--voip-active-call)
))
;; Stickers updates
(defun telega--on-updateInstalledStickerSets (event)
"The list of installed sticker sets was updated."
(cl-case (telega--tl-type (plist-get event :sticker_type))
(stickerTypeMask
(telega-debug "TODO: `telega--on-updateInstalledStickerSets' for masks"))