forked from zevlg/telega.el
-
Notifications
You must be signed in to change notification settings - Fork 0
/
telega-media.el
1058 lines (956 loc) · 45.3 KB
/
telega-media.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-media.el --- Media support for telega -*- lexical-binding:t -*-
;; Copyright (C) 2018-2019 by Zajcev Evgeny.
;; Author: Zajcev Evgeny <[email protected]>
;; Created: Tue Jul 10 15:20:09 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:
;; Code to work with media in telega:
;; - Download/upload files to cloud
;; - Thumbnails
;; - Stickers
;; - Animations
;; - Web pages
;; etc
;;; Code:
(require 'telega-core)
(require 'telega-tdlib)
(declare-function telega-root-view--update "telega-root" (on-update-prop &rest args))
(declare-function telega-chat-color "telega-chat" (chat))
(declare-function telega-chat-title "telega-chat" (chat &optional with-username))
(declare-function telega-msg-redisplay "telega-msg" (msg))
(declare-function telega-image-view-file "telega-modes" (tl-file &optional for-msg))
;;; Files downloading/uploading
(defun telega-file--ensure (file)
"Ensure FILE is in `telega--files'.
Return FILE.
As side-effect might update root view, if current root view is \"Files\"."
(when telega-debug
(cl-assert file))
(plist-put file :telega-file-recency (telega-time-seconds))
(puthash (plist-get file :id) file telega--files)
(telega-root-view--update :on-file-update file)
file)
(defun telega-file-get (file-id &optional locally)
"Return file associated with FILE-ID."
(or (gethash file-id telega--files)
(unless locally
(telega-file--ensure (telega--getFile file-id)))))
(defun telega-file--renew (place prop)
"Renew file value at PLACE and PROP."
(when-let* ((ppfile (plist-get place prop))
(file-id (plist-get ppfile :id))
(file (or (gethash file-id telega--files)
(telega-file--ensure ppfile))))
(plist-put place prop file)
file))
(defun telega-file--update (file)
"FILE has been updated, call any pending callbacks."
(let* ((file-id (plist-get file :id))
(old-file (gethash file-id telega--files))
(throttle-p
;; NOTE: Throttle number of update callbacks calls
;; Throttle only if `:downloaded_size'/`:uploaded_size'
;; property advances more then 1/100s part of the file size
;; See https://github.com/zevlg/telega.el/issues/164
(or (and (telega-file--uploading-p file)
(telega-file--uploading-p old-file)
(< (- (telega-file--uploading-progress file)
(telega-file--uploading-progress old-file))
0.01))
(and (telega-file--downloading-p file)
(telega-file--downloading-p old-file)
(< (- (telega-file--downloading-progress file)
(telega-file--downloading-progress old-file))
0.01)))))
(unless throttle-p
(telega-file--ensure file)
(let* ((callbacks (gethash file-id telega--files-updates))
(left-cbs (cl-loop for cb in callbacks
when (funcall cb file)
collect cb)))
(telega-debug "%s %S started with %d callbacks, left %d callbacks"
(propertize "FILE-UPDATE" 'face 'bold)
file-id (length callbacks) (length left-cbs))
(if left-cbs
(puthash file-id left-cbs telega--files-updates)
(remhash file-id telega--files-updates))
(when (and (not (telega-file--downloaded-p old-file))
(telega-file--downloaded-p file))
(run-hook-with-args 'telega-file-downloaded-hook file))
))))
(defun telega-file--callback-wrap (callback check-fun)
"Wrapper for CALLBACK.
Removes callback in case downloading is canceled or completed."
(when callback
(lambda (file)
(funcall callback file)
(funcall check-fun file))))
(defun telega-file--ensure-update-callback (file-id update-callback)
"Ensure FILE-ID is monitored with UPDATE-CALLBACK."
(cl-assert update-callback)
(let ((cb-list (gethash file-id telega--files-updates)))
(unless (memq update-callback cb-list)
(puthash file-id (cons update-callback cb-list)
telega--files-updates))))
(defun telega-file--download (file &optional priority callback
&rest parts)
"Download file denoted by FILE-ID.
PRIORITY - (1-32) the higher the PRIORITY, the earlier the file
will be downloaded. (default=1)
Run CALLBACK every time FILE gets updated.
To cancel downloading use `telega--cancelDownloadFile', it will
remove the callback as well.
PARTS - list of file parts to download sequentually."
(declare (indent 2))
;; - If file already downloaded, then just call the callback
;; - If file already downloading, then just install the callback
;; - If file can be downloaded, then start downloading file and
;; install callback after file started downloading
(let* ((file-id (plist-get file :id))
(dfile (telega-file-get file-id))
(cbwrap (telega-file--callback-wrap
callback 'telega-file--downloading-p)))
(cond ((telega-file--downloaded-p dfile)
(when cbwrap
(funcall cbwrap dfile)))
((or (telega-file--downloading-p dfile)
(telega-file--can-download-p dfile))
(when cbwrap
(telega-file--ensure-update-callback file-id cbwrap))
(unless (telega-file--downloading-p dfile)
(let ((next-parts (cdr parts)))
(telega--downloadFile file-id
:priority priority
:offset (car (car parts))
:limit (cdr (car parts))
:sync-p next-parts
;; NOTE: Continue downloading other parts
;; If downloading is canceled, callback is not called,
;; this is exactly what we want
:callback
(lambda (downfile)
;; NOTE: update callback maybe deleted,
;; before file actually starts
;; downloading
(when (and cbwrap (not next-parts))
(telega-file--ensure-update-callback file-id cbwrap))
(telega-file--update downfile)
(when next-parts
(apply #'telega-file--download downfile
priority callback next-parts))))))))))
(defun telega-file--upload-internal (file &optional callback)
"Monitor FILE uploading progress by installing CALLBACK."
(declare (indent 1))
(let* ((file-id (plist-get file :id))
(cbwrap (telega-file--callback-wrap
callback 'telega-file--uploading-p)))
(if (telega-file--uploaded-p file)
(when cbwrap
(funcall cbwrap file))
(when cbwrap
(let ((cb-list (gethash file-id telega--files-updates)))
(puthash file-id (cons cbwrap cb-list)
telega--files-updates))))
file))
(defun telega-file--upload (filename &optional file-type priority callback)
"Upload FILENAME to the cloud.
Return file object, obtained from `telega--uploadFile'."
(declare (indent 3))
(let ((file (telega--uploadFile
(expand-file-name filename) file-type priority)))
(telega-file--upload-internal file callback)
file))
;;; Photos
(defmacro telega-thumbnail--get (type thumbnails)
"Get thumbnail of TYPE from list of THUMBNAILS.
Thumbnail TYPE and its sizes:
\"s\" box 100x100
\"m\" box 320x320
\"x\" box 800x800
\"y\" box 1280x1280
\"w\" box 2560x2560
\"a\" crop 160x160
\"b\" crop 320x320
\"c\" crop 640x640
\"d\" crop 1280x1280"
`(cl-find ,type ,thumbnails :test 'string= :key (telega--tl-prop :type)))
(defun telega-photo--highres (photo)
"Return thumbnail of highest resolution for the PHOTO.
Return thumbnail that can be downloaded."
(cl-some (lambda (tn)
(let ((tn-file (telega-file--renew tn :photo)))
(when (or (telega-file--downloaded-p tn-file)
(telega-file--can-download-p tn-file))
tn)))
;; From highest res to lower
(reverse (plist-get photo :sizes))))
(defun telega-photo--thumb (photo)
"While downloading best photo, get small thumbnail for the PHOTO."
(let ((photo-sizes (plist-get photo :sizes)))
(or (cl-some (lambda (tn)
(when (telega-file--downloaded-p
(telega-file--renew tn :photo))
tn))
photo-sizes)
(cl-some (lambda (tn)
(when (telega-file--downloading-p
(telega-file--renew tn :photo))
tn))
photo-sizes)
(cl-some (lambda (tn)
(when (telega-file--can-download-p
(telega-file--renew tn :photo))
tn))
photo-sizes)
)))
(defun telega-photo--best (photo &optional limits)
"Select best thumbnail from PHOTO suiting LIMITS.
By default LIMITS is `telega-photo-size-limits'."
(unless limits
(setq limits telega-photo-size-limits))
(let ((lim-xwidth (telega-chars-xwidth (nth 2 limits)))
(lim-xheight (telega-chars-xheight (nth 3 limits)))
ret)
;; NOTE: `reverse' is used to start from highes sizes
(seq-doseq (thumb (reverse (plist-get photo :sizes)))
(let* ((thumb-file (telega-file--renew thumb :photo))
(tw (plist-get thumb :width))
(th (plist-get thumb :height)))
;; NOTE: By default (not ret) use any downloadable file, even
;; if size does not fits
;; Select sizes larger then limits, because downscaling works
;; betten then upscaling
(when (and (or (telega-file--downloaded-p thumb-file)
(and (telega-file--can-download-p thumb-file)
(not (telega-file--downloaded-p
(plist-get ret :photo)))))
(or (not ret)
(and (>= tw lim-xwidth)
(>= th lim-xheight)))
;; NOTE: prefer thumbs with `:progressive_sizes' set
(or (not ret)
(and (telega-file--can-download-p (plist-get ret :photo))
(not (plist-get ret :progressive_sizes))
(plist-get thumb :progressive_sizes)))
)
(setq ret thumb))))
ret))
(defun telega-photo--open (photo &optional for-msg)
"Download highres PHOTO asynchronously and open it as a file.
If FOR-MSG is non-nil, then FOR-MSG is message containing PHOTO."
(let* ((hr (telega-photo--highres photo))
(hr-file (telega-file--renew hr :photo)))
(telega-file--download hr-file 32
(lambda (tl-file)
(when for-msg
(telega-msg-redisplay for-msg))
(when (telega-file--downloaded-p tl-file)
(when (telega--tl-get for-msg :content :is_secret)
(telega--openMessageContent for-msg))
(if (memq 'photo telega-open-message-as-file)
(telega-open-file (telega--tl-get tl-file :local :path) for-msg)
(telega-image-view-file tl-file for-msg)))))))
(defun telega-image-supported-file-p (filename &optional error-if-unsupported)
"Same as `image-supported-file-p'.
Trigger an error if ERROR-IF-UNSUPPORTED is specified and FILENAME is
not natively supported."
(or (funcall (if (fboundp 'image-supported-file-p)
'image-supported-file-p
'image-type-from-file-name)
filename)
(and error-if-unsupported
(error "telega: \"%s\" image's format is unsupported"
filename))))
(defun telega-image--telega-text (img &optional slice-num)
"Return text version for image IMG and its slice SLICE-NUM.
Return nil if `:telega-text' is not specified in IMG."
(let ((tt (plist-get (cdr img) :telega-text)))
(cond ((null tt) nil)
((and (stringp tt) (string-empty-p tt)) nil)
((stringp tt) tt)
((listp tt)
(if slice-num
(progn
(cl-assert (> (length tt) slice-num))
(nth slice-num tt))
(mapconcat 'identity tt "\n")))
(t (cl-assert nil nil "Invalid value for :telega-text=%S" tt)))))
(defun telega-media--cheight-for-limits (width height limits)
"Calculate cheight for image of WIDTHxHEIGHT size fitting into LIMITS."
(let ((ratio (min (/ (float (telega-chars-xwidth (nth 2 limits))) width)
(/ (float (telega-chars-xheight (nth 3 limits))) height))))
(if (< ratio 1.0)
(telega-chars-in-height (floor (* height ratio)))
(let ((cheight (telega-chars-in-height height)))
(if (< cheight (nth 1 limits))
(nth 1 limits)
(cl-assert (<= cheight (nth 3 limits)))
cheight))
)))
(defun telega-media--cwidth-xmargin (width height char-height &optional _max-cwidth)
"Calculate width in chars and margins X pixels.
MAX-CWIDTH is maximum width in chars.
Return cons cell, where car is width in char and cdr is margin value."
;; NOTE: handle case where WIDTH or HEIGHT can be zero
(let* ((pix-h (telega-chars-xheight char-height))
(pix-w (if (zerop height)
0
(* (/ (float width) height) pix-h)))
(cw (telega-chars-in-width pix-w))
(xmargin (/ (- (telega-chars-xwidth cw) pix-w) 2)))
; (cl-assert (> cw 0))
(cons cw (floor xmargin))))
(defun telega-media--progress-svg (file width height cheight)
"Generate svg showing downloading progress for FILE."
(let* ((xh (telega-chars-xheight cheight))
(cwidth-xmargin (telega-media--cwidth-xmargin
(if (zerop width) xh width)
(if (zerop height) xh height) cheight))
(w-chars (car cwidth-xmargin))
(xw (telega-chars-xwidth w-chars))
(svg (telega-svg-create xw xh))
(progress (telega-file--downloading-progress file)))
(telega-svg-progress svg progress)
(telega-svg-image svg :scale 1.0
:width xw :height xh
:ascent 'center
:mask 'heuristic
;; text of correct width
:telega-text (make-string w-chars ?X))))
(defsubst telega-photo--progress-svg (photo cheight)
"Generate svg for the PHOTO."
(telega-media--progress-svg
(telega-file--renew photo :photo)
(plist-get photo :width)
(plist-get photo :height)
cheight))
(defun telega-media--create-image (file width height &optional cheight
progressive-sizes)
"Create image to display FILE.
WIDTH and HEIGHT specifies size of the FILE's image.
CHEIGHT is the height in chars to use (default=1).
PROGRESSIVE-SIZES specifies list of jpeg's progressive file sizes."
(unless cheight
(setq cheight 1))
(if (or (telega-file--downloaded-p file)
(and progressive-sizes
(>= (telega-file--downloaded-size file)
(car progressive-sizes))))
(let ((cw-xmargin (telega-media--cwidth-xmargin width height cheight))
(image-filename (telega--tl-get file :local :path)))
;; NOTE: Handle case when file is partially downloaded and
;; some progressive size is reached. In this case create
;; temporary image file writing corresponding progress bytes
;; into it and displaying it
(unless (telega-file--downloaded-p file)
(let* ((tmp-size (cl-find (telega-file--downloaded-size file)
(reverse progressive-sizes) :test #'>=))
(tmp-fname (expand-file-name
(format "%s-%d.%s"
(file-name-base image-filename)
tmp-size
(file-name-extension image-filename))
telega-temp-dir))
(coding-system-for-write 'binary))
(unless (file-exists-p tmp-fname)
(telega-debug "Creating progressive img: %d / %S -> %s"
(telega-file--downloaded-size file)
progressive-sizes
tmp-fname)
(with-temp-buffer
(set-buffer-multibyte nil)
(insert-file-contents-literally image-filename)
(write-region 1 (+ 1 tmp-size) tmp-fname nil 'quiet)))
(setq image-filename tmp-fname)))
(telega-create-image
(if (string-empty-p image-filename)
(telega-etc-file "non-existing.jpg")
image-filename)
(when (fboundp 'imagemagick-types) 'imagemagick) nil
:height (telega-chars-xheight cheight)
:scale 1.0
:ascent 'center
:margin (cons (cdr cw-xmargin) 0)
:telega-text (make-string (car cw-xmargin) ?X)))
(telega-media--progress-svg file width height cheight)))
(defun telega-minithumb--create-image (minithumb cheight)
"Create image and use MINITHUMB minithumbnail as data."
(let* ((xwidth (plist-get minithumb :width))
(xheight (plist-get minithumb :height))
(cwidth-xmargin (telega-media--cwidth-xmargin xwidth xheight cheight)))
(telega-create-image
(base64-decode-string (plist-get minithumb :data))
(if (and (fboundp 'image-transforms-p)
(funcall 'image-transforms-p))
'jpeg
(when (fboundp 'imagemagick-types)
'imagemagick))
t
:height (telega-chars-xheight cheight)
:scale 1.0
:ascent 'center
:margin (cons (cdr cwidth-xmargin) 0)
:telega-text (make-string (car cwidth-xmargin) ?X))))
(defun telega-thumb--create-image (thumb &optional _file cheight)
"Create image for the thumbnail THUMB.
THUMB could be `photoSize' or `thumbnail'.
CHEIGHT is the height in chars (default=1)."
(telega-media--create-image
(let ((thumb-tl-type (telega--tl-type thumb)))
(if (eq thumb-tl-type 'photoSize)
(telega-file--renew thumb :photo)
(cl-assert (eq thumb-tl-type 'thumbnail))
(telega-file--renew thumb :file)))
(plist-get thumb :width)
(plist-get thumb :height)
cheight
(append (plist-get thumb :progressive_sizes) nil)))
(defun telega-thumb--create-image-one-line (thumb &optional file)
"Create image for thumbnail (photoSize) for one line use."
(telega-thumb--create-image thumb file 1))
(defun telega-thumb--create-image-two-lines (thumb &optional file)
"Create image for thumbnail (photoSize) for two lines use."
(telega-thumb--create-image thumb file 2))
(defun telega-thumb--create-image-three-lines (thumb &optional file)
"Create image for thumbnail (photoSize) for three lines use."
(telega-thumb--create-image thumb file 3))
(defun telega-thumb--create-image-as-is (thumb &optional file)
"Create image for thumbnail THUMB (photoSize) with size as is."
(telega-thumb--create-image
thumb file (telega-chars-in-height (plist-get thumb :height))))
(defun telega-thumb-or-minithumb--create-image (tl-obj &optional _file
custom-thumb
custom-minithumb)
"Create image fol TL-OBJ that has :thumbnail and/or :minithumbnail prop."
(let* ((thumb (or custom-thumb (plist-get tl-obj :thumbnail)))
(thumb-cheight (telega-media--cheight-for-limits
(plist-get thumb :width)
(plist-get thumb :height)
telega-thumbnail-size-limits))
(thumb-file (telega-file--renew thumb :file))
(minithumb (or custom-minithumb (plist-get tl-obj :minithumbnail))))
(cond ((telega-file--downloaded-p thumb-file)
(telega-thumb--create-image
thumb thumb-file thumb-cheight))
(minithumb
(telega-minithumb--create-image
minithumb thumb-cheight))
(t
(telega-thumb--create-image
thumb thumb-file thumb-cheight)))))
(defun telega-msg--preview-photo-image (msg)
"Return one line preview for the photo message MSG.
Return nil if preview image is unavailable."
(when (and telega-use-images
(telega-chat-match-p (telega-msg-chat msg 'offline)
telega-use-one-line-preview-for))
(let* ((photo (telega--tl-get msg :content :photo))
(best (telega-photo--best photo '(1 1 1 1)))
(minithumb (plist-get photo :minithumbnail))
(cached-preview
(plist-get photo :telega-preview-1))
(preview-new
(cond ((and (telega-file--downloaded-p (plist-get best :photo))
(not (eq 'best (car cached-preview))))
(cons 'best
(telega-preview-one-line-create-svg
(telega--tl-get best :photo :local :path) nil
(plist-get best :width) (plist-get best :height))))
(cached-preview
cached-preview)
(minithumb
(cons 'mini
(telega-preview-one-line-create-svg
(base64-decode-string (plist-get minithumb :data)) t
(plist-get minithumb :width)
(plist-get minithumb :height)))))))
(plist-put photo :telega-preview-1 preview-new)
(cdr preview-new))))
(defun telega-msg--preview-video-image (msg)
"Return one line preview for the video message MSG..
Return nil if preview image is unavailable."
(when (and telega-use-images
(telega-chat-match-p (telega-msg-chat msg 'offline)
telega-use-one-line-preview-for))
(let* ((video (telega--tl-get msg :content :video))
(thumb (plist-get video :thumbnail))
(minithumb (plist-get video :minithumbnail))
(cached-preview
(plist-get video :telega-preview-1))
(preview-new
(cond ((and thumb
(memq (telega--tl-type (plist-get thumb :format))
'(thumbnailFormatJpeg thumbnailFormatPng))
(telega-file--downloaded-p (plist-get thumb :file))
(not (eq 'best (car cached-preview))))
(cons 'best
(telega-preview-one-line-create-svg
(telega--tl-get thumb :file :local :path) nil
(plist-get thumb :width) (plist-get thumb :height)
'video)))
(cached-preview
cached-preview)
(minithumb
(cons 'mini
(telega-preview-one-line-create-svg
(base64-decode-string (plist-get minithumb :data)) t
(plist-get minithumb :width)
(plist-get minithumb :height)
'video))))))
(plist-put video :telega-preview-1 preview-new)
(cdr preview-new))))
(defun telega-audio--create-image (audio &optional file)
"Function to create image for AUDIO album cover."
(telega-thumb-or-minithumb--create-image
audio file
(plist-get audio :album_cover_thumbnail)
(plist-get audio :album_cover_minithumbnail)))
;; TODO: draw tringle inside preview image
(defun telega-video--create-image (video &optional file)
"Create image to preview VIDEO content."
(if (not (fboundp 'svg-embed-base-uri-image))
(telega-thumb-or-minithumb--create-image video file)
;; SVG's `:base-uri' is available
(let ((thumb (plist-get video :thumbnail))
(minithumb (plist-get video :minithumbnail)))
(cond ((and (memq (telega--tl-type (plist-get thumb :format))
'(thumbnailFormatJpeg thumbnailFormatPng))
(telega-file--downloaded-p (plist-get thumb :file)))
(telega-video--create-svg
(telega--tl-get thumb :file :local :path) nil
(plist-get thumb :width) (plist-get thumb :height)))
(minithumb
(telega-video--create-svg
(base64-decode-string (plist-get minithumb :data)) t
(plist-get minithumb :width)
(plist-get minithumb :height)))
(t
(telega-video--create-svg
nil nil
(plist-get video :width)
(plist-get video :height))))
)))
(defun telega-media--image-update (obj-spec file &optional cache-prop)
"Called to update the image contents for the OBJ-SPEC.
OBJ-SPEC is cons of object and create image function.
Create image function accepts two arguments - object and FILE.
Return updated image, cached or created with create image function.
CACHE-PROP specifies property name to cache image at OBJ-SPEC.
Default is `:telega-image'."
(let ((cached-image (plist-get (car obj-spec) (or cache-prop :telega-image)))
(simage (funcall (cdr obj-spec) (car obj-spec) file)))
;; NOTE: Sometimes `create' function returns nil results
(when (and telega-use-images (not simage))
(error "telega: [BUG] Image create (%S %S %S) -> nil"
(cdr obj-spec) (car obj-spec) file))
(unless (equal cached-image simage)
;; Update the image
(if cached-image
(setcdr cached-image (cdr simage))
(setq cached-image simage))
;; NOTE: We call `image-flush' because only filename in
;; the image spec can be changed (during animation for
;; example), and image caching won't notice this because
;; `(sxhash cached-image)' and `(sxhash simage)' might
;; return the same!
;;
;; We do it under `ignore-errors' to avoid any image related errors
;; see https://github.com/zevlg/telega.el/issues/349
;; and https://t.me/emacs_telega/33101
(when telega-use-images
(ignore-errors (image-flush cached-image)))
(plist-put (car obj-spec) (or cache-prop :telega-image) cached-image))
cached-image))
(defun telega-media--image (obj-spec file-spec &optional force-update cache-prop)
"Return image for media object specified by OBJ-SPEC.
File is specified with FILE-SPEC.
CACHE-PROP specifies property name to cache image at OBJ-SPEC.
Default is `:telega-image'."
(let ((cached-image (plist-get (car obj-spec) (or cache-prop :telega-image))))
(when (or force-update (not cached-image))
(let ((media-file (telega-file--renew (car file-spec) (cdr file-spec))))
;; First time image is created or update is forced
(setq cached-image
(telega-media--image-update obj-spec media-file cache-prop))
;; Possible initiate file downloading
(when (and telega-use-images
(or (telega-file--need-download-p media-file)
(telega-file--downloading-p media-file)))
(telega-file--download media-file nil
(lambda (dfile)
(telega-media--image-update obj-spec dfile cache-prop)
(force-window-update))))))
cached-image))
(defun telega-photo--image (photo limits)
"Return best suitable image for the PHOTO."
(let* ((best (telega-photo--best photo limits))
(cheight (telega-media--cheight-for-limits
(plist-get best :width)
(plist-get best :height)
limits))
(create-image-fun
(progn
(cl-assert (> cheight 0))
(cl-assert (<= cheight (nth 3 limits)))
(lambda (_photoignored &optional _fileignored)
;; 1) FILE downloaded, show photo
;; 2) Thumbnail is downloaded, use it
;; 2.5) Minithumbnail is available, use it
;; 3) FILE downloading, fallback to progress svg
(let ((best-file (telega-file--renew best :photo)))
(if (telega-file--downloaded-p best-file)
(telega-thumb--create-image best best-file cheight)
(let* ((thumb (telega-photo--thumb photo))
(thumb-file (telega-file--renew thumb :photo)))
(if (telega-file--downloaded-p thumb-file)
(telega-thumb--create-image thumb thumb-file cheight)
(if-let ((minithumb (plist-get photo :minithumbnail)))
(telega-minithumb--create-image minithumb cheight)
(telega-photo--progress-svg best cheight))))))))))
(telega-media--image
(cons photo create-image-fun)
(cons best :photo)
'force-update)))
(defun telega-avatar--title-text (sender)
"Create textual avatar for the SENDER (chat or user).
Return string of width 3."
(let ((title (telega-msg-sender-title sender))
(title-faces (telega-msg-sender-title-faces sender)))
(if telega-avatar-text-compose-chars
(concat (propertize (compose-chars (aref telega-symbol-circle 0)
(aref title 0))
'face title-faces)
" ")
(concat "("
(propertize (substring title 0 1) 'face title-faces)
")"))))
(defun telega-avatar--create-image (sender file &optional cheight addon-function)
"Create SENDER (char or user) avatar image.
CHEIGHT specifies avatar height in chars, default is 2."
;; NOTE:
;; - For CHEIGHT==1 align avatar at vertical center
;; - For CHEIGHT==2 make svg height to be 3 chars, so if font size
;; is increased, there will be no gap between two slices
(unless cheight (setq cheight 2))
(let* ((base-dir (telega-directory-base-uri telega-database-dir))
(photofile (telega--tl-get file :local :path))
(factors (alist-get cheight telega-avatar-factors-alist))
(cfactor (or (car factors) 0.9))
(mfactor (or (cdr factors) 0.1))
(xh (telega-chars-xheight cheight))
(margin (* mfactor xh))
(ch (* cfactor xh))
(cfull (floor (+ ch margin)))
(aw-chars (telega-chars-in-width ch))
(aw-chars-3 (if (> aw-chars 3) (- aw-chars 3) 0))
(svg-xw (telega-chars-xwidth aw-chars))
(svg-xh (cond ((= cheight 1) cfull)
((= cheight 2) (+ cfull (telega-chars-xheight 1)))
(t xh)))
(svg (telega-svg-create svg-xw svg-xh))
(name (telega-msg-sender-title sender)))
(if (telega-file-exists-p photofile)
(let ((img-type (telega-image-supported-file-p photofile))
(clip (telega-svg-clip-path svg "clip")))
(svg-circle clip (/ svg-xw 2) (/ cfull 2) (/ ch 2))
(telega-svg-embed svg (list (file-relative-name photofile base-dir)
base-dir)
(format "image/%S" img-type)
nil
:x (/ (- svg-xw ch) 2) :y (/ margin 2)
:width ch :height ch
:clip-path "url(#clip)"))
;; Draw initials
(let ((fsz (/ ch 2))
(colors (telega-msg-sender-color sender)))
(svg-gradient svg "cgrad" 'linear
(list (cons 0 (telega-color-name-as-hex-2digits
(or (nth 1 colors) "gray75")))
(cons ch (telega-color-name-as-hex-2digits
(or (nth 0 colors) "gray25")))))
(svg-circle svg (/ svg-xw 2) (/ cfull 2) (/ ch 2) :gradient "cgrad")
(svg-text svg (substring name 0 1)
:font-size (/ ch 2)
:font-weight "bold"
:fill "white"
:font-family "monospace"
;; XXX insane X/Y calculation
:x (- (/ svg-xw 2) (/ fsz 3))
:y (+ (/ fsz 3) (/ cfull 2)))))
;; XXX: Apply additional function, used by `telega-patrons-mode'
;; Also used to outline currently speaking users in voice chats
(when addon-function
(funcall addon-function svg (list (/ svg-xw 2) (/ cfull 2) (/ ch 2))))
(telega-svg-image svg :scale 1.0
:width svg-xw :height svg-xh
:ascent 'center
:mask 'heuristic
:base-uri (expand-file-name "dummy" base-dir)
;; Correct text for tty-only avatar display
:telega-text
(cons (concat (telega-avatar--title-text sender)
(make-string aw-chars-3 ?\u00A0))
(mapcar (lambda (_ignore)
(make-string (+ 3 aw-chars-3) ?\u00A0))
(make-list (1- cheight) 'not-used))))
))
(defun telega-avatar--create-image-one-line (sender file)
"Create SENDER (chat or user) avatar image for one line use."
(telega-avatar--create-image sender file 1))
(defun telega-avatar--create-image-three-lines (sender file)
"Create SENDER (chat or user) avatar image for three lines use."
(telega-avatar--create-image sender file 3))
(defun telega-msg-sender-avatar-image (msg-sender
&optional create-image-fun
force-update cache-prop)
"Create avatar image for the MSG-SENDER.
By default CREATE-IMAGE-FUN is `telega-avatar--create-image'."
(cl-assert msg-sender)
(telega-media--image
(cons msg-sender (or create-image-fun #'telega-avatar--create-image))
(if (telega-user-p msg-sender)
(cons (plist-get msg-sender :profile_photo) :small) ;user
(cl-assert (telega-chat-p msg-sender))
(cons (plist-get msg-sender :photo) :small)) ;chat
force-update cache-prop))
(defun telega-msg-sender-avatar-image-one-line (msg-sender
&optional create-image-fun
force-update cache-prop)
"Create one-line avatar for the MSG-SENDER.
By default CREATE-IMAGE-FUN is `telega-avatar--create-image-one-line'."
(telega-msg-sender-avatar-image
msg-sender (or create-image-fun #'telega-avatar--create-image-one-line)
force-update (or cache-prop :telega-avatar-1)))
(defun telega-msg-sender-avatar-image-three-lines (msg-sender
&optional create-image-fun
force-update cache-prop)
"Create three lines avatar for the MSG-SENDER.
By default CREATE-IMAGE-FUN is `telega-avatar--create-image-three-lines'."
(telega-msg-sender-avatar-image
msg-sender (or create-image-fun #'telega-avatar--create-image-three-lines)
force-update (or cache-prop :telega-avatar-3)))
;; Location
(defun telega-map--embed-sender (svg map sender sender-loc)
"Embed sender to the location map.
SENDER can be a nil, meaning venue location is to be displayed."
(let* ((base-dir (telega-directory-base-uri telega-database-dir))
(width (plist-get map :width))
(height (plist-get map :height))
(map-loc (plist-get map :map-location)) ;at image center
(raw-map-sender (plist-get map :sender))
(map-sender (when raw-map-sender
(telega-msg-sender raw-map-sender)))
(user-loc sender-loc)
(user-loc-off
(telega-location-distance map-loc user-loc 'components))
(user-y (+ (/ height 2)
(telega-map--distance-pixels
(car user-loc-off) user-loc (plist-get map :zoom))))
(user-x (+ (/ width 2)
(telega-map--distance-pixels
(cdr user-loc-off) user-loc (plist-get map :zoom))))
(sender-shown-p nil))
;; NOTE: Always show map sender, otherwise show sender only if it
;; fits into map image
(when-let* ((show-sender-p (and sender
(or (eq sender map-sender)
(and (< 0 user-x width)
(< 0 user-y height)))))
(sender-photo (if (telega-user-p sender)
(telega--tl-get sender :profile_photo :small)
(cl-assert (telega-chat-p sender))
(telega--tl-get sender :photo :small))))
(when (telega-file--downloaded-p sender-photo)
(let* ((photofile (telega--tl-get sender-photo :local :path))
(img-type (telega-image-supported-file-p photofile))
(clip-name (make-temp-name "user-clip"))
(clip (telega-svg-clip-path svg clip-name))
(sz (/ (plist-get map :height) 8))
(sz2 (/ sz 2)))
(svg-circle clip (+ user-x sz2) (- user-y sz2) sz2)
(svg-polygon clip (list (cons user-x user-y)
(cons (+ user-x (/ sz2 4))
(- user-y sz2))
(cons (+ user-x sz2)
(- user-y (/ sz2 4)))))
(telega-svg-embed svg (list (file-relative-name photofile base-dir)
base-dir)
(format "image/%S" img-type) nil
:x user-x :y (- user-y sz)
:width sz :height sz
:clip-path (format "url(#%s)" clip-name)))
(setq sender-shown-p t)))
(cond ((or (null sender) (eq sender map-sender))
;; Always show dot for map sender
(svg-circle svg user-x user-y 8
:stroke-width 4
:stroke-color "white"
:fill-color (face-foreground 'telega-blue))
;; User's direction heading 1-360, 0 if unknown
(let ((heading (or (plist-get map :user-heading) 0)))
(unless (zerop heading)
(let* ((w2 user-x)
(h2 user-y)
(angle1 (* float-pi (/ (- (+ heading 200)) 180.0)))
(angle2 (* float-pi (/ (- (+ heading 160)) 180.0)))
(h-dx1 (* 100 (sin angle1)))
(h-dy1 (* 100 (cos angle1)))
(h-dx2 (* 100 (sin angle2)))
(h-dy2 (* 100 (cos angle2)))
(hclip (telega-svg-clip-path svg "headclip")))
(telega-svg-path hclip (format "M %d %d L %f %f L %f %f Z"
w2 h2 (+ w2 h-dx1) (+ h2 h-dy1)
(+ w2 h-dx2) (+ h2 h-dy2)))
(telega-svg-gradient
svg "headgrad" 'radial
(list (list 0 (telega-color-name-as-hex-2digits
(face-foreground 'telega-blue))
:opacity 0.9)
;; (list 50 (telega-color-name-as-hex-2digits
;; (face-foreground 'telega-blue))
;; :opacity 0.5)
(list 100 (telega-color-name-as-hex-2digits
(face-foreground 'telega-blue))
:opacity 0.0)))
(svg-circle svg w2 h2 50
:gradient "headgrad"
:clip-path "url(#headclip)")
)))
;; Proximity Alert Radius
(let* ((alert-radius (or (plist-get map :user-alert-radius) 0))
(radius-px (unless (zerop alert-radius)
(telega-map--distance-pixels
alert-radius
(plist-get map :user-location)
(plist-get map :zoom)))))
(when radius-px
(svg-circle svg user-x user-y radius-px
:fill "none"
:stroke-dasharray "4 6"
:stroke-width 4
:stroke-opacity "0.6"
:stroke-color "black")))
)
(sender-shown-p
(svg-circle svg user-x user-y 4
:stroke-width 2
:stroke-color "white"
:fill-color "black")))
(or (eq sender map-sender) sender-shown-p)))
(defun telega-map--create-image (map &optional _file)
"Create map image for location MAP."
(let* ((base-dir (telega-directory-base-uri telega-database-dir))
(map-photo (telega-file--renew map :photo))
(map-photofile (when map-photo
(telega--tl-get map-photo :local :path)))
;; NOTE: `raw-map-sender' is nil for `venue' locations
(raw-map-sender (plist-get map :sender))
(map-sender (when raw-map-sender
(telega-msg-sender raw-map-sender)))
(width (plist-get map :width))
(height (plist-get map :height))
(svg (telega-svg-create width height)))
(cl-assert (and (integerp width) (integerp height)))
(if (and (telega-file--downloaded-p map-photo)
(telega-file-exists-p map-photofile))
(telega-svg-embed svg (list (file-relative-name map-photofile base-dir)
base-dir)
"image/png" nil
:x 0 :y 0 :width width :height height)
(svg-rectangle svg 0 0 width height
:fill-color (telega-color-name-as-hex-2digits
(or (face-foreground 'shadow) "gray50"))))
;; TODO: show other users close enough to `:sender'
;; NOTE: First draw other users
(when (and telega-location-show-me
telega-my-location
(not (telega-me-p map-sender)))
(telega-map--embed-sender svg map (telega-user-me) telega-my-location))
;; Show map sender with heading and proximity alert zone
;; NOTE: map sender can be nil for venue messages
(telega-map--embed-sender svg map map-sender (plist-get map :user-location))
(telega-svg-image svg :scale 1.0
:width width :height height
:ascent 'center
:base-uri (expand-file-name "dummy" base-dir))))
;; See
;; https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Resolution_and_Scale
(defun telega-map--distance-pixels (meters loc zoom)
"Convert METERS distance at LOC to the pixels distance at ZOOM level."
(let ((lat (plist-get loc :latitude)))
(round (/ meters
(/ (* 156543.03 (cos (degrees-to-radians lat)))
(expt 2 zoom))))))
(defun telega-map--need-new-map-photo-p (map loc)
"Return non-nil if need to fetch new map photo for new user location LOC."
(or (and (not (plist-get map :photo))
(not (plist-get map :get-map-extra)))
(not loc)
(not (plist-get map :map-location))
(let* ((map-xh (telega-chars-xheight (car telega-location-size)))
(distance
(telega-location-distance (plist-get map :map-location) loc))
(distance-px (telega-map--distance-pixels
distance loc (plist-get map :zoom))))
(> distance-px (/ map-xh 4)))))
(defun telega-map--get-thumbnail-file (map loc &optional msg)