-
Notifications
You must be signed in to change notification settings - Fork 117
/
slack-conversations.el
575 lines (539 loc) · 23.9 KB
/
slack-conversations.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
;;; slack-conversations.el --- -*- lexical-binding: t; -*-
;; Copyright (C) 2018
;; Author: <yuya373@yuya373>
;; 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 'slack-util)
(require 'slack-request)
(require 'slack-room)
(require 'slack-counts)
(require 'slack-modeline)
(require 'slack-message)
(require 'slack-create-message)
(defcustom slack-exclude-archived-channels t
"If t, filter out archived channels for listing and selection. If nil, include archived channels."
:type 'boolean
:group 'slack)
(defvar slack-completing-read-function)
(defconst slack-conversations-archive-url
"https://slack.com/api/conversations.archive")
(defconst slack-conversations-unarchive-url
"https://slack.com/api/conversations.unarchive")
(defconst slack-conversations-invite-url
"https://slack.com/api/conversations.invite")
(defconst slack-conversations-join-url
"https://slack.com/api/conversations.join")
(defconst slack-conversations-leave-url
"https://slack.com/api/conversations.leave")
(defconst slack-conversations-rename-url
"https://slack.com/api/conversations.rename")
(defconst slack-conversations-set-purpose-url
"https://slack.com/api/conversations.setPurpose")
(defconst slack-conversations-set-topic-url
"https://slack.com/api/conversations.setTopic")
(defconst slack-conversations-members-url
"https://slack.com/api/conversations.members")
(defconst slack-conversations-kick-url
"https://slack.com/api/conversations.kick")
(defconst slack-conversations-list-url
"https://slack.com/api/conversations.list")
(defconst slack-conversations-info-url
"https://slack.com/api/conversations.info")
(defconst slack-conversations-replies-url
"https://slack.com/api/conversations.replies")
(defconst slack-conversations-close-url
"https://slack.com/api/conversations.close")
(defconst slack-conversations-create-url
"https://slack.com/api/conversations.create")
(defconst slack-conversations-history-url
"https://slack.com/api/conversations.history")
(defconst slack-conversations-open-url
"https://slack.com/api/conversations.open")
(defconst slack-conversations-view-url
"https://slack.com/api/conversations.view")
(defconst slack-conversations-mark-url
"https://slack.com/api/conversations.mark")
(cl-defun slack-conversations-success-handler (team &key on-errors on-success)
(cl-function
(lambda (&key data &allow-other-keys)
(cl-labels
((replace-underscore-with-space (s)
(replace-regexp-in-string "_"
" "
s))
(log-error
(_)
(slack-if-let*
((err (plist-get data :error))
(message (format "%s"
(replace-underscore-with-space
err))))
(slack-log message team :level 'error))
(slack-if-let*
((errors (plist-get data :errors))
(has-handler (functionp on-errors)))
(funcall on-errors errors))))
(slack-request-handle-error
(data "conversations" #'log-error)
(slack-if-let* ((warning (plist-get data :warning)))
(slack-log (format "%s" (replace-underscore-with-space
warning))
team
:level 'warn)
(when (functionp on-success)
(funcall on-success data))))))))
(defun slack-conversations-archive (room team)
(let ((id (oref room id)))
(slack-request
(slack-request-create
slack-conversations-archive-url
team
:type "POST"
:params (list (cons "channel" id))
:success (slack-conversations-success-handler team)))))
(defun slack-conversations-unarchive (room team)
(let ((channel (oref room id)))
(slack-request
(slack-request-create
slack-conversations-unarchive-url
team
:type "POST"
:params (list (cons "channel" channel))
:success (slack-conversations-success-handler team)))))
(defun slack-conversations-invite (room team)
(let* ((channel (oref room id))
(user-names (slack-user-names
team #'(lambda (users)
(cl-remove-if #'(lambda (e)
(or (string= (oref team self-id)
(plist-get e :id))
(string= (plist-get e :id)
"USLACKBOT")
(cl-find (plist-get e :id)
(slack-room-members room)
:test #'string=)))
users))))
(users nil))
(cl-labels
((already-selected-p
(user-name)
(cl-find-if #'(lambda (e)
(string= e
(plist-get (cdr user-name)
:id)))
users))
(filter-selected (user-names)
(cl-remove-if #'already-selected-p
user-names)))
(cl-loop for i from 1 upto 30
as candidates = (filter-selected user-names)
as selected = (slack-select-from-list
(candidates "Select User: "))
while selected
do (push (plist-get selected :id) users)))
(setq users (mapconcat #'identity users ","))
(cl-labels
((errors-handler
(errors)
(let ((message
(mapconcat #'(lambda (err)
(let ((msg (plist-get err :error))
(user (plist-get err :user)))
(format "%s%s"
(replace-regexp-in-string "_" " " msg)
(or (and user (format ": %s" user))
""))))
errors
", ")))
(slack-log message team :level 'error))))
(slack-request
(slack-request-create
slack-conversations-invite-url
team
:type "POST"
:params (list (cons "channel" channel)
(cons "users" users))
:success (slack-conversations-success-handler team
:on-errors
#'errors-handler))))))
(defun slack-conversations-join (room team &optional on-success)
(cl-labels
((success (data)
(when (eq 'slack-channel
(eieio-object-class-name room))
(oset room is-member t))
(when (functionp on-success)
(funcall on-success data))))
(let ((channel (oref room id)))
(slack-request
(slack-request-create
slack-conversations-join-url
team
:type "POST"
:params (list (cons "channel" channel))
:success (slack-conversations-success-handler
team
:on-success #'success))))))
(defun slack-conversations-leave (room team)
(let ((channel (oref room id)))
(slack-request
(slack-request-create
slack-conversations-leave-url
team
:type "POST"
:params (list (cons "channel" channel))
:success (slack-conversations-success-handler team)))))
(defun slack-conversations-rename (room team)
(let ((channel (oref room id))
(name (read-from-minibuffer "Name: ")))
(slack-request
(slack-request-create
slack-conversations-rename-url
team
:type "POST"
:params (list (cons "channel" channel)
(cons "name" name))
:success (slack-conversations-success-handler team)))))
(defun slack-conversations-set-purpose (room team)
(let ((channel (oref room id))
(purpose (read-from-minibuffer "Purpose: ")))
(cl-labels
((on-success (data)
(let* ((channel (plist-get data :channel))
(purpose (plist-get channel :purpose)))
(oset room purpose purpose))))
(slack-request
(slack-request-create
slack-conversations-set-purpose-url
team
:type "POST"
:params (list (cons "channel" channel)
(cons "purpose" purpose))
:success (slack-conversations-success-handler team
:on-success
#'on-success))))))
(defun slack-conversations-set-topic (room team)
(let ((channel (oref room id))
(topic (read-from-minibuffer "Topic: ")))
(cl-labels
((on-success (data)
(let* ((channel (plist-get data :channel))
(topic (plist-get channel :topic)))
(oset room topic topic))))
(slack-request
(slack-request-create
slack-conversations-set-topic-url
team
:type "POST"
:params (list (cons "channel" channel)
(cons "topic" topic))
:success (slack-conversations-success-handler team
:on-success
#'on-success))))))
(defun slack-conversations-kick (room team)
(let* ((candidates (cl-loop for user in (slack-user-names team)
if (cl-find (plist-get (cdr user) :id)
(slack-room-members room)
:test #'string=)
collect user))
(selected (funcall slack-completing-read-function
"Select User: "
candidates
nil t))
(user (cdr-safe (cl-assoc selected candidates :test #'string=))))
(when user
(slack-request
(slack-request-create
slack-conversations-kick-url
team
:type "POST"
:params (list (cons "channel" (oref room id))
(cons "user" (plist-get user :id)))
:success (slack-conversations-success-handler team))))))
(defun slack-conversations-list (team success-callback &optional types)
(let ((cursor nil)
(channels nil)
(groups nil)
(ims nil)
(types (or types (list "public_channel"
"private_channel"
"mpim"
"im"))))
(cl-labels
((on-success
(&key data &allow-other-keys)
(slack-request-handle-error
(data "slack-conversations-list")
(cl-loop for c in (plist-get data :channels)
do (cond
((eq t (plist-get c :is_channel))
(push (slack-room-create c 'slack-channel)
channels))
((eq t (plist-get c :is_im))
(push (slack-room-create c 'slack-im)
ims))
((eq t (plist-get c :is_group))
(push (slack-room-create c 'slack-group)
groups))))
(slack-if-let*
((meta (plist-get data :response_metadata))
(next-cursor (plist-get meta :next_cursor))
(has-cursor (< 0 (length next-cursor))))
(progn
(setq cursor next-cursor)
(request))
(funcall success-callback
channels groups ims))))
(request ()
(slack-request
(slack-request-create
slack-conversations-list-url
team
:params (list (cons "types" (mapconcat #'identity types ","))
(and slack-exclude-archived-channels (cons "exclude_archived" "true"))
(and cursor (cons "cursor" cursor)))
:success #'on-success))))
(request))))
(defun slack-conversations-info (room team &optional after-success)
(slack-request
(slack-conversations-info-request room team after-success)))
(defun slack-conversations-info-request (room team &optional after-success)
(cl-labels
((success (&key data &allow-other-keys)
(slack-request-handle-error
(data "slack-conversations-info")
(let ((new-room (slack-room-create
(plist-get data :channel)
(eieio-object-class-name room))))
(slack-team-set-room team new-room))
(when (functionp after-success)
(funcall after-success)))))
(slack-request-create
slack-conversations-info-url
team
:params (list (cons "channel" (oref room id)))
:success #'success)))
(cl-defun slack-conversations-replies (room ts team &key after-success (cursor nil) (oldest nil))
(let ((channel (oref room id)))
(cl-labels
((create-message (payload)
(slack-message-create payload
team
room))
(callback (messages next-cursor has-more)
(when (functionp after-success)
(funcall after-success
messages
next-cursor
has-more)))
(on-success (&key data &allow-other-keys)
(slack-request-handle-error
(data "slack-conversations-replies")
(let* ((messages (mapcar #'create-message
(plist-get data :messages)))
(meta (plist-get data :response_metadata))
(next-cursor (and meta (plist-get meta :next_cursor)))
(has-more (eq t (plist-get data :has_more)))
(user-ids (slack-team-missing-user-ids
team (cl-loop for m in messages
nconc (slack-message-user-ids m)))))
(if (< 0 (length user-ids))
(slack-users-info-request
user-ids team
:after-success #'(lambda ()
(callback messages next-cursor has-more)))
(callback messages next-cursor has-more))))))
(slack-request
(slack-request-create
slack-conversations-replies-url
team
:params (list (cons "channel" channel)
(cons "ts" ts)
(if cursor (cons "cursor" cursor)
(cons "oldest" oldest)))
:success #'on-success)))))
(defun slack-conversations-close (room team &optional after-success)
(let ((channel (oref room id)))
(cl-labels
((on-success (data)
(when (functionp after-success)
(funcall after-success data))))
(slack-request
(slack-request-create
slack-conversations-close-url
team
:type "POST"
:params (list (cons "channel" channel))
:success (slack-conversations-success-handler
team :on-success #'on-success))))))
(cl-defun slack-conversations-create (team &optional (is-private "false"))
(let ((name (read-from-minibuffer "Name: "))
(is-private (or is-private
(if (y-or-n-p "Private? ")
"true" "false"))))
(slack-request
(slack-request-create
slack-conversations-create-url
team
:type "POST"
:params (list (cons "name" name)
(cons "is_private" is-private))
:success (slack-conversations-success-handler team)))))
(cl-defun slack-conversations-history (room team &key
(after-success nil)
(cursor nil)
(latest nil)
(oldest nil)
(inclusive nil)
(limit "100"))
(let ((channel (oref room id)))
(cl-labels
((callback (messages next-cursor)
(when (functionp after-success)
(funcall after-success
messages
next-cursor)))
(success (data)
(let* ((meta (plist-get data :response_metadata))
(next-cursor (or (plist-get meta :next_cursor) ""))
(messages (cl-loop for e in (plist-get data :messages)
collect (slack-message-create e team room)))
(user-ids (slack-team-missing-user-ids
team (cl-loop for m in messages
nconc (slack-message-user-ids m)))))
(if (< 0 (length user-ids))
(slack-user-info-request
user-ids team
:after-success #'(lambda ()
(callback messages next-cursor)))
(callback messages next-cursor)))))
(slack-request
(slack-request-create
slack-conversations-history-url
team
:params (list (cons "channel" channel)
(cons "limit" limit)
(and cursor (cons "cursor" cursor))
(and latest (cons "latest" latest))
(and oldest (cons "oldest" oldest))
(and inclusive (cons "inclusive" inclusive)))
:success (slack-conversations-success-handler
team :on-success #'success))))))
(defun slack-conversations-members (room team &optional cursor after-success)
(if (slack-room-members-loaded-p room)
(when (functionp after-success)
(funcall after-success (slack-room-members room) ""))
(cl-labels
((callback (members next-cursor)
(when (< (length next-cursor) 1)
(slack-room-members-loaded room))
(when (functionp after-success)
(funcall after-success members next-cursor)))
(success (data)
(let* ((meta (plist-get data :response_metadata))
(next-cursor (or (and meta (plist-get meta :next_cursor)) ""))
(members (plist-get data :members))
(missing-user-ids (slack-team-missing-user-ids team members)))
(slack-room-set-members room members)
(if (< 0 (length missing-user-ids))
(slack-user-info-request
missing-user-ids
team
:after-success #'(lambda ()
(callback members next-cursor)))
(callback members next-cursor)))))
(slack-request
(slack-request-create
slack-conversations-members-url
team
:params (list (cons "channel" (oref room id))
(cons "limit" "100")
(and cursor (cons "cursor" cursor)))
:success (slack-conversations-success-handler
team :on-success #'success))))))
(cl-defun slack-conversations-open (team &key room user-ids)
(let ((channel (or (and room (oref room id))
""))
(users (mapconcat #'identity user-ids ",")))
(slack-request
(slack-request-create
slack-conversations-open-url
team
:type "POST"
:params (list (if (< 0 (length users))
(cons "users" users)
(cons "channel" channel)))
:success (slack-conversations-success-handler team)))))
(cl-defun slack-conversations-view (room team &key
(after-success nil)
(cursor nil)
(latest nil)
(oldest nil)
(inclusive nil)
(count "100"))
(cl-labels
((success (&key data &allow-other-keys)
(slack-request-handle-error
(data "slack-conversations-view")
(let* ((key (cl-case (eieio-object-class-name room)
(slack-channel :channel)
(slack-im :im)
(slack-group :group)))
(new-room (slack-room-create
(plist-get data key)
(eieio-object-class-name room)))
(bots (plist-get data :bots))
(users (plist-get data :users))
(history (plist-get data :history))
(messages (cl-loop for e in (plist-get history :messages)
collect (slack-message-create e
team
room)))
(meta (plist-get data :response_metadata))
(next-cursor (or (and meta (plist-get meta :next_cursor))
"")))
(slack-team-set-room team new-room)
(slack-team-set-users team users)
(slack-team-set-bots team bots)
(when (functionp after-success)
(funcall after-success messages next-cursor))))))
(slack-request
(slack-request-create
slack-conversations-view-url
team
:type "POST"
:params (list (cons "name" (oref room id))
(cons "include_pin_count" "true")
(cons "include_full_users" "true")
(cons "count" count)
(and cursor (cons "cursor" cursor))
(and latest (cons "latest" latest))
(and oldest (cons "oldest" oldest))
(and inclusive (cons "inclusive" inclusive)))
:success #'success))))
(defun slack-conversations-mark (room team ts &optional after-success)
(cl-labels ((on-success (&rest _ignore)
(when (functionp after-success)
(funcall after-success))))
(slack-request
(slack-request-create
slack-conversations-mark-url
team
:type "POST"
:params (list (cons "channel" (oref room id))
(cons "ts" ts))
:success (slack-conversations-success-handler team :on-success #'on-success)))))
(provide 'slack-conversations)
;;; slack-conversations.el ends here