Skip to content

Commit 49a6206

Browse files
committed
add slack-room-history
1 parent ec754c8 commit 49a6206

10 files changed

+201
-143
lines changed

slack-buffer.el

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -91,36 +91,6 @@
9191
(when point
9292
(goto-char point))))
9393

94-
(defmethod slack-buffer-insert-previous-link ((room slack-room))
95-
(let ((oldest (slack-room-prev-link-info room)))
96-
(if oldest
97-
(slack-buffer-widen
98-
(let ((inhibit-read-only t))
99-
(goto-char (point-min))
100-
(insert
101-
(concat
102-
(propertize "(load more message)"
103-
'face '(:underline t)
104-
'oldest oldest
105-
'keymap (let ((map (make-sparse-keymap)))
106-
(define-key map (kbd "RET")
107-
#'slack-room-load-prev-messages)
108-
map))
109-
"\n\n"))
110-
(set-marker lui-output-marker (point)))))))
111-
112-
(defmethod slack-buffer-insert-prev-messages ((room slack-room) team oldest-ts)
113-
(slack-buffer-widen
114-
(let ((messages (slack-room-prev-messages room oldest-ts)))
115-
(if messages
116-
(progn
117-
(slack-buffer-insert-previous-link room)
118-
(cl-loop for m in messages
119-
do (slack-buffer-insert m team t)))
120-
(set-marker lui-output-marker (point-min))
121-
(lui-insert "(no more messages)\n"))
122-
(lui-recover-output-marker))))
123-
12494
(cl-defun slack-buffer-create (room team)
12595
(let ((buffer (slack-get-buffer-create room)))
12696
(with-current-buffer buffer

slack-channel.el

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
(defvar slack-buffer-function)
3333

34-
(defconst slack-channel-history-url "https://slack.com/api/channels.history")
3534
(defconst slack-channel-list-url "https://slack.com/api/channels.list")
3635
(defconst slack-channel-buffer-name "*Slack - Channel*")
3736
(defconst slack-channel-update-mark-url "https://slack.com/api/channels.mark")
@@ -217,9 +216,6 @@
217216
team
218217
#'on-channel-unarchive))))
219218

220-
(defmethod slack-room-history-url ((_room slack-channel))
221-
slack-channel-history-url)
222-
223219
(defmethod slack-room-subscribedp ((room slack-channel) team)
224220
(with-slots (subscribed-channels) team
225221
(let ((name (slack-room-name room)))

slack-file.el

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,8 @@
205205
(with-slots (messages) room
206206
(if messages
207207
(slack-file-create-buffer team)
208-
(slack-room-history room team nil
209-
#'(lambda ()
210-
(slack-file-create-buffer team)))))))
208+
(slack-room-history-request room team)
209+
(slack-file-create-buffer team)))))
211210

212211
(defmethod slack-room-history ((room slack-file-room) team
213212
&optional

slack-group.el

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
(require 'slack-buffer)
3131

3232
(defconst slack--group-open-url "https://slack.com/api/groups.open")
33-
(defconst slack-group-history-url "https://slack.com/api/groups.history")
3433
(defconst slack-group-buffer-name "*Slack - Private Group*")
3534
(defconst slack-group-list-url "https://slack.com/api/groups.list")
3635
(defconst slack-group-update-mark-url "https://slack.com/api/groups.mark")
@@ -242,7 +241,7 @@
242241
(slack-room-equal-p group g))
243242
groups)))
244243
(if (plist-get data :already_closed)
245-
(message "Direct Message Channel with %s Already Closed"
244+
(message "Direct Message Channel with %s Already Closed"
246245
(slack-group-members-s group)))))))
247246
(slack-request
248247
slack-mpim-close-url
@@ -252,12 +251,9 @@
252251
:success #'on-success
253252
:sync nil)))))
254253

255-
254+
256255
(defmethod slack-mpim-p ((room slack-group))
257256
(oref room is-mpim))
258257

