-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathtelega-ins.el
2547 lines (2336 loc) · 108 KB
/
telega-ins.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-ins.el --- Inserters for the telega -*- lexical-binding:t -*-
;; Copyright (C) 2018 by Zajcev Evgeny.
;; Author: Zajcev Evgeny <[email protected]>
;; Created: Sat Jul 14 19:06:40 2018
;; Keywords:
;; telega is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; telega is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with telega. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Inserter is the function that inserts some content.
;; Different inserters accepts different arguments
;; Inserter can examine previously inserted content.
;; Inserter returns non-nil if something was inserted and nil if
;; nothing has been inserted.
;;; Code:
(require 'format-spec)
(require 'telega-core)
(require 'telega-tdlib)
(require 'telega-inline)
(require 'telega-folders)
(require 'telega-customize)
;; telega-chat.el depends on telega-ins.el
(declare-function telega-msg-delete0 "telega-chat" (msg &optional revoke))
(declare-function telega-msg-redisplay "telega-chat" (msg &optional node))
(declare-function telega-chat-get "telega-chat" (chat-id &optional offline-p))
(declare-function telega-chat-color "telega-chat" (chat))
(declare-function telega-chat-title "telega-chat" (chat &optional with-username))
(declare-function telega-chat-title-with-brackets "telega-chat" (chat &optional with-username))
(declare-function telega-chat-public-p "telega-chat" (chat &optional chat-type))
(declare-function telega-chat--goto-msg "telega-chat" (chat msg-id &optional highlight))
(declare-function telega-chat-avatar-image "telega-chat" (chat))
(declare-function telega-chat-avatar-image-one-line "telega-chat" (chat))
(declare-function telega-describe-chat "telega-chat" (chat))
(declare-function telega-chat-private-p "telega-chat" (chat &optional inc-bots-p))
(declare-function telega-chat-secret-p "telega-chat" (chat))
(declare-function telega-chat-user "telega-chat" (chat &optional include-bots-p))
(declare-function telega-chat-brackets "telega-chat" (chat))
(declare-function telega-chat-muted-p "telega-chat" (chat))
(declare-function telega-chat--type "telega-chat" (chat &optional no-interpret))
(declare-function telega-chat-channel-p "telega-chat" (chat))
(declare-function telega-chat--info "telega-chat" (chat))
(declare-function telega-chat-pinned-msg "telega-chat" (chat &optional offline-p callback))
(declare-function telega-chat-delete "telega-chat" (chat &optional leave-p))
(declare-function telega-chat-admin-get "telega-chat" (chat user))
(declare-function telega-chat--update-pinned-message "telega-chat" (chat &optional offline-p old-pin-msg-id))
;; telega-filter.el depends on telega-ins.el
(declare-function telega-filter-chats "telega-filter" (chat-list &optional chat-filter))
(declare-function telega-chat-match-p "telega-filter" (chat chat-filter))
(defvar telega-filters--inhibit-list)
(defun telega-button--endings-func (label)
"Function to generate endings for the button with LABEL."
(if (member label (list " " " " "✕" telega-symbol-heavy-checkmark))
(cons "" "")
;; NOTE: In newer Emacs we can use cons as `:line-width` in
;; telega-button face, so width of the button is not increased in
;; contrast with using single negative number for `:line-width'
;; However is older Emacs this is not implemented, see
;; https://t.me/emacs_telega/22129
;; XXX inclose LABEL with shrink version of spaces, so button
;; width will be char aligned
;; NOTE: non-breakable space is used, so if line is feeded at the
;; beginning of button, it won't loose its leading space
(let* ((box-width (- (or (plist-get (face-attribute 'telega-button :box)
:line-width)
0)))
(space `(space (,(- (telega-chars-xwidth 1) box-width))))
(end (propertize "\u00A0" 'display space)))
(cons end end))))
(defun telega-ins--button (label &rest props)
"Insert pressable button labeled with LABEL.
If custom face is specified in PROPS, then
`telega-button--sensor-func' is not set as sensor function."
(declare (indent 1))
(unless (plist-get props 'face)
(let ((ends (if (functionp telega-button-endings)
(funcall telega-button-endings label)
telega-button-endings)))
(setq label (concat (or (car ends) "[")
label
(or (cdr ends) "]"))))
(setq props (plist-put props 'face 'telega-button))
(setq props (plist-put props 'cursor-sensor-functions
'(telega-button--sensor-func))))
(unless (plist-get props 'action)
(setq props (plist-put props 'action 'telega-button--action)))
(button-at (apply 'insert-text-button label props)))
(defun telega-ins--image (img &optional slice-num &rest props)
"Insert image IMG generated by telega.
Uses internal `:telega-text' to keep correct column.
If SLICE-NUM is specified, then insert single slice.
Special property `:no-display-if' is supported in PROPS to
ommit image display if value is for this property is non-nil."
;; NOTE: do not check SLICE-NUM
(let ((slice (when slice-num
(list 0 (telega-chars-xheight slice-num)
1.0 (telega-chars-xheight 1)))))
(telega-ins--with-props
(nconc (list 'rear-nonsticky '(display))
(unless (plist-get props :no-display-if)
(list 'display
(if slice
(list (cons 'slice slice) img)
img)))
props)
(telega-ins
(or (telega-image--telega-text img slice-num)
;; Otherwise use slow `image-size' to get correct
;; `:telega-text'
(make-string (telega-chars-in-width
(or (plist-get (cdr img) :width)
(progn
(telega-debug "WARN: `image-size' used for %S" img)
(cl-assert img)
(car (image-size img t (telega-x-frame)))))) ?X))))))
(defun telega-ins--image-slices (image &optional props slice-func)
"Insert sliced IMAGE at current column.
PROPS - additional image properties.
SLICE-FUNC - function called after inserting slice. Called with
single argument - slice number, starting from 0."
(declare (indent 2))
(if (or (not telega-use-images)
(not (display-graphic-p (telega-x-frame))))
(telega-ins "<IMAGE>")
(let* ((img-xheight (plist-get (cdr image) :height))
(img-slices (if img-xheight
(telega-chars-in-height img-xheight)
(ceiling
(progn
(warn "`image-size' used for %S" image)
(cdr (image-size image nil (telega-x-frame))))))))
(telega-ins--column (current-column) nil
(dotimes (slice-num img-slices)
(apply #'telega-ins--image image slice-num props)
(when slice-func
(funcall slice-func slice-num))
(unless (= slice-num (1- img-slices))
(telega-ins--with-props (list 'line-height t)
(telega-ins "\n"))))))))
(defun telega-ins--actions (actions)
"Insert chat ACTIONS alist."
(when actions
;; NOTE: Display only last action
(let* ((user (telega-user-get (caar actions)))
(action (cdar actions)))
(telega-ins (telega-user--name user 'short) " ")
(telega-ins
(propertize (concat "is " (substring (plist-get action :@type) 10))
'face 'shadow)))))
(defun telega-ins--filesize (filesize)
"Insert FILESIZE in human readable format."
(telega-ins (file-size-human-readable filesize)))
(defun telega-ins--date (timestamp)
"Insert TIMESTAMP.
Format is:
- HH:MM if today
- Mon/Tue/.. if on this week
- DD.MM.YY otherwise"
(let* ((dtime (decode-time timestamp))
(current-ts (time-to-seconds (current-time)))
(ctime (decode-time current-ts))
(today00 (telega--time-at00 current-ts ctime)))
(if (and (> timestamp today00)
(< timestamp (+ today00 (* 24 60 60))))
(telega-ins-fmt "%02d:%02d" (nth 2 dtime) (nth 1 dtime))
(let* ((week-day (nth 6 ctime))
(mdays (+ week-day
(- (if (< week-day telega-week-start-day) 7 0)
telega-week-start-day)))
(week-start00 (telega--time-at00
(- current-ts (* mdays 24 3600)))))
(if (and (> timestamp week-start00)
(< timestamp (+ week-start00 (* 7 24 60 60))))
(telega-ins (nth (nth 6 dtime) telega-week-day-names))
(telega-ins-fmt "%02d.%02d.%02d"
(nth 3 dtime) (nth 4 dtime) (- (nth 5 dtime) 2000))))
)))
(defun telega-ins--date-iso8601 (timestamp &rest args)
"Insert TIMESTAMP in ISO8601 format."
(apply 'telega-ins (format-time-string "%FT%T%z" timestamp) args))
(defun telega-ins--date-full (timestamp &rest args)
"Insert TIMESTAMP in full format - DAY MONTH YEAR."
(apply 'telega-ins (format-time-string "%d %B %Y" timestamp) args))
(defun telega-ins--msg-sender (msg-sender &optional prefer-username)
"Insert message sender.
If PREFER-USERNAME is non-nil, then prefer using @<username>
instead of the sender title."
(cl-assert msg-sender)
(telega-ins (or (when prefer-username
(telega-msg-sender-username msg-sender 'with-a))
(telega-msg-sender-title msg-sender))))
(defun telega-ins--username (user-id &optional fmt-type)
"Insert username for user denoted by USER-ID
FMT-TYPE is passed directly to `telega-user--name' (default=`short')."
(unless (zerop user-id)
(telega-ins
(telega-user--name (telega-user-get user-id) (or fmt-type 'short)))))
(defun telega-ins--chat-member-status (status)
"Format chat member STATUS."
(telega-ins
(cl-case (telega--tl-type status)
(chatMemberStatusAdministrator
(or (telega-tl-str status :custom_title)
(telega-i18n "lng_admin_badge")))
(chatMemberStatusCreator
(or (telega-tl-str status :custom_title)
(telega-i18n "lng_owner_badge")))
(chatMemberStatusMember
nil)
(t
(downcase (substring (plist-get status :@type) 16))))))
(defun telega-ins--user-status (user)
"Insert USER's online status."
;; TODO: check online's `:expires'
(let* ((status (telega--tl-type (plist-get user :status)))
(online-dur (- (telega-time-seconds)
(or (plist-get user :telega-last-online) 0))))
(telega-ins--with-face (if (eq status 'userStatusOnline)
'telega-user-online-status
'telega-user-non-online-status)
(telega-ins
(cond ((eq status 'userStatusOnline)
;; I18N: lng_status_online
"online")
((< online-dur 60)
;; I18N: lng_status_lastseen_now
"last seen just now")
((< online-dur (* 3 24 60 60))
(format "last seen in %s"
(telega-duration-human-readable online-dur 1)))
((eq status 'userStatusRecently)
;; I18N: lng_status_recently
"last seen recently")
(t
;; TODO: other cases
(symbol-name status)))))))
(defun telega-ins--user-relationship (user)
"Insert relationship with USER.
🚹←🚹 - if user is contact.
🚹↔🚹 - if user is mutual contact."
(let ((contact-p (plist-get user :is_contact))
(mutual-contact-p (plist-get user :is_mutual_contact)))
(when (or contact-p mutual-contact-p)
(telega-ins (propertize "🚹" 'face 'shadow)
(if mutual-contact-p "↔" "←")
(propertize "🚹" 'face 'bold)))))
(defun telega-ins--user-nearby-distance (user)
"Insert distance to the USER nearby."
(when-let* ((user-chat (telega-chat-get (plist-get user :id) 'offline))
(distance (telega-chat-nearby-distance user-chat)))
(telega-ins--with-face 'shadow
(telega-ins (telega-distance-human-readable distance) " away"))))
(defun telega-ins--user (user &optional member show-phone-p)
"Insert USER, aligning multiple lines at current column.
MEMBER specifies corresponding \"ChatMember\" object.
If SHOW-PHONE-P is non-nil, then show USER's phone number."
(let ((avatar (telega-user-avatar-image user))
(off-column (telega-current-column)))
(telega-ins--image avatar 0
:no-display-if (not telega-user-show-avatars))
(telega-ins (telega-user--name user 'name))
(telega-ins--user-online-status user)
(when-let ((username (telega-tl-str user :username)))
(telega-ins " ")
(telega-ins--with-face 'telega-username
(telega-ins "@" username)))
(when (and member
(telega-ins-prefix " ("
(telega-ins--chat-member-status
(plist-get member :status))))
(telega-ins ")"))
(when show-phone-p
(telega-ins-prefix " +"
(telega-ins (plist-get user :phone_number))))
;; Insert (him)in<-->out(me) relationship
(when (and telega-user-show-relationship
(not (telega-me-p user)))
(telega-ins " ")
(telega-ins--user-relationship user))
;; Block/scam mark, without requesting
(when (telega-msg-sender-blocked-p user 'locally)
(telega-ins " " telega-symbol-blocked))
(telega-ins "\n")
(telega-ins (make-string off-column ?\s))
(telega-ins--image avatar 1
:no-display-if (not telega-user-show-avatars))
(telega-ins--user-status user)
(when-let ((join-date (plist-get member :joined_chat_date)))
(unless (zerop join-date)
(telega-ins ", joined ")
(telega-ins--date join-date)))
(telega-ins-prefix ", "
(telega-ins--user-nearby-distance user))
t))
(defun telega-ins--chat-member (member)
"Formatting for the chat MEMBER.
Return COLUMN at which user name is inserted."
(telega-ins--user
(telega-user-get (plist-get member :user_id)) member))
(defun telega-ins--user-list (users &optional button-type)
"Insert list of the USERS using BUTTON-TYPE.
By default BUTTON-TYPE is `telega-user'."
(while users
(telega-ins " ")
(telega-button--insert (or button-type 'telega-user) (car users))
(setq users (cdr users))
(when users
(telega-ins "\n")
;; NOTE: to apply `height' property \n must be included
(telega-ins--with-props
'(face default display ((space-width 2) (height 0.5)))
(telega-ins--column 4 nil
(telega-ins (make-string 30 ?─) "\n"))))
t))
(defun telega-ins--chat-members (members)
"Insert chat MEMBERS list."
(telega-ins--user-list members 'telega-member)
(telega-ins "\n"))
(defun telega-ins-progress-bar (progress duration nbars &optional p-char e-char)
"Insert progress bar for PROGRESS at overall DURATION.
Use NBARS characters for progress bar.
P-CHAR - progress char, default is \".\"
E-CHAR - empty char, default is space."
(let ((pbars (ceiling
(* nbars (/ (or progress 0)
(if (zerop duration) 0.1 duration))))))
(when (> pbars nbars) (setq pbars nbars)) ; check for overflows
(telega-ins (make-string pbars (or p-char ?\.))
(make-string (- nbars pbars) (or e-char ?\s)))))
(defun telega-ins--file-progress (msg file)
"Insert Upload/Download status for the document."
;; Downloading status:
;; /link/to-file if file has been downloaded
;; [Download] if no local copy
;; [... 20%] [Cancel] if download in progress
(cond ((telega-file--uploading-p file)
(let ((progress (telega-file--uploading-progress file))
(nbsp-char 160))
(telega-ins "[")
(telega-ins-progress-bar progress 1.0 10 ?\+ nbsp-char)
(telega-ins-fmt "%d%%]%c" (round (* progress 100)) nbsp-char)
(telega-ins--button "Cancel"
'action (lambda (_ignored)
(telega-msg-delete0 msg)))))
((telega-file--downloading-p file)
(let ((progress (telega-file--downloading-progress file))
(nbsp-char 160))
(telega-ins "[")
(telega-ins-progress-bar progress 1.0 10 ?\. nbsp-char)
(telega-ins-fmt "%d%%]%c" (round (* progress 100)) nbsp-char)
(telega-ins--button "Cancel"
'action (lambda (_ignored)
(telega--cancelDownloadFile
(plist-get file :id))))))
((not (telega-file--downloaded-p file))
(telega-ins--button "Download"
'action (lambda (_ignored)
(telega-file--download file 32
(lambda (_fileignored)
(telega-msg-redisplay msg))))))))
(defun telega-ins--outgoing-status (msg)
"Insert outgoing status of the message MSG."
(when (plist-get msg :is_outgoing)
(let ((sending-state (plist-get (plist-get msg :sending_state) :@type))
(chat (telega-chat-get (plist-get msg :chat_id))))
(telega-ins--with-face 'telega-msg-outgoing-status
(telega-ins
(cond ((plist-get msg :scheduling_state)
(telega-symbol 'alarm))
((and (stringp sending-state)
(string= sending-state "messageSendingStatePending"))
(telega-symbol 'pending))
((and (stringp sending-state)
(string= sending-state "messageSendingStateFailed"))
(telega-symbol 'failed))
((>= (plist-get chat :last_read_outbox_message_id)
(plist-get msg :id))
telega-symbol-heavy-checkmark)
(t telega-symbol-checkmark)))))))
(defun telega-ins--fmt-text-as-markdown (fmt-text)
"Insert formatted text FMT-TEXT as markdown syntax."
(when fmt-text
(telega-ins
(telega--desurrogate-apply (telega--fmt-text-markdown fmt-text)))))
(defun telega-ins--fmt-text (fmt-text &optional for-msg)
"Insert formatted TEXT applying telegram entities.
If AS-MARKDOWN is non-nil, then instead of applying faces, apply
markdown syntax to the TEXT.
FOR-MSG is message we are inserting TEXT for."
(when fmt-text
(telega-ins
(telega--desurrogate-apply (telega--fmt-text-faces fmt-text for-msg)))))
(defun telega-ins--photo (photo &optional msg limits show-details)
"Inserter for the PHOTO.
SHOW-DETAILS - non-nil to show photo details."
(let* ((hr (telega-photo--highres photo))
(hr-file (telega-file--renew hr :photo))
(show-progress
(and (telega-file--downloading-p hr-file) msg)))
;; Show photo details and download progress for highres thumbnail
(when (or show-details show-progress)
;; Monitor downloading progress for the HR-FILE
(when show-progress
(telega-file--download hr-file 32
(lambda (_fileignored)
(telega-msg-redisplay msg))))
(telega-ins (telega-symbol 'photo) " ")
(telega-ins-fmt "(%dx%d %s)"
(plist-get hr :width) (plist-get hr :height)
(file-size-human-readable (telega-file--size hr-file)))
(when show-progress
(telega-ins " ")
(telega-ins--file-progress msg hr-file))
(telega-ins "\n"))
(if (telega--tl-get msg :content :is_secret)
(let ((ttl-in (or (plist-get msg :ttl_expires_in) 0)))
(when (> ttl-in 0)
(telega-ins (propertize "Self-descruct in" 'face 'shadow) " "
(telega-duration-human-readable ttl-in) "\n"))
(telega-ins--image-slices
(telega-self-destruct-create-svg
(plist-get photo :minithumbnail)
(telega-symbol (if (> ttl-in 0) 'flames 'lock)))))
(telega-ins--image-slices
(telega-photo--image photo (or limits telega-photo-size-limits)))
)))
(defun telega-ins--audio (msg &optional audio music-symbol)
"Insert audio message MSG.
If MUSIC-SYMBOL is specified, use it instead of play/pause."
(unless audio
(setq audio (telega--tl-get msg :content :audio)))
(let* ((dur (plist-get audio :duration))
(proc (plist-get msg :telega-ffplay-proc))
(proc-status (and (process-live-p proc)
(process-status proc)))
(played (and proc-status
(plist-get (process-plist proc) :progress)))
(audio-name (plist-get audio :file_name))
(audio-file (telega-file--renew audio :audio)))
;; Play/pause and downloading status
(if (eq proc-status 'run)
(telega-ins (or music-symbol (telega-symbol 'pause)))
(telega-ins (or music-symbol (telega-symbol 'play))))
(telega-ins " ")
(telega-ins--with-attrs (list :max (/ telega-chat-fill-column 2)
:elide t
:elide-trail (/ telega-chat-fill-column 4))
(if (telega-file--downloaded-p audio-file)
(let ((local-path (telega--tl-get audio-file :local :path)))
(telega-ins--with-props
(telega-link-props 'file local-path)
(telega-ins (telega-short-filename local-path))))
(telega-ins audio-name)))
(telega-ins-fmt " (%s %s)"
(file-size-human-readable (telega-file--size audio-file))
(telega-duration-human-readable dur))
(when msg
(telega-ins-prefix " "
(telega-ins--file-progress msg audio-file)))
;; Progress and [Stop] button
(when played
(telega-ins "\n")
(unless (zerop dur)
(telega-ins "[")
(telega-ins-progress-bar
played dur (/ telega-chat-fill-column 2) ?\. ?\s)
(telega-ins "] "))
(telega-ins--button "Stop"
'action (lambda (_ignored)
(telega-ffplay-stop))))
;; Title --Performer
(when-let ((title (telega-tl-str audio :title)))
(telega-ins "\n")
(telega-ins--with-face 'bold
(telega-ins title))
(when-let ((performer (telega-tl-str audio :performer)))
(telega-ins " --" performer)))
;; Album cover
(let ((thumb (plist-get audio :album_cover_thumbnail))
(minithumb (plist-get audio :album_cover_minithumbnail)))
(when (or minithumb thumb)
(telega-ins "\n")
(let ((timg (telega-media--image
(cons audio 'telega-audio--create-image)
(cons thumb :file))))
(telega-ins--image-slices timg))
(telega-ins " ")))
t))
(defun telega-ins--video (msg &optional video no-thumbnail-p)
"Insert video message MSG.
If NO-THUMBNAIL-P is non-nil, then do not insert thumbnail."
(unless video
(setq video (telega--tl-get msg :content :video)))
(let ((video-name (telega-tl-str video :file_name))
(video-file (telega-file--renew video :video)))
(telega-ins (telega-symbol 'video) " ")
(if (telega-file--downloaded-p video-file)
(let ((local-path (telega--tl-get video-file :local :path)))
(telega-ins--with-props
(telega-link-props 'file local-path)
(telega-ins (telega-short-filename local-path))))
(telega-ins (or video-name "")))
(telega-ins-fmt " (%dx%d %s %s)"
(plist-get video :width)
(plist-get video :height)
(file-size-human-readable (telega-file--size video-file))
(telega-duration-human-readable (plist-get video :duration)))
(telega-ins-prefix " "
(telega-ins--file-progress msg video-file))
;; Video's thumbnail, if any
(unless no-thumbnail-p
(if (telega--tl-get msg :content :is_secret)
;; Secret video
(let ((ttl-in (or (plist-get msg :ttl_expires_in) 0)))
(when (> ttl-in 0)
(telega-ins "\n"
(propertize "Self-descruct in" 'face 'shadow) " "
(telega-duration-human-readable ttl-in)
"\n"))
(telega-ins--image-slices
(telega-self-destruct-create-svg
(plist-get video :minithumbnail)
(telega-symbol (if (> ttl-in 0) 'flames 'lock)))))
(let ((thumb (plist-get video :thumbnail))
(minithumb (plist-get video :minithumbnail)))
(when (or thumb minithumb)
(telega-ins "\n")
(telega-ins--image-slices
(telega-media--image
(cons video 'telega-thumb-or-minithumb--create-image)
(cons thumb :file)))
(telega-ins " ")))))
t))
(defun telega-ins--voice-note (msg &optional voice-note)
"Insert message MSG with VOICE-NOTE content."
(let* ((note (or voice-note (telega--tl-get msg :content :voice_note)))
(dur (plist-get note :duration))
(proc (plist-get msg :telega-ffplay-proc))
(proc-status (and (process-live-p proc)
(process-status proc)))
(played (and proc-status
(plist-get (process-plist proc) :progress)))
(note-file (telega-file--renew note :voice))
(waveform (plist-get note :waveform))
(waves (telega-vvnote--waveform-decode waveform)))
;; play/pause only for messages
(when msg
(if (eq proc-status 'run)
(telega-ins (telega-symbol 'pause))
(telega-ins (telega-symbol 'play)))
(telega-ins " "))
;; waveform image
(if telega-use-images
(telega-ins--image
(telega-vvnote--waves-svg
waves (round (telega-chars-xheight
telega-vvnote-waves-height-factor))
dur played))
;; tty version
(telega-ins "[")
(telega-ins-progress-bar played dur 15 ?\# ?\.)
(telega-ins "]"))
;; Duration
(telega-ins " (" (telega-duration-human-readable dur) ")")
;; Show download status/button only if inserted for message
(when msg
(when (telega--tl-get msg :content :is_listened)
(telega-ins (telega-symbol 'eye)))
(telega-ins-prefix " "
(telega-ins--file-progress msg note-file)))
))
(defun telega-ins--video-note (msg &optional video-note)
"Insert message MSG with VIDEO-NOTE content."
(let* ((note (or video-note (telega--tl-get msg :content :video_note)))
(note-file (telega-file--renew note :video))
(viewed-p (telega--tl-get msg :content :is_viewed)))
(telega-ins (propertize "NOTE" 'face 'shadow))
(telega-ins-fmt " (%dx%d %s %s)"
(plist-get note :length) (plist-get note :length)
(file-size-human-readable (telega-file--size note-file))
(telega-duration-human-readable (plist-get note :duration)))
(when viewed-p
(telega-ins (telega-symbol 'eye)))
(telega-ins-prefix " "
(telega-ins--file-progress msg note-file))
(let ((thumb (plist-get note :thumbnail))
(minithumb (plist-get note :minithumbnail)))
(when-let ((img (or (plist-get msg :telega-ffplay-frame)
(when (or minithumb thumb)
(telega-media--image
(cons note 'telega-vvnote-video--create-image)
(cons thumb :file))))))
(telega-ins "\n")
(telega-ins--image-slices img)
(telega-ins " ")))))
(defun telega-ins--document-header (doc &optional no-attach-symbol)
"Attach header for the document DOC.
If NO-ATTACH-SYMBOL is specified, then do not insert attachment symbol."
(let ((fname (telega-tl-str doc :file_name))
(doc-file (telega-file--renew doc :document)))
(unless no-attach-symbol
(telega-ins telega-symbol-attachment " "))
(if (telega-file--downloaded-p doc-file)
(telega-ins--with-face 'telega-link
(telega-ins (telega-short-filename
(telega--tl-get doc-file :local :path))))
(telega-ins fname))
(telega-ins " (" (file-size-human-readable
(telega-file--size doc-file)) ") ")))
(defun telega-ins--document (msg &optional doc)
"Insert document DOC."
(unless doc
(setq doc (telega--tl-get msg :content :document)))
(telega-ins--document-header doc)
(telega-ins--file-progress msg (telega-file--renew doc :document))
;; document's thumbnail preview (if any)
(let ((thumb (plist-get doc :thumbnail))
(minithumb (plist-get doc :minithumbnail)))
(when (or thumb minithumb)
(telega-ins "\n")
(telega-ins--image-slices
(telega-media--image
(cons doc 'telega-thumb-or-minithumb--create-image)
(cons thumb :file)))
(telega-ins " "))))
(defun telega-ins--game (msg &optional game-value)
"Insert GAME."
(let ((game (or game-value (telega--tl-get msg :content :game))))
(telega-ins telega-symbol-game " "
(propertize "GAME" 'face 'shadow)
"\n")
(telega-ins (telega-symbol 'vertical-bar))
(telega-ins--with-attrs (list :fill-prefix (telega-symbol 'vertical-bar)
:fill-column (- telega-chat-fill-column 4)
:fill 'left)
(when-let ((photo (plist-get game :photo)))
(telega-ins--photo photo msg)
(telega-ins "\n"))
(telega-ins--with-face 'telega-webpage-sitename
(telega-ins (telega-tl-str game :title)))
(telega-ins "\n")
(unless (telega-ins--fmt-text (plist-get game :text) msg)
(telega-ins (telega-tl-str game :description)))
t)))
(defun telega-ins--webpage (msg &optional web-page)
"Insert WEB-PAGE preview.
Return `non-nil' if WEB-PAGE has been inserted."
(unless web-page
(setq web-page (telega--tl-get msg :content :web_page)))
(when web-page
(telega-ins (telega-symbol 'vertical-bar))
(telega-ins--with-attrs (list :fill-prefix (telega-symbol 'vertical-bar)
:fill-column (- telega-chat-fill-column 4)
:fill 'left)
(when-let ((sitename (telega-tl-str web-page :site_name)))
(telega-ins--with-face 'telega-webpage-sitename
(telega-ins sitename))
(telega-ins "\n"))
(when-let ((title (telega-tl-str web-page :title)))
(telega-ins--with-face 'telega-webpage-title
(telega-ins title))
(telega-ins "\n"))
(when-let ((desc (telega-tl-str web-page :description)))
(telega-ins desc)
(telega-ins "\n"))
;; NOTE: animation uses it's own thumbnails
(let ((photo (plist-get web-page :photo)))
(when (and photo (not (plist-get web-page :animation)))
(telega-ins--photo photo msg telega-webpage-preview-size-limits)
(telega-ins "\n"))
;; See `telega-msg-open-webpage'
(cond ((plist-get web-page :video)
(telega-ins--video
msg (plist-get web-page :video) (when photo 'no-thumbnail))
(telega-ins "\n"))
((plist-get web-page :animation)
(telega-ins--animation-msg msg (plist-get web-page :animation))
(telega-ins "\n"))
((plist-get web-page :document)
(telega-ins--document msg (plist-get web-page :document))
(telega-ins "\n")))))
;; Additional View button
(if (zerop (plist-get web-page :instant_view_version))
(when-let ((title (pcase (plist-get web-page :type)
("telegram_channel" "VIEW CHANNEL")
((or "telegram_chat"
"telegram_megagroup") "VIEW GROUP")
("telegram_message" "VIEW MESSAGE")
("telegram_background" "VIEW BACKGROUND")
("telegram_user" "VIEW CHAT")
("telegram_theme" "VIEW THEME"))))
(telega-ins--button (concat " " title " ")
'action 'telega-msg-button--action))
(telega-ins--button
(concat " " telega-symbol-thunder " INSTANT VIEW ")
'action 'telega-msg-button--iv-action))
;; Remove trailing newline, if any
(when (= (char-before) ?\n)
(delete-char -1))
t))
(defun telega-ins--location (location)
"Inserter for the LOCATION."
(telega-ins (telega-symbol 'location) " ")
(telega-ins-fmt "%fN, %fE"
(plist-get location :latitude) (plist-get location :longitude)))
(defun telega-ins--location-live (msg)
"Insert live location description for location message MSG."
;; NOTE: in case of unexpired live location show last update
;; time and expiration period
(let* ((content (plist-get msg :content))
(live-period (plist-get content :live_period))
(expires-in (plist-get content :expires_in)))
(unless (or (zerop live-period) (zerop expires-in))
(telega-ins " " (propertize "Live" 'face 'shadow))
(let* ((current-ts (truncate (float-time)))
(since (if (zerop (plist-get msg :edit_date))
(plist-get msg :date)
(plist-get msg :edit_date)))
(live-for (- (+ since expires-in) current-ts)))
(when (> live-for 0)
(telega-ins-fmt " for %s"
(telega-duration-human-readable live-for 1))
(telega-ins-fmt " (updated %s ago)"
(telega-duration-human-readable
(- current-ts since) 1)))))))
(defun telega-ins--contact (contact &optional no-phone)
"One line variant inserter for CONTACT."
(telega-ins telega-symbol-contact " ")
(telega-ins (telega-user--name contact 'name))
(when (eq (telega--tl-type contact) 'user)
(telega-ins--user-online-status contact))
(unless no-phone
(telega-ins " (" (plist-get contact :phone_number) ")")))
(defun telega-ins--contact-msg (msg)
"Inserter for contact message MSG."
;; Two lines for the contact
(let* ((content (plist-get msg :content))
(contact (plist-get content :contact))
(user-id (plist-get contact :user_id))
(user (unless (zerop user-id) (telega-user-get user-id)))
(user-ava (when (and telega-user-show-avatars user)
(telega-user-avatar-image user))))
(when user-ava
(telega-ins--image user-ava 0))
(telega-ins--contact (plist-get content :contact))
(telega-ins "\n")
(when user-ava
(telega-ins--image user-ava 1))
(telega-ins--button (concat " VIEW CONTACT ")
'action 'telega-msg-button--action)))
(defun telega-ins--call-msg (msg)
"Insert call message MSG."
(let* ((content (plist-get msg :content))
(video-p (plist-get content :is_video))
(call-symbol (telega-symbol (if video-p 'video 'phone)))
(reason (telega--tl-type (plist-get content :discard_reason)))
(label (cond ((plist-get msg :is_outgoing)
(if (eq reason 'callDiscardReasonMissed)
"Cancelled call" ;; I18N: lng_call_cancelled
"Outgoing call")) ;; I18N: lng_call_outgoing
((eq reason 'callDiscardReasonMissed)
"Missed call") ;; I18N: lng_call_missed
((eq reason 'callDiscardReasonDeclined)
"Declined call") ;; I18N: lng_call_declined
(t
"Incoming call")))) ;; I18N: lng_call_incoming
(telega-ins (cond ((memq reason '(callDiscardReasonMissed
callDiscardReasonDeclined))
(propertize call-symbol 'face 'error))
((plist-get msg :is_outgoing)
(concat call-symbol "🠺"))
(t
(concat call-symbol "🠸"))) " ")
(telega-ins (propertize label 'face 'shadow))
(telega-ins-fmt " (%s)"
(telega-duration-human-readable
(plist-get content :duration)))))
(defun telega-ins--dice-msg (msg &optional one-line-p)
"Inserter for the \"messageDice\" MSG."
(let* ((content (plist-get msg :content))
(dice-value (plist-get content :value))
(dice-emoji (telega-tl-str content :emoji)))
(telega-ins (or dice-emoji (car telega-symbol-dice-list)) " ")
(telega-ins--with-face 'shadow
(telega-ins-i18n "telega_random_dice"))
(telega-ins " " (number-to-string dice-value))
(unless one-line-p
(telega-ins "\n")
;; TODO: Animated sticker as dice roll, also see
;; `:initial_state_sticker'
(if-let ((dice-isticker nil)) ; (plist-get content :final_state_sticker)))
(telega-ins--sticker-image dice-isticker 'slices)
(let ((dice-symbol (or (nth dice-value telega-symbol-dice-list)
(number-to-string dice-value))))
(if telega-emoji-use-images
(telega-ins--image-slices
(telega-emoji-create-svg dice-symbol (car telega-sticker-size)))
(telega-ins dice-symbol))
))
)))
(defun telega-ins--invoice (invoice)
"Insert invoice message MSG."
(let ((title (plist-get invoice :title))
(desc (plist-get invoice :description))
(photo (plist-get invoice :photo)))
(telega-ins telega-symbol-invoice " ")
(telega-ins-fmt "%.2f%s" (/ (plist-get invoice :total_amount) 100.0)
(plist-get invoice :currency))
(when (plist-get invoice :is_test)
(telega-ins " (Test)"))
(telega-ins "\n")
(when photo
(telega-ins--photo photo)
(telega-ins "\n"))
(telega-ins--with-face 'telega-webpage-title
(telega-ins title))
(telega-ins "\n")
(telega-ins desc)))
(defun telega-ins--poll (msg)
"Insert poll message MSG."
(let* ((content (plist-get msg :content))
(poll (plist-get content :poll))
(poll-type (plist-get poll :type))
(closed-p (plist-get poll :is_closed))
(anonymous-p (plist-get poll :is_anonymous))
(options (append (plist-get poll :options) nil))
(choices (cl-loop for popt in options
for popt-id from 0
if (plist-get popt :is_chosen)
collect popt-id))
(quiz-p (eq (telega--tl-type poll-type) 'pollTypeQuiz))
(quiz-popt-id (when quiz-p
(plist-get poll-type :correct_option_id)))
(multiple-answers-p (unless quiz-p
(plist-get poll-type :allow_multiple_answers)))
(max-opt-len (apply #'max
(mapcar #'length
(mapcar (telega--tl-prop :text)
options))))
(option-symbols (if quiz-p
telega-symbol-quiz-options
(if multiple-answers-p
telega-symbol-poll-multiple-options
telega-symbol-poll-options)))
(opt-sym-len (apply #'max (mapcar #'string-width option-symbols))))
;; Poll header
;; NOTE: Currently all polls are anonymous, this might be changed
;; in future See https://telegram.org/blog/polls
(telega-ins telega-symbol-poll " ")
(telega-ins--with-face 'shadow
(telega-ins-i18n (cond ((and anonymous-p quiz-p) "lng_polls_anonymous_quiz")
(anonymous-p "lng_polls_anonymous")
(quiz-p "lng_polls_public_quiz")
(t "lng_polls_public"))))
(when (and quiz-p (plist-get poll-type :explanation))
(telega-ins " " (telega-symbol 'bulp)))
;; I18N: polls_votes_count -> {count} votes
(telega-ins ", " (telega-i18n (if quiz-p
"lng_polls_answers_count"
"lng_polls_votes_count")
:count (plist-get poll :total_voter_count)))
(when closed-p
(telega-ins ", " (propertize "closed" 'face 'error)))
(when (and (not closed-p) (plist-get msg :can_be_edited))
(telega-ins " ")
(telega-ins--button (concat "Close " (if quiz-p "Quiz" "Poll"))
'action (lambda (_ignored)
(when (yes-or-no-p (telega-i18n "lng_polls_stop_warning"))
(telega--stopPoll msg)))))
(telega-ins "\n")
;; Question and options
(telega-ins--with-face 'bold
(telega-ins (telega-tl-str poll :question)))
(dotimes (popt-id (length options))
(let ((popt (nth popt-id options)))
(telega-ins "\n")
(telega-ins--raw-button
(list 'action (lambda (_ignore)
(if (plist-get popt :is_chosen)
(apply #'telega--setPollAnswer msg
(delete popt-id choices))
(if multiple-answers-p
(apply #'telega--setPollAnswer msg
(cons popt-id choices))
(telega--setPollAnswer msg popt-id)))))
(telega-ins--with-face 'telega-link
(telega-ins
(if quiz-p
(cond ((eq quiz-popt-id popt-id)
(if (plist-get popt :is_chosen)
(propertize (nth 0 option-symbols) 'face 'bold)
(nth 0 option-symbols)))
((plist-get popt :is_chosen)
(propertize (nth 2 option-symbols) 'face 'error))
(t
(nth 1 option-symbols)))