|
| 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