blob: f6ac4242e87bbdb35797733a155a747bc26e7163 (
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
|
;;; slack-file.el --- handle files -*- lexical-binding: t; -*-
;; Copyright (C) 2016 南優也
;; 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-request)
(defconst slack-file-history-url "https://slack.com/api/files.list")
(defconst slack-file-list-url "https://slack.com/api/files.list")
(defconst slack-file-upload-url "https://slack.com/api/files.upload")
(defconst slack-file-delete-url "https://slack.com/api/files.delete")
(defclass slack-file ()
((id :initarg :id)
(created :initarg :created)
(name :initarg :name)
(size :initarg :size)
(public :initarg :public)
(filetype :initarg :filetype)
(user :initarg :user)
(preview :initarg :preview)
(permalink :initarg :permalink)
(channels :initarg :channels :type list)
(groups :initarg :groups :type list)
(ims :initarg :ims :type list)
(username :initarg :username)
(page :initarg :page :initform 1)
(pages :initarg :pages :initform nil)
(thumb-64 :initarg :thumb_64 :initform nil)
(thumb-80 :initarg :thumb_80 :initform nil)
(thumb-360 :initarg :thumb_360 :initform nil)
(thumb-360-w :initarg :thumb_360_w :initform nil)
(thumb-360-h :initarg :thumb_360_h :initform nil)
(thumb-160 :initarg :thumb_160 :initform nil)
(thumb-pdf :initarg :thumb_pdf :initform nil)
(thumb-pdf-w :initarg :thumb_pdf_w :initform nil)
(thumb-pdf-h :initarg :thumb_pdf_h :initform nil)
(original-w :initarg :original_w :initform nil)
(original-h :initarg :original_h :initform nil)
(is-starred :initarg :is_starred :initform nil)
(mimetype :initarg :mimetype :type string :initform "")
(title :initarg :title :type (or null string) :initform nil)
(pretty-type :initarg :pretty_type :type (or null string) :initform nil)
(is-public :initarg :is_public :initform nil)
(url :initarg :url :initform "" :type string)
(url-download :initarg :url_download :initform "" :type string)
(url-private :initarg :url_private :initform "" :type string)
(url-private-download :initarg :url_private_download :initform "" :type string)
(timestamp :initarg :timestamp :type number)
(comments :initarg :comments :type list :initform '())
))
(defclass slack-file-email (slack-file)
((from :initarg :from :type (or null list) :initform nil)
(to :initarg :to :type (or null list) :initform nil)
;; TODO verify type
(cc :initarg :cc :type (or null list) :initform nil)
(subject :initarg :subject :type (or null string))
(plain-text :initarg :plain_text :type string)
(preview-plain-text :initarg :preview_plain_text :type string)
(is-expanded :initform nil :type boolean)))
(defclass slack-file-email-from ()
((address :initarg :address :type string)
(name :initarg :name :type string)
(original :initarg :original :type string)))
(defclass slack-file-email-to (slack-file-email-from) ())
(defclass slack-file-email-cc (slack-file-email-from) ())
(defclass slack-file-comment ()
((id :initarg :id :type string)
(created :initarg :created :type number)
(timestamp :initarg :timestamp :type number)
(user :initarg :user :type string)
(is-intro :initarg :is_intro)
(comment :initarg :comment :type string)))
(defmethod slack-merge ((old slack-reaction) new)
(with-slots (count users) old
(setq count (oref new count))
(setq users (cl-remove-duplicates (append users (oref new users))
:test #'string=))))
(defmethod slack-merge ((old string) _new) old)
(defmethod slack-equalp ((old string) new) (string= old new))
(defmethod slack-merge ((old slack-file) new)
(cl-labels
((slack-merge-string-list
(new old)
(cl-remove-duplicates (append new old) :test #'string=)))
(slack-merge-list (oref old channels) (oref new channels))
(slack-merge-list (oref old groups) (oref new groups))
(slack-merge-list (oref old ims) (oref new ims))
))
(defclass slack-file-room (slack-room) ())
(defun slack-file-find (id team)
(let ((files (oref (slack-file-room-obj team) messages)))
(cl-find-if #'(lambda (file) (string= (oref file id) id))
files)))
(defun slack-file-room-obj (team)
(with-slots (file-room) team
(if file-room
file-room
(setq file-room (slack-file-room "file-room"
:name "Files"
:id "F"
:created (format-time-string "%s")
:latest nil
:unread_count 0
:unread_count_display 0
:messages '())))))
(defun slack-file-create-email-from (payload &optional type)
(and payload
(make-instance (or (and (eq type 'to) 'slack-file-email-to)
(and (eq type 'cc) 'slack-file-email-cc)
'slack-file-email-from)
:original (plist-get payload :original)
:name (plist-get payload :name)
:address (plist-get payload :address))))
(defun slack-file-create (payload)
(setq payload (append payload nil))
(plist-put payload :channels (append (plist-get payload :channels) nil))
(plist-put payload :groups (append (plist-get payload :groups) nil))
(plist-put payload :ims (append (plist-get payload :ims) nil))
(plist-put payload :pinned_to (append (plist-get payload :pinned_to) nil))
(plist-put payload :channel "F")
(let* ((file (if (string= "email" (plist-get payload :filetype))
(progn
(plist-put payload :from
(mapcar #'slack-file-create-email-from
(plist-get payload :from)))
(plist-put payload :to
(mapcar #'(lambda (e)
(slack-file-create-email-from e 'to))
(plist-get payload :to)))
(plist-put payload :cc
(mapcar #'(lambda (e)
(slack-file-create-email-from e 'cc))
(plist-get payload :cc)))
(apply #'slack-file-email "file-email"
(slack-collect-slots 'slack-file-email
payload)))
(apply #'slack-file "file"
(slack-collect-slots 'slack-file payload))))
)
file))
(defmethod slack-message-equal ((f slack-file) other)
(string= (oref f id) (oref other id)))
(defmethod slack-equalp ((old slack-file) new)
(string= (oref old id) (oref new id)))
(defmethod slack-file-pushnew ((f slack-file) team)
(let ((room (slack-file-room-obj team)))
(slack-merge-list (oref room messages) (list f))
;; (oset room messages (slack-room-sort-messages (oref room messages)))
;; (slack-room-update-latest room (car (last (oref room messages))))
))
(defconst slack-file-info-url "https://slack.com/api/files.info")
(defun slack-file-update ()
(interactive)
(slack-if-let* ((buf slack-current-buffer))
(with-slots (file team) buf
(slack-if-let* ((page (oref file page)))
(slack-file-request-info
file page team
#'(lambda (file team)
(slack-redisplay file team)))))))
(defun slack-file-comment-create (payload)
(apply 'make-instance 'slack-file-comment
(slack-collect-slots 'slack-file-comment payload)))
(defun slack-file-request-info (file-id page team &optional after-success)
(cl-labels
((on-file-info (&key data &allow-other-keys)
(slack-request-handle-error
(data "slack-file-info")
(let* ((paging (plist-get data :paging))
(file (slack-file-create (plist-get data :file)))
(comments (mapcar #'slack-file-comment-create
(plist-get data :comments))))
(oset file comments comments)
(slack-file-pushnew file team)
(if after-success
(funcall after-success file team))))))
(slack-request
(slack-request-create
slack-file-info-url
team
:params (list (cons "file" file-id)
(cons "page" (number-to-string page)))
:success #'on-file-info))))
(defmethod slack-file-gdoc-p ((this slack-file))
(string= (oref this filetype) "gdoc"))
(defmethod slack-file-gdoc-to-string ((this slack-file))
(with-slots (pretty-type name title url-private permalink) this
(let ((title (propertize (format "<%s|%s>" permalink (or title name))
'face '(:weight bold)))
(description (format "<%s|%s>" url-private pretty-type)))
(slack-format-message title description))))
(defmethod slack-message-body-to-string ((file slack-file) team)
(cond
((slack-file-gdoc-p file) (slack-file-gdoc-to-string file))
(t (with-slots (name title size filetype permalink) file
(slack-message-put-text-property
(format "name: %s\nsize: %s\ntype: %s\n%s\n"
(or title name) size filetype permalink))))))
(defmethod slack-team-display-image-inlinep ((_file slack-file) team)
(slack-team-display-file-image-inlinep team))
(defmethod slack-message-image-to-string ((file slack-file))
(slack-image-string (slack-file-thumb-image-spec file)))
(defmethod slack-file-image-p ((this slack-file))
(string= (car (split-string (oref this mimetype) "/"))
"image"))
(defmethod slack-message-large-image-to-string ((file slack-file))
(slack-image-string (slack-file-image-spec file)))
(defmethod slack-message-to-string ((this slack-file-email) ts team)
(let ((body (slack-file-summary this ts team))
(thumb (slack-image-string (slack-file-thumb-image-spec this))))
(slack-format-message body thumb)))
(defmethod slack-message-to-string ((this slack-file) ts team)
(let ((body (slack-file-summary this ts team))
(thumb (slack-image-string (slack-file-thumb-image-spec this))))
(slack-format-message body thumb)))
(defmethod slack-message-to-string ((this slack-file-comment) team)
(with-slots (user comment) this
(let ((name (slack-user-name user team))
(status (slack-user-status user team)))
(format "%s\n%s\n"
(propertize (format "%s %s" name status)
'face 'slack-message-output-header)
(slack-message-unescape-string comment team)))))
(defun slack-file-list ()
(interactive)
(let* ((team (slack-team-select))
(room (slack-file-room-obj team)))
(slack-room-display room team)))
(cl-defmethod slack-room-history-request ((room slack-file-room) team &key oldest after-success async)
(cl-labels
((on-file-list
(&key data &allow-other-keys)
(slack-request-handle-error
(data "slack-file-list")
(let ((files (cl-loop for e in (plist-get data :files)
collect (slack-file-create e))))
(if oldest
(slack-room-set-prev-messages room files)
(slack-room-set-messages room files)))
(if after-success
(funcall after-success)))))
(slack-request
(slack-request-create
slack-file-list-url
team
:params (list (if oldest
(cons "ts_to" oldest)))
:success #'on-file-list))))
(defun slack-file-select-sharing-channels (current-room-name team)
(cl-labels
((select-channels
(channels acc)
(let ((selected (apply slack-completing-read-function
(if acc
(list "Select another channel (or leave empty): "
(cons "" channels) nil t)
(list "Select channel: " channels nil t current-room-name)))))
(if (< 0 (length selected))
(select-channels (cl-remove-if (lambda (x) (equal selected (car-safe x))) channels)
(cons selected acc))
acc)))
(channel-id (selected channels)
(oref (cdr (cl-assoc selected channels :test #'string=))
id)))
(let* ((channels (slack-room-names
(append (oref team ims)
(oref team channels)
(oref team groups))
team))
(target-channels (select-channels channels '())))
(mapcar #'(lambda (selected) (channel-id selected channels))
(cl-delete-if #'null target-channels)))))
(defun slack-file-select-upload-file-as-buffer ()
(find-file-noselect
(car (find-file-read-args
"Select File: "
(confirm-nonexistent-file-or-buffer)))
t t))
(defun slack-file-upload ()
(interactive)
(slack-if-let*
((slack-buffer slack-current-buffer)
(team (oref slack-buffer team))
(buf (slack-file-select-upload-file-as-buffer))
(filename (read-from-minibuffer "Filename: "
(file-name-nondirectory
(buffer-file-name buf))))
(filetype (read-from-minibuffer "Filetype: "
(file-name-extension
(buffer-file-name buf))))
(initial-comment (read-from-minibuffer "Message: ")))
(cl-labels
((on-file-upload (&key data &allow-other-keys)
(slack-request-handle-error
(data "slack-file-upload"))))
(slack-request
(slack-request-create
slack-file-upload-url
team
:type "POST"
:params (append (slack-file-upload-params slack-buffer)
(list
(cons "filename" filename)
(cons "filetype" filetype)
(if initial-comment
(cons "initial_comment" initial-comment))))
:files (list (cons "file" buf))
:headers (list (cons "Content-Type" "multipart/form-data"))
:success #'on-file-upload)))
(error "Call from message buffer or thread buffer")))
(defun slack-file-delete ()
(interactive)
(cl-labels
((on-file-delete (&key data &allow-other-keys)
(slack-request-handle-error
(data "slack-file-delete"))))
(let* ((team (slack-team-select))
(files (oref (slack-file-room-obj team) messages))
(your-files (cl-remove-if #'(lambda (f)
(not (string= (oref f user)
(oref team self-id))))
files))
(candidates (mapcar #'(lambda (f)
(cons (concat
(slack-message-time-to-string (slack-ts f))
" "
(oref f (or title name)))
f))
your-files))
(selected (funcall slack-completing-read-function "Select File: " candidates))
(deleting-file (cdr (cl-assoc selected candidates :test #'string=))))
(slack-request
(slack-request-create
slack-file-delete-url
team
:params (list (cons "file" (oref deleting-file id)))
:success #'on-file-delete)))))
(defmethod slack-file-id ((file slack-file))
(oref file id))
(defmethod slack-file-channel ((_file slack-file))
nil)
(defmethod slack-file-thumb-image-spec ((file slack-file))
(with-slots (thumb-360 thumb-360-w thumb-360-h thumb-160 thumb-80 thumb-64 thumb-pdf thumb-pdf-w thumb-pdf-h) file
(or (and thumb-360 (list thumb-360 thumb-360-w thumb-360-h))
(and thumb-160 (list thumb-160 nil nil))
(and thumb-80 (list thumb-80 nil nil))
(and thumb-64 (list thumb-64 nil nil))
(and thumb-pdf (list thumb-pdf thumb-pdf-w thumb-pdf-h))
(list nil nil nil))))
(defmethod slack-file-image-spec ((this slack-file))
(with-slots (is-public url-download url-private-download) this
(list url-private-download
nil
nil
nil
(floor (* 0.9 (frame-pixel-width))))))
(defmethod slack-file-channel-ids ((file slack-file))
(append (oref file channels)
(oref file ims)
(oref file groups)))
(defun slack-file-link-info (file-id text)
(propertize text
'file file-id
'face '(:underline t :weight bold)
'keymap (let ((map (make-sparse-keymap)))
(define-key map (kbd "RET")
#'slack-file-display)
map)))
(defmethod slack-file-summary ((file slack-file) _ts team)
(with-slots (pretty-type mimetype permalink name title) file
(format "uploaded this %s: %s <%s|open in browser>"
(or pretty-type mimetype)
(slack-file-link-info (oref file id)
(slack-message-unescape-string (or title name)
team))
permalink)))
(defmethod slack-file-summary ((this slack-file-email) ts team)
(with-slots (preview-plain-text plain-text is-expanded) this
(let* ((has-more (< (length preview-plain-text)
(length plain-text)))
(body (slack-message-unescape-string
(or (and is-expanded plain-text)
(or (and has-more (format "%s…" preview-plain-text))
preview-plain-text))
team)))
(format "%s\n\n%s\n\n%s"
(call-next-method)
(propertize body
'slack-defer-face #'slack-put-preview-overlay)
(propertize (or (and is-expanded "Collapse ↑")
"+ Click to expand inline")
'face '(:underline t)
'keymap (let ((map (make-sparse-keymap)))
(define-key map (kbd "RET")
#'(lambda ()
(interactive)
(slack-buffer-toggle-email-expand
slack-current-buffer
ts (oref this id))))
map))))))
(defmacro slack-with-file (id team &rest body)
(declare (indent 2) (debug t))
`(cl-loop for file in (oref (slack-file-room-obj ,team) messages)
do (when (string= (oref file id) ,id)
,@body)))
(defmethod slack-room-buffer-name ((this slack-file) _team)
(with-slots (name) this
(format "*Slack File - %s*" name)))
(define-derived-mode slack-file-info-mode lui-mode "Slack File Info"
""
(setq-local default-directory slack-default-directory)
)
(defun slack-file-display ()
(interactive)
(slack-if-let* ((id (get-text-property (point) 'file))
(buf slack-current-buffer))
(slack-buffer-display-file buf id)))
(defmethod slack-message-star-added ((this slack-file))
(oset this is-starred t))
(defmethod slack-message-star-removed ((this slack-file))
(oset this is-starred nil))
(defmethod slack-message-star-api-params ((this slack-file))
(cons "file" (oref this id)))
(defun slack-file-process-star-api (url team file-id)
(slack-with-file file-id team
(slack-message-star-api-request url
(list (slack-message-star-api-params file))
team)))
(defmethod slack-message-body-to-string ((this slack-file-email) _team)
(let ((from (format "From: %s" (mapconcat #'(lambda (e) (oref e original))
(oref this from)
", ")))
(to (format "To: %s" (mapconcat #'(lambda (e) (oref e original))
(oref this to)
", ")))
(cc (format "CC: %s" (mapconcat #'(lambda (e) (oref e original))
(oref this cc)
", ")))
(subject (format "Subject: %s" (oref this subject)))
(body (propertize (format "\n%s" (oref this plain-text))
'slack-defer-face #'slack-put-email-body-overlay))
(date (format "Date: %s" (slack-message-time-to-string (oref this created)))))
(mapconcat #'identity
(list from to cc subject date body)
"\n")))
(defun slack-redisplay (file team)
;; (slack-if-let* ((buffer (slack-buffer-find 'slack-file-info-buffer file team)))
;; (slack-buffer--replace buffer nil))
(slack-if-let* ((buffer (slack-buffer-find 'slack-file-list-buffer
(slack-file-room-obj team)
team)))
(slack-buffer-replace buffer file)))
(defmethod slack-message-update ((this slack-file) team &rest _args)
(slack-if-let* ((buffer (slack-buffer-find 'slack-file-info-buffer
this
team)))
(progn
(oset buffer file this)
(slack-buffer-update buffer))))
(defmethod slack-ts ((this slack-file))
(number-to-string (oref this timestamp)))
(defmethod slack-thread-message-p ((this slack-file))
nil)
(provide 'slack-file)
;;; slack-file.el ends here
|