about summary refs log tree commit diff
path: root/configs/shared/emacs/.emacs.d/elpa/slack-20180913.651/slack-user.el
blob: 3653a6d1c184d7eb43257c87c3bb3675e4847849 (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
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
;;; slack-user.el ---slack user 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 'slack-util)
(require 'slack-request)
(require 'slack-room)

(defconst slack-dnd-team-info-url "https://slack.com/api/dnd.teamInfo")
(defconst slack-dnd-end-dnd-url "https://slack.com/api/dnd.endDnd")
(defconst slack-dnd-set-snooze-url "https://slack.com/api/dnd.setSnooze")
(defconst slack-set-presence-url "https://slack.com/api/users.setPresence")
(defconst slack-user-info-url "https://slack.com/api/users.info")
(defconst slack-user-profile-set-url "https://slack.com/api/users.profile.set")
(defconst slack-bot-info-url "https://slack.com/api/bots.info")
(defconst slack-bot-list-url "https://slack.com/api/bots.list")
(defvar slack-current-user-id nil)

(defun slack-user--find (id team)
  (with-slots (users) team
    (cl-find-if (lambda (user)
                  (string= id (plist-get user :id)))
                users)))

(defun slack-user-find-by-name (name team)
  (with-slots (users) team
    (cl-find-if (lambda (user)
                  (string= name (plist-get user :name)))
                users)))

(defun slack-user-get-id (name team)
  (let ((user (slack-user-find-by-name name team)))
    (if user
        (plist-get user :id))))

(defun slack-user-name (id team)
  (slack-if-let* ((user (slack-user--find id team)))
      (if (oref team full-and-display-names)
          (plist-get user :real_name)
        (plist-get user :name))))

(defun slack-user-status (id team)
  (let* ((user (slack-user--find id team))
         (profile (and user (plist-get user :profile)))
         (emoji (and profile (plist-get profile :status_emoji)))
         (text (and profile (plist-get profile :status_text))))
    (mapconcat #'identity (cl-remove-if #'null (list emoji text))
               " ")))

(defun slack-user-names (team)
  (with-slots (users) team
    (mapcar (lambda (u) (cons (plist-get u :name) u))
            (cl-remove-if #'slack-user-hidden-p users))))

(defun slack-user-dnd-in-range-p (user)
  (let ((current (time-to-seconds))
        (dnd-start (plist-get (plist-get user :dnd_status) :next_dnd_start_ts))
        (dnd-end (plist-get (plist-get user :dnd_status) :next_dnd_end_ts)))
    (and dnd-start dnd-end
         (<= dnd-start current)
         (<= current dnd-end))))

(defun slack-user-dnd-status-to-string (user)
  (if (slack-user-dnd-in-range-p user)
      "Z"
    nil))

(defun slack-user-presence-to-string (user)
  (if (string= (plist-get user :presence) "active")
      "*"
    " "))

(defun slack-user-set-status ()
  (interactive)
  (let ((team (slack-team-select))
        (emoji (slack-select-emoji))
        (text (read-from-minibuffer "Text: ")))
    (slack-user-set-status-request  team emoji text)))

(defun slack-user-set-status-request (team emoji text)
  (cl-labels ((on-success
               (&key data &allow-other-keys)
               (slack-request-handle-error
                (data "slack-user-set-status-request"))))
    (slack-request
     (slack-request-create
      slack-user-profile-set-url
      team
      :type "POST"
      :data (list (cons "id" (oref team self-id))
                  (cons "profile"
                        (json-encode (list (cons "status_text" text)
                                           (cons "status_emoji" emoji)))))
      :success #'on-success))))

(defun slack-bot-info-request (bot_id team &optional after-success)
  (cl-labels
      ((on-success (&key data &allow-other-keys)
                   (slack-request-handle-error
                    (data "slack-bot-info-request")
                    (push (plist-get data :bot) (oref team bots))
                    (if after-success
                        (funcall after-success team)))))
    (slack-request
     (slack-request-create
      slack-bot-info-url
      team
      :params (list (cons "bot" bot_id))
      :success #'on-success))))

(defun slack-bot-list-update (&optional team)
  (interactive)
  (let ((team (or team (slack-team-select))))
    (cl-labels
        ((on-success
          (&key data &allow-other-keys)
          (slack-request-handle-error
           (data "slack-bot-list-update")
           (oset team bots (append (plist-get data :bots) nil)))))
      (slack-request
       (slack-request-create
        slack-bot-list-url
        team
        :success #'on-success)))))

(defface slack-user-profile-header-face
  '((t (:foreground "#FFA000"
                    :weight bold
                    :height 1.5)))
  "Face used to user profile header."
  :group 'slack)

(defface slack-user-profile-property-name-face
  '((t (:weight bold :height 1.2)))
  "Face used to user property."
  :group 'slack)

(define-derived-mode slack-user-profile-mode fundamental-mode "Slack User"
  ""
  (setq-local default-directory slack-default-directory))

(defun slack-user-profile (user)
  (plist-get user :profile))

(defun slack-user-fname (user)
  (plist-get (slack-user-profile user) :first_name))

(defun slack-user-lname (user)
  (plist-get (slack-user-profile user) :last_name))

(defun slack-user-header (user)
  (let* ((fname (slack-user-fname user))
         (lname (slack-user-lname user))
         (name (plist-get user :name)))
    (or (and fname lname
             (format "%s %s - @%s"
                     (slack-user-fname user)
                     (slack-user-lname user)
                     (plist-get user :name)))
        name)))

(defun slack-user-timezone (user)
  (let ((offset (/ (plist-get user :tz_offset) (* 60 60))))
    (format "%s, %s"
            (or (plist-get user :tz)
                (plist-get user :tz_label))
            (if (<= 0 offset)
                (format "+%s hour" offset)
              (format "%s hour" offset)))))

(defun slack-user-property-to-str (value title)
  (and value (< 0 (length value))
       (format "%s\n\t%s"
               (propertize title 'face 'slack-user-profile-property-name-face)
               value)))

(defun slack-user-profile-to-string (id team)
  (let* ((user (slack-user--find id team))
         (image (slack-image-string (list (slack-user-image-url user 512)
                                          nil nil nil (window-width
                                                       (get-buffer-window
                                                        (current-buffer))
                                                       t))
                                    nil t))
         (profile (slack-user-profile user))
         (header (propertize (slack-user-header user)
                             'face 'slack-user-profile-header-face))
         (presence (slack-user-property-to-str (plist-get user :presence) "Presence"))
         (status (slack-user-property-to-str (slack-user-status id team) "Status"))
         (timezone (slack-user-property-to-str (slack-user-timezone user) "Timezone"))
         (email (slack-user-property-to-str (plist-get profile :email) "Email"))
         (phone (slack-user-property-to-str (plist-get profile :phone) "Phone"))
         (skype (slack-user-property-to-str (plist-get profile :skype) "Skype"))
         (body (mapconcat #'identity
                          (cl-remove-if #'null
                                        (list presence status timezone email phone skype))
                          "\n"))
         (dm-button (propertize "[Open Direct Message]"
                                'face '(:underline t)
                                'keymap (let ((map (make-sparse-keymap)))
                                          (define-key map (kbd "RET")
                                            #'(lambda ()
                                                (interactive)
                                                (slack-if-let* ((buf slack-current-buffer))
                                                    (slack-buffer-display-im buf))))
                                          map))))
    (format "\n%s\n\n%s%s\n%s\n\n%s" image header (format "  (%s)" id) body dm-button)))

(defun slack-user-self-p (user-id team)
  (string= user-id (oref team self-id)))

(defun slack-user-name-alist (team &key filter)
  (let ((users (oref team users)))
    (mapcar #'(lambda (e) (cons (slack-user-name (plist-get e :id) team) e))
            (if filter (funcall filter users)
              users))))

(defun slack-user-hidden-p (user)
  (not (eq (plist-get user :deleted) :json-false)))

(defun slack--user-select (team)
  (slack-select-from-list ((slack-user-names team) "Select User: ")))

(defun slack-user-select ()
  (interactive)
  (let* ((team (slack-team-select))
         (alist (slack-user-name-alist
                 team
                 :filter #'(lambda (users)
                             (cl-remove-if #'slack-user-hidden-p users)))))
    (slack-select-from-list (alist "Select User: ")
        (let ((buf (slack-create-user-profile-buffer team (plist-get selected :id))))
          (slack-buffer-display buf)))))

(cl-defun slack-user-info-request (user-id team &key after-success)
  (cl-labels
      ((on-success
        (&key data &allow-other-keys)
        (slack-request-handle-error
         (data "slack-user-info-request")
         (oset team users (cons (plist-get data :user)
                                (cl-remove-if #'(lambda (user)
                                                  (string= (plist-get user :id) user-id))
                                              (oref team users))))
         (slack-im-open (plist-get data :user))
         (when after-success (funcall after-success)))))
    (slack-request
     (slack-request-create
      slack-user-info-url
      team
      :params (list (cons "user" user-id))
      :success #'on-success))))

(defun slack-user-image-url-24 (user)
  (plist-get (slack-user-profile user) :image_24))

(defun slack-user-image-url-32 (user)
  (plist-get (slack-user-profile user) :image_32))

(defun slack-user-image-url-48 (user)
  (plist-get (slack-user-profile user) :image_48))

(defun slack-user-image-url-72 (user)
  (plist-get (slack-user-profile user) :image_72))

(defun slack-user-image-url-512 (user)
  (plist-get (slack-user-profile user) :image_512))

(defun slack-user-image-url (user size)
  (cond
   ((eq size 24) (slack-user-image-url-24 user))
   ((eq size 32) (slack-user-image-url-32 user))
   ((eq size 48) (slack-user-image-url-48 user))
   ((eq size 72) (slack-user-image-url-72 user))
   ((eq size 512) (slack-user-image-url-512 user))
   (t (slack-user-image-url-32 user))))

(defun slack-user-fetch-image (user size team)
  (let* ((image-url (slack-user-image-url user size))
         (file-path (and image-url (slack-profile-image-path image-url team))))
    (when file-path
      (if (file-exists-p file-path) file-path
        (slack-url-copy-file image-url file-path
                             :success (lambda ()
                                        (slack-log (format "Success download Image: %s"
                                                           file-path)
                                                   team)))))
    file-path))

(cl-defun slack-user-image (user team &optional (size 32))
  (when user
    (let ((image (slack-user-fetch-image user size team)))
      (when image
        (create-image image nil nil :ascent 80)))))

(defun slack-user-presence (user)
  (plist-get user :presence))

(defun slack-request-set-presence (team &optional presence)
  (unless presence
    (let ((current-presence (slack-user-presence (slack-user--find (oref team self-id)
                                                                   team))))

      (setq presence (or (and (string= current-presence "away") "auto")
                         "away"))
      ))
  (cl-labels
      ((on-success (&key data &allow-other-keys)
                   (slack-request-handle-error
                    (data "slack-request-set-presence"))))
    (slack-request
     (slack-request-create
      slack-set-presence-url
      team
      :success #'on-success
      :params (list (cons "presence" presence))))))

(defun slack-request-dnd-set-snooze (team time)
  (cl-labels
      ((on-success (&key data &allow-other-keys)
                   (slack-request-handle-error
                    (data "slack-request-dnd-set-snooze")
                    (message "setSnooze: %s" data))))
    (let* ((input (slack-parse-time-string time))
           (num-minutes (and time (/ (- (time-to-seconds input) (time-to-seconds))
                                     60))))
      (unless num-minutes
        (error "Invalid time string %s" time))
      (slack-request
       (slack-request-create
        slack-dnd-set-snooze-url
        team
        :success #'on-success
        :params (list (cons "num_minutes" (format "%s" num-minutes))))))))

(defun slack-request-dnd-end-dnd (team)
  (cl-labels
      ((on-success (&key data &allow-other-keys)
                   (slack-request-handle-error
                    (data "slack-request-dnd-end-dnd")
                    (message "endDnd: %s" data))))
    (slack-request
     (slack-request-create
      slack-dnd-end-dnd-url
      team
      :success #'on-success
      ))))

(defun slack-user-update-dnd-status (user dnd-status)
  (plist-put user :dnd_status dnd-status))

(defun slack-request-dnd-team-info (team &optional after-success)
  (cl-labels
      ((on-success
        (&key data &allow-other-keys)
        (slack-request-handle-error
         (data "slack-request-dnd-team-info")
         (let ((users (plist-get data :users)))
           (oset team users
                 (cl-loop for user in (oref team users)
                          collect (plist-put
                                   user
                                   :dnd_status
                                   (plist-get users
                                              (intern (format ":%s"
                                                              (plist-get user :id)))))))))
        (when (functionp after-success)
          (funcall after-success team))))
    (slack-request
     (slack-request-create
      slack-dnd-team-info-url
      team
      :success #'on-success))))

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