-
Notifications
You must be signed in to change notification settings - Fork 117
/
slack-reminder.el
271 lines (234 loc) · 9.88 KB
/
slack-reminder.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
260
261
262
263
264
265
266
267
268
269
270
271
;;; slack-reminder.el --- -*- lexical-binding: t; -*-
;; Copyright (C) 2016 南優也
;; 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-team)
(require 'slack-request)
(require 'slack-message-formatter)
(require 'slack-message-faces)
(defconst slack-reminder-list-url "https://slack.com/api/reminders.list")
(defconst slack-reminder-delete-url "https://slack.com/api/reminders.delete")
(defconst slack-reminder-complete-url "https://slack.com/api/reminders.complete")
(defconst slack-reminder-info-url "https://slack.com/api/reminders.info")
(defconst slack-reminder-add-from-message-url "https://slack.com/api/reminders.addFromMessage")
(defvar slack-buffer-function)
(defvar slack-completing-read-function)
(defclass slack-reminder-base ()
((id :initarg :id :type string)
(creator :initarg :creator :type string)
(user :initarg :user :type string)
(text :initarg :text :type string)))
(defclass slack-recurring-reminder (slack-reminder-base)
())
(defclass slack-reminder (slack-reminder-base)
((time :initarg :time :type integer)
(complete-ts :initarg :complete_ts :type integer)))
(cl-defmethod slack-reminder-user ((r slack-reminder-base) team)
(slack-user-find r team))
(cl-defmethod slack-reminder-creator ((r slack-reminder-base) team)
(slack-user--find (oref r creator) team))
(cl-defmethod slack-reminder-completedp ((r slack-reminder))
(not (eq 0 (oref r complete-ts))))
(cl-defmethod slack-reminder-completedp ((_r slack-recurring-reminder))
nil)
(defun slack-reminder-create (payload)
(let ((klass (if (eq :json-false (plist-get payload :recurring))
'slack-reminder
'slack-recurring-reminder)))
(apply #'make-instance klass
(slack-collect-slots klass payload))))
(cl-defmethod slack-reminder-to-body ((r slack-reminder))
(with-slots (text time complete-ts) r
(let ((time-str (format "Remind At: %s"
(slack-format-ts
(number-to-string time))))
(completed (format "Completed: %s"
(if (eq complete-ts 0)
"Not Yet"
(slack-format-ts
(number-to-string complete-ts))))))
(format "%s\n%s\n\n%s" time-str completed text))))
(cl-defmethod slack-reminder-to-body ((r slack-recurring-reminder))
(oref r text))
(cl-defmethod slack-reminder-to-string ((r slack-reminder-base) team)
(with-slots (creator user) r
(let* ((header (slack-message-put-header-property
(format "From: %s To: %s"
(slack-user-name creator team)
(slack-user-name user team))))
(body (slack-reminder-to-body r)))
(format "%s\n%s\n\n" header body))))
(cl-defmethod slack-create-reminder-buffer ((team slack-team))
(let* ((buf-name "*Slack - Reminders*")
(buf (get-buffer-create buf-name)))
(with-current-buffer buf
(setq buffer-read-only nil)
(erase-buffer)
(goto-char (point-min))
(with-slots (reminders) team
(cl-loop for reminder in reminders
do (insert (slack-reminder-to-string reminder team))))
(setq buffer-read-only t))
buf))
(cl-defmethod slack-reminder-sort-key ((r slack-reminder))
(oref r time))
(cl-defmethod slack-reminder-sort-key ((_r slack-recurring-reminder))
0)
(defun slack-reminder-sort (team)
(with-slots (reminders) team
(setq reminders
(cl-sort reminders #'<
:key #'(lambda (r) (slack-reminder-sort-key r))))))
(defun slack-reminder-list ()
(interactive)
(let ((team (slack-team-select)))
(cl-labels
((on-reminder-list
(&key data &allow-other-keys)
(slack-request-handle-error
(data "slack-reminder-list")
(oset team reminders
(cl-loop
for payload in (slack-decode
(append (plist-get data :reminders)
nil))
collect (slack-reminder-create payload)))
(slack-reminder-sort team)
(if (< 0 (length (oref team reminders)))
(funcall
slack-buffer-function
(slack-create-reminder-buffer team))
(message "No Reminders!")))))
(slack-request
(slack-request-create
slack-reminder-list-url
team
:success #'on-reminder-list)))))
(cl-defmethod slack-reminders-alist ((team slack-team) &optional filter)
(cl-labels ((text (r)
(with-slots (creator user text) r
(format "Creator: %s Target: %s Content: %s"
(slack-user-name creator team)
(slack-user-name user team)
text))))
(with-slots (reminders) team
(mapcar #'(lambda (r) (cons (text r) r))
(if filter
(cl-remove-if-not #'(lambda (r) (funcall filter r))
reminders)
reminders)))))
(cl-defmethod slack-team-delete-reminder ((team slack-team) r)
(with-slots (reminders) team
(setq reminders
(cl-remove-if #'(lambda (e)
(string= (oref e id) (oref r id)))
reminders))))
(defun slack-reminder-select (team &optional filter)
(slack-select-from-list
((slack-reminders-alist team filter) "Select: ")))
(defun slack-reminder-delete ()
(interactive)
(let* ((team (slack-team-select))
(reminder (slack-reminder-select team)))
(cl-labels
((on-reminder-delete (&key data &allow-other-keys)
(slack-request-handle-error
(data "slack-reminder-delete")
(slack-team-delete-reminder team reminder)
(message "Reminder Deleted!"))))
(slack-request
(slack-request-create
slack-reminder-delete-url
team
:params (list (cons "reminder" (oref reminder id)))
:success #'on-reminder-delete)))))
(cl-defmethod slack-reminder-info ((r slack-reminder-base) team callback)
(cl-labels
((on-reminder-info (&key data &allow-other-keys)
(slack-request-handle-error
(data "slack-reminder-info")
(let ((reminder (slack-reminder-create
(plist-get (slack-decode data)
:reminder))))
(funcall callback reminder)))))
(slack-request
(slack-request-create
slack-reminder-info-url
team
:params (list (cons "reminder" (oref r id)))
:success #'on-reminder-info))))
(cl-defmethod slack-reminder-refresh ((r slack-reminder-base) team)
(slack-reminder-info
r team
#'(lambda (reminder)
(with-slots (reminders) team
(setq reminders
(cl-remove-if #'(lambda (e) (string= (oref e id)
(oref reminder id)))
reminders))
(push reminder reminders))
(message "Reminder Updated!"))))
(defun slack-reminder-complete ()
(interactive)
(let* ((team (slack-team-select))
(reminder (slack-reminder-select
team
#'(lambda (r)
(not (slack-reminder-completedp r))))))
(cl-labels
((on-reminder-complete (&key data &allow-other-keys)
(slack-request-handle-error
(data "slack-reminder-complete")
(slack-reminder-refresh reminder team))))
(slack-request
(slack-request-create
slack-reminder-complete-url
team
:params (list (cons "reminder" (oref reminder id)))
:success #'on-reminder-complete)))))
(cl-defmethod slack-user-find ((r slack-reminder-base) team)
(slack-user--find (oref r user) team))
(cl-defun slack-reminder-add-from-message (room message team)
(let ((message-channel (oref room id))
(message-ts (slack-ts message))
(respond-in-channel "false")
(time (funcall slack-completing-read-function
"When: "
'("In 20 minutes"
"In 1 hour"
"In 3 hour"
"Tomorrow"
"Next week")
nil t)))
(cl-labels
((success (&key data &allow-other-keys)
(slack-request-handle-error
(data "slack-remind-add-from-message")
(message "DATA: %S" data))))
(slack-request
(slack-request-create
slack-reminder-add-from-message-url
team
:params (list (cons "message_channel" message-channel)
(cons "message_ts" message-ts)
(cons "respond_in_channel" respond-in-channel)
(cons "time" time))
:success #'success)))))
(provide 'slack-reminder)
;;; slack-reminder.el ends here