259-
(defmethod slack-room-history-url ((_room slack-group))
260-
slack-group-history-url)
261-
262258
(provide 'slack-group)
263259
;;; slack-group.el ends here

slack-im.el

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232

3333
(defvar slack-buffer-function)
3434

35-
(defconst slack-im-history-url "https://slack.com/api/im.history")
3635
(defconst slack-im-buffer-name "*Slack - Direct Messages*")
3736
(defconst slack-user-list-url "https://slack.com/api/users.list")
3837
(defconst slack-im-list-url "https://slack.com/api/im.list")
@@ -127,9 +126,6 @@
127126
(defmethod slack-room-update-mark-url ((_room slack-im))
128127
slack-im-update-mark-url)
129128

130-
(defmethod slack-room-history-url ((_room slack-im))
131-
slack-im-history-url)
132-
133129
(defun slack-im-close ()
134130
(interactive)
135131
(let* ((team (slack-team-select))

slack-room-history.el

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
;;; slack-room-history.el --- impl for room's prev messages -*- lexical-binding: t; -*-
2+
3+
;; Copyright (C) 2017 南優也
4+
5+
;; Author: 南優也 <yuyaminami@minamiyuuya-no-MacBook.local>
6+
;; Keywords:
7+
8+
;; This program is free software; you can redistribute it and/or modify
9+
;; it under the terms of the GNU General Public License as published by
10+
;; the Free Software Foundation, either version 3 of the License, or
11+
;; (at your option) any later version.
12+
13+
;; This program is distributed in the hope that it will be useful,
14+
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
;; GNU General Public License for more details.
17+
18+
;; You should have received a copy of the GNU General Public License
19+
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
21+
;;; Commentary:
22+
23+
;;
24+
25+
;;; Code:
26+
27+
(require 'eieio)
28+
(require 'slack-room)
29+
(require 'slack-search)
30+
(require 'slack-buffer)
31+
(require 'slack-team)
32+
33+
(defvar slack-current-room-id)
34+
(defvar slack-current-team-id)
35+
(defconst slack-channel-history-url "https://slack.com/api/channels.history")
36+
(defconst slack-group-history-url "https://slack.com/api/groups.history")
37+
(defconst slack-im-history-url "https://slack.com/api/im.history")
38+
39+
(defclass _slack-room-history ()
40+
((room :initarg :room)
41+
(team :initarg :team)
42+
(oldest :initarg :oldest)
43+
(current-ts :initarg :current-ts)))
44+
45+
(defclass _slack-search-history (_slack-room-history)
46+
((channel-id :initarg :channel-id)))
47+
48+
(defclass _slack-file-search-history (_slack-room-history) ())
49+
50+
(defmethod slack-room-propertize-load-more ((room slack-room) base-text)
51+
(propertize base-text
52+
'oldest (oref (oref room oldest) ts)
53+
'class '_slack-room-history))
54+
55+
(defmethod slack-room-propertize-load-more ((room slack-search-result) base-text)
56+
(with-slots (oldest) room
57+
(with-slots (info ts) oldest
58+
(propertize base-text
59+
'oldest ts
60+
'channel-id (oref info channel-id)
61+
'class '_slack-search-history))))
62+
63+
(defmethod slack-room-propertize-load-more ((room slack-file-search-result) base-text)
64+
(propertize base-text
65+
'oldest (oref (oref room oldest) ts)
66+
'class '_slack-file-search-history))
67+
68+
(defmethod slack-room-insert-previous-link ((room slack-room) buf)
69+
(with-current-buffer buf
70+
(slack-buffer-widen
71+
(let* ((inhibit-read-only t)
72+
(base (propertize "(load more message)"
73+
'face '(:underline t)
74+
'keymap (let ((map (make-sparse-keymap)))
75+
(define-key map (kbd "RET")
76+
#'slack-room-history-load)
77+
map)))
78+
(text (slack-room-propertize-load-more room base)))
79+
(goto-char (point-min))
80+
(insert (format "%s\n\n" text))
81+
(set-marker lui-output-marker (point))))))
82+
83+
(defmethod slack-room-history--collect-meta ((this _slack-room-history))
84+
this)
85+
86+
(defmethod slack-room-history--collect-meta ((this _slack-search-history))
87+
(oset this channel-id (get-text-property 0 'channel-id (thing-at-point 'line))))
88+
89+
(defmethod slack-room-history--insert ((this _slack-room-history))
90+
(with-slots (team room oldest current-ts) this
91+
(slack-room-with-buffer room team
92+
(slack-buffer-widen
93+
(let ((inhibit-read-only t)
94+
(loading-message-end (slack-buffer-ts-eq (point-min) (point-max) oldest))
95+
(messages (slack-room-history--collect-message this)))
96+
(goto-char (point-min))
97+
(delete-region (point-min) loading-message-end)
98+
99+
(if (and messages (< 0 (length messages)))
100+
(slack-room-insert-previous-link room buf)
101+
(set-marker lui-output-marker (point-min))
102+
(lui-insert "(no more messages)\n"))
103+
104+
(cl-loop for m in messages
105+
do (slack-buffer-insert m team t))))
106+
107+
(lui-recover-output-marker)
108+
(slack-buffer-goto current-ts))))
109+
110+
(defmethod slack-room-history--request ((this _slack-room-history))
111+
(with-slots (team room oldest) this
112+
(slack-room-history-request room team :oldest oldest)))
113+
114+
(defmethod slack-room-history--collect-message ((this _slack-room-history))
115+
(with-slots (team room oldest) this
116+
(cl-remove-if #'(lambda (m)
117+
(or (string< oldest (oref m ts))
118+
(string= oldest (oref m ts))))
119+
(slack-room-sort-messages (copy-sequence (oref room messages))))))
120+
121+
(defmethod slack-room-history--collect-message ((this _slack-search-history))
122+
(with-slots (team room oldest channel-id) this
123+
(let* ((messages (reverse (oref room messages)))
124+
(nth (slack-search-get-index room messages oldest channel-id)))
125+
(if nth
126+
(nreverse (nthcdr (1+ nth) messages))))))
127+
128+
(defmethod slack-room-history--collect-message ((this _slack-file-search-history))
129+
(with-slots (team room oldest) this
130+
(let* ((messages (reverse (oref room messages)))
131+
(nth (slack-search-get-index room messages oldest)))
132+
(if nth
133+
(nreverse (nthcdr (1+ nth) messages))))))
134+
135+
(defun slack-room-history-load ()
136+
(interactive)
137+
(let* ((cur-point (point))
138+
(class (get-text-property 0 'class (thing-at-point 'line)))
139+
(team (slack-team-find slack-current-team-id))
140+
(prev-messages (make-instance class
141+
:room (slack-room-find slack-current-room-id team)
142+
:team team
143+
:oldest (get-text-property 0 'oldest (thing-at-point 'line))
144+
:current-ts (get-text-property (next-single-property-change cur-point
145+
'ts)
146+
'ts))))
147+
(slack-room-history--collect-meta prev-messages)
148+
(slack-room-history--request prev-messages)
149+
(slack-room-history--insert prev-messages)))
150+
151+
(defmethod slack-room-history-url ((_room slack-channel))
152+
slack-channel-history-url)
153+
154+
(defmethod slack-room-history-url ((_room slack-group))
155+
slack-group-history-url)
156+
157+
(defmethod slack-room-history-url ((_room slack-im))
158+
slack-im-history-url)
159+
160+
(defmethod slack-room-history-request ((room slack-room) team &key oldest after-success async)
161+
(cl-labels
162+
((on-request-update
163+
(&key data &allow-other-keys)
164+
(slack-request-handle-error
165+
(data "slack-room-request-update")
166+
(let* ((datum (plist-get data :messages))
167+
(messages
168+
(cl-loop for data across datum
169+
collect (slack-message-create data team :room room))))
170+
(if oldest
171+
(slack-room-set-prev-messages room messages)
172+
(slack-room-set-messages room messages)
173+
(slack-room-reset-last-read room))
174+
(if (and after-success (functionp after-success))
175+
(funcall after-success))))))
176+
(slack-request
177+
(slack-room-history-url room)
178+
team
179+
:params (list (cons "channel" (oref room id))
180+
(if oldest (cons "latest" oldest)))
181+
:success #'on-request-update
182+
:sync (not async))))
183+
184+
(provide 'slack-room-history)
185+
;;; slack-room-history.el ends here

0 commit comments

Comments
 (0)