forked from zevlg/telega.el
-
Notifications
You must be signed in to change notification settings - Fork 0
/
telega-sticker.el
1045 lines (933 loc) · 42.9 KB
/
telega-sticker.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-sticker.el --- Stickers for the telega -*- lexical-binding:t -*-
;; Copyright (C) 2019 by Zajcev Evgeny.
;; Author: Zajcev Evgeny <[email protected]>
;; Created: Fri Feb 8 21:08:24 2019
;; Keywords:
;; telega is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; telega is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with telega. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
(require 'seq) ;seq-doseq
(require 'telega-core)
(require 'telega-tdlib)
(require 'telega-util)
(require 'telega-media)
(require 'telega-ffplay)
;; shutup compiler
(defvar ido-matches)
(defvar ivy--index)
(defvar ivy--old-cands)
(declare-function telega-chatbuf-sticker-insert "telega-chat" (sticker))
(declare-function telega-chatbuf-animation-insert "telega-chat" (animation))
(defvar telega-help-win--emoji nil
"Emoji for which help window is displayed.")
(make-variable-buffer-local 'telega-help-win--emoji)
(defvar telega-help-win--stickerset nil
"Stickerset for which help window is displayed.")
(make-variable-buffer-local 'telega-help-win--stickerset)
(defvar telega-sticker--use-thumbnail nil
"Bind this variable to non-nil to use thumbnail instead of image.
Thumbnail is a smaller (and faster) version of sticker image.")
(defvar telega-sticker-button-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map button-map)
(define-key map (kbd "f") 'telega-sticker-toggle-favorite)
(define-key map (kbd "*") 'telega-sticker-toggle-favorite)
(define-key map (kbd "i") 'telega-sticker-help)
(define-key map (kbd "h") 'telega-sticker-help)
map))
(define-button-type 'telega-sticker
:supertype 'telega
:inserter 'telega-ins--sticker-image
; 'read-only t
'keymap telega-sticker-button-map)
(defun telega-sticker-at (&optional pos)
"Retur sticker at POS."
(let ((button (button-at (or pos (point)))))
(when (and button (eq (button-type button) 'telega-sticker))
(button-get button :value))))
(defun telega-sticker-static-p (sticker)
"Return non-nil if sticker is a static sticker."
(eq 'stickerFormatWebp (telega--tl-type (plist-get sticker :format))))
(defsubst telega-sticker--file (sticker)
"Return STICKER's file."
(telega-file--renew sticker :sticker))
(defsubst telega-sticker--thumb-file (sticker)
"Return STICKER's thumbnail file."
;; NOTE: return only for non-animated thumbnails, otherwise
;; minithumbnail will be used
(when-let ((thumb (plist-get sticker :thumbnail)))
(when (memq (telega--tl-type (plist-get thumb :format))
'(thumbnailFormatJpeg thumbnailFormatPng thumbnailFormatWebp))
(telega-file--renew thumb :file))))
(defsubst telega-sticker-favorite-p (sticker)
"Return non-nil if STICKER is in favorite list."
(memq (telega--tl-get sticker :sticker :id) telega--stickers-favorite))
(defun telega-sticker--download (sticker)
"Ensure STICKER data is downloaded.
Download only if `telega-use-images' is non-nil."
(let ((sticker-file (telega-sticker--file sticker))
(thumb-file (telega-sticker--thumb-file sticker)))
(when telega-use-images
(when (telega-file--need-download-p thumb-file)
(telega-file--download thumb-file :priority 6))
(unless telega-sticker--use-thumbnail
(when (telega-file--need-download-p sticker-file)
(telega-file--download sticker-file :priority 2))))
))
(defsubst telega-sticker-emoji (sticker &optional no-properties)
"Return STICKER's emoji or empty string."
(or (telega-tl-str sticker :emoji no-properties) ""))
(defun telega-sticker-size (sticker)
"Return size suitable to display a STICKER."
(or (plist-get sticker :telega-sticker-size) telega-sticker-size))
(defun telega-stickerset--download (sset)
"Ensure sticker set SSET data is downloaded."
(seq-doseq (sticker (plist-get sset :stickers))
(telega-sticker--download sticker)))
(defun telega-stickerset--ensure (sset)
"Ensure sticker set SSET is put into `telega--stickersets'."
(setf (alist-get (plist-get sset :id) telega--stickersets nil nil 'equal)
sset)
(when telega-sticker-set-download
(telega-stickerset--download sset))
sset)
(defun telega-stickerset-get (set-id &optional locally-p callback)
"Get stickerset by SET-ID.
If LOCALLY-P is non-nil, then do not perform request to telega-server.
If CALLBACK is specified and stickerset is not yet fetched, then
fetch stickerset asynchronously and call the CALLBACK function
with one argument - stickerset."
(declare (indent 2))
(let ((sset (cdr (assoc set-id telega--stickersets))))
(if (or locally-p sset)
(if callback
(funcall callback sset)
sset)
(telega--getStickerSet set-id callback))))
(defun telega-stickerset-installed-p (sset)
"Return non-nil if sticker set SSET is installed."
(or (member (plist-get sset :id) telega--stickersets-installed-ids)
(cl-find (plist-get sset :id) telega--stickersets-custom-emojis
:key (telega--tl-prop :id) :test #'equal)))
(defun telega-sticker--dice-get (dice-value &optional locally-p callback)
"Return sticker for the DICE-VALUE.
Return nil, if sticker is not found.
CALLBACK is called without arguments"
(declare (indent 2))
(when-let ((dice-sset-name
(plist-get telega--options :animated_dice_sticker_set_name)))
(let ((sset (cl-find dice-sset-name
telega--stickersets-system
:key (telega--tl-prop :name)
:test #'equal)))
(if (or locally-p sset)
(cl-find (format "%d\uFE0F⃣" dice-value)
(plist-get sset :stickers)
:key #'telega-sticker-emoji
:test #'equal)
(telega--searchStickerSet dice-sset-name
(lambda (dice-sset)
(cl-assert (equal (plist-get dice-sset :name) dice-sset-name))
(setq telega--stickersets-system
(push dice-sset telega--stickersets-system))
(when callback
(funcall callback))))
))))
(defun telega-sticker--emoji-get (emoji)
"Get Animated Sticker for emoji."
;; NOTE: EMOJI itself or sticker's emoji could be a fe0f emoji
(when (telega-emoji-fe0f-p emoji)
(setq emoji (substring emoji 0 1)))
(cl-find emoji (plist-get (telega-stickerset-get
telega--animated-emojis-stickerset-id 'locally)
:stickers)
:test #'equal
:key (lambda (obj)
(let ((sticker-emoji (telega-tl-str obj :emoji)))
(if (telega-emoji-fe0f-p sticker-emoji)
(substring sticker-emoji 0 1)
sticker-emoji)))))
(defun telega-sticker-toggle-favorite (sticker)
"Toggle sticker as favorite."
(interactive (list (telega-sticker-at)))
(funcall (if (telega-sticker-favorite-p sticker)
#'telega--removeFavoriteSticker
#'telega--addFavoriteSticker)
(list :@type "inputFileId"
:id (telega--tl-get sticker :sticker :id))
(lambda (_ignoredreply)
;; Update corresponding sticker image
(telega-media--image-update
(cons sticker 'telega-sticker--create-image)
(cons sticker :sticker))
(force-window-update))))
(defun telega-sticker--svg-outline-path (svg tl-path factor &rest args)
"Draw closed TL-PATH into SVG."
(let* ((x-factor (car factor))
(y-factor (cdr factor))
(tl-path-commands (plist-get tl-path :commands))
(tl-cmd1 (aref tl-path-commands 0))
(tl-move-to (cl-ecase (telega--tl-type tl-cmd1)
(vectorPathCommandCubicBezierCurve
(plist-get tl-cmd1 :start_control_point))
(vectorPathCommandLine
(plist-get tl-cmd1 :end_point))))
(svg-path-commands
(mapconcat
(lambda (path-cmd)
(cl-ecase (telega--tl-type path-cmd)
(vectorPathCommandCubicBezierCurve
(let ((p1 (plist-get path-cmd :start_control_point))
(p2 (plist-get path-cmd :end_control_point))
(p3 (plist-get path-cmd :end_point)))
(format "C%f,%f,%f,%f,%f,%f"
(* x-factor (plist-get p1 :x))
(* y-factor (plist-get p1 :y))
(* x-factor (plist-get p2 :x))
(* y-factor (plist-get p2 :y))
(* x-factor (plist-get p3 :x))
(* y-factor (plist-get p3 :y)))))
(vectorPathCommandLine
(let ((end-point (plist-get path-cmd :end_point)))
(format "L%f,%f"
(* x-factor (plist-get end-point :x))
(* y-factor (plist-get end-point :y)))))))
tl-path-commands "\n")))
(apply #'telega-svg-path svg
(concat (format "M%f,%f\n"
(* x-factor (plist-get tl-move-to :x))
(* y-factor (plist-get tl-move-to :y)))
svg-path-commands "Z")
args)))
(defun telega-sticker--svg-outline (svg sticker &rest args)
"Draw STICKER outline path to the SVG."
(declare (indent 2))
(cl-assert (plist-get sticker :outline))
(let ((factor (cons (/ (float (telega-svg-width svg))
(plist-get sticker :width))
(/ (float (telega-svg-height svg))
(plist-get sticker :height)))))
(seq-doseq (outline-path (plist-get sticker :outline))
(apply #'telega-sticker--svg-outline-path
svg outline-path factor args))))
(defun telega-sticker--progress-svg (sticker)
"Generate svg for STICKER showing download progress."
(let* ((emoji (telega-sticker-emoji sticker 'no-props))
(sticker-size (telega-sticker-size sticker))
(cheight (car sticker-size))
(xh (telega-chars-xheight cheight))
(w-chars (telega-chars-in-width xh))
(xw (telega-chars-xwidth w-chars))
(svg (telega-svg-create xw xh))
(font-size (/ xh 2)))
(if (plist-get sticker :outline)
(telega-sticker--svg-outline svg sticker
:fill (telega-color-name-as-hex-2digits
(or (face-foreground 'telega-shadow) "gray50")))
;; draw emoji and progress circle
(svg-text svg (if (string-empty-p emoji)
"?"
(substring emoji 0 1))
:font-size font-size
:font-weight "bold"
:fill "white"
:font-family "monospace"
:x "50%"
:text-anchor "middle"
;; Insane Y calculation
:y (+ (/ xh 2) (/ font-size 3)))
(telega-svg-progress svg (telega-file--downloading-progress
(telega-sticker--file sticker))))
(telega-svg-image svg
:scale 1.0
:height (telega-ch-height cheight)
:telega-nslices cheight
:ascent 'center
:mask 'heuristic)))
(defvar telega-sticker--convert-cmd
(if (or telega-use-docker
(executable-find "dwebp")
(not (telega-ffplay-has-decoder-p "webp")))
"dwebp -nofancy -mt -o %p %w"
"ffmpeg -v quiet -i %w %p")
"Command to convert WEBP file to PNG file.
%p - png filename
%w - webp filename.")
(defun telega-sticker--webp-to-png (webp-filename)
"Convert FILENAME in webp format to png.
Return path to png file."
(let ((png-filename (concat (file-name-sans-extension webp-filename)
"_telega.png")))
(unless (file-exists-p png-filename)
(message "telega: Converting %s to png..."
(file-name-nondirectory webp-filename))
(let ((convert-cmd
(telega-docker-exec-cmd
(format-spec telega-sticker--convert-cmd
(format-spec-make ?p png-filename
?w webp-filename))
'try-host-cmd-first nil 'no-error)))
(cond (convert-cmd
(telega-debug "WEBP -> PNG: %s" convert-cmd)
(shell-command-to-string convert-cmd))
((image-type-available-p 'webp)
;; NOTE: If `webp' image is supported by Emacs, use it.
;; We use webp->png converter by default, because it
;; gives better results, then Emacsen `webp' images.
;; NOTE: Emacs Bug causing bad quality for webp has
;; been fixed.
(setq png-filename webp-filename))
(t
(telega-help-message 'no-dwebp-binary
"Can't find `%s' binary. `webp' system package not installed?"
(car (split-string telega-sticker--convert-cmd))))))
(message "telega: Converting %s to png...DONE"
(file-name-nondirectory webp-filename)))
(when (file-exists-p png-filename)
png-filename)))
(defun telega-sticker--create-image (sticker &optional _ignoredfile)
"Return image for the STICKER."
;; Three cases:
;; 1) Sticker downloaded (in case if static)
;; Just show (and cache) sticker image
;;
;; 2) Thumbnail is downloaded, while sticker still downloading
;; Show thumbnail (caching temporary), waiting for sticker to
;; be downloaded
;;
;; 3) Thumbnail and sticker downloading
;; Fallback to `telega-sticker--progress-svg', waiting for
;; thumbnail or sticker to be downloaded
(let* ((sticker-size (telega-sticker-size sticker))
(cheight (car sticker-size))
(sfile (telega-sticker--file sticker))
(tfile (telega-sticker--thumb-file sticker))
(filename (or (and (or telega-sticker--use-thumbnail
(not (telega-sticker-static-p sticker)))
(telega-file--downloaded-p tfile) tfile)
(and (telega-sticker-static-p sticker)
(telega-file--downloaded-p sfile) sfile)
(and (telega-file--downloaded-p tfile) tfile)))
(img-file (or (plist-get sticker :telega-ffplay-frame-filename)
(when-let ((fn (telega--tl-get filename :local :path)))
(if (or (fboundp 'imagemagick-types)
(image-type-available-p 'webp)
(not (equal (file-name-extension fn) "webp")))
fn
(telega-sticker--webp-to-png fn)))))
(custom-create-image-function
(plist-get sticker :telega-create-image-function))
(img
(cond (custom-create-image-function
;; NOTE: Custom emoji stickers has custom create
;; image function
(funcall custom-create-image-function sticker img-file))
(img-file
(apply #'telega-create-image img-file nil nil
:height (telega-ch-height cheight)
:telega-nslices cheight
:scale 1.0
:ascent 'center
(when (telega-sticker-favorite-p sticker)
(list :background telega-sticker-favorite-background))
))
(t
;; Fallback to svg
(telega-sticker--progress-svg sticker)))))
img))
(defun telega-sticker--create-image-one-line (sticker &optional file)
"Create image for one-line STICKER usage."
(let ((telega-sticker-size (cons 1 (cdr telega-sticker-size))))
(telega-sticker--create-image sticker file)))
(defun telega-sticker--image (sticker &optional image-create-fun cache-prop)
"Return image for the STICKER."
(telega-media--image
(cons sticker (or image-create-fun #'telega-sticker--create-image))
(if (or (and telega-sticker--use-thumbnail
(plist-get sticker :thumbnail))
(not (telega-sticker-static-p sticker)))
(cons (plist-get sticker :thumbnail) :file)
(cons sticker :sticker))
nil cache-prop))
(defun telega-ins--sticker-image (sticker &optional slices-p)
"Inserter for the STICKER.
If SLICES-P is non-nil, then insert STICKER using slices."
(if (or (not telega-use-images)
(not (display-graphic-p (telega-x-frame))))
(telega-ins "<STICKER\u00A0" (telega-sticker-emoji sticker) ">")
(funcall (if slices-p #'telega-ins--image-slices #'telega-ins--image)
(telega-sticker--image sticker))))
(defun telega-ins--stickerset-change-button (sset)
(telega-ins--box-button (if (telega-stickerset-installed-p sset)
;; I18N: XXX
"Uninstall"
"Install")
:value sset
'action 'telega-button--stickerset-change-action))
(defun telega-button--stickerset-change-action (button)
(let ((sset (button-get button :value)))
(telega--changeStickerSet sset (not (telega-stickerset-installed-p sset)))
(telega-save-cursor
(telega-button--change button
(telega-ins--stickerset-change-button sset)))))
(defun telega-sticker--chosen-action (button)
"Execute action when sticker BUTTON is pressed."
(cl-assert (eq major-mode 'help-mode))
(let ((sticker (telega-sticker-at button))
(custom-action (button-get button :action))
(thw-emoji telega-help-win--emoji)
(chat telega--chat))
(cl-assert (or telega--chat custom-action))
;; NOTE: Kill help win before modifying chatbuffer, because it
;; recovers window configuration on kill
(quit-window 'kill-buffer)
(if custom-action
(funcall custom-action sticker)
;; NOTE: Default action is to insert selected sticker into
;; chatbuf
(with-telega-chatbuf chat
;; Substitute emoji with sticker, in case help win has
;; `telega-help-win--emoji' set
(when thw-emoji
(let* ((start (save-excursion (telega-emoji-backward) (point)))
(end (point))
(emoji (buffer-substring start end)))
(unless (string= emoji thw-emoji)
(error "Emoji changed %s -> %s" thw-emoji emoji))
(delete-region start end)))
(if (telega-custom-emoji-sticker-p sticker)
(telega-chatbuf-custom-emoji-insert sticker thw-emoji)
(telega-chatbuf-sticker-insert sticker))))))
(cl-defun telega-ins--sticker-list (stickers &key column custom-action)
"Insert STICKERS list int current buffer.
COLUMN specifies column to fill stickers into. By default
`fill-column' is used.
CUSTOM-ACTION - function accepting single argument (a sticker) to be
called when some sticker is selected."
(declare (indent 1))
(unless column
(setq column (current-fill-column)))
(seq-doseq (sticker stickers)
(telega-ins-prefix (unless (bolp) "\n")
(telega-button--insert 'telega-sticker sticker
'help-echo (let ((emoji (telega-sticker-emoji sticker)))
(concat "Emoji: " emoji " " (telega-emoji-name emoji)))
'action #'telega-sticker--chosen-action
:action custom-action
:telega-add-sensor-func
(when (and (not (telega-sticker-static-p sticker))
telega-sticker-animated-play)
(telega-sticker--gen-sensor-func sticker)))
(when telega-sticker-set-show-emoji
(telega-ins (telega-sticker-emoji sticker) " "))
;; NOTE: `telega-ins-prefix' will insert "\n" if next sexp
;; evaluates to non-nil
(and (> column 0) (> (current-column) column)))
;; (redisplay)
))
(defun telega-describe-stickerset (sset &optional for-chat)
"Describe the sticker set.
SSET can be either `sticker' or `stickerSetInfo'."
(with-telega-help-win "*Telegram Sticker Set*"
(setq telega--chat for-chat)
(setq telega-help-win--stickerset sset)
(telega-ins-describe-item "Title"
(telega-ins (telega-tl-str sset :title))
(when (plist-get sset :is_official)
(telega-ins (telega-symbol 'verified)))
(telega-ins " ")
(telega-ins--stickerset-change-button sset))
(telega-ins-describe-item "Link"
(let ((link (concat (or (plist-get telega--options :t_me_url)
"https://t.me/")
"addstickers/" (plist-get sset :name))))
(telega-ins--raw-button (telega-link-props 'url link 'face 'telega-link)
(telega-ins link))))
(when telega-debug
(telega-ins-describe-item "Get"
(telega-ins-fmt "(telega-stickerset-get \"%s\")"
(plist-get sset :id))))
(when (plist-get sset :needs_repainting)
(telega-ins-describe-item "Adaptive"
(telega-ins (telega-symbol 'checkmark))))
(when (plist-get sset :is_owned)
(telega-ins-describe-item "Created by me"
(telega-ins (telega-symbol 'checkmark))))
;; NOTE: In case SSET is "stickerSetInfo" fetch real sticker set
;; and insert all the stickers
(let ((sticker-list-ins
(lambda (sticker-set)
(let ((stickers (plist-get sticker-set :stickers)))
(telega-ins-describe-item
(cl-ecase (telega--tl-type (plist-get sticker-set :sticker_type))
(stickerTypeMask
(telega-i18n "lng_stickers_masks_tab"))
(stickerTypeCustomEmoji
"Custom Emojis")
(stickerTypeRegular
(telega-i18n "lng_stickers_installed_tab")))
(telega-ins-fmt "%d\n" (length stickers))
(telega-ins--sticker-list stickers)))))
(sset-id (plist-get sset :id)))
(when (eq (telega--tl-type sset) 'stickerSetInfo)
(setq sset (telega-stickerset-get sset-id 'locally)))
;; Now SSET is always "stickerSet"
(if sset
(funcall sticker-list-ins sset)
;; Set `telega--help-win-param', so callback won't insert
;; stickers list of stickerset changes during request to
;; telega-server.
(setq telega--help-win-param sset-id)
(telega-stickerset-get sset-id nil
(telega--gen-ins-continuation-callback 'loading
sticker-list-ins
sset-id))))))
(defun telega-sticker-help (sticker)
"Describe sticker set for STICKER."
(interactive (list (telega-sticker-at (point))))
(telega-describe-stickerset
(telega-stickerset-get (plist-get sticker :set_id))))
(defun telega-sticker-choose-favorite-or-recent (for-chat)
"Choose recent sticker FOR-CHAT."
(interactive (list telega-chatbuf--chat))
(cl-assert for-chat)
(let ((help-window-select t))
(with-telega-help-win "*Telegram Stickers*"
(setq telega--chat for-chat)
;; NOTE: use callbacks for async stickers loading
(telega-ins (telega-i18n "lng_mac_touchbar_favorite_stickers") ":\n")
(telega--getFavoriteStickers
(telega--gen-ins-continuation-callback 'loading
#'telega-ins--sticker-list))
(telega-ins "\n")
(telega-ins "\n" (telega-i18n "lng_recent_stickers") ":\n")
(telega--getRecentStickers nil
(telega--gen-ins-continuation-callback 'loading
#'telega-ins--sticker-list)))))
(defun telega-sticker-choose-emoji (emoji for-chat &optional custom-emojis-only)
"Choose sticker by EMOJI FOR-CHAT.
If CUSTOM-EMOJIS-ONLY is specified, then list only custom emoji
stickers for the EMOJI."
(let ((help-window-select t))
(with-telega-help-win "*Telegram Stickers*"
(setq telega--chat for-chat)
(setq telega-help-win--emoji emoji)
;; NOTE: use callbacks for async stickers loading
(when (or (telega-chat-match-p for-chat 'saved-messages)
(telega-user-match-p (telega-user-me) 'is-premium))
(telega-ins--with-face 'telega-describe-item-title
(telega-ins-fmt "Custom Emoji for %s:\n" emoji))
(telega-ins--line-wrap-prefix " "
(telega--getStickers emoji
:chat for-chat
:tl-sticker-type '(:@type "stickerTypeCustomEmoji")
:callback (telega--gen-ins-continuation-callback 'loading
(lambda (stickers &rest args)
(apply #'telega-ins--sticker-list
(mapcar #'telega-custom-emoji-from-sticker
stickers)
args)))))
(telega-ins "\n"))
(unless custom-emojis-only
(telega-ins--with-face 'telega-describe-item-title
(telega-ins "Installed Stickers:\n"))
(telega-ins--line-wrap-prefix " "
(telega--getStickers emoji
:callback (telega--gen-ins-continuation-callback 'loading
#'telega-ins--sticker-list))
(telega-ins "\n"))
(telega-ins--with-face 'telega-describe-item-title
(telega-ins "Public Stickers:\n"))
(telega-ins--line-wrap-prefix " "
(telega--searchStickers emoji
:callback
(telega--gen-ins-continuation-callback 'loading
#'telega-ins--sticker-list))))
)))
(defun telega-stickerset--minibuf-post-command ()
"Function to complete stickerset for `completion-in-region-function'."
;; NOTE:
;; - Avoid help window selection by binding `help-window-select'
;; - Avoid smart packages (such as shackle) to handle help window by
;; binding `display-buffer-alist'
(let* ((help-window-select nil)
(display-buffer-alist nil) ;XXX
(start (minibuffer-prompt-end))
(end (point))
(str (cond ((memq 'ido-exhibit post-command-hook)
;; ido used
;;(and (boundp 'ido-cur-item) ido-matches)
(caar ido-matches))
((memq 'ivy--queue-exhibit post-command-hook)
;; ivy used
(nth ivy--index ivy--old-cands))
(t (buffer-substring start end))))
(comp (when str (car (all-completions str telega-minibuffer--choices))))
(sset-id (when comp (cadr (assoc comp telega-minibuffer--choices))))
(sset (when sset-id
(or (cl-find sset-id telega--stickersets-installed
:test 'equal
:key (telega--tl-prop :id))
(telega-stickerset-get sset-id))))
(tss-buffer (get-buffer "*Telegram Sticker Set*")))
(when (and sset
(or (not (buffer-live-p tss-buffer))
(not (with-current-buffer tss-buffer
(and (eq telega-minibuffer--chat telega--chat)
(eq sset telega-help-win--stickerset))))))
(let ((telega-sticker--use-thumbnail t))
(telega-describe-stickerset sset telega-minibuffer--chat)
;; Remove annoying "Type C-x 1 to delete the help window."
;; message
(message nil)))
;; Always pop to buffer, it might be hidden at the moment
(when (buffer-live-p tss-buffer)
(temp-buffer-window-show tss-buffer))
))
(defun telega-stickerset-title (sset &optional raw-p)
"Return title for the sticker set SSET.
If RAW-P is specified, then do not hack first char for helm."
(concat (when telega-sticker-set-show-cover
(when-let ((cover (car (append (or (plist-get sset :covers)
(plist-get sset :stickers))
nil))))
;; NOTE: helm uses 'display property of the first
;; character and expects it to be string, so we add
;; zero-width space in there
(concat (unless raw-p "\u200B")
(propertize
(telega-sticker-emoji cover)
'display (telega-sticker--image
cover #'telega-sticker--create-image-one-line
:telega-sticker-cover-1)))))
(or (telega-tl-str sset :title)
(telega-tl-str sset :name))))
(defun telega-stickerset-completing-read (prompt &optional sticker-sets)
"Read stickerset completing their names.
If STICKER-SETS is specified, then they are used,
otherwise installed stickersets is used.
Return sticker set."
(message "Loading stickers, please wait...")
(let* ((completion-ignore-case t)
(ssets (or sticker-sets
telega--stickersets-installed
;; NOTE: "getInstalledStickerSets" could generate
;; "updateInstalledStickerSets" event, we don't
;; want to handle it, since we are already getting
;; list of installed stickersets
(let ((telega-server--inhibit-events
(cons "updateInstalledStickerSets"
telega-server--inhibit-events)))
(setq telega--stickersets-installed
(telega--getInstalledStickerSets))
(setq telega--stickersets-installed-ids
(mapcar (telega--tl-prop :id)
telega--stickersets-installed))
telega--stickersets-installed)
(user-error "No installed sticker sets")))
;; Bindings used in `telega-stickerset-completing-read'
(telega-minibuffer--chat telega-chatbuf--chat)
(telega-minibuffer--choices
(mapcar (lambda (sset)
(list (telega-stickerset-title sset) (plist-get sset :id)))
ssets))
(sset-name
(minibuffer-with-setup-hook
(lambda ()
(add-hook 'post-command-hook
'telega-stickerset--minibuf-post-command t t))
(telega-completing-read prompt telega-minibuffer--choices nil t))))
(cl-find (cadr (assoc sset-name telega-minibuffer--choices)) ssets
:test #'equal
:key (telega--tl-prop :id))
))
(defun telega-stickerset-choose (sset &optional chat)
"Interactive choose stickerset."
(interactive (list (telega-stickerset-completing-read "Sticker set: ")))
(let ((tss-buffer (get-buffer "*Telegram Sticker Set*"))
(chat (or chat telega-chatbuf--chat)))
(if (and (buffer-live-p tss-buffer)
(with-current-buffer tss-buffer
(and (eq telega-help-win--stickerset sset)
(eq telega--chat chat))))
(select-window
(temp-buffer-window-show tss-buffer))
(let ((help-window-select t))
(telega-describe-stickerset sset chat)))))
(defun telega-stickerset--choose-from-multiple (prompt sticker-sets
&optional chat)
"Choose single sticker set from multiple STICKER-SETS.
PROMPT is displayed in case of multiple sticker sets."
(telega-stickerset-choose
(if (> (length sticker-sets) 1)
;; Multiple sticker sets
(telega-stickerset-completing-read prompt sticker-sets)
;; Single sticker set
(car sticker-sets))
chat))
(defun telega-stickerset-search (query)
"Search interactively for sticker matching QUERY."
(interactive "sStickerSet query: ")
(let ((sticker-sets (telega--searchStickerSets query)))
(unless sticker-sets
(user-error "No sticker set found for: %s" query))
(telega-stickerset--choose-from-multiple "Sticker set: " sticker-sets)))
(defun telega-stickerset-trends (&optional premium-p)
"Show trending stickers.
If prefix argument is specified, then show trends in Premium stickers."
(interactive "P")
(let ((sticker-sets (or (when premium-p
telega--stickersets-trending-premium)
(telega--getTrendingStickerSets))))
(unless sticker-sets
(user-error "No trending sticker sets"))
(telega-stickerset-choose
(telega-stickerset-completing-read
"Trending sticker set: " sticker-sets))))
(defun telega-sticker--animate-callback (_proc frame sticker &optional for-msg)
"Callback for inline animated sticker playback."
(plist-put sticker :telega-ffplay-frame-filename (cdr frame))
;; NOTE: for some animated stickers (not having thumbnail or
;; outline) image update can fail when all frames are player
;; (i.e. (cdr frame) is nil)
(ignore-errors
(telega-media--image-update
(cons sticker #'telega-sticker--create-image) nil))
(if for-msg
(telega-msg-redisplay for-msg)
;; NOTE: just redisplay the image, not redisplaying full message
(force-window-update)))
(defun telega-sticker--animate-to-png (sticker-file xheight callback
&rest callback-args)
"Animate animated sticker to series of PNG files."
(declare (indent 2))
(cl-assert (telega-file--downloaded-p sticker-file))
;; Kill previously running animations/ffplay if any
; (telega-ffplay-stop)
;; XXX: heavy heuristics to detect HiDPI displays on MacOS
;; If resulting PNG is too small and DPI is high, then consider HiDPI
;; mode is in use
;; png height = 76 * 4 = 306 is still generates pretty fast on modern PC
;; See https://t.me/emacs_telega/25785
(when (and (< xheight 76)
(> (round (/ (display-pixel-height) (/ (display-mm-height) 25.4)))
96))
(setq xheight (* xheight 4)))
(let* ((prefix (telega-temp-name "png-sticker-anim"))
(shell-cmd
(if telega-use-docker
(telega-docker-exec-cmd
(format "sh -c \"gunzip -c '%s' | tgs2png -s 0x%d - | telega-server -E %s\""
(telega--tl-get sticker-file :local :path)
xheight prefix))
(format
"gunzip -c '%s' | %s -s 0x%d - | %s -E %s"
(telega--tl-get sticker-file :local :path)
(or (executable-find "tgs2png")
(error "tgs2png not found in `exec-path', \
Install from https://github.com/zevlg/tgs2png"))
xheight (telega-server--process-command) prefix)))
(process-adaptive-read-buffering nil) ;no buffering please
(proc (start-process-shell-command
"sticker-animate"
(generate-new-buffer-name " *tgs2png telega*")
shell-cmd)))
(telega-debug "Running sticker-animate: %s" shell-cmd)
(set-process-plist proc (list :prefix prefix
:nframes -1
:frames nil
:callback callback
:callback-args callback-args))
(set-process-query-on-exit-flag proc nil)
(set-process-sentinel proc 'telega-ffplay--png-sentinel)
(set-process-filter proc 'telega-ffplay--png-filter)
proc))
(defun telega-sticker--animate (sticker &optional for-msg)
"Start animating animated STICKER."
(cl-assert (not (telega-sticker-static-p sticker)))
(telega-file--download (plist-get sticker :sticker)
:priority 32
:update-callback
(lambda (file)
(telega-file--renew sticker :sticker)
(when (telega-file--downloaded-p file)
(cl-ecase (telega--tl-type (plist-get sticker :format))
(stickerFormatTgs
;; NOTE: do not start animation if already started
(unless (plist-get sticker :telega-ffplay-frame-filename)
(telega-sticker--animate-to-png file
(telega-chars-xheight (car (telega-sticker-size sticker)))
#'telega-sticker--animate-callback sticker for-msg)))
(stickerFormatWebm
(telega-ffplay-to-png (telega--tl-get file :local :path)
(format "-pix_fmt rgba -vf scale=-2:%d -an"
(telega-chars-xheight
(car (telega-sticker-size sticker))))
(list #'telega-sticker--animate-callback sticker for-msg)
;; NOTE: We use "libvpx-vp9" to decode WEBM with alpha
;; channel
:vcodec "libvpx-vp9"))))
)))
(defun telega-sticker--gen-sensor-func (sticker)
"Return sensor function to animate STICKER when entered."
(cl-assert sticker)
(lambda (_window _oldpos dir)
(when (and (not (telega-sticker-static-p sticker))
telega-sticker-animated-play)
(if (eq dir 'entered)
(telega-sticker--animate sticker)
;; dir == left
(telega--cancelDownloadFile (plist-get sticker :sticker))))))
;;; Animations
(define-button-type 'telega-animation
:supertype 'telega
:inserter 'telega-ins--animation-image)
(defun telega-animation-at (&optional pos)
"Retur sticker at POS."
(let ((button (button-at (or pos (point)))))
(when (and button (eq (button-type button) 'telega-animation))
(button-get button :value))))
(defsubst telega-animation--file (animation)
"Return ANIMATIONS's file."
(telega-file--renew animation :animation))
(defalias 'telega-animation--thumb-file 'telega-sticker--thumb-file)
(defun telega-animation--download (animation)
"Ensure media content for ANIMATION has been downloaded."
(let ((animation-file (telega-animation--file animation))
(thumb-file (telega-animation--thumb-file animation)))
(when (telega-file--need-download-p thumb-file)
(telega-file--download thumb-file :priority 5))
(when telega-animation-download-saved
(when (telega-file--need-download-p animation-file)
(telega-file--download animation-file)))
))
(defun telega-animation--progress-svg (animation)
"Generate svg for STICKER showing download progress."
(let* ((xh (telega-chars-xheight telega-animation-height))
(xw (telega-chars-xwidth (telega-chars-in-width xh)))
(svg (telega-svg-create xw xh)))
(telega-svg-progress svg (telega-file--downloading-progress
(telega-animation--file animation))
'with-border)
(telega-svg-image svg
:scale 1.0
:height (telega-ch-height telega-animation-height)
:telega-nslices telega-animation-height
:ascent 'center)))
(defun telega-animation--create-image (animation &optional _fileignored)
"Return image for the ANIMATION."
;; Cases:
;; 1) Next inline animation frame (set in
;; `telega-animation--ffplay-callback') is available - display it
;;
;; 2) Thumbnail is downloaded, while animation still downloading
;; Show thumbnail (caching temporary), waiting for animation to
;; be downloaded
;;
;; 2.5) Animation/thumbnail is downloading, minithumbnail is
;; available - display it
;;
;; 3) Thumbnail and animation downloading
;; Fallback to svg loading image
(let* ((anim-frame-filename
(plist-get animation :telega-ffplay-frame-filename))
(minithumb (plist-get animation :minithumbnail))
(tfile (telega-animation--thumb-file animation)))
(let ((img-props
(list :height (telega-ch-height telega-animation-height)
:telega-nslices telega-animation-height
:scale 1.0
:ascent 'center)))
(cond ((and anim-frame-filename
;; NOTE: Check file for existance, to avoid errors in
;; `insert-file-contents-literally' if file does not
;; exists for some strange reason
(file-exists-p anim-frame-filename))
;; Remove this prop, because file is about to be deleted
(plist-put animation :telega-ffplay-frame-filename nil)
(apply #'telega-create-image
(with-temp-buffer
(set-buffer-multibyte nil)
(insert-file-contents-literally anim-frame-filename)
(buffer-string))
nil t
img-props))
((telega-file--downloaded-p tfile)
(apply #'telega-create-image
(telega--tl-get tfile :local :path) nil nil img-props))
(minithumb
(telega-minithumb--create-image minithumb telega-animation-height))
(t (telega-animation--progress-svg animation))))))
(defun telega-ins--animation-image (animation &optional slices-p)
"Inserter for the ANIMATION.
If SLICES-P is non-nil, then insert ANIMATION using slices."
(let ((aimage (telega-media--image
(cons animation 'telega-animation--create-image)
(cons (plist-get animation :thumbnail) :file))))
(if slices-p
(telega-ins--image-slices aimage)
(telega-ins--image aimage))))
(defun telega-animation--choosen-action (button)
"Execute action when animation BUTTON is pressed."
(cl-assert telega--chat)
(cl-assert (eq major-mode 'help-mode))
(let ((animation (telega-animation-at button))
(chat telega--chat))
;; NOTE: Kill help win before modifying chatbuffer, because it
;; recovers window configuration on kill
(quit-window 'kill-buffer)
(with-telega-chatbuf chat
(telega-chatbuf-animation-insert animation))))
(defun telega-animation--gen-sensor-func (anim)
"Return sensor function to animate ANIM when entered."
(cl-assert anim)
(lambda (_window _oldpos dir)
(when telega-animation-play-inline