forked from emacs-slack/emacs-slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack-im.el
182 lines (154 loc) · 6.33 KB
/
slack-im.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
;;; slack-im.el ---slack direct message interface -*- lexical-binding: t; -*-
;; Copyright (C) 2015 南優也
;; Author: 南優也 <[email protected]>
;; 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)
(defvar slack-buffer-function)
(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)
(is-open :initarg :is_open :initform nil)))
(defmethod slack-room-open-p ((room slack-im))
t)
(defmethod slack-room-name-with-team-name ((room slack-im))
(with-slots (team-id user) room
(let* ((team (slack-team-find team-id))
(user-name (slack-user-name user team)))
(format "%s - %s" (oref team name) user-name))))
(defmethod slack-im-user-presence ((room slack-im))
(with-slots ((user-id user) team-id) room
(let* ((team (slack-team-find team-id))
(user (slack-user-find user-id team)))
(slack-user-presence-to-string user))))
(defmethod slack-room-name ((room slack-im))
(with-slots (user team-id) room
(slack-user-name user (slack-team-find team-id))))
(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-name-with-team-name room)))
(defun slack-im-select ()
(interactive)
(let ((team (slack-team-select)))
(slack-room-select
(cl-loop for team in (list team)
for ims = (cl-remove-if #'(lambda (im) (not (oref im is-open)))
(oref team ims))
nconc ims))))
(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)
(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))
(oset team ims
(mapcar #'(lambda (d)
(slack-room-create d team 'slack-im))
(plist-get data :ims)))
(message "Slack Im List Updated"))))
(slack-room-list-update slack-im-list-url
#'on-update-room-list
team
:sync nil)))
(defun slack-im-list-update ()
(interactive)
(let ((team (slack-team-select)))
(slack-request
slack-user-list-url
team
:success (cl-function (lambda (&key data &allow-other-keys)
(slack-request-handle-error (data "slack-im-list-update")
(let ((users (plist-get data :members)))
(slack-im-update-room-list users team)))))
:sync nil)))
(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-im-close-url
team
:type "POST"
:params (list (cons "channel" (oref selected id)))
:success #'on-success
:sync nil)))))
(defun slack-im-open ()
(interactive)
(let* ((team (slack-team-select))
(alist (cl-remove-if #'(lambda (im-names)
(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-open")
(if (plist-get data :already_open)
(let ((im (slack-room-find (oref selected id) team)))
(oset im is-open t)
(message "Direct Message Channel with %s Already Open"
(slack-user-name (oref im user) team)))))))
(slack-request
slack-im-open-url
team
:type "POST"
:params (list (cons "user" (oref selected user)))
:success #'on-success
:sync nil)))))
(provide 'slack-im)
;;; slack-im.el ends here