forked from emacs-slack/emacs-slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack-emoji.el
148 lines (131 loc) · 6.54 KB
/
slack-emoji.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
;;; slack-emoji.el --- -*- lexical-binding: t; -*-
;; Copyright (C) 2017 南優也
;; Author: 南優也 <[email protected]>
;; 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 <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
(require 'slack-request)
(require 'slack-image)
(declare-function emojify-get-emoji "emojify")
(declare-function emojify-image-dir "emojify")
(declare-function emojify-create-emojify-emojis "emojify")
(defvar emojify-user-emojis)
(defvar slack-current-buffer)
(defconst slack-emoji-list "https://slack.com/api/emoji.list")
;; [How can I get the FULL list of slack emoji through API? - Stack Overflow](https://stackoverflow.com/a/39654939)
(defconst slack-emoji-master-data-url
"https://raw.githubusercontent.com/iamcal/emoji-data/master/emoji.json")
(defvar slack-emoji-master (make-hash-table :test 'equal :size 1600))
(defun slack-download-emoji (team after-success)
(when (require 'emojify nil t)
;; create slack image file directory if it doesn't exist, otherwise curl complains
(ignore-errors (mkdir slack-image-file-directory 'parent-if-needed))
(cl-labels
((handle-alias (name emojis)
(let* ((raw-url (plist-get emojis name))
(alias (if (string-prefix-p "alias:" raw-url)
(intern (format ":%s" (cadr (split-string raw-url ":")))))))
(or
(and (not raw-url) (handle-alias (intern ":slack") emojis)) ;some aliases are b0rked
(and (string-prefix-p "alias:" raw-url) ;recursive alias
(handle-alias (intern (replace-regexp-in-string "alias" "" raw-url)) emojis))
(and alias (or (plist-get emojis alias)
(let ((emoji (emojify-get-emoji (format "%s:" alias))))
(if emoji
(concat (emojify-image-dir) "/" (gethash "image" emoji))))))
raw-url)))
(push-new-emoji (emoji)
(puthash (car emoji) t (oref team emoji-master))
(cl-pushnew emoji emojify-user-emojis
:test #'string=
:key #'car))
(on-success
(&key data &allow-other-keys)
(slack-request-handle-error
(data "slack-download-emoji")
(emojify-create-emojify-emojis)
(let* ((emojis (plist-get data :emoji))
(paths nil))
(cl-loop for (name _) on emojis by #'cddr
do (let* ((url (handle-alias name emojis))
(path (if (file-exists-p url) url
(slack-image-path url)))
(emoji (cons (format "%s:" name)
(list (cons "name" (substring (symbol-name name) 1))
(cons "image" path)
(cons "style" "github")))))
(if (file-exists-p path)
(push-new-emoji emoji)
(slack-url-copy-file
url
path
:success #'(lambda () (push-new-emoji emoji)))
)
(push path paths)))
(when (functionp after-success) (funcall after-success paths))))))
(slack-request
(slack-request-create
slack-emoji-list
team
:success #'on-success)))))
(defun slack-select-emoji (team)
"Select emoji for TEAM."
(unless (< 0 (hash-table-count slack-emoji-master)) (slack-emoji-fetch-master-data (car (hash-table-values slack-teams-by-token))))
(if (and (fboundp 'emojify-completing-read)
(fboundp 'emojify-download-emoji-maybe))
(progn (emojify-download-emoji-maybe)
(cl-labels
((select ()
(emojify-completing-read "Select Emoji: "
#'(lambda (data &rest args)
(unless (null args)
(slack-log (format "Invalid completing arguments: %s, %s" data args)
team :level 'debug))
(let ((emoji (car (split-string data " "))))
(or (gethash emoji
slack-emoji-master
nil)
(gethash emoji
(oref team emoji-master)
nil)))))))
(select)))
(completing-read "Emoji: " (hash-table-keys slack-emoji-master))))
(defun slack-emoji-fetch-master-data (team)
(cl-labels
((success (&key data &allow-other-keys)
(slack-request-handle-error
(data "slack-emoji-fetch-master-data")
(cl-loop for emoji in data
do (let ((short-names (plist-get emoji :short_names)))
(when short-names
(cl-loop for name in short-names
do (puthash (format ":%s:" name)
t
slack-emoji-master))))))))
(slack-request
(slack-request-create
slack-emoji-master-data-url
team
:type "GET"
:success #'success
:without-auth t
:sync t
))))
(defun slack-insert-emoji ()
(interactive)
(slack-if-let* ((buffer slack-current-buffer)
(team (slack-buffer-team buffer)))
(insert (slack-select-emoji team))))
(provide 'slack-emoji)
;;; slack-emoji.el ends here