about summary refs log tree commit diff
path: root/configs/shared/emacs/.emacs.d/elpa/slack-20180913.651/slack-channel.el
blob: d41fcbb4728b3ad92085704df9185d08167dc38e (plain) (blame)
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
;;; slack-channel.el ---slack channel implement      -*- lexical-binding: t; -*-

;; Copyright (C) 2015  yuya.minami

;; Author: yuya.minami <yuya.minami@yuyaminami-no-MacBook-Pro.local>
;; 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-group)
(require 'slack-buffer)
(require 'slack-util)
(require 'slack-request)

(defvar slack-buffer-function)

(defconst slack-channel-history-url "https://slack.com/api/channels.history")
(defconst slack-channel-list-url "https://slack.com/api/channels.list")
(defconst slack-channel-buffer-name "*Slack - Channel*")
(defconst slack-channel-update-mark-url "https://slack.com/api/channels.mark")
(defconst slack-create-channel-url "https://slack.com/api/channels.create")
(defconst slack-channel-rename-url "https://slack.com/api/channels.rename")
(defconst slack-channel-invite-url "https://slack.com/api/channels.invite")
(defconst slack-channel-leave-url "https://slack.com/api/channels.leave")
(defconst slack-channel-join-url "https://slack.com/api/channels.join")
(defconst slack-channel-info-url "https://slack.com/api/channels.info")
(defconst slack-channel-archive-url "https://slack.com/api/channels.archive")
(defconst slack-channel-unarchive-url "https://slack.com/api/channels.unarchive")
(defconst slack-channel-info-url "https://slack.com/api/channels.info")

(defclass slack-channel (slack-group)
  ((is-member :initarg :is_member :initform nil)
   (num-members :initarg :num_members :initform 0)))

(defmethod slack-merge ((this slack-channel) other)
  (call-next-method)
  (with-slots (is-member num-members) this
    (setq is-member (oref other is-member))
    (setq num-members (oref other num-members))))

(defmethod slack-room-buffer-name ((room slack-channel) team)
  (concat slack-channel-buffer-name
          " : "
          (slack-room-display-name room team)))

(defun slack-channel-names (team &optional filter)
  (with-slots (channels) team
    (slack-room-names channels team filter)))

(defmethod slack-room-member-p ((room slack-channel))
  (oref room is-member))

(defun slack-channel-select ()
  (interactive)
  (let* ((team (slack-team-select))
         (room (slack-room-select
                (cl-loop for team in (list team)
                         for channels = (oref team channels)
                         nconc channels)
                team)))
    (slack-room-display room team)))

(defun slack-channel-list-update (&optional team after-success)
  (interactive)
  (let ((team (or team (slack-team-select))))
    (cl-labels ((on-list-update
                 (&key data &allow-other-keys)
                 (slack-request-handle-error
                  (data "slack-channel-list-update")
                  (slack-merge-list (oref team channels)
                                    (mapcar #'(lambda (d)
                                                (slack-room-create d
                                                                   team
                                                                   'slack-channel))
                                            (plist-get data :channels)))

                  (if after-success
                      (funcall after-success team))
                  (mapc #'(lambda (room)
                            (slack-request-worker-push
                             (slack-room-create-info-request room team)))
                        (oref team channels))
                  (slack-log "Slack Channel List Updated" team :level 'info))))
      (slack-room-list-update slack-channel-list-url
                              #'on-list-update
                              team
                              :sync nil))))

(defmethod slack-room-update-mark-url ((_room slack-channel))
  slack-channel-update-mark-url)

(defun slack-create-channel ()
  (interactive)
  (let ((team (slack-team-select)))
    (cl-labels
        ((on-create-channel (&key data &allow-other-keys)
                            (slack-request-handle-error
                             (data "slack-channel-create"))))
      (slack-create-room slack-create-channel-url
                         team
                         #'on-create-channel))))

(defun slack-channel-rename ()
  (interactive)
  (slack-room-rename slack-channel-rename-url
                     #'slack-channel-names))

(defun slack-channel-invite ()
  (interactive)
  (slack-room-invite slack-channel-invite-url
                     #'slack-channel-names))

(defun slack-channel-leave (&optional team select)
  (interactive)
  (let* ((team (or team (slack-team-select)))
         (channel (slack-current-room-or-select
                   #'(lambda ()
                       (slack-channel-names
                        team
                        #'(lambda (channels)
                            (cl-remove-if-not #'slack-room-member-p
                                              channels))))
                   select)))
    (slack-channel-request-leave channel team)))

(defun slack-channel-request-leave (channel team)
  (cl-labels
      ((on-channel-leave (&key data &allow-other-keys)
                         (slack-request-handle-error
                          (data "slack-channel-leave")
                          (oset channel is-member nil)
                          (message "Left Channel: %s"
                                   (slack-room-name channel team)))))
    (slack-room-request-with-id slack-channel-leave-url
                                (oref channel id)
                                team
                                #'on-channel-leave)))

(defun slack-channel-join (&optional team select)
  (interactive)
  (cl-labels
      ((filter-channel (channels)
                       (cl-remove-if
                        #'(lambda (c)
                            (or (slack-room-member-p c)
                                (slack-room-archived-p c)))
                        channels)))
    (let* ((team (or team (slack-team-select)))
           (channel (slack-current-room-or-select
                     #'(lambda ()
                         (slack-channel-names team
                                              #'filter-channel))
                     select)))
      (slack-channel-request-join channel team))))

(defun slack-channel-request-join (channel team)
  (cl-labels
      ((on-channel-join (&key data &allow-other-keys)
                        (slack-request-handle-error
                         (data "slack-channel-join"))))
    (slack-request
     (slack-request-create
      slack-channel-join-url
      team
      :params (list (cons "name" (slack-room-name channel team)))
      :success #'on-channel-join))))

(defun slack-channel-create-from-info (id team)
  (cl-labels
      ((on-create-from-info
        (&key data &allow-other-keys)
        (slack-request-handle-error
         (data "slack-channel-create-from-info")
         (let* ((c-data (plist-get data :channel)))
           (if (plist-get c-data :is_channel)
               (let ((channel (slack-room-create c-data team 'slack-channel)))
                 (with-slots (channels) team (push channel channels))
                 (message "Channel: %s created"
                          (slack-room-display-name channel
                                                   team))))))))
    (slack-channel-fetch-info id team #'on-create-from-info)))

(defun slack-channel-fetch-info (id team success)
  (slack-request
   (slack-request-create
    slack-channel-info-url
    team
    :params (list (cons "channel" id))
    :success success)))

(defun slack-channel-archive ()
  (interactive)
  (let* ((team (slack-team-select))
         (channel (slack-current-room-or-select
                   #'(lambda ()
                       (slack-channel-names
                        team
                        #'(lambda (channels)
                            (cl-remove-if #'slack-room-archived-p
                                          channels)))))))
    (cl-labels
        ((on-channel-archive (&key data &allow-other-keys)
                             (slack-request-handle-error
                              (data "slack-channel-archive"))))
      (slack-room-request-with-id slack-channel-archive-url
                                  (oref channel id)
                                  team
                                  #'on-channel-archive))))

(defun slack-channel-unarchive ()
  (interactive)
  (let* ((team (slack-team-select))
         (channel (slack-current-room-or-select
                   #'(lambda ()
                       (slack-channel-names
                        team
                        #'(lambda (channels)
                            (cl-remove-if-not #'slack-room-archived-p
                                              channels)))))))
    (cl-labels
        ((on-channel-unarchive (&key data &allow-other-keys)
                               (slack-request-handle-error
                                (data "slack-channel-unarchive"))))
      (slack-room-request-with-id slack-channel-unarchive-url
                                  (oref channel id)
                                  team
                                  #'on-channel-unarchive))))

(defmethod slack-room-subscribedp ((room slack-channel) team)
  (with-slots (subscribed-channels) team
    (let ((name (slack-room-name room team)))
      (and name
           (memq (intern name) subscribed-channels)))))

(defmethod slack-room-get-info-url ((_room slack-channel))
  slack-channel-info-url)

(defmethod slack-room-update-info ((room slack-channel) data team)
  (let ((new-room (slack-room-create (plist-get data :channel)
                                     team
                                     'slack-channel)))

    (slack-merge room new-room)))

(defmethod slack-room-history-url ((_room slack-channel))
  slack-channel-history-url)

(defmethod slack-room-replies-url ((_room slack-channel))
  "https://slack.com/api/channels.replies")

(defmethod slack-room-hidden-p ((room slack-channel))
  (slack-room-archived-p room))

(defmethod slack-room-member-p ((this slack-channel))
  (oref this is-member))

(provide 'slack-channel)
;;; slack-channel.el ends here