-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
telega-ins.el
4498 lines (4181 loc) · 193 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 'transient)
(require 'telega-core)
(require 'telega-tdlib)
(require 'telega-inline)
(require 'telega-folders)
(require 'telega-topic)
(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-title "telega-chat" (chat &optional no-badges))
(declare-function telega-chat--goto-msg "telega-chat" (chat msg-id &optional highlight callback))
(declare-function telega-describe-chat "telega-chat" (chat))
(declare-function telega-chat-secret-p "telega-chat" (chat))
(declare-function telega-chat-user "telega-chat" (chat))
(declare-function telega-chat-muted-p "telega-chat" (chat))
(declare-function telega-chat-channel-p "telega-chat" (chat))
(declare-function telega-chat--info "telega-chat" (chat))
(declare-function telega-chat-delete "telega-chat" (chat &optional leave-p))
(declare-function telega-chat-admin-get "telega-chat" (chat user))
(declare-function telega-topic-button-action "telega-root" (chat-topic))
(defun telega-ins--text-button (label &rest props)
"Insert pressable button labeled with LABEL."
(declare (indent 1))
(unless (plist-get props 'action)
(setq props (plist-put props 'action 'telega-button--action)))
(telega-ins--raw-button props
(telega-ins label)))
(defun telega-box-button--endings-func (label)
"Function to generate endings for the button with LABEL."
(cond
((member label (list " " " " "✕" telega-symbol-heavy-checkmark))
;; NOTE: " " is arguable, incorrect rendering for @chessy_bot
(cons "" ""))
((or (string-equal label "2×")
(string-equal label "1×")
(string-equal label telega-symbol-button-close))
;; Special case for voice/video notes speedup button
;; and for `telega-symbol-button-close'
(let ((half-space (propertize " " 'display '(space :width 0.5))))
(cons half-space half-space)))
(t
;; NOTE: In newer Emacs we can use cons as `:line-width` in
;; telega-box-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
;;
;; We handle both cases, for `:line-width' as cons and as negative
;; number
;; 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* ((line-width (plist-get (face-attribute 'telega-box-button :box)
:line-width))
(box-width (if (consp line-width)
(car line-width)
(- (or line-width 0))))
(space (when (> box-width 0)
`(space (,(- (telega-chars-xwidth 1) box-width)))))
(end (if space
(propertize telega-symbol-nbsp 'display space)
telega-symbol-nbsp)))
(cons end end)))))
(defun telega-ins--box-button (label &rest props)
"Insert box button."
(declare (indent 1))
(let ((ends (if (functionp telega-box-button-endings)
(funcall telega-box-button-endings label)
telega-box-button-endings)))
(setq label (concat (or (car ends) "[")
label
(or (cdr ends) "]"))))
(setq props (plist-put props 'face 'telega-box-button))
(setq props (plist-put props 'cursor-sensor-functions
'(telega-button-highlight--sensor-func)))
;; NOTE: buttons must not be breakable by filling logic, so we use
;; non-breakable spaces instead of regular
(apply #'telega-ins--text-button
(replace-regexp-in-string " " telega-symbol-nbsp 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.
SLICE-NUM can be a list in form (SLICE-NUM SLICE-Y SLICE-H).
Special property `:no-display-if' is supported in PROPS to
ommit image display if value is for this property is non-nil."
;; NOTE: IMG might be nil if `telega-use-images' is nil
;; See https://github.com/zevlg/telega.el/issues/274
(if (or (not img) (not telega-use-images)
(not (display-graphic-p (telega-x-frame))))
(telega-ins (or (plist-get props :telega-text)
(telega-image--telega-text img slice-num)
"<IMAGE>"))
;; NOTE: do not check SLICE-NUM
(let ((slice (cond ((numberp slice-num)
(list 0 (telega-chars-xheight slice-num)
1.0 (telega-chars-xheight 1)))
((listp slice-num)
(prog1
(list 0 (nth 1 slice-num)
1.0 (nth 2 slice-num))
(setq slice-num (nth 0 slice-num))))
(slice-num
(error "Invalid slice-num: %S" slice-num)))))
(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 (plist-get props :telega-text)
(telega-image--telega-text img slice-num)
(make-string (ceiling (car (image-size img nil (telega-x-frame))))
?X)))))))
(defun telega-image--adjust-slice (start end slice-hoff slice-height)
"Adjust slice of the image display at START END region."
(when-let ((dsp (get-text-property start 'display)))
(when (listp dsp)
(let ((old-slice (nth 0 dsp))
(img (nth 1 dsp)))
(unless (and (eq (nth 2 old-slice) slice-hoff)
(eq (nth 4 old-slice) slice-height))
;; Slice parameters has been changed, need to update
(set-text-properties start end
`(display ((slice 0 ,slice-hoff 1.0 ,slice-height)
,img))))))))
(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>")
;; NOTE: it is okay to call `image-size' now, because image will
;; be read anyway, and `image-size' puts image to the image cache
(let ((nslices (or (plist-get (cdr image) :telega-nslices)
(round (cdr (image-size image nil (telega-x-frame)))))))
(dotimes (slice-num nslices)
(apply #'telega-ins--image image slice-num props)
(when slice-func
(funcall slice-func slice-num))
;; NOTE: do not insert newline for the last slice
(when (< (1+ slice-num) nslices)
(telega-ins--with-props (list 'line-height t)
(telega-ins "\n"))))
(> nslices 0))))
(defun telega-ins--actions (actions)
"Insert chat ACTIONS alist."
(when actions
;; NOTE: Display only first action
(let* ((first-action (car actions))
(sender (telega-msg-sender (car first-action)))
(action (cdr first-action))
(sender-title (telega-ins--as-string
(telega-ins--msg-sender sender :with-avatar-p t)))
(_start (point)))
(prog1
(telega-ins--with-face 'telega-shadow
(telega-ins (telega-symbol 'typing))
(cl-case (telega--tl-type action)
(chatActionTyping
(let ((more-senders
(delq nil (mapcar (lambda (spec)
(when (eq (telega--tl-type (cdr spec))
'chatActionTyping)
(telega-msg-sender (car spec))))
(cdr actions)))))
(cond ((= 0 (length more-senders))
(telega-ins-i18n "lng_user_typing"
:user sender-title))
((= 1 (length more-senders))
(telega-ins-i18n "lng_users_typing"
:user sender-title
:second_user (telega-ins--as-string
(telega-ins--msg-sender (car more-senders)
:with-avatar-p t))))
(t
(telega-ins-i18n "lng_many_typing"
:count (1+ (length more-senders)))))))
(chatActionRecordingVideoNote
(telega-ins-i18n "lng_user_action_record_round"
:user sender-title))
(chatActionUploadingVideoNote
(telega-ins-i18n "lng_user_action_upload_round"
:user sender-title))
(chatActionRecordingVoiceNote
(telega-ins-i18n "lng_user_action_record_audio"
:user sender-title))
(chatActionUploadingVoiceNote
(telega-ins-i18n "lng_user_action_upload_audio"
:user sender-title))
(chatActionWatchingAnimations
(telega-ins sender-title
(telega-i18n "lng_user_action_watching_animations"
:emoji (telega-tl-str action :emoji))))
(chatActionUploadingVideo
(telega-ins-i18n "lng_user_action_upload_video"
:user sender-title))
(chatActionChoosingSticker
(telega-ins-i18n "lng_user_action_choose_sticker"
:user sender-title))
(t
(telega-ins sender-title " is "
(substring (plist-get action :@type)
(eval-when-compile
(length "chatAction"))))))
(when-let ((progress (plist-get action :progress)))
(telega-ins-fmt " %d%%" progress))
)
;; NOTE: `replace-string-in-region' is not available in Emacs27
;; (replace-string-in-region " " telega-symbol-nbsp start (point))
))))
(defun telega-ins--filesize (filesize)
"Insert FILESIZE in human readable format."
(telega-ins (file-size-human-readable filesize)))
(defun telega-ins--date (timestamp &optional fmt-type)
"Insert TIMESTAMP.
Use date format from `telega-date-format-alist' corresponding to FMT-TYPE.
By default FMT-TYPE is determined by TIMESTAMP value.
FMT-TYPE can be a string, directly specifying time format string."
(unless fmt-type
(let* ((current-ts (telega-time-seconds))
(ctime (decode-time current-ts))
(today00 (telega--time-at00 current-ts ctime)))
(if (and (> timestamp today00)
(< timestamp (+ today00 (* 24 60 60))))
(setq fmt-type 'today)
(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))))
(setq fmt-type 'this-week)
(setq fmt-type 'old))))))
(telega-ins
(format-time-string (or (and (stringp fmt-type) fmt-type)
(cdr (assq fmt-type telega-date-format-alist))
"%FT%T%z")
timestamp)))
(defun telega-ins--birthdate (birthdate &optional with-years-old-p)
"Inserter for the BIRTHDATE tl struct.
If WITH-YEARS-OLD-P is specified, insert years old as well."
(let ((bd-day (plist-get birthdate :day))
(bd-month (plist-get birthdate :month))
(bd-year (plist-get birthdate :year)))
;; NOTE: Birthday year might not be specified
(if (and with-years-old-p (not (telega-zerop bd-year)))
(let ((nowdate (decode-time (telega-time-seconds))))
(telega-ins-i18n "lng_info_birthday_years"
:date (telega-ins--as-string
(telega-ins--birthdate birthdate))
:count (- (decoded-time-year nowdate)
bd-year
(if (or (> bd-month (decoded-time-month nowdate))
(and (= bd-month (decoded-time-month nowdate))
(> bd-day (decoded-time-day nowdate))))
1
0))))
(let* ((bd-decoded (list 0 0 0 bd-day bd-month bd-year))
(bd-timestamp
(round (time-to-seconds (apply #'encode-time bd-decoded)))))
(telega-ins--date bd-timestamp (if (telega-zerop bd-year)
"%d %B"
'date-long))))))
(defun telega-ins--date-relative (timestamp)
"Insert relative date for the timestamp."
(let* ((dtime (decode-time timestamp))
(today00 (telega--time-at00 (telega-time-seconds)))
(tomorrow00 (+ today00 (* 24 3600)))
(yesterday00 (- today00 (* 24 3600)))
(formatted-time (format "%02d:%02d" (nth 2 dtime) (nth 1 dtime))))
(cond ((and (> timestamp yesterday00)
(< timestamp today00))
(telega-ins-i18n "lng_mediaview_yesterday"
:time formatted-time))
((and (> timestamp today00)
(< timestamp tomorrow00))
(telega-ins-i18n "lng_mediaview_today"
:time formatted-time))
(t
(telega-ins-i18n "lng_mediaview_date_time"
:date (telega-ins--as-string
(telega-ins--date timestamp 'date-long))
:time formatted-time)))
))
(cl-defun telega-ins--msg-sender (msg-sender &key
with-title
with-avatar-p
with-username-p
with-brackets-p
(with-badges-p t)
(with-title-faces-p t)
with-palette
(trail-delim " ")
trail-inserter)
"Insert message's sender title.
If WITH-AVATAR-P is 2, then insert 2 lines version of an avatar.
WITH-TITLE specifies custom title to use for this message sender.
WITH-BADGES-P is ignored if WITH-TITLE is specified."
(declare (indent 1))
(let* ((chat-p (telega-chat-p msg-sender))
(title (cond (with-title with-title)
(chat-p
(telega-chat-title msg-sender (not with-badges-p)))
(t
(cl-assert (telega-user-p msg-sender))
(telega-user-title
msg-sender 'full-name (not with-badges-p)))))
(title-faces (when (or with-title-faces-p with-palette)
(telega-msg-sender-title-faces msg-sender with-palette)))
(brackets (when with-brackets-p
(telega-msg-sender-brackets msg-sender))))
(when (and with-avatar-p
(if chat-p
telega-chat-show-avatars
telega-user-show-avatars))
(if (eq with-avatar-p 2)
(telega-ins--image
(telega-msg-sender-avatar-image msg-sender) 0)
(telega-ins--image
(telega-msg-sender-avatar-image-one-line msg-sender))))
(when brackets
(telega-ins (car brackets)))
(telega-ins--with-face title-faces
(telega-ins title))
(when with-username-p
(when-let ((username (telega-msg-sender-username msg-sender 'with-@)))
(telega-ins--with-face 'telega-shadow
(telega-ins " • "))
(telega-ins--with-face (if (facep with-username-p)
with-username-p
title-faces)
(telega-ins username))))
;; Trailer before closing bracket
(when trail-inserter
(telega-ins-prefix trail-delim
(funcall trail-inserter msg-sender)))
(when brackets
(telega-ins (cadr brackets)))
(when (eq with-avatar-p 2)
(telega-ins "\n")
(telega-ins--image (telega-msg-sender-avatar-image msg-sender) 1))
t))
(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)
(telega-i18n "lng_status_online"))
((< online-dur 60)
(telega-i18n "lng_status_lastseen_now"))
((< online-dur (* 60 60))
(telega-i18n "lng_status_lastseen_minutes"
:count (/ online-dur 60)))
((< online-dur (* 24 60 60))
(telega-i18n "lng_status_lastseen_hours"
:count (/ online-dur (* 60 60))))
((< online-dur (* 2 24 60 60))
(telega-i18n "lng_status_lastseen_yesterday"
:time (telega-ins--as-string
(telega-ins--date (- online-dur (* 24 60 60))))))
((eq status 'userStatusRecently)
(telega-i18n "lng_status_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 'telega-shadow)
(if mutual-contact-p "↔" "←")
(propertize "🚹" 'face 'bold)))))
(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-msg-sender-avatar-image user))
;; NOTE: Do not use `telega-current-column', because
;; `telega-ins--user' might be used under
;; `telega-ins--line-wrap-prefix' and `telega-current-column'
;; accounts line/wrap prefix
(off-column (current-column)))
(telega-ins--image avatar 0
:no-display-if (not telega-user-show-avatars))
(telega-ins--msg-sender user
:with-username-p 'telega-username)
(telega-ins--with-face 'telega-shadow
(when (and member
(telega-ins-prefix " ("
(telega-ins--chat-member-status
(plist-get member :status))))
(telega-ins ")")))
(when show-phone-p
(when-let ((phone-number (telega-tl-str user :phone_number)))
(telega-ins--with-face 'telega-shadow
(telega-ins " • "))
(telega-ins "+" 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))
(telega-ins "\n")
(telega-ins (make-string off-column ?\s))
(telega-ins--image avatar 1
:no-display-if (not telega-user-show-avatars))
;; Setup `off-column' for "invited by" string
(setq off-column (current-column))
(telega-ins--user-status user)
(when-let ((join-date (plist-get member :joined_chat_date)))
(unless (zerop join-date)
(telega-ins ", " (telega-i18n "lng_group_invite_joined_status"
:date ""))
(telega-ins--date join-date)))
(when-let* ((inviter-id (plist-get member :inviter_user_id))
(inviter-user (unless (zerop inviter-id)
(telega-user-get inviter-id 'local))))
(telega-ins "\n")
(telega-ins (make-string off-column ?\s))
(telega-ins "invited by ")
(telega-ins--raw-button (telega-link-props 'user inviter-id 'type 'telega)
(telega-ins--msg-sender inviter-user :with-avatar-p t)))
t))
(defun telega-ins--chat-member (member)
"Formatting for the chat MEMBER.
Return COLUMN at which user name is inserted."
(let ((sender (telega-msg-sender (plist-get member :member_id))))
(if (telega-user-p sender)
(telega-ins--user sender member)
;; TODO: support for chat as 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-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 telega-shadow 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 \".\"
could be a cons cell, where care is filling char, and cdr is
trailing char. For example (?= . ?>) to draw ====> progress
bar.
E-CHAR - empty char, default is non-break 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 (if (consp p-char)
(cond ((= 0 pbars) "")
((= 1 pbars) (char-to-string (cdr p-char)))
(t (concat (make-string (1- pbars) (car p-char))
(char-to-string (cdr p-char)))))
(make-string pbars (or p-char ?\.)))
(make-string (- nbars pbars) (or e-char 160)))))
(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))
(progress-100 (round (* progress 100)))
(nbsp-char 160))
(telega-ins "[")
(telega-ins-progress-bar
progress 1.0 10 telega-symbol-upload-progress nbsp-char)
(when (< progress-100 10)
(telega-ins nbsp-char))
(telega-ins-fmt "%d%%]%c" progress-100 nbsp-char)
(telega-ins--box-button (telega-i18n "lng_context_cancel_upload")
'action (lambda (_ignored)
(telega-msg-delete0 msg)))))
((telega-file--downloading-p file)
(let* ((progress (telega-file--downloading-progress file))
(progress-100 (round (* progress 100)))
(nbsp-char 160))
(telega-ins "[")
(telega-ins-progress-bar
progress 1.0 10 telega-symbol-download-progress nbsp-char)
(when (< progress-100 10)
(telega-ins nbsp-char))
(telega-ins-fmt "%d%%]%c" progress-100 nbsp-char)
(telega-ins--box-button (telega-i18n "lng_context_cancel_download")
'action (lambda (_ignored)
(telega--cancelDownloadFile file)))))
((not (telega-file--downloaded-p file))
(let* ((progress (telega-file--downloading-progress file))
(progress-100 (round (* progress 100)))
(nbsp-char 160))
(when (> progress 0)
(telega-ins "[")
(telega-ins-progress-bar progress 1.0 10 (cons ?= ?⏸) nbsp-char)
(when (< progress-100 10)
(telega-ins nbsp-char))
(telega-ins-fmt "%d%%]%c" progress-100 nbsp-char))
(telega-ins--box-button (telega-i18n "lng_media_download")
'action (lambda (_ignored)
(telega-file--download file
:priority 32
:update-callback
(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
(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 (fmt-text &optional for-msg)
"Insert formatted TEXT applying telegram entities.
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--chat-photo (chat-photo &optional slices-p)
"Inserter for user CHAT-PHOTO (TDLib's ChatPhoto)."
(if-let ((fake-anim (plist-get chat-photo :telega-fake-animation)))
(telega-ins--animation-image fake-anim)
(funcall (if slices-p #'telega-ins--image-slices #'telega-ins--image)
(telega-photo--image
chat-photo
(list telega-user-photo-size
telega-user-photo-size
telega-user-photo-size
telega-user-photo-size)))))
(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
:priority 20
:update-callback
(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-let ((tl-ttl (plist-get msg :self_destruct_type)))
(telega-ins ", ")
(telega-ins--self-destruct-type tl-ttl 'short))
(let ((album-id (plist-get msg :media_album_id)))
(unless (telega-zerop album-id)
(telega-ins--with-face 'telega-shadow
(telega-ins " " "album-id: " album-id))))
(when show-progress
(telega-ins " ")
(telega-ins--file-progress msg hr-file))
(telega-ins "\n"))
(let ((msg-content (plist-get msg :content)))
(cond ((plist-get msg-content :is_secret)
(let ((ttl-in (plist-get msg :self_destruct_in)))
(unless (telega-zerop ttl-in)
(telega-ins--with-face 'telega-shadow
(telega-ins "Self-descruct in "))
(telega-ins " " (telega-duration-human-readable ttl-in) "\n"))
(telega-ins--image-slices
(telega-self-destruct-create-svg
(plist-get photo :minithumbnail)
(telega-symbol
(if (plist-get msg :self_destruct_type) 'flames 'lock))))))
((and (plist-get msg-content :has_spoiler)
(not (plist-get msg-content :telega-spoiler-removed)))
(telega-ins--image-slices
(telega-spoiler-create-svg
(plist-get photo :minithumbnail)
(plist-get hr :width)
(plist-get hr :height)
telega-thumbnail-size-limits))
(telega-ins "\n")
(telega-ins--box-button (telega-i18n "lng_context_disable_spoiler")
:action #'telega-msg-remove-media-spoiler)
(telega-ins " "))
(t
(telega-ins--image-slices
(telega-photo--image
photo (or limits telega-photo-size-limits))))))
t))
(defun telega-ins--audio (msg &optional audio how music-symbol)
"Insert audio message MSG.
HOW is one of `header' or `thumbnail'. If ommited, then both metainfo
and thumbnail are shown.
If MUSIC-SYMBOL is specified, use it instead of play/pause."
(unless audio
(setq audio (telega--tl-get msg :content :audio)))
(let (ret)
(unless (eq how 'thumbnail)
(let* ((dur (plist-get audio :duration))
(proc (plist-get msg :telega-ffplay-proc))
(playing-p (telega-ffplay-playing-p proc))
(played (if (telega-ffplay-playing-p proc)
(telega-ffplay-progress proc)
(telega-ffplay-paused-p proc)))
(audio-name (plist-get audio :file_name))
(audio-file (telega-file--renew audio :audio)))
;; Play/pause and downloading status
(if playing-p
(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--raw-button
(telega-link-props 'file local-path 'face 'telega-link)
(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) ?\.)
(telega-ins "]" (telega-duration-human-readable played) " "))
(if (telega-ffplay-playing-p proc)
(telega-ins--ffplay-controls msg 'no-2x-button)
(telega-ins--box-button (telega-i18n "lng_mac_menu_player_stop")
:value msg
:action #'telega-msg--vvnote-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))))
(setq ret t))
;; Album cover
(unless (eq how 'metainfo)
(let ((thumb (plist-get audio :album_cover_thumbnail))
(minithumb (plist-get audio :album_cover_minithumbnail)))
(when (or minithumb thumb)
(telega-ins-from-newline
(telega-ins--image-slices (telega-media--image
(cons audio 'telega-audio--create-image)
(cons thumb :file))))
(setq ret t))))
ret))
(defun telega-ins--video (msg &optional video how)
"Insert video message MSG.
HOW is one of `header' or `thumbnail'. If ommited, then both metainfo
and thumbnail are shown."
(let* ((content (plist-get msg :content))
(video (or video (plist-get content :video)))
(video-name (telega-tl-str video :file_name))
(video-file (telega-file--renew video :video))
ret)
(unless (eq how 'thumbnail)
(telega-ins (telega-symbol 'video) " ")
(if (telega-file--downloaded-p video-file)
(let ((local-path (telega--tl-get video-file :local :path)))
(telega-ins--raw-button
(telega-link-props 'file local-path 'face 'telega-link)
(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)))
(when-let ((tl-ttl (plist-get msg :self_destruct_type)))
(telega-ins ", ")
(telega-ins--self-destruct-type tl-ttl 'short))
(telega-ins-prefix " "
(telega-ins--file-progress msg video-file))
(setq ret t))
;; Video's thumbnail, if any
(unless (eq how 'metainfo)
(telega-ins-from-newline
(cond ((plist-get content :is_secret)
;; Secret video
(let ((ttl-in (plist-get msg :self_destruct_in)))
(unless (telega-zerop ttl-in)
(telega-ins--with-face 'telega-shadow
(telega-ins "Self-descruct in "))
(telega-ins (telega-duration-human-readable ttl-in))
(telega-ins "\n"))
(telega-ins--image-slices
(telega-self-destruct-create-svg
(plist-get video :minithumbnail)
(telega-symbol
(if (plist-get msg :self_destruct_type) 'flames 'lock))))
(setq ret t)))
((and (plist-get content :has_spoiler)
(not (plist-get content :telega-spoiler-removed)))
(telega-ins--image-slices
(telega-spoiler-create-svg
(plist-get video :minithumbnail)
(plist-get video :width)
(plist-get video :height)
telega-thumbnail-size-limits))
(telega-ins "\n")
(telega-ins--box-button (telega-i18n "lng_context_disable_spoiler")
:action #'telega-msg-remove-media-spoiler)
(telega-ins " ")
(setq ret t))
(t
(let ((thumb (plist-get video :thumbnail))
(minithumb (plist-get video :minithumbnail)))
(when (or thumb minithumb)
(telega-ins--image-slices
(telega-media--image
(cons video #'telega-video--create-image)
(cons thumb :file)))
(setq ret t)))))))
ret))
(defun telega-ins--ffplay-controls (msg &optional no-2x-button)
"Insert controls for voice/video notes.
If NO-2X-BUTTON is specified, then do not display \"2x\" button."
(telega-ins--box-button (telega-symbol "⏪")
'action (lambda (_button)
(telega-msg--vvnote-rewind msg -10)))
(telega-ins " ")
(telega-ins--box-button (telega-symbol "⏩")
'action (lambda (_button)
(telega-msg--vvnote-rewind msg 10)))
(telega-ins " ")
(unless no-2x-button
(let* ((label2x (if (eq telega-vvnote-play-speed 1) "1×" "2×"))
(ends (if (functionp telega-box-button-endings)
(funcall telega-box-button-endings label2x)
telega-box-button-endings)))
(setq label2x (concat (or (car ends) "[") label2x (or (cdr ends) "]")))
(telega-ins--box-button label2x
:value msg
:action #'telega-msg--vvnote-play-speed-toggle))
(telega-ins " "))
(telega-ins--box-button (telega-i18n "lng_mac_menu_player_stop")
:value msg
:action #'telega-msg--vvnote-stop))
(defun telega--can-speech-recognize-p (&optional duration)
"Return non-nil if speech recognition is available."
(or (telega-user-match-p (telega-user-me) 'is-premium)
(and telega--speech-recognition-trial
(> (plist-get telega--speech-recognition-trial :left_count) 0)
(or (null duration)
(< duration (plist-get telega--speech-recognition-trial
:max_media_duration))))))
(defun telega-ins--speech-recognition-button (recognition for-msg
&optional duration)
"Insert speech recognize button."
;; NOTE: if previous recognition results in error, then also show
;; the recognize button
(when (and (or (not recognition)
(eq 'speechRecognitionResultError
(telega--tl-type recognition)))
(telega--can-speech-recognize-p duration))
(telega-ins " ")
(telega-ins--box-button "🠆A"
:value for-msg
:action #'telega--recognizeSpeech
'help-echo "Recognize speech")))
(defun telega-ins--speech-recognition-text (recognition)
"Insert results of the voice/video message recognition."
(cl-assert recognition)
(cl-ecase (telega--tl-type recognition)
(speechRecognitionResultPending
(telega-ins--with-face 'telega-shadow
(telega-ins (or (telega-tl-str recognition :partial_text)
"Recognizing")
"...")))
(speechRecognitionResultText
(telega-ins--with-face 'telega-shadow
(telega-ins (telega-tl-str recognition :text))))
(speechRecognitionResultError
(telega-ins--with-face 'error
(telega-ins
(telega-tl-str (plist-get recognition :error) :message))))))
(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))
(playing-p (telega-ffplay-playing-p proc))
(played (if (telega-ffplay-playing-p proc)
(telega-ffplay-progress proc)
(telega-ffplay-paused-p proc)))
(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 playing-p
(telega-ins (telega-symbol 'pause))
(telega-ins (telega-symbol 'play)))
(telega-ins " "))
;; waveform image or text, if tty is in use
(telega-ins--image
(telega-vvnote--waves-svg waves dur played))
;; Duration / self destruct
(telega-ins " (" (telega-duration-human-readable dur) ")")
(when-let ((tl-ttl (plist-get msg :self_destruct_type)))
(telega-ins ", ")
(telega-ins--self-destruct-type tl-ttl 'short))
;; ffplay controls to seek/2x/stop
(when (telega-ffplay-playing-p proc)
(telega-ins " ")
(telega-ins--ffplay-controls msg)
(telega-ins " "))
;; 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)))
(let ((recognition (plist-get note :speech_recognition_result)))
(telega-ins--speech-recognition-button recognition msg dur)
(when recognition
(telega-ins "\n")
(telega-ins--speech-recognition-text recognition)))
))