forked from zevlg/telega.el
-
Notifications
You must be signed in to change notification settings - Fork 0
/
telega-tdlib-events.el
1197 lines (992 loc) · 46.5 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 <zevlg@yandex.ru>
;; 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)
(defvar tracking-buffers nil)
(declare-function telega--authorization-ready "telega")
(defconst telega--event-chat-update-type-list
'((("updateUserChatAction" "updateChatActionBar"
"updateChatLastMessage")
footer)
(("updateMessageInteractionInfo")
thread-footer)
(("updateUserStatus" "updateChatOnlineMemberCount"
"updateChatMessageTtlSetting" "updateMessageMentionRead")
modeline)
(("updateChatReadInbox" "updateChatUnreadMentionCount")
footer modeline)
(("updateChatPhoto" "updateChatIsBlocked")
prompt)))
(defun telega--event-chat-update-types (event)
"Return list of update types, that should be applied to the chat.
Each element in returned list is: `reorder', `footer',
`thread-footer', `modeline' or `prompt'."
(let* ((event-type (plist-get event :@type))
(event-reorder-p
(or (member event-type
'("updateNewChat" "updateChatPosition"
"updateChatLastMessage" "updateChatDraftMessage"
;; Special fake event used by telega to force chat
;; reordering
"telegaChatReorder"
))
(and telega--sort-criteria
(cl-some (lambda (criteria-sym)
(member event-type
(get criteria-sym :telega-order-events)))
telega--sort-criteria)))))
(nconc (when event-reorder-p (list 'reorder))
(cdr (cl-find event-type telega--event-chat-update-type-list
:key #'car :test #'member)))))
(defun telega-chat--update (chat &rest events)
"Something changed in CHAT, button needs to be updated.
If FOR-REORDER is non-nil, then CHAT's node is ok, just update filters."
(telega-debug "IN: `telega-chat--update': %s" (telega-chat-title chat))
;; Apply updates to the chat and root view
(let ((chat-update-types
(apply #'append (mapcar #'telega--event-chat-update-types events))))
;; Reordering requires? Reorder CHAT by removing and then adding
;; it again at correct place
(when (memq 'reorder chat-update-types)
(plist-put chat :telega-need-reorder-p t)
(setq telega--ordered-chats (delq chat telega--ordered-chats))
(telega--ordered-chats-insert chat))
;; Update root ewocs and filters
(telega-root-view--update :on-chat-update chat events)
(telega-filters--chat-update chat)
(plist-put chat :telega-need-reorder-p nil)
;; Update chat buffer
(with-telega-chatbuf chat
;; Modeline
(when (memq 'modeline chat-update-types)
(telega-chatbuf--modeline-update))
;; Footer
(when (or (memq 'footer chat-update-types)
(and (memq 'thread-footer chat-update-types)
telega-chatbuf--thread-msg))
(telega-chatbuf--footer-update))
;; NOTE: also update it if usj prompt is currently used and chat
;; reorders
(when (or (memq 'prompt chat-update-types)
(and (memq 'reorder chat-update-types)
(telega-chatbuf--prompt-unblock-start-join-p)))
(telega-chatbuf--prompt-update))
))
(telega-describe-chat--maybe-redisplay chat)
(run-hook-with-args 'telega-chat-update-hook chat))
(defun telega-chat--mark-dirty (chat &rest events)
"Mark CHAT as dirty by EVENTS."
(let ((dc (assq chat telega--dirty-chats)))
(if dc
(setcdr (last dc) events)
(setq telega--dirty-chats
(cons (cons chat events) telega--dirty-chats))))
;; 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))
)
(defun telega-chats-dirty--update ()
"Update dirty chats."
(dolist (dirty-chat (prog1 telega--dirty-chats
(setq telega--dirty-chats nil)))
(apply #'telega-chat--update dirty-chat)))
(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)
(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-updateUserChatAction (event)
"Some user has actions on chat."
(let* ((chat-id (plist-get event :chat_id))
(chat-actions (gethash chat-id telega--actions))
(user-id (plist-get event :user_id))
(user-action (assq user-id chat-actions))
(action (plist-get event :action))
(cancel-p (eq (telega--tl-type action) 'chatActionCancel)))
(cond (cancel-p
(let ((new-chat-actions (assq-delete-all user-id chat-actions)))
(if new-chat-actions
(puthash chat-id new-chat-actions telega--actions)
(remhash chat-id telega--actions))))
(user-action
(setcdr user-action action))
(t (puthash chat-id (cons (cons user-id 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 (eq user-id telega--me-id)
(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)
(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)
(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))
(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))
(old-pos (cl-find (plist-get new-pos :list) (plist-get chat :positions)
:key (telega--tl-prop :list) :test #'equal)))
(if old-pos
;; If new order is \"0\", then remove this position, otherwise
;; modify it inplace
(if (equal "0" (plist-get new-pos :order))
(plist-put chat :positions
(vconcat (seq-remove
(apply-partially #'eq old-pos)
(plist-get chat :positions))))
(plist-put old-pos :order (plist-get new-pos :order))
(plist-put old-pos :is_pinned (plist-get new-pos :is_pinned)))
(plist-put chat :positions (vconcat (plist-get chat :positions)
(list new-pos))))
(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
;; NOTE: if all messages are read (in another telegram client),
;; then remove the chatbuf from tracking
(when (and (zerop unread-count)
(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 (telega-chat-get (plist-get event :chat_id) 'offline)))
(cl-assert chat)
;; 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
(plist-put chat :last_message (plist-get event :last_message))
(plist-put chat :positions (plist-get event :positions))
(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-updateChatIsBlocked (event)
"Chat/User has been blocked/unblocked."
(let ((chat (telega-chat-get (plist-get event :chat_id) 'offline)))
(cl-assert chat)
(plist-put chat :is_blocked
(plist-get event :is_blocked))
(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-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-updateChatChatList (event)
(let ((chat (telega-chat-get (plist-get event :chat_id) 'offline))
(chat-list (plist-get event :chat_list)))
(cl-assert chat)
(plist-put chat :chat_list chat-list)
(telega-chat--mark-dirty chat event)))
(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-updateChatMessageTtlSetting (event)
(let ((chat (telega-chat-get (plist-get event :chat_id))))
(cl-assert chat)
(plist-put chat :message_ttl_setting (plist-get event :message_ttl_setting))
(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-initial-chats-fetch (chats)
"Process initially fetched CHATS, and continue fetching chats."
(if (> (length chats) 0)
(progn
;; 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 chats)
(when-let ((last-message (plist-get chat :last_message)))
(when (telega-msg-run-ignore-predicates last-message 'last-msg)
(telega-chat--update chat (list :@type "telegaChatReorder")))))
;; Continue fetching chats
(telega--getChats (car (last chats)) (list :@type "chatListMain")
#'telega--on-initial-chats-fetch))
;; All chats has been fetched
(telega-status--set nil "")
(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-updateChatVoiceChat (event)
(let ((chat (telega-chat-get (plist-get event :chat_id) 'offline)))
(cl-assert chat)
(plist-put chat :voice_chat (plist-get event :voice_chat))
(telega-chat--mark-dirty chat event)
(with-telega-chatbuf chat
(telega-chatbuf--voice-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 (cl-find (plist-get new-group-call :id)
telega--ordered-chats
:key (telega--tl-prop :voice_chat :group_call_id))))
(with-telega-chatbuf chat
(telega-chatbuf--footer-update)))
))
(defun telega--on-updateGroupCallParticipant (event)
(let ((group-call-id (plist-get event :group_call_id))
(call-user (plist-get event :participant)))
(when-let ((chat (cl-find group-call-id
telega--ordered-chats
:key (telega--tl-prop :voice_chat :group_call_id))))
(with-telega-chatbuf chat
;; TODO: voice-chats
;; update `telega-chatbuf--group-call-users'
)
)))
;; Chat filters
(defun telega--on-updateChatFilters (event)
"List of chat filters has been updated."
(setq telega-tdlib--chat-filters
(append (plist-get event :chat_filters) nil))
;; Update custom filters ewoc
(with-telega-root-buffer
(telega-save-cursor
(telega-filters--refresh))
(run-hooks 'telega-root-update-hook))
)
;; Messages updates
(defun telega-msg-id= (msg1 msg2)
(= (plist-get msg1 :id) (plist-get msg2 :id)))
(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)
;; NOTE: View ignored message, so modeline/appindicator
;; won't show there is something important if ignored
;; message contains mention
(telega--viewMessages chat (list new-msg) 'force)
(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-chat-match-p
telega-chatbuf--chat telega-use-tracking-for)
(not (telega-msg-ignored-p new-msg))
(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 visibible in some window, then mark it as read
;; see https://github.com/zevlg/telega.el/issues/4
(when (telega-msg-observable-p new-msg telega-chatbuf--chat node)
(telega--viewMessages telega-chatbuf--chat (list 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)))
(ewoc-delete telega-chatbuf--ewoc node)
(with-telega-deferred-events
(if 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
(progn
(ewoc-enter-before telega-chatbuf--ewoc before-node new-msg)
(ewoc-invalidate telega-chatbuf--ewoc before-node))
(ewoc-enter-last telega-chatbuf--ewoc new-msg)))))))
(unless (plist-get new-msg :sending_state)
(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-code (plist-get event :error_code))
(err-msg (plist-get event :error_message)))
(message "telega: Failed to send message: %d %s" err-code err-msg)
))
(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
(telega-chatbuf--redisplay-node node)))))))
(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-button with message from EVENT,
;; then redisplay aux as well, see
;; https://t.me/emacs_telega/7243
(let ((aux-msg (or (telega-chatbuf--replying-msg)
(telega-chatbuf--editing-msg))))
(when (and aux-msg (eq (plist-get msg :id) (plist-get aux-msg :id)))
(cl-assert (not (button-get telega-chatbuf--aux-button 'invisible)))
(telega-save-excursion
(telega-button--update-value telega-chatbuf--aux-button msg))))
)))
(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))
(when node
(with-telega-chatbuf chat
(telega-chatbuf--redisplay-node node)
(when telega-chatbuf--thread-msg
(telega-chatbuf--footer-update))))))
(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))
(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-user--name user) (plist-get err :message))))
(callStateDiscarded
(let ((discad (plist-get state :reason))
(user (telega-user-get (plist-get call :user_id))))
(message "Call %s discaded: %s" (telega-user--name user)
(substring (plist-get discad :@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."
(if (plist-get event :is_masks)
(telega-debug "TODO: `telega--on-updateInstalledStickerSets' is_mask=True")
(setq telega--stickersets-installed-ids
(append (plist-get event :sticker_set_ids) nil))
;; NOTE: Refresh `telega--stickersets-installed' on next call to
;; `telega-stickerset-completing-read'
(setq telega--stickersets-installed nil)
;; Asynchronously update value for `telega--stickersets-installed'
;; and download covers for these sticker sets
;; (telega--getInstalledStickerSets nil
;; (lambda (ssets)
;; (setq telega--stickersets-installed ssets)
;; (dolist (sset ssets)
;; (mapc #'telega-sticker--download (plist-get sset :covers)))))
))
(defun telega--on-updateTrendingStickerSets (event)
"The list of trending sticker sets was updated or some of them were viewed."
(let ((ssets-info (telega--tl-get event :sticker_sets :sets)))
(setq telega--stickersets-trending
(append ssets-info nil))))
(defun telega--on-updateRecentStickers (event)
"Recent stickers has been updated."
;; NOTE: attached recent stickers are not supported
(unless (plist-get event :is_attached)
(setq telega--stickers-recent
(append (plist-get event :sticker_ids) nil))
;; Asynchronously download corresponding files
; (mapc 'telega--downloadFile telega--stickers-recent)
))
(defun telega--on-updateFavoriteStickers (event)
"Favorite stickers has been updated."
(setq telega--stickers-favorite
(append (plist-get event :sticker_ids) nil))
;; Asynchronously download corresponding files
; (mapc 'telega--downloadFile telega--stickers-favorite)
)
(defun telega--on-updateSavedAnimations (event)
"List of saved animations has been updated."
(setq telega--animations-saved
(append (plist-get event :animation_ids) nil))
;; Asynchronously download corresponding files
(when telega-animation-download-saved
(mapc 'telega--downloadFile telega--animations-saved)))
;; since TDLib 1.6.3
(defun telega--on-updateStickerSet (event)
(telega-stickerset--ensure (plist-get event :sticker_set)))
(defun telega--on-updateBasicGroup (event)
(let ((basicgroup (plist-get event :basic_group)))
(telega--info-update basicgroup)
(when telega--sort-criteria
(when-let ((chat (cl-find basicgroup telega--ordered-chats
:test 'eq :key #'telega-chat--info)))
(telega-chat--mark-dirty chat event))
;; TODO: chatbuf might need to be updated, status might be
;; changed due to someone removed me from basic group
)))
(defun telega--on-updateBasicGroupFullInfo (event)
(let ((ufi (cdr (assq 'basicGroup telega--full-info))))
(puthash (plist-get event :basic_group_id)
(plist-get event :basic_group_full_info) ufi)))
(defun telega--on-updateSupergroup (event)
"Handle supergroup update EVENT."
(let* ((supergroup (plist-get event :supergroup))
(old-my-status
(plist-get (telega--info 'supergroup (plist-get supergroup :id)
'locally) :status))
(me-was-owner (and old-my-status
(eq 'chatMemberStatusCreator
(telega--tl-type old-my-status)))))
(telega--info-update supergroup)
(when-let ((chat (cl-find supergroup telega--ordered-chats
:test 'eq :key 'telega-chat--supergroup)))
;; NOTE: notify if someone transferred ownership to me
(when (and (not me-was-owner)
(telega-chat-match-p chat 'me-is-owner))
(message "telega: me is now owner of the %s"
(telega-chat-title-with-brackets chat " ")))
(telega-chat--mark-dirty chat event)
;; NOTE: Chatbuf prompt might be affected as well by "status"
;; change, see `telega-chatbuf--unblock-start-join'
(with-telega-chatbuf chat
(telega-chatbuf--prompt-update)))
))
(defun telega--on-updateSupergroupFullInfo (event)
(let ((supergroup-id (plist-get event :supergroup_id))
(supergroup-fi (plist-get event :supergroup_full_info))
(fi-table (cdr (assq 'supergroup telega--full-info))))
(puthash supergroup-id supergroup-fi fi-table)
;; NOTE: if slow delay expiration changes, then save timestamp of
;; this update event, so we could calculate time left in slow mode
;; before expiration
(plist-put supergroup-fi :telega-update-event-timestamp
(unless (zerop (plist-get
supergroup-fi :slow_mode_delay_expires_in))
(float-time)))
;; Check number of the admins has been changed, it might be not up
;; to date, see https://github.com/tdlib/td/issues/1040
(when-let ((chat (telega-chat-get
(string-to-number (format "-100%d" supergroup-id))
'offline)))
;; TODO: Might affect root's buffer view
;; NOTE: chatbuf might need to be updated, since for example
;; pinned message might change
(telega-chat--mark-dirty chat event)
(with-telega-chatbuf chat
(unless (equal (plist-get supergroup-fi :administrator_count)
(length telega-chatbuf--administrators))
(telega-chatbuf--admins-fetch)))
)))