forked from emacs-slack/emacs-slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack-attachment.el
155 lines (136 loc) · 6.85 KB
/
slack-attachment.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
;;; slack-attachment.el --- -*- lexical-binding: t; -*-
;; Copyright (C) 2017 南優也
;; Author: 南優也 <yuyaminami@minamiyuuya-no-MacBook.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)
(defclass slack-attachment ()
((fallback :initarg :fallback :initform nil)
(title :initarg :title :initform nil)
(title-link :initarg :title_link :initform nil)
(pretext :initarg :pretext :initform nil)
(text :initarg :text :initform nil)
(author-name :initarg :author_name :initform nil)
(author-link :initarg :author_link)
(author-icon :initarg :author_icon)
(fields :initarg :fields :initform '())
(image-url :initarg :image_url :initform nil)
(image-width :initarg :image_width :initform nil)
(image-height :initarg :image_height :initform nil)
(thumb-url :initarg :thumb_url)
(is-share :initarg :is_share :initform nil)
(footer :initarg :footer :initform nil)
(color :initarg :color :initform nil)
(ts :initarg :ts :initform nil)
(author-subname :initarg :author_subname :initform nil)))
(defclass slack-shared-message (slack-attachment)
((channel-id :initarg :channel_id :initform nil)
(channel-name :initarg :channel_name :initform nil)
(from-url :initarg :from_url :initform nil)))
(defmethod slack-message-to-string ((attachment slack-attachment) image-renderer)
(with-slots
(fallback text ts color from-url footer fields pretext) attachment
(let* ((pad-raw (propertize "|" 'face 'slack-attachment-pad))
(pad (or (and color (propertize pad-raw 'face (list :foreground (concat "#" color))))
pad-raw))
(header-raw (slack-attachment-header attachment))
(header (and (not (slack-string-blankp header-raw))
(format "%s\t%s" pad
(propertize header-raw
'face 'slack-attachment-header))))
(pretext (and pretext (format "%s\t%s" pad pretext)))
(body (and text (format "%s\t%s" pad (mapconcat #'identity
(split-string text "\n")
(format "\n\t%s\t" pad)))))
(fields (if fields (mapconcat #'(lambda (field)
(slack-attachment-field-to-string field
(format "\t%s" pad)))
fields
(format "\n\t%s\n" pad))))
(footer (if footer
(format "%s\t%s"
pad
(propertize
(format "%s%s" footer
(or (and ts (format "|%s" (slack-message-time-to-string ts)))
""))
'face 'slack-attachment-footer))))
(image (when (functionp image-renderer)
(funcall image-renderer attachment))))
(if (and (slack-string-blankp header)
(slack-string-blankp pretext)
(slack-string-blankp body)
(slack-string-blankp fields)
(slack-string-blankp footer))
fallback
(format "%s%s%s%s%s%s"
(or (and header (format "\t%s\n" header)) "")
(or (and pretext (format "\t%s\n" pretext)) "")
(or (and body (format "\t%s" body)) "")
(or (and fields fields) "")
(or (and footer (format "\n\t%s" footer)) "")
(or (and image (format "\n%s" image)) ""))
))))
(defmethod slack-message-has-imagep ((attachment slack-attachment))
(oref attachment image-url))
(cl-defmethod slack-image-create ((attachment slack-attachment) &key success error)
(with-slots (image-url image-height image-width) attachment
(when image-url
(let ((path (slack-image-path image-url)))
(when path
(cl-labels
((create-image () (slack-image--create path :height image-height :width image-width))
(on-success () (funcall success (create-image)))
(on-error () (funcall error (create-image))))
(if (file-exists-p path) (create-image)
(progn
(slack-url-copy-file image-url path
:success #'on-success
:error #'on-error)
nil))))))))
(defmethod slack-attachment-header ((attachment slack-attachment))
(with-slots (title title-link author-name author-subname) attachment
(concat (or (and title title-link (slack-linkfy title title-link))
title
"")
(or author-name author-subname ""))))
(defmethod slack-attachment-field-to-string ((field slack-attachment-field) &optional pad)
(unless pad (setq pad ""))
(let ((title (propertize (or (oref field title) "") 'face 'slack-attachment-field-title))
(value (mapconcat #'(lambda (e) (format "\t%s" e))
(split-string (or (oref field value) "") "\n")
(format "\n%s\t" pad))))
(format "%s\t%s\n%s\t%s" pad title pad value)))
(defmethod slack-attachment-to-alert ((a slack-attachment))
(oref a fallback))
(defmethod slack-open-image ((attachment slack-attachment) team)
(cl-labels
((render (image)
(funcall slack-buffer-function
(slack-render-image image team))))
(render (slack-image-create attachment
:success #'render
:error #'render))))
(defmethod slack-message-view-image-to-string ((attachment slack-attachment) team)
(and (slack-message-has-imagep attachment)
(cl-labels
((open-image () (interactive) (slack-open-image attachment team)))
(propertize "[View Image]"
'face '(:underline t)
'keymap (let ((keymap (make-sparse-keymap)))
(define-key keymap (kbd "RET") #'open-image)
keymap)))))
(provide 'slack-attachment)
;;; slack-attachment.el ends here