about summary refs log tree commit diff
path: root/configs/shared/emacs/.emacs.d/elpa/slack-20180712.2222/slack-im.el
blob: ac11d5fae6b89a998f1185c3d4230a5ea8b0a77f (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
;;; slack-im.el ---slack direct message interface    -*- lexical-binding: t; -*-

;; Copyright (C) 2015  南優也

;; Author: 南優也 <yuyaminami@minamiyuunari-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-util)
(require 'slack-room)
(require 'slack-buffer)
(require 'slack-user)
(require 'slack-request)

(defvar slack-buffer-function)

(defconst slack-im-history-url "https://slack.com/api/im.history")
(defconst slack-im-buffer-name "*Slack - Direct Messages*")
(defconst slack-user-list-url "https://slack.com/api/users.list")
(defconst slack-im-list-url "https://slack.com/api/im.list")
(defconst slack-im-close-url "https://slack.com/api/im.close")
(defconst slack-im-open-url "https://slack.com/api/im.open")
(defconst slack-im-update-mark-url "https://slack.com/api/im.mark")

(defclass slack-im (slack-room)
  ((user :initarg :user :initform "")
   (is-open :initarg :is_open :initform t)
   (is-user-deleted :initarg :is_user_deleted :initform nil)))

(defmethod slack-merge ((this slack-im) other)
  (call-next-method)
  (with-slots (user is-open) this
    (setq user (oref other user))
    (setq is-open (oref other is-open))))

(defmethod slack-room-open-p ((room slack-im))
  (oref room is-open)
  (not (oref room is-user-deleted)))

(defmethod slack-im-user-presence ((room slack-im))
  (with-slots (team-id) room
    (let ((team (slack-team-find team-id)))
      (slack-user-presence-to-string (slack-user-find room team)))))

(defmethod slack-im-user-dnd-status ((room slack-im))
  (with-slots (team-id) room
    (slack-user-dnd-status-to-string (slack-user-find room
                                                      (slack-team-find team-id)))))

(defmethod slack-room-name ((room slack-im))
  (with-slots (user team-id) room
    (slack-user-name user (slack-team-find team-id))))

(defmethod slack-room-display-name ((room slack-im))
  "To Display emoji in minibuffer configure `emojify-inhibit-in-buffer-functions'"
  (let* ((team (slack-team-find (oref room team-id)))
         (status (slack-user-status (oref room user) team))
         (room-name (or (and status
                             (format "%s %s"
                                     (slack-room-name room)
                                     status))
                        (slack-room-name room))))
    (if slack-display-team-name
        (format "%s - %s"
                (oref (slack-room-team room) name)
                room-name)
      room-name)))

(defun slack-im-user-name (im team)
  (with-slots (user) im
    (slack-user-name user team)))

(defun slack-im-names (team)
  (with-slots (ims) team
    (slack-room-names ims
                      #'(lambda (ims)
                          (cl-remove-if #'(lambda (im) (not (oref im is-open)))
                                        ims)))))

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

(defun slack-im-select ()
  (interactive)
  (let* ((team (slack-team-select))
         (candidates (cl-loop for team in (list team)
                              for ims = (cl-remove-if #'(lambda (im)
                                                          (not (oref im is-open)))
                                                      (oref team ims))
                              nconc ims))
         (room (slack-room-select candidates)))
    (slack-room-display room team)))

(defun slack-user-equal-p (a b)
  (string= (plist-get a :id) (plist-get b :id)))

(defun slack-user-pushnew (user team)
  (with-slots (users) team
    (cl-pushnew user users :test #'slack-user-equal-p)))

(defun slack-im-update-room-list (users team &optional after-success)
  (cl-labels ((on-update-room-list
               (&key data &allow-other-keys)
               (slack-request-handle-error
                (data "slack-im-update-room-list")
                (mapc #'(lambda (u) (slack-user-pushnew u team))
                      (append users nil))
                (slack-merge-list (oref team ims)
                                  (mapcar #'(lambda (d)
                                              (slack-room-create d team 'slack-im))
                                          (plist-get data :ims)))
                (if after-success
                    (funcall after-success team))
                (slack-request-dnd-team-info team)
                (mapc #'(lambda (room)
                          (slack-room-info-request room team))
                      (oref team ims))
                (slack-log "Slack Im List Updated" team :level 'info))))
    (slack-room-list-update slack-im-list-url
                            #'on-update-room-list
                            team
                            :sync nil)))

(defun slack-im-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-im-list-update")
           (let* ((members (append (plist-get data :members) nil)))
             (slack-im-update-room-list members team after-success)))))
      (slack-request
       (slack-request-create
        slack-user-list-url
        team
        :success #'on-list-update)))))

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

(defun slack-im-close ()
  (interactive)
  (let* ((team (slack-team-select))
         (alist (cl-remove-if #'(lambda (im-names)
                                  (not (oref (cdr im-names) is-open)))
                              (slack-im-names team))))
    (slack-select-from-list
        (alist "Select User: ")
        (cl-labels
            ((on-success
              (&key data &allow-other-keys)
              (slack-request-handle-error
               (data "slack-im-close")
               (if (plist-get data :already_closed)
                   (let ((im (slack-room-find (oref selected id) team)))
                     (oset im is-open nil)
                     (message "Direct Message Channel with %s Already Closed"
                              (slack-user-name (oref im user) team)))))))
          (slack-request
           (slack-request-create
            slack-im-close-url
            team
            :type "POST"
            :params (list (cons "channel" (oref selected id)))
            :success #'on-success))))))

(defun slack-im-open (&optional user after-success)
  (interactive)
  (let* ((team (slack-team-select))
         (user (or user (slack-select-from-list
                            ((slack-user-name-alist
                              team
                              :filter #'(lambda (users) (cl-remove-if #'slack-user-hidden-p users)))
                             "Select User: ")))))
    (cl-labels
        ((on-success
          (&key data &allow-other-keys)
          (slack-request-handle-error
           (data "slack-im-open")
           (if (plist-get data :already_open)
               (let ((im (slack-room-find (plist-get (plist-get data :channel) :id) team)))
                 (oset im is-open t)
                 (message "Direct Message Channel with %s Already Open"
                          (slack-user-name (oref im user) team))))
           (when (functionp after-success)
             (funcall after-success)))))
      (slack-request
       (slack-request-create
        slack-im-open-url
        team
        :type "POST"
        :params (list (cons "user" (plist-get user :id)))
        :success #'on-success)))))

(defmethod slack-room-label-prefix ((room slack-im))
  (format "%s "
          (or
           (slack-im-user-dnd-status room)
           (slack-im-user-presence room))))

(defmethod slack-room-get-info-url ((_room slack-im))
  slack-im-open-url)

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

    (slack-merge room new-room)))

(defmethod slack-room-info-request-params ((room slack-im))
  (list (cons "user" (oref room user))
        (cons "return_im" "true")))

(defmethod slack-room-get-members ((room slack-im))
  (list (oref room user)))

(defun slack-im-find-by-user-id (user-id team)
  (cl-find-if #'(lambda (im) (string= user-id (oref im user)))
              (oref team ims)))

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

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

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