forked from emacs-slack/emacs-slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack-group.el
259 lines (225 loc) · 9.46 KB
/
slack-group.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
;;; slack-group.el --- slack private group interface -*- lexical-binding: t; -*-
;; Copyright (C) 2015 Yuya Minami
;; Author: Yuya Minami
;; 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 'slack-room)
(require 'slack-util)
(require 'slack-buffer)
(defconst slack--group-open-url "https://slack.com/api/groups.open")
(defconst slack-group-buffer-name "*Slack - Private Group*")
(defconst slack-group-list-url "https://slack.com/api/groups.list")
(defconst slack-group-update-mark-url "https://slack.com/api/groups.mark")
(defconst slack-create-group-url "https://slack.com/api/groups.create")
(defconst slack-group-rename-url "https://slack.com/api/groups.rename")
(defconst slack-group-invite-url "https://slack.com/api/groups.invite")
(defconst slack-group-leave-url "https://slack.com/api/groups.leave")
(defconst slack-group-archive-url "https://slack.com/api/groups.archive")
(defconst slack-group-unarchive-url "https://slack.com/api/groups.unarchive")
(defconst slack-mpim-close-url "https://slack.com/api/mpim.close")
(defconst slack-mpim-open-url "https://slack.com/api/mpim.open")
(defvar slack-buffer-function)
(defclass slack-group (slack-room)
((name :initarg :name :type string)
(is-group :initarg :is_group)
(creator :initarg :creator)
(is-archived :initarg :is_archived)
(is-mpim :initarg :is_mpim)
(members :initarg :members :type list)
(topic :initarg :topic)
(unread-count-display :initarg :unread_count_display :initform 0 :type integer)
(purpose :initarg :purpose)))
(defun slack-group-names (team &optional filter)
(with-slots (groups) team
(slack-room-names groups filter)))
(defmethod slack-room-subscribedp ((room slack-group) team)
(with-slots (subscribed-channels) team
(let ((name (slack-room-name room)))
(and name
(memq (intern name) subscribed-channels)))))
(defmethod slack-room-buffer-name ((room slack-group))
(concat slack-group-buffer-name
" : "
(slack-room-name-with-team-name room)))
(defun slack-group-select ()
(interactive)
(let ((team (slack-team-select)))
(slack-room-select
(cl-loop for team in (list team)
for groups = (oref team groups)
nconc groups))))
(defun slack-group-list-update ()
(interactive)
(let ((team (slack-team-select)))
(cl-labels ((on-list-update
(&key data &allow-other-keys)
(slack-request-handle-error
(data "slack-group-list-update")
(with-slots (groups) team
(setq groups
(mapcar #'(lambda (g)
(slack-room-create g team 'slack-group))
(plist-get data :groups))))
(message "Slack Group List Updated"))))
(slack-room-list-update slack-group-list-url
#'on-list-update
:sync nil))))
(defmethod slack-room-update-mark-url ((_room slack-group))
slack-group-update-mark-url)
(defun slack-create-group ()
(interactive)
(let ((team (slack-team-select)))
(cl-labels
((on-create-group (&key data &allow-other-keys)
(slack-request-handle-error
(data "slack-create-group"))))
(slack-create-room slack-create-group-url
team
#'on-create-group))))
(defun slack-group-rename ()
(interactive)
(slack-room-rename slack-group-rename-url
#'slack-group-names))
(defun slack-group-invite ()
(interactive)
(slack-room-invite slack-group-invite-url
#'slack-group-names))
(defun slack-group-leave ()
(interactive)
(let* ((team (slack-team-select))
(group (slack-current-room-or-select
#'(lambda ()
(slack-group-names team)))))
(cl-labels
((on-group-leave
(&key data &allow-other-keys)
(slack-request-handle-error
(data "slack-group-leave")
(with-slots (groups) team
(setq groups
(cl-delete-if #'(lambda (g)
(slack-room-equal-p group g))
groups)))
(message "Left Group: %s"
(slack-room-name-with-team-name group)))))
(slack-room-request-with-id slack-group-leave-url
(oref group id)
team
#'on-group-leave))))
(defmethod slack-room-archived-p ((room slack-group))
(oref room is-archived))
(defun slack-group-archive ()
(interactive)
(let* ((team (slack-team-select))
(group (slack-current-room-or-select
#'(lambda ()
(slack-group-names
team
#'(lambda (groups)
(cl-remove-if #'slack-room-archived-p
groups)))))))
(cl-labels
((on-group-archive (&key data &allow-other-keys)
(slack-request-handle-error
(data "slack-group-archive"))))
(slack-room-request-with-id slack-group-archive-url
(oref group id)
team
#'on-group-archive))))
(defun slack-group-unarchive ()
(interactive)
(let* ((team (slack-team-select))
(group (slack-current-room-or-select
#'(lambda ()
(slack-group-names
team
#'(lambda (groups)
(cl-remove-if-not #'slack-room-archived-p
groups)))))))
(cl-labels
((on-group-unarchive (&key _data &allow-other-keys)
(data "slack-group-unarchive")))
(slack-room-request-with-id slack-group-unarchive-url
(oref group id)
team
#'on-group-unarchive))))
(defun slack-group-members-s (group)
(with-slots (members team-id) group
(mapconcat #'(lambda (user) (slack-user-name user
(slack-team-find team-id)))
members ", ")))
(defun slack-group-mpim-open ()
(interactive)
(let* ((team (slack-team-select))
(users (slack-user-names team)))
(cl-labels
((select-users (users acc)
(let ((selected (completing-read "Select User: "
users nil t)))
(if (< 0 (length selected))
(select-users users
(push (cdr (cl-assoc selected users :test #'string=)) acc))
acc)))
(on-success
(&key data &allow-other-keys)
(slack-request-handle-error
(data "slack-group-mpim-open")
(if (plist-get data :already_open)
(message "Direct Message Channel with %s Already Open"
(slack-group-members-s (slack-room-find (oref selected id) team)))
(oset team groups
(cons (slack-room-create (plist-get data :group) team 'slack-group)
(oref team groups)))))))
(slack-request
slack-mpim-open-url
team
:type "POST"
:params (list (cons "users" (mapconcat (lambda (u) (plist-get u :id)) (select-users users '()) ",")))
:success #'on-success
:sync nil))))
(defun slack-group-mpim-close ()
(interactive)
(let* ((team (slack-team-select)))
(slack-select-from-list
((slack-group-names team #'(lambda (groups)
(cl-remove-if-not #'slack-mpim-p
groups)))
"Select MPIM: ")
(cl-labels
((on-success
(&key data &allow-other-keys)
(slack-request-handle-error
(data "slack-group-mpim-close")
(let ((group (slack-room-find (oref selected id) team)))
(with-slots (groups) team
(setq groups
(cl-delete-if #'(lambda (g)
(slack-room-equal-p group g))
groups)))
(if (plist-get data :already_closed)
(message "Direct Message Channel with %s Already Closed"
(slack-group-members-s group)))))))
(slack-request
slack-mpim-close-url
team
:type "POST"
:params (list (cons "channel" (oref selected id)))
:success #'on-success
:sync nil)))))
(defmethod slack-mpim-p ((room slack-group))
(oref room is-mpim))
(provide 'slack-group)
;;; slack-group.el ends here