forked from emacs-slack/emacs-slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack-message-editor.el
224 lines (196 loc) · 8.42 KB
/
slack-message-editor.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
;;; slack-message-editor.el --- edit message interface -*- lexical-binding: t; -*-
;; Copyright (C) 2015 南優也
;; 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-message-sender)
(defconst slack-message-edit-url "https://slack.com/api/chat.update")
(defconst slack-message-edit-buffer-name "*Slack - Edit message*")
(defconst slack-message-write-buffer-name "*Slack - Write message*")
(defconst slack-message-share-buffer-name "*Slack - Share message*")
(defconst slack-share-url "https://slack.com/api/chat.shareMessage")
(defvar slack-buffer-function)
(defvar slack-target-ts)
(make-local-variable 'slack-target-ts)
(defvar slack-message-edit-buffer-type)
(make-local-variable 'slack-message-edit-buffer-type)
(defvar slack-current-room-id)
(defvar slack-current-team-id)
(defvar slack-edit-message-mode-map
(let ((keymap (make-sparse-keymap)))
(define-key keymap (kbd "C-s C-m") #'slack-message-embed-mention)
(define-key keymap (kbd "C-s C-c") #'slack-message-embed-channel)
(define-key keymap (kbd "C-c C-k") #'slack-message-cancel-edit)
(define-key keymap (kbd "C-c C-c") #'slack-message-send-from-buffer)
keymap))
(define-derived-mode slack-edit-message-mode fundamental-mode "Slack Edit Msg"
""
(slack-buffer-enable-emojify))
(defun slack-message-share ()
(interactive)
(let ((ts (slack-get-ts))
(buf (get-buffer-create slack-message-share-buffer-name))
(room-id slack-current-room-id)
(team slack-current-team))
(unless ts
(error "Can't find message."))
(with-current-buffer buf
(slack-message-setup-edit-buf (slack-room-find room-id team)
'share
:ts ts
:team team))
(funcall slack-buffer-function buf)))
(defun slack-message-share--send (team room ts msg)
(let* ((slack-room-list (or (and (object-of-class-p room 'slack-channel)
(slack-message-room-list team))
(list (cons (slack-room-name-with-team-name room)
room))))
(share-channel-id (oref (slack-select-from-list
(slack-room-list
"Select Channel: "
:initial
(slack-room-name room)))
id)))
(cl-labels
((on-success (&key data &allow-other-keys)
(slack-request-handle-error
(data "slack-message-share"))))
(slack-request
slack-share-url
team
:type "POST"
:params (list (cons "channel" (oref room id))
(cons "timestamp" ts)
(cons "text" msg)
(cons "share_channel" share-channel-id))
:sync nil
:success))))
(defun slack-message-write-another-buffer ()
(interactive)
(let* ((cur-buf (current-buffer))
(cur-mode (with-current-buffer cur-buf major-mode))
(team (slack-team-find slack-current-team-id))
(target-room (if (boundp 'slack-current-room-id)
(slack-room-find slack-current-room-id
team)
(slack-message-read-room team)))
(buf (get-buffer-create slack-message-write-buffer-name)))
(cl-case cur-mode
('slack-mode
(with-current-buffer buf
(slack-message-setup-edit-buf target-room 'new
:team team)))
('slack-thread-mode
(let ((ts slack-target-ts))
(with-current-buffer buf
(slack-thread-setup-edit-buf ts
target-room
team
'thread-message-new)))))
(funcall slack-buffer-function buf)))
(defmethod slack-message-get-user-id ((m slack-user-message))
(oref m user))
(defmethod slack-message-get-user-id ((m slack-file-comment-message))
(oref (oref m comment) user))
(defun slack-message-edit ()
(interactive)
(let* ((team (slack-team-find slack-current-team-id))
(room (slack-room-find slack-current-room-id
team))
(ts (slack-get-ts))
(msg (slack-room-find-message room ts)))
(unless msg
(error "Can't find original message"))
(unless (string= (oref team self-id) (slack-message-get-user-id msg))
(error "Cant't edit other user's message"))
(slack-message-edit-text msg room)))
(defmethod slack-message-edit-type ((_m slack-message))
'edit)
(defmethod slack-message-edit-type ((_m slack-file-comment-message))
'edit-file-comment)
(defmethod slack-message-get-text ((m slack-message))
(oref m text))
(defmethod slack-message-get-text ((m slack-file-comment-message))
(oref (oref m comment) comment))
(defun slack-message-edit-text (msg room)
(let ((buf (get-buffer-create slack-message-edit-buffer-name))
(team (slack-team-find slack-current-team-id)))
(with-current-buffer buf
(slack-edit-message-mode)
(slack-message-setup-edit-buf room
(slack-message-edit-type msg)
:ts (oref msg ts)
:team team)
(insert (slack-message-get-text msg)))
(funcall slack-buffer-function buf)))
(cl-defun slack-message-setup-edit-buf (room buf-type &key ts team)
(slack-edit-message-mode)
(setq buffer-read-only nil)
(erase-buffer)
(set (make-local-variable 'slack-target-ts) ts)
(set (make-local-variable 'slack-message-edit-buffer-type) buf-type)
(slack-buffer-set-current-room-id room)
(slack-buffer-set-current-team-id team)
(message "C-c C-c to send edited msg"))
(defun slack-message-cancel-edit ()
(interactive)
(let* ((team (slack-team-find slack-current-team-id))
(room (slack-room-find slack-current-room-id
team)))
(erase-buffer)
(delete-window)
(slack-room-create-buffer room team)))
(defun slack-message-send-from-buffer ()
(interactive)
(cl-labels ((get-team () (slack-team-find slack-current-team-id))
(get-room (team) (slack-room-find slack-current-room-id team)))
(let ((buf-string (buffer-substring (point-min) (point-max))))
(cl-case slack-message-edit-buffer-type
('edit-file-comment
(slack-file-comment-edit slack-current-room-id
slack-current-team-id
slack-target-ts
buf-string))
('edit
(let ((team (get-team)))
(slack-message--edit (oref (get-room team) id)
team
slack-target-ts
buf-string)))
('share
(let ((team (get-team)))
(slack-message-share--send team
(get-room team)
slack-target-ts
buf-string)))
('new (slack-message--send buf-string))
('thread-message-new (slack-thread-message--send buf-string)))
(kill-buffer)
(delete-window))))
(defun slack-message--edit (channel team ts text)
(cl-labels ((on-edit (&key data &allow-other-keys)
(slack-request-handle-error
(data "slack-message--edit"))))
(slack-request
slack-message-edit-url
team
:type "POST"
:sync nil
:params (list (cons "channel" channel)
(cons "ts" ts)
(cons "text" text))
:success #'on-edit)))
(provide 'slack-message-editor)
;;; slack-message-editor.el ends here