-
Notifications
You must be signed in to change notification settings - Fork 117
/
slack-block.el
1287 lines (1091 loc) · 59.7 KB
/
slack-block.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
;;; slack-block.el --- -*- lexical-binding: t; -*-
;; Copyright (C) 2019
;; Author: <yuya373@archlinux>
;; Keywords:
;; This program 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.
;; This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
(require 'eieio)
(require 'lui)
(require 'slack-util)
(require 'slack-request)
(require 'slack-image)
(require 'slack-usergroup)
(require 'slack-mrkdwn)
(require 'slack-room)
(defvar slack-completing-read-function)
(defvar slack-channel-button-keymap)
(defvar slack-current-buffer)
;; Layout Blocks
;; [Reference: Message layout blocks | Slack](https://api.slack.com/reference/messaging/blocks)
(defclass slack-layout-block ()
((type :initarg :type :type string)
(block-id :initarg :block_id :type (or string null) :initform nil)
(payload :initarg :payload :initform nil)))
(cl-defmethod slack-block-find-action ((_this slack-layout-block) _action-id)
nil)
(cl-defmethod slack-block-to-string ((this slack-layout-block) &optional _option)
(format "Implement `slack-block-to-string' for %S" (oref this payload)))
(defun slack-create-layout-block (payload)
(let ((type (plist-get payload :type)))
(cond
((string= "section" type)
(slack-create-section-layout-block payload))
((string= "divider" type)
(slack-create-divider-layout-block payload))
((string= "image" type)
(slack-create-image-layout-block payload))
((string= "actions" type)
(slack-create-actions-layout-block payload))
((string= "context" type)
(slack-create-context-layout-block payload))
((string= "rich_text" type)
(slack-create-rich-text-block payload))
(t (make-instance 'slack-layout-block
:type type
:payload payload))
;; ;; TODO https://api.slack.com/reference/block-kit/blocks#file
;; ((string= "file" type)
;; (message "TODO: %S" payload)
;; nil
;; )
;; ;; TODO https://api.slack.com/reference/block-kit/blocks#input
;; ((string= "input" type)
;; (message "TODO: %S" payload)
;; nil
;; )
)))
;; Rich Text Blocks
;; [Changes to message objects on the way to support WYSIWYG | Slack](https://api.slack.com/changelog/2019-09-what-they-see-is-what-you-get-and-more-and-less)
(defclass slack-rich-text-block ()
((type :initarg :type :type string)
(block-id :initarg :block_id :type string)
(elements :initarg :elements :type list) ;; list of slack-rich-text-section
))
(cl-defmethod slack-block-to-string ((this slack-rich-text-block) &optional option)
(mapconcat #'(lambda (element) (slack-block-to-string element option))
(oref this elements)
""))
(cl-defmethod slack-block-to-mrkdwn ((this slack-rich-text-block) &optional option)
(mapconcat #'(lambda (element) (slack-block-to-mrkdwn element option))
(oref this elements)
""))
(defun slack-create-rich-text-block (payload)
(make-instance 'slack-rich-text-block
:type (plist-get payload :type)
:block_id (plist-get payload :block_id)
:elements (mapcar #'slack-create-rich-text-block-element
(plist-get payload :elements))))
(defclass slack-rich-text-block-element ()
((type :initarg :type :type string)
(elements :initarg :elements :type list) ;; list of slack-rich-text-element
(payload :initarg :payload :type (or null list) :initform nil)))
(cl-defmethod slack-block-to-string ((this slack-rich-text-block-element) &optional _option)
(format "Implement `slack-block-to-string' for %S\n" (oref this payload)))
(cl-defmethod slack-block-to-mrkdwn ((this slack-rich-text-block-element) &optional _option)
(format "Implement `slack-block-to-mrkdwn' for %S\n" (oref this payload)))
(defun slack-create-rich-text-block-element (payload)
(let* ((type (plist-get payload :type))
(element (cond
((string= "rich_text_section" type)
(slack-create-rich-text-section payload))
((string= "rich_text_preformatted" type)
(slack-create-rich-text-preformatted payload))
((string= "rich_text_quote" type)
(slack-create-rich-text-quote payload))
((string= "rich_text_list" type)
(slack-create-rich-text-list payload))
(t
(make-instance 'slack-rich-text-block-element
:type (plist-get payload :type)
:elements (mapcar #'slack-create-rich-text-element
(plist-get payload :elements)))))))
(oset element payload payload)
element))
(defclass slack-rich-text-section (slack-rich-text-block-element) ())
(cl-defmethod slack-block-to-string ((this slack-rich-text-section) &optional option)
(mapconcat #'(lambda (element) (slack-block-to-string element option))
(oref this elements)
""))
(cl-defmethod slack-block-to-mrkdwn ((this slack-rich-text-section) &optional option)
(mapconcat #'(lambda (element) (slack-block-to-mrkdwn element option))
(oref this elements)
""))
(defun slack-create-rich-text-section (payload)
(make-instance 'slack-rich-text-section
:type (plist-get payload :type)
:elements (mapcar #'slack-create-rich-text-element
(plist-get payload :elements))))
;; Code block does not use `style' in `slack-rich-text-element'
(defclass slack-rich-text-preformatted (slack-rich-text-block-element) ())
(cl-defmethod slack-block-to-string ((this slack-rich-text-preformatted) &optional option)
(let ((text (mapconcat #'(lambda (element) (slack-block-to-string element option))
(oref this elements)
"")))
(propertize (concat text "\n")
'slack-defer-face #'(lambda (beg end)
(overlay-put (make-overlay beg end)
'face 'slack-mrkdwn-code-block-face))
'face 'slack-mrkdwn-code-block-face)))
(cl-defmethod slack-block-to-mrkdwn ((this slack-rich-text-preformatted) &optional option)
(let ((text (mapconcat #'(lambda (element) (slack-block-to-mrkdwn element option))
(oref this elements)
"")))
(format "```%s```\n" text)))
(defun slack-create-rich-text-preformatted (payload)
(make-instance 'slack-rich-text-preformatted
:type (plist-get payload :type)
:elements (mapcar #'slack-create-rich-text-element
(plist-get payload :elements))
))
(defclass slack-rich-text-quote (slack-rich-text-block-element) ())
(cl-defmethod slack-block-to-string ((this slack-rich-text-quote) &optional option)
(let* ((text (mapconcat #'(lambda (element) (slack-block-to-string element option))
(oref this elements)
""))
(texts (split-string text "[\n\r]" nil nil))
(text-with-pad (mapconcat #'(lambda (text) (format "%s%s"
slack-mrkdwn-blockquote-sign
text))
texts
"\n")))
(propertize (concat text-with-pad "\n")
'face 'slack-mrkdwn-blockquote-face)))
(cl-defmethod slack-block-to-mrkdwn ((this slack-rich-text-quote) &optional option)
(let* ((text (mapconcat #'(lambda (element) (slack-block-to-mrkdwn element option))
(oref this elements)
""))
(texts (split-string text "[\n\r]" nil nil)))
(concat (mapconcat #'(lambda (text) (format "> %s" text))
texts
"\n")
"\n")))
(defun slack-create-rich-text-quote (payload)
(make-instance 'slack-rich-text-quote
:type (plist-get payload :type)
:elements (mapcar #'slack-create-rich-text-element
(plist-get payload :elements))))
(defclass slack-rich-text-list (slack-rich-text-block-element)
((indent :initarg :indent :type number)
(style :initarg :style :type string) ;; bullet or ordered
))
(cl-defmethod slack-block-to-string ((this slack-rich-text-list) &optional option)
(let ((indent (make-string (* 2 (oref this indent)) ? ))
(texts (mapcar #'(lambda (element) (slack-block-to-string element option))
(oref this elements)))
(texts-with-dot nil)
(dot slack-mrkdwn-list-bullet)
(i 1))
(dolist (text texts)
(push (format "%s%s %s"
indent
(propertize (if (string= (oref this style) "ordered")
(format "%s." i)
dot)
'face 'slack-mrkdwn-list-face)
text)
texts-with-dot)
(setq i (+ i 1)))
(concat (mapconcat #'identity (reverse texts-with-dot) "\n")
"\n")))
(cl-defmethod slack-block-to-mrkdwn ((this slack-rich-text-list) &optional option)
(let* ((indent (make-string (* 2 (oref this indent)) ? ))
(dot "-")
(i 1))
(concat (mapconcat #'(lambda (element)
(let ((text (format "%s%s %s"
indent
(if (string= (oref this style) "ordered")
(format "%s." i)
dot)
(slack-block-to-mrkdwn element option))))
(setq i (+ i 1))
text))
(oref this elements)
"\n")
"\n")))
(defun slack-create-rich-text-list (payload)
(make-instance 'slack-rich-text-list
:type (plist-get payload :type)
:elements (mapcar #'slack-create-rich-text-block-element
(plist-get payload :elements))
:indent (plist-get payload :indent)
:style (plist-get payload :style)))
(defclass slack-rich-text-element-style ()
((bold :initarg :bold :type boolean)
(italic :initarg :italic :type boolean)
(strike :initarg :strike :type boolean)
(code :initarg :code :type boolean)))
(cl-defmethod slack-block-to-string ((this slack-rich-text-element-style) text)
(let ((face (progn (or (and (oref this bold) 'slack-mrkdwn-bold-face)
(and (oref this italic) 'slack-mrkdwn-italic-face)
(and (oref this strike) 'slack-mrkdwn-strike-face)
(and (oref this code) 'slack-mrkdwn-code-face)))))
(propertize text 'face face)))
(cl-defmethod slack-block-to-mrkdwn ((this slack-rich-text-element-style) text)
(or (and (oref this bold) (format "*%s*" text))
(and (oref this italic) (format "_%s_" text))
(and (oref this strike) (format "~%s~" text))
(and (oref this code) (format "`%s`" text))
text))
(defun slack-create-rich-text-element-style (payload)
(when payload
(make-instance 'slack-rich-text-element-style
:bold (eq t (plist-get payload :bold))
:italic (eq t (plist-get payload :italic))
:strike (eq t (plist-get payload :strike))
:code (eq t (plist-get payload :code)))))
(defclass slack-rich-text-element ()
((type :initarg :type :type string)
(style :initarg :style :type (or null slack-rich-text-element-style))
(payload :initarg :payload :type (or null list) :initform nil)))
(cl-defmethod slack-block-to-string ((this slack-rich-text-element) &optional _option)
(format "Implement `slack-block-to-string' for %S\n" (oref this payload)))
(cl-defmethod slack-block-to-mrkdwn ((this slack-rich-text-element) &optional _option)
(format "Implement `slack-block-to-mrkdwn' for %S\n" (oref this payload)))
(defun slack-create-rich-text-element (payload)
(let* ((type (plist-get payload :type))
(element (cond
((string= "text" type)
(slack-create-rich-text-text-element payload))
((string= "channel" type)
(slack-create-rich-text-channel-element payload))
((string= "user" type)
(slack-create-rich-text-user-element payload))
((string= "emoji" type)
(slack-create-rich-text-emoji-element payload))
((string= "link" type)
(slack-create-rich-text-link-element payload))
((string= "team" type)
(slack-create-rich-text-team-element payload))
((string= "usergroup" type)
(slack-create-rich-text-usergroup-element payload))
((string= "date" type)
(slack-create-rich-text-date-element payload))
((string= "broadcast" type)
(slack-create-rich-text-broadcast-element payload))
(t
(make-instance 'slack-rich-text-element
:type (plist-get payload :type)
:style (slack-create-rich-text-element-style
(plist-get payload :style)))))))
(oset element payload payload)
element))
(defclass slack-rich-text-text-element (slack-rich-text-element)
((text :initarg :text :type string)))
(cl-defmethod slack-block-to-string ((this slack-rich-text-text-element) &optional _option)
(let ((style (oref this style))
(text (oref this text)))
(if style (slack-block-to-string style text)
(propertize text 'face 'slack-message-output-text))))
(cl-defmethod slack-block-to-mrkdwn ((this slack-rich-text-text-element) &optional _option)
(let ((style (oref this style))
(text (oref this text)))
(if style (slack-block-to-mrkdwn style text)
text)))
(defun slack-create-rich-text-text-element (payload)
(make-instance 'slack-rich-text-text-element
:type (plist-get payload :type)
:text (plist-get payload :text)
:style (slack-create-rich-text-element-style
(plist-get payload :style))))
;; (:type "channel" :channel_id "CE096203E")
(defclass slack-rich-text-channel-element (slack-rich-text-element)
((channel-id :initarg :channel_id :type string)))
(cl-defmethod slack-block-to-string ((this slack-rich-text-channel-element) option)
(let ((team (plist-get option :team))
(id (oref this channel-id)))
(unless team
(error "`slack-rich-text-channel-element' need team as option"))
(propertize (format "#%s" (slack-room-name (slack-room-find id team) team))
'room-id id
'keymap slack-channel-button-keymap
'face 'slack-channel-button-face)))
(cl-defmethod slack-block-to-mrkdwn ((this slack-rich-text-channel-element) option)
(let ((team (plist-get option :team))
(id (oref this channel-id)))
(unless team
(error "`slack-rich-text-channel-element' need team as option"))
(slack-propertize-mention-text 'slack-message-mention-face
(format "#%s" (slack-room-name (slack-room-find id team) team))
(format "<#%s>" id))))
(defun slack-create-rich-text-channel-element (payload)
(make-instance 'slack-rich-text-channel-element
:type (plist-get payload :type)
:channel_id (plist-get payload :channel_id)
:style (slack-create-rich-text-element-style
(plist-get payload :style))))
(defclass slack-rich-text-user-element (slack-rich-text-element)
((user-id :initarg :user_id :type string)))
(cl-defmethod slack-block-to-string ((this slack-rich-text-user-element) option)
(let ((team (plist-get option :team))
(id (oref this user-id)))
(unless team
(error "`slack-rich-text-user-element' need team as option"))
(propertize (format "@%s" (slack-user-name id team))
'face 'slack-message-mention-face)))
(cl-defmethod slack-block-to-mrkdwn ((this slack-rich-text-user-element) option)
(let ((team (plist-get option :team))
(id (oref this user-id)))
(unless team
(error "`slack-rich-text-user-element' need team as option"))
(slack-propertize-mention-text 'slack-message-mention-face
(format "@%s" (slack-user-name id team))
(format "<@%s>" id))))
(defun slack-create-rich-text-user-element (payload)
(make-instance 'slack-rich-text-user-element
:type (plist-get payload :type)
:user_id (plist-get payload :user_id)
:style (slack-create-rich-text-element-style
(plist-get payload :style))))
(defclass slack-rich-text-emoji-element (slack-rich-text-element)
((name :initarg :name :type string)))
(cl-defmethod slack-block-to-string ((this slack-rich-text-emoji-element) &optional _option)
(let ((name (oref this name)))
(format ":%s:" name)))
(cl-defmethod slack-block-to-mrkdwn ((this slack-rich-text-emoji-element) &optional option)
(slack-block-to-string this option))
(defun slack-create-rich-text-emoji-element (payload)
(make-instance 'slack-rich-text-emoji-element
:type (plist-get payload :type)
:name (plist-get payload :name)))
(defclass slack-rich-text-link-element (slack-rich-text-element)
((url :initarg :url :type string)
(text :initarg :text :type (or null string) :initform nil)))
(cl-defmethod slack-block-to-string ((this slack-rich-text-link-element) &optional _option)
(let ((text (oref this text))
(url (oref this url)))
(format "<%s|%s>" url (or text url))))
(cl-defmethod slack-block-to-mrkdwn ((this slack-rich-text-link-element) &optional _option)
(let ((url (oref this url)))
url))
(defun slack-create-rich-text-link-element (payload)
(make-instance 'slack-rich-text-link-element
:type (plist-get payload :type)
:url (plist-get payload :url)
:text (plist-get payload :text)
:style (slack-create-rich-text-element-style
(plist-get payload :style))))
(defclass slack-rich-text-team-element (slack-rich-text-element)
((team-id :initarg :team_id :type string)))
(cl-defmethod slack-block-to-string ((this slack-rich-text-team-element) &optional _option)
(let* ((team-id (oref this team-id))
(team (slack-team-find team-id)))
(propertize (format "%s" (slack-team-name team))
'face 'slack-message-mention-face)))
(cl-defmethod slack-block-to-mrkdwn ((this slack-rich-text-team-element) &optional _option)
(slack-block-to-string this))
(defun slack-create-rich-text-team-element (payload)
(make-instance 'slack-rich-text-team-element
:type (plist-get payload :type)
:team_id (plist-get payload :team_id)
:style (slack-create-rich-text-element-style
(plist-get payload :style))))
(defclass slack-rich-text-usergroup-element (slack-rich-text-element)
((usergroup-id :initarg :usergroup_id :type string)))
(cl-defmethod slack-block-to-string ((this slack-rich-text-usergroup-element) &optional option)
(let ((team (plist-get option :team))
(id (oref this usergroup-id)))
(unless team
(error "`slack-rich-text-usergroup-element' need team as option"))
(let ((usergroup (slack-usergroup-find id team)))
(propertize (format "@%s" (oref usergroup handle))
'face 'slack-message-mention-keyword-face))))
(cl-defmethod slack-block-to-mrkdwn ((this slack-rich-text-usergroup-element) &optional option)
(let ((team (plist-get option :team))
(id (oref this usergroup-id)))
(unless team
(error "`slack-rich-text-usergroup-element' need team as option"))
(let ((usergroup (slack-usergroup-find id team)))
(slack-propertize-mention-text 'slack-message-mention-keyword-face
(format "@%s" (oref usergroup handle))
(format "<!subteam^%s>" id)))))
(defun slack-create-rich-text-usergroup-element (payload)
(make-instance 'slack-rich-text-usergroup-element
:type (plist-get payload :type)
:usergroup_id (plist-get payload :usergroup_id)))
(defclass slack-rich-text-date-element (slack-rich-text-element)
((timestamp :initarg :timestamp :type number)))
(cl-defmethod slack-block-to-string ((this slack-rich-text-date-element) &optional _option)
(slack-format-ts (oref this timestamp)))
(defun slack-create-rich-text-date-element (payload)
(make-instance 'slack-rich-text-date-element
:type (plist-get payload :type)
:timestamp (if (stringp (plist-get payload :timestamp))
(string-to-number (plist-get payload :timestamp))
(plist-get payload :timestamp))))
(defclass slack-rich-text-broadcast-element (slack-rich-text-element)
((range :initarg :range :type string) ;; channel or everyone or here
))
(cl-defmethod slack-block-to-string ((this slack-rich-text-broadcast-element) &optional _option)
(propertize (format "@%s" (oref this range))
'face 'slack-message-mention-keyword-face))
(cl-defmethod slack-block-to-mrkdwn ((this slack-rich-text-broadcast-element) &optional _option)
(slack-propertize-mention-text 'slack-message-mention-keyword-face
(format "@%s" (oref this range))
(format "<!%s>" (oref this range))))
(defun slack-create-rich-text-broadcast-element (payload)
(make-instance 'slack-rich-text-broadcast-element
:type (plist-get payload :type)
:range (plist-get payload :range)))
(defclass slack-section-layout-block (slack-layout-block)
((type :initarg :type :type string :initform "section")
(text :initarg :text :type (or null slack-text-message-composition-object) :initform nil)
(fields :initarg :fields :type (or list null) :initform nil) ;; list of slack-text-message-composition-object
(accessory :initarg :accessory :initform nil :type (or null slack-block-element))))
(defun slack-create-section-layout-block (payload)
(let ((accessory (slack-create-block-element
(plist-get payload :accessory)
(plist-get payload :block_id))))
(make-instance 'slack-section-layout-block
:text (slack-create-text-message-composition-object
(plist-get payload :text))
:block_id (plist-get payload :block_id)
:fields (mapcar #'slack-create-text-message-composition-object
(plist-get payload :fields))
:accessory accessory)))
(cl-defmethod slack-block-to-string ((this slack-section-layout-block) &optional _option)
(with-slots (fields accessory text) this
(slack-format-message (slack-block-to-string text)
(mapconcat #'identity
(mapcar #'slack-block-to-string
fields)
"\n")
(slack-block-to-string accessory))))
(cl-defmethod slack-block-find-action ((this slack-section-layout-block) action-id)
(with-slots (accessory) this
(when (and accessory
(string= (slack-block-action-id accessory)
action-id))
accessory)))
(defclass slack-divider-layout-block (slack-layout-block)
((type :initarg :type :type string :initform "divider")))
(defun slack-create-divider-layout-block (payload)
(make-instance 'slack-divider-layout-block
:block_id (plist-get payload :block_id)))
(cl-defmethod slack-block-to-string ((_this slack-divider-layout-block) &optional _option)
(let ((columns (or lui-fill-column
0)))
(make-string columns ?-)))
(defclass slack-image-layout-block (slack-layout-block)
((type :initarg :type :type string :initform "image")
(image-url :initarg :image_url :type string)
(alt-text :initarg :alt_text :type string)
(title :initarg :title :initform nil (or null slack-text-message-composition-object))
(image-height :initarg :image_height :type number)
(image-width :initarg :image_width :type number)
(image-bytes :initarg :image_bytes :type number)))
(defun slack-create-image-layout-block (payload)
(make-instance 'slack-image-layout-block
:image_url (plist-get payload :image_url)
:alt_text (plist-get payload :alt_text)
:title (slack-create-text-message-composition-object
(plist-get payload :title))
:block_id (plist-get payload :block_id)
:image_width (plist-get payload :image_width)
:image_height (plist-get payload :image_height)
:image_bytes (plist-get payload :image_bytes)))
(cl-defmethod slack-block-to-string ((this slack-image-layout-block) &optional _option)
(with-slots (image-url alt-text title image-height image-width image-bytes) this
(let ((spec (list image-url
image-width
image-height
slack-image-max-height)))
(slack-format-message (format "%s (%s kB)" alt-text (round (/ image-bytes 1000.0)))
(slack-image-string spec)))))
(defclass slack-actions-layout-block (slack-layout-block)
((type :initarg :type :type string :initform "actions")
(elements :initarg :elements :type list) ;; max 5 elements
))
(defun slack-create-actions-layout-block (payload)
(make-instance 'slack-actions-layout-block
:elements (mapcar #'(lambda (e)
(slack-create-block-element
e (plist-get payload :block_id)))
(plist-get payload :elements))
:block_id (plist-get payload :block_id)))
(cl-defmethod slack-block-to-string ((this slack-actions-layout-block) &optional _option)
(with-slots (elements) this
(mapconcat #'identity
(mapcar #'slack-block-to-string
elements)
" ")))
(cl-defmethod slack-block-find-action ((this slack-actions-layout-block) action-id)
(with-slots (elements) this
(cl-find-if #'(lambda (e) (string= action-id (slack-block-action-id e)))
elements)))
(defclass slack-context-layout-block (slack-layout-block)
((type :initarg :type :type string :initform "context")
(elements :initarg :elements :type list)))
(defun slack-create-context-layout-block (payload)
(make-instance 'slack-context-layout-block
:elements (mapcar #'(lambda (e)
(or (if (string= "image" (plist-get e :type))
(slack-create-block-element e (plist-get payload :block_id))
(slack-create-text-message-composition-object e))))
(plist-get payload :elements))
:block_id (plist-get payload :block_id)))
(cl-defmethod slack-block-to-string ((this slack-context-layout-block) &optional _option)
(with-slots (elements) this
(mapconcat #'identity
(mapcar #'(lambda (e) (slack-block-to-string e '(:max-image-height 30 :max-image-width 30)))
elements)
" ")))
(cl-defmethod slack-block-find-action ((this slack-context-layout-block) action-id)
(with-slots (elements) this
(cl-find-if #'(lambda (e) (string= action-id (slack-block-action-id e)))
elements)))
;; Block Elements
;; [Reference: Block elements | Slack](https://api.slack.com/reference/messaging/block-elements)
(defclass slack-block-element ()
((type :initarg :type :type string)
(action-id :initarg :action_id :type string :initform "")
(confirm :initarg :confirm :type (or null slack-confirmation-dialog-message-composition-object) :initform nil)
(payload :initarg :payload :initform nil)))
(cl-defmethod slack-block-action-id ((this slack-block-element))
(oref this action-id))
(cl-defmethod slack-block-to-string ((this slack-block-element) &optional _option)
(format "Implement `slack-block-to-string' for %S" (oref this payload)))
(cl-defmethod slack-block-handle-confirm ((this slack-block-element))
(let ((confirm (oref this confirm)))
(if (null confirm) t
(slack-block-handle-confirm confirm))))
(cl-defmethod slack-block-select-from-options ((_this slack-block-element) options)
(let ((alist (mapcar #'(lambda (e) (cons (slack-block-to-string e) e))
options)))
(slack-select-from-list (alist "Select Option: "))))
(defun slack-create-block-element (payload block-id)
(when payload
(let ((type (plist-get payload :type)))
(cond
((string= "image" type)
(slack-create-image-block-element payload))
((string= "button" type)
(slack-create-button-block-element payload block-id))
((string= "static_select" type)
(slack-create-static-select-block-element payload block-id))
((string= "external_select" type)
(slack-create-external-select-block-element payload block-id))
((string= "users_select" type)
(slack-create-user-select-block-element payload block-id))
((string= "conversations_select" type)
(slack-create-conversation-select-block-element payload block-id))
((string= "channels_select" type)
(slack-create-channel-select-block-element payload block-id))
((string= "overflow" type)
(slack-create-overflow-block-element payload block-id))
((string= "datepicker" type)
(slack-create-datepicker-block-element payload block-id))
(t (make-instance 'slack-block-element
:type type
:payload payload))))))
(defclass slack-image-block-element (slack-block-element)
((type :initarg :type :type string :initform "image")
(image-url :initarg :image_url :type string)
(alt-text :initarg :alt_text :type string)
(image-height :initarg :image_height :type (or null number))
(image-width :initarg :image_width :type (or null number))
(image-bytes :initarg :image_bytes :type (or null number))))
(defun slack-create-image-block-element (payload)
(make-instance 'slack-image-block-element
:image_url (plist-get payload :image_url)
:alt_text (plist-get payload :alt_text)
:image_height (plist-get payload :image_height)
:image_width (plist-get payload :image_width)
:image_bytes (plist-get payload :image_bytes)))
(cl-defmethod slack-block-to-string ((this slack-image-block-element) &optional option)
(with-slots (image-url image-height image-width) this
(let ((spec (list image-url
image-width
image-height
(or (plist-get option :max-image-height)
slack-image-max-height)
(plist-get option :max-image-width))))
(slack-image-string (cl-remove-if #'null spec)))))
(defclass slack-button-block-element (slack-block-element)
((type :initarg :type :type string :initform "button")
(text :initarg :text :type slack-text-message-composition-object)
(action-id :initarg :action_id :type string)
(block-id :initarg :block_id :type (or null string) :initform nil)
(url :initarg :url :type (or string null) :initform nil)
(value :initarg :value :type (or string null) :initform nil)
(style :initarg :style :type string :initform "default") ;; primary, danger
(confirm :initarg :confirm :initform nil :type (or null slack-confirmation-dialog-message-composition-object))))
(defun slack-create-button-block-element (payload block-id)
(make-instance 'slack-button-block-element
:text (slack-create-text-message-composition-object
(plist-get payload :text))
:block_id block-id
:action_id (plist-get payload :action_id)
:url (plist-get payload :url)
:value (plist-get payload :value)
:style (or (plist-get payload :style) "default")
:confirm (slack-create-confirmation-dialog-message-composition-object
(plist-get payload :confirm))))
(cl-defgeneric slack-buffer-execute-button-block-action (buffer))
(defun slack-execute-button-block-action ()
(interactive)
(slack-if-let* ((buf slack-current-buffer))
(slack-buffer-execute-button-block-action buf)))
(cl-defmethod slack-block-to-string ((this slack-button-block-element) &optional _option)
(with-slots (text style) this
(let ((face (cond ((string= "danger" style) 'slack-button-danger-block-element-face)
((string= "primary" style) 'slack-button-primary-block-element-face)
(t 'slack-button-block-element-face))))
(propertize (slack-block-to-string text)
'face face
'slack-action-payload (slack-block-action-payload this)
'keymap (let ((map (make-sparse-keymap)))
(define-key map (kbd "RET")
#'slack-execute-button-block-action)
map)))))
(defface slack-button-block-element-face
'((t (:box (:line-width 1 :style released-button :foreground "#2aa198"))))
"Used to button block element"
:group 'slack)
(defface slack-button-danger-block-element-face
'((t (:inherit slack-button-block-element-face :foreground "#dc322f")))
"Used to danger button block element"
:group 'slack)
(defface slack-button-primary-block-element-face
'((t (:inherit slack-button-block-element-face :foreground "#859900")))
"Used to primary button block element"
:group 'slack)
(cl-defmethod slack-block-action-payload ((this slack-button-block-element))
(with-slots (block-id action-id value text) this
(cl-remove-if #'null
(list (cons "block_id" (or block-id ""))
(cons "action_id" action-id)
(when value
(cons "value" value))
(cons "type" "button")
(cons "text" (slack-block-action-payload text))))))
(defclass slack-select-block-element (slack-block-element)
((placeholder :initarg :placeholder :type slack-text-message-composition-object)
(action-id :initarg :action_id :type string)
(confirm :initarg :confirm :initform nil :type (or null slack-confirmation-dialog-message-composition-object))))
(cl-defmethod slack-block-to-string ((this slack-select-block-element) &optional _option)
(format "Implement `slack-block-to-string' for %S" (oref this payload)))
(cl-defmethod slack-block-select-from-option-groups ((_this slack-select-block-element) option-groups)
(slack-if-let* ((group-alist (mapcar #'(lambda (e) (cons (slack-block-to-string e) e))
option-groups))
(group (slack-select-from-list (group-alist "Select Group: "))))
(slack-block-select-from-option-group group)))
(defface slack-select-block-element-face
'((t (:box (:line-width 1 :style released-button :forground "#2aa198"))))
"Used to select block element"
:group 'slack)
(defclass slack-static-select-block-element (slack-select-block-element)
((type :initarg :type :type string :initform "static_select")
(options :initarg :options :type (or null list) :initform nil) ;; list of slack-option-message-composition-object
(option-groups :initarg :option_groups :type (or list null) :initform nil) ;; list of slack-option-groups-composition-object
(initial-option :initarg :initial_option :initform nil (or null
slack-option-message-composition-object))
(block-id :initarg :block_id :type (or null string) :initform nil)))
(defun slack-create-static-select-block-element (payload block-id)
(let* ((options (plist-get payload :options))
(option-groups (plist-get payload :option_groups))
(initial-option (plist-get payload :initial_option)))
(make-instance 'slack-static-select-block-element
:placeholder (slack-create-text-message-composition-object
(plist-get payload :placeholder))
:action_id (plist-get payload :action_id)
:block_id block-id
:confirm (slack-create-confirmation-dialog-message-composition-object
(plist-get payload :confirm))
:options (when options
(mapcar #'slack-create-option-message-composition-object
options))
:option_groups (unless options
(mapcar #'slack-create-option-group-message-composition-object
option-groups))
:initial_option (slack-create-option-message-composition-object
initial-option))))
(cl-defgeneric slack-buffer-execute-static-select-block-action (buffer))
(defun slack-execute-static-select-block-action ()
(interactive)
(slack-if-let* ((buf slack-current-buffer))
(slack-buffer-execute-static-select-block-action buf)))
(cl-defmethod slack-block-to-string ((this slack-static-select-block-element) &optional _option)
(with-slots (initial-option placeholder) this
(propertize (slack-block-to-string (or initial-option placeholder))
'face 'slack-select-block-element-face
'slack-action-payload (slack-block-action-payload this)
'keymap (let ((map (make-sparse-keymap)))
(define-key map (kbd "RET") #'slack-execute-static-select-block-action)
map))))
(cl-defmethod slack-block-action-payload ((this slack-static-select-block-element))
(with-slots (type action-id block-id placeholder) this
(list (cons "type" type)
(cons "action_id" action-id)
(cons "block_id" block-id)
(cons "placeholder" (slack-block-action-payload placeholder)))))
(cl-defmethod slack-block-select-option ((this slack-static-select-block-element))
(with-slots (options option-groups) this
(if options
(slack-block-select-from-options this options)
(slack-block-select-from-option-groups this option-groups))))
(defclass slack-external-select-block-element (slack-select-block-element)
((type :initarg :type :type string :initform "external_select")
(initial-option :initarg :initial_option :initform nil :type (or null
slack-option-message-composition-object
slack-option-group-message-composition-object))
(min-query-length :initarg :min_query_length :type (or integer null) :initform nil)
(block-id :initarg :block_id :type (or null string) :initform nil)))
(cl-defgeneric slack-buffer-execute-external-select-block-action (buffer))
(defun slack-execute-external-select-block-action ()
(interactive)
(slack-if-let* ((buf slack-current-buffer))
(slack-buffer-execute-external-select-block-action buf)))
(cl-defmethod slack-block-to-string ((this slack-external-select-block-element))
(with-slots (placeholder initial-option) this
(propertize (slack-block-to-string (or initial-option placeholder))
'face 'slack-select-block-element-face
'slack-action-payload (slack-block-action-payload this)
'keymap (let ((map (make-sparse-keymap)))
(define-key map (kbd "RET") #'slack-execute-external-select-block-action)
map))))
(defun slack-create-external-select-block-element (payload block-id)
(make-instance 'slack-external-select-block-element
:placeholder (slack-create-text-message-composition-object
(plist-get payload :placeholder))
:action_id (plist-get payload :action_id)
:block_id block-id
:initial_option (slack-create-option-message-composition-object
(plist-get payload :initial_option))
:min_query_length (plist-get payload :min_query_length)
:confirm (slack-create-confirmation-dialog-message-composition-object
(plist-get payload :confirm))))
(cl-defmethod slack-block-action-payload ((this slack-external-select-block-element))
(with-slots (action-id block-id type) this
(list (cons "type" type)
(cons "action_id" action-id)
(cons "block_id" block-id))))
(defconst slack-block-suggestions-url "https://slack.com/api/blocks.suggestions")
(cl-defmethod slack-block-fetch-suggestions ((this slack-external-select-block-element) service-id container team on-success)
(with-slots (action-id block-id min-query-length) this
(let* ((query (read-from-minibuffer
(format "Query (minimum length: %s): " min-query-length)))
(data (json-encode-alist (list (cons "value" query)
(cons "action_id" action-id)
(cons "block_id" block-id)
(cons "service_id" service-id)
(cons "container" container)))))
(cl-labels
((success (&key data &allow-other-keys)
(slack-request-handle-error
(data "slack-block-fetch-suggestions")
(let ((options (mapcar #'slack-create-option-message-composition-object
(plist-get data :options)))
(option-groups (mapcar #'slack-create-option-group-message-composition-object
(plist-get data :option_groups))))
(run-with-timer 1 nil on-success options option-groups)))))
(slack-request
(slack-request-create
slack-block-suggestions-url
team
:type "POST"
:data data
:headers (list (cons "Content-Type"
"application/json;charset=utf-8"))
:success #'success))))))
(defclass slack-user-select-block-element (slack-select-block-element)
((type :initarg :type :type string :initform "users_select")
(initial-user :initarg :initial_user :type (or string null) :initform nil)
(block-id :initarg :block_id :type (or null string) :initform nil)))
(defun slack-create-user-select-block-element (payload block-id)
(make-instance 'slack-user-select-block-element
:placeholder (slack-create-text-message-composition-object
(plist-get payload :placeholder))
:action_id (plist-get payload :action_id)
:block_id block-id
:initial_user (plist-get payload :initial_user)
:confirm (slack-create-confirmation-dialog-message-composition-object
(plist-get payload :confirm))))
(cl-defgeneric slack-buffer-execute-user-select-block-action (buffer))
(defun slack-execute-user-select-block-action ()
(interactive)
(slack-if-let* ((buf slack-current-buffer))
(slack-buffer-execute-user-select-block-action buf)))
(cl-defmethod slack-block-to-string ((this slack-user-select-block-element) &optional _option)
(with-slots (initial-user placeholder) this
(let ((props (list
'face 'slack-select-block-element-face
'slack-action-payload (slack-block-action-payload this)
'keymap (let ((map (make-sparse-keymap)))
(define-key map (kbd "RET") #'slack-execute-user-select-block-action)
map))))
(if initial-user
(apply #'propertize (format "USER: %s" initial-user)
(append (list 'slack-user-id initial-user
'slack-lazy-user-name t)
props))
(apply #'propertize (slack-block-to-string placeholder) props)))))
(cl-defmethod slack-block-action-payload ((this slack-user-select-block-element))
(with-slots (type action-id block-id) this
(list (cons "type" type)
(cons "action_id" action-id)
(cons "block_id" block-id))))
(defclass slack-conversation-select-block-element (slack-select-block-element)
((type :initarg :type :type string :initform "conversations_select")
(initial-conversation :initarg :initial_conversation :type (or string null) :initform nil)
(block-id :initarg :block_id :type (or null string) :initform nil)))
(defun slack-create-conversation-select-block-element (payload block-id)
(make-instance 'slack-conversation-select-block-element
:placeholder (slack-create-text-message-composition-object
(plist-get payload :placeholder))
:action_id (plist-get payload :action_id)
:block_id block-id