forked from emacs-slack/emacs-slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack-thread.el
347 lines (302 loc) · 15.6 KB
/
slack-thread.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
;;; slack-thread.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 'eieio)
(require 'lui)
(require 'slack-message)
(defvar lui-prompt-string "> ")
(defconst all-threads-url "https://slack.com/api/subscriptions.thread.getView")
(define-derived-mode slack-thread-mode slack-mode "Slack - Thread"
""
(lui-set-prompt lui-prompt-string)
(setq lui-input-function 'slack-thread-message--send))
(defclass slack-thread ()
((thread-ts :initarg :thread_ts :initform "")
(messages :initarg :messages :initform '())
(has-unreads :initarg :has_unreads :initform nil)
(mention-count :initarg :mention_count :initform 0)
(reply-count :initarg :reply_count :initform 0)
(replies :initarg :replies :initform '())
(active :initarg :active :initform t)
(root :initarg :root :type slack-message)))
(defmethod slack-thread-messagep ((m slack-message))
(if (and (oref m thread-ts) (not (slack-message-thread-parentp m)))
t
nil))
(defun slack-thread-start ()
(interactive)
(let* ((team (slack-team-find slack-current-team-id))
(room (and team(slack-room-find slack-current-room-id team)))
(ts (slack-get-ts))
(message (slack-room-find-message room ts))
(buf (and room ts (slack-thread-get-buffer-create room team ts))))
(unless buf (error "Can't create slack thread buffer"))
(if (object-of-class-p message 'slack-reply-broadcast-message)
(error "Can't start thread from broadcasted message"))
(with-current-buffer buf
(slack-thread-insert-as-root-message message team))
(funcall slack-buffer-function buf)))
(defun slack-thread-message--send (message)
(if slack-current-team-id
(let* ((team (slack-team-find slack-current-team-id))
(room (slack-room-find slack-current-room-id team))
(message (slack-message-prepare-links (slack-escape-message message) team))
(broadcast (y-or-n-p (format "Also send to %s ? " (slack-room-name room)))))
(slack-message-inc-id team)
(with-slots (message-id sent-message self-id) team
(let* ((payload (list :id message-id
:channel (oref room id)
:reply_broadcast broadcast
:thread_ts slack-target-ts
:type "message"
:user self-id
:text message))
(json (json-encode payload))
(obj (slack-message-create payload team :room room)))
(slack-ws-send json team)
(puthash message-id obj sent-message))))))
(defmethod slack-thread-update-buffer ((thread slack-thread) message room team &key replace)
(let ((buf (get-buffer (slack-thread-buf-name room (oref thread thread-ts)))))
(when buf
(if replace
(slack-buffer-replace buf message)
(with-current-buffer buf
(slack-buffer-insert message team))))))
(defun slack-thread-buf-name (room thread-ts)
(format "%s %s - %s" (slack-room-buffer-name room) "Thread" thread-ts))
(defun slack-thread-get-buffer-create (room team thread-ts)
(let* ((buf-name (slack-thread-buf-name room thread-ts))
(buf (get-buffer buf-name)))
(when buf
(with-current-buffer buf (kill-buffer))
(setq buf nil))
(unless buf
(setq buf (generate-new-buffer buf-name))
(with-current-buffer buf
(slack-thread-mode)
(slack-buffer-enable-emojify)
(goto-char lui-input-marker)
(set (make-local-variable 'slack-target-ts) thread-ts)
(set (make-local-variable 'slack-current-team-id) (oref team id))
(set (make-local-variable 'slack-current-room-id) (oref room id))
;; (add-hook 'kill-buffer-hook 'slack-reset-room-last-read nil t)
(add-hook 'lui-pre-output-hook 'slack-buffer-buttonize-link nil t)))
buf))
(defun slack-thread-show-or-create ()
(interactive)
(if (eq major-mode 'slack-thread-mode)
(error "Already in thread")
(let* ((line (thing-at-point 'line))
(team-id slack-current-team-id)
(team (and team-id (slack-team-find team-id)))
(room-id slack-current-room-id)
(room (and room-id team (slack-room-find room-id team)))
(ts (slack-get-ts)))
(if (or (not team) (not room) (not ts))
(error "Can't find: %s" (or (and (not team) "team")
(and (not room) "room")
(and (not ts) "ts"))))
(let* ((thread (slack-room-find-thread room ts)))
(if thread
(slack-thread-show-messages thread room team)
(slack-thread-start))))))
(defun slack-thread-insert-as-root-message (message team)
(slack-buffer-insert message team)
(let ((lui-time-stamp-position nil))
(lui-insert (format "%s\n" (make-string lui-fill-column ?=)) t)))
(defmethod slack-room-replies-url ((_room slack-channel))
"https://slack.com/api/channels.replies")
(defmethod slack-room-replies-url ((_room slack-group))
"https://slack.com/api/groups.replies")
(defmethod slack-room-replies-url ((_room slack-im))
"https://slack.com/api/im.replies")
(defmethod slack-thread-request-messages ((thread slack-thread) room team)
(with-slots (thread-ts) thread
(with-slots (id) room
(cl-labels
((on-success (&key data &allow-other-keys)
(slack-request-handle-error
(data "slack-thread-request-messages")
(let ((messages (mapcar #'(lambda (payload) (slack-message-create payload team :room room))
(plist-get data :messages))))
(mapc #'(lambda (m) (slack-thread-add-message thread m))
(cl-remove-if #'slack-message-thread-parentp messages))))))
(slack-request (slack-room-replies-url room)
team
:params (list (cons "thread_ts" thread-ts)
(cons "channel" id))
:success #'on-success)))))
(defmethod slack-thread-show-messages ((thread slack-thread) room team)
(let* ((buf (slack-thread-get-buffer-create room team (oref thread thread-ts)))
(messages (slack-room-sort-messages (copy-sequence (oref thread messages)))))
(if (< (length messages) (oref thread reply-count))
(progn
(slack-thread-request-messages thread room team)
(setq messages (oref thread messages))))
(with-current-buffer buf
(slack-thread-insert-as-root-message (oref thread root) team)
(cl-loop for m in messages
do (slack-buffer-insert m team)))
(funcall slack-buffer-function buf)
(let* ((msg (car (last messages)))
(room (and msg (slack-room-find (oref msg channel) team))))
(when (and msg room (string< (oref room last-read) (oref msg ts)))
(slack-room-update-last-read room msg)
(slack-room-update-mark room team msg)))))
(defmethod slack-thread-to-string ((m slack-message) team)
(with-slots (thread) m
(if thread
(let* ((usernames (mapconcat #'identity
(cl-remove-duplicates
(mapcar #'(lambda (reply) (slack-user-name (plist-get reply :user) team))
(oref thread replies))
:test #'string=)
" "))
(text (format "\n%s reply from %s" (oref thread reply-count) usernames)))
(propertize text
'face '(:underline t)
'keymap (let ((map (make-sparse-keymap)))
(define-key map (kbd "RET") #'slack-thread-show-messages)
map)))
"")))
(defmethod slack-thread-create ((m slack-message) team &optional payload)
(let* ((replies (and payload (append (plist-get payload :replies) nil)))
(reply-count (and payload (plist-get payload :reply_count)))
(thread (make-instance 'slack-thread
:thread_ts (oref m ts)
:root m
:replies replies
:reply_count (or reply-count 0))))
;; (with-slots (threads) team
;; (cl-pushnew thread (oref threads all) :test #'slack-thread-equal))
;; thread)
thread))
(defmethod slack-thread-set-messages ((thread slack-thread) messages)
(let ((count (length messages)))
(oset thread messages messages)
(oset thread reply-count count)
(oset thread replies (mapcar #'(lambda (m) (list :user (slack-message-sender-id m)
:ts (oref m ts)))
messages))))
(defmethod slack-thread-add-message ((thread slack-thread) msg)
(with-slots (messages) thread
(cl-pushnew msg messages :test #'slack-message-equal)
(setq messages (slack-room-sort-messages (copy-sequence messages)))))
(defmethod slack-message-thread-parentp ((m slack-message))
(let* ((thread (oref m thread))
(thread-ts (or (and thread (oref thread thread-ts))
(oref m thread-ts))))
(and thread-ts (string= (oref m ts) thread-ts))))
(defmethod slack-message-thread-messagep ((m slack-message))
(and (oref m thread-ts) (not (slack-message-thread-parentp m))))
(defun slack-thread-update-state (payload team)
(let* ((room (slack-room-find (plist-get payload :channel) team))
(state (plist-get payload :message))
(message (and room (slack-room-find-message room (plist-get state :ts))))
(thread (and message (slack-message-get-thread message team))))
(when thread
(with-slots (replies reply-count) thread
(setq replies (append (plist-get state :replies) nil))
(setq reply-count (plist-get state :reply_count)))
(slack-message-update message team t t))))
(defmethod slack-thread-equal ((thread slack-thread) other)
(and (string-equal (oref thread thread-ts)
(oref other thread-ts))
(string-equal (oref (oref thread root) channel)
(oref (oref other root) channel))))
(cl-defun slack-thread-get-all (&key (sync nil) (ts nil))
(let ((team (slack-team-select)))
(cl-labels
((on-success (&key data &allow-other-keys)
(slack-request-handle-error
(data "slack-thread-get-all")
(let ((threads-data (append (plist-get data :threads) nil))
(total-unread (plist-get data :total_unread_replies))
(more (if (eq :json-false (plist-get data :has_more)) nil t))
(new-count (plist-get data :new_threads_count)))
(with-slots (threads) team
(with-slots
(initializedp total-unread-replies new-threads-count has-more) threads
(setq has-more more)
(setq initializedp t)
(setq total-unread-replies total-unread)
(setq new-threads-count new-count)
(let ((parents (cl-loop for thread in threads-data
collect (slack-message-create
(plist-get thread :root_msg) team))))
(mapc #'(lambda (parent) (slack-message-update parent team nil t))
parents))))))))
(slack-request
all-threads-url
team
:type "POST"
:params (list (cons "limit" "10")
(cons "current_ts" (or ts (format-time-string "%s"))))
:sync sync
:success #'on-success))))
(defmethod slack-thread-title ((thread slack-thread) team)
(with-slots (root) thread
(let ((room (slack-room-find (oref root channel) team))
(body (slack-message-body root team)))
(when room
(format "%s - %s" (slack-room-name room)
(concat (substring body 0 (min 50 (length body))) "..."))))))
(defun slack-thread-select (&optional reload)
(interactive)
(cl-labels
((load-threads (threads)
(slack-thread-get-all :sync t
:ts (cl-first
(cl-sort
(mapcar #'(lambda (thread) (oref thread thread-ts)) threads)
#'string<))))
(select-thread (threads team has-more)
(let* ((alist (cl-remove-if-not
#'(lambda (cons) (car cons))
(mapcar #'(lambda (thread)
(let ((title (slack-thread-title thread team)))
(and title (cons title thread))))
threads)))
(maybe-has-more (if has-more
(append alist (list (cons "(load more)" 'load-more))) alist))
(selected (slack-select-from-list (maybe-has-more "Select Thread: "))))
selected))
(collect-thread-parents (messages)
(mapcar #'(lambda (m) (oref m thread))
(cl-remove-if #'(lambda (m) (not (slack-message-thread-parentp m)))
messages)))
(collect-threads (team)
(cl-loop for room in (with-slots (groups ims channels) team
(append ims groups channels))
append (collect-thread-parents (oref room messages)))))
(let* ((team (slack-team-select)))
(with-slots (initializedp has-more) (oref team threads)
(if (or (not initializedp) has-more) (load-threads (collect-threads team))))
(let ((selected (select-thread (collect-threads team) team nil)))
(if (eq selected 'load-more)
(slack-thread-select t)
(slack-thread-show-messages selected
(slack-room-find (oref (oref selected root) channel) team)
team))))))
(defun slack-thread-setup-edit-buf (thread-ts room team type)
(slack-message-setup-edit-buf room type :ts thread-ts :team team))
(defmethod slack-thread-delete-message ((thread slack-thread) message)
(with-slots (messages) thread
(setq messages (cl-remove-if #'(lambda (e) (string= (oref e ts) (oref message ts)))
messages))))
(provide 'slack-thread)
;;; slack-thread.el ends here