forked from emacs-slack/emacs-slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack-room-history.el
185 lines (152 loc) · 7.26 KB
/
slack-room-history.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
;;; slack-room-history.el --- impl for room's prev messages -*- lexical-binding: t; -*-
;; Copyright (C) 2017 南優也
;; 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-room)
(require 'slack-search)
(require 'slack-buffer)
(require 'slack-team)
(defvar slack-current-room-id)
(defvar slack-current-team-id)
(defconst slack-channel-history-url "https://slack.com/api/channels.history")
(defconst slack-group-history-url "https://slack.com/api/groups.history")
(defconst slack-im-history-url "https://slack.com/api/im.history")
(defclass _slack-room-history ()
((room :initarg :room)
(team :initarg :team)
(oldest :initarg :oldest)
(current-ts :initarg :current-ts)))
(defclass _slack-search-history (_slack-room-history)
((channel-id :initarg :channel-id)))
(defclass _slack-file-search-history (_slack-room-history) ())
(defmethod slack-room-propertize-load-more ((room slack-room) base-text)
(propertize base-text
'oldest (oref (oref room oldest) ts)
'class '_slack-room-history))
(defmethod slack-room-propertize-load-more ((room slack-search-result) base-text)
(with-slots (oldest) room
(with-slots (info ts) oldest
(propertize base-text
'oldest ts
'channel-id (oref info channel-id)
'class '_slack-search-history))))
(defmethod slack-room-propertize-load-more ((room slack-file-search-result) base-text)
(propertize base-text
'oldest (oref (oref room oldest) ts)
'class '_slack-file-search-history))
(defmethod slack-room-insert-previous-link ((room slack-room) buf)
(with-current-buffer buf
(slack-buffer-widen
(let* ((inhibit-read-only t)
(base (propertize "(load more message)"
'face '(:underline t)
'keymap (let ((map (make-sparse-keymap)))
(define-key map (kbd "RET")
#'slack-room-history-load)
map)))
(text (slack-room-propertize-load-more room base)))
(goto-char (point-min))
(insert (format "%s\n\n" text))
(set-marker lui-output-marker (point))))))
(defmethod slack-room-history--collect-meta ((this _slack-room-history))
this)
(defmethod slack-room-history--collect-meta ((this _slack-search-history))
(oset this channel-id (get-text-property 0 'channel-id (thing-at-point 'line))))
(defmethod slack-room-history--insert ((this _slack-room-history))
(with-slots (team room oldest current-ts) this
(slack-room-with-buffer room team
(slack-buffer-widen
(let ((inhibit-read-only t)
(loading-message-end (slack-buffer-ts-eq (point-min) (point-max) oldest))
(messages (slack-room-history--collect-message this)))
(goto-char (point-min))
(delete-region (point-min) loading-message-end)
(if (and messages (< 0 (length messages)))
(slack-room-insert-previous-link room buf)
(set-marker lui-output-marker (point-min))
(lui-insert "(no more messages)\n"))
(cl-loop for m in messages
do (slack-buffer-insert m team t))))
(lui-recover-output-marker)
(slack-buffer-goto current-ts))))
(defmethod slack-room-history--request ((this _slack-room-history))
(with-slots (team room oldest) this
(slack-room-history-request room team :oldest oldest)))
(defmethod slack-room-history--collect-message ((this _slack-room-history))
(with-slots (team room oldest) this
(cl-remove-if #'(lambda (m)
(or (string< oldest (oref m ts))
(string= oldest (oref m ts))))
(slack-room-sort-messages (copy-sequence (oref room messages))))))
(defmethod slack-room-history--collect-message ((this _slack-search-history))
(with-slots (team room oldest channel-id) this
(let* ((messages (reverse (oref room messages)))
(nth (slack-search-get-index room messages oldest channel-id)))
(if nth
(nreverse (nthcdr (1+ nth) messages))))))
(defmethod slack-room-history--collect-message ((this _slack-file-search-history))
(with-slots (team room oldest) this
(let* ((messages (reverse (oref room messages)))
(nth (slack-search-get-index room messages oldest)))
(if nth
(nreverse (nthcdr (1+ nth) messages))))))
(defun slack-room-history-load ()
(interactive)
(let* ((cur-point (point))
(class (get-text-property 0 'class (thing-at-point 'line)))
(team (slack-team-find slack-current-team-id))
(prev-messages (make-instance class
:room (slack-room-find slack-current-room-id team)
:team team
:oldest (get-text-property 0 'oldest (thing-at-point 'line))
:current-ts (get-text-property (next-single-property-change cur-point
'ts)
'ts))))
(slack-room-history--collect-meta prev-messages)
(slack-room-history--request prev-messages)
(slack-room-history--insert prev-messages)))
(defmethod slack-room-history-url ((_room slack-channel))
slack-channel-history-url)
(defmethod slack-room-history-url ((_room slack-group))
slack-group-history-url)
(defmethod slack-room-history-url ((_room slack-im))
slack-im-history-url)
(cl-defmethod slack-room-history-request ((room slack-room) team &key oldest after-success async)
(cl-labels
((on-request-update
(&key data &allow-other-keys)
(slack-request-handle-error
(data "slack-room-request-update")
(let* ((datum (plist-get data :messages))
(messages
(cl-loop for data across datum
collect (slack-message-create data team :room room))))
(if oldest
(slack-room-set-prev-messages room messages)
(slack-room-set-messages room messages)
(slack-room-reset-last-read room))
(if (and after-success (functionp after-success))
(funcall after-success))))))
(slack-request
(slack-room-history-url room)
team
:params (list (cons "channel" (oref room id))
(if oldest (cons "latest" oldest)))
:success #'on-request-update
:sync (not async))))
(provide 'slack-room-history)
;;; slack-room-history.el ends here