perf(ui): remove redundant history refresh after send_post
The message now appears automatically via WebSocket fan-out, eliminating the extra get_history round-trip.
This commit is contained in:
@@ -68,8 +68,11 @@
|
||||
(time-str (if create-at
|
||||
(format-time-string "%H:%M" (seconds-to-time (/ create-at 1000)))
|
||||
"??:??"))
|
||||
(user-id (or (plist-get post :user_id) "unknown")))
|
||||
(insert (format "[%s] %s: %s\n" time-str user-id msg-text))))
|
||||
;; Используем user_display_name, если есть, иначе user_id
|
||||
(sender (or (plist-get post :user_display_name)
|
||||
(plist-get post :user_id)
|
||||
"unknown")))
|
||||
(insert (format "[%s] %s: %s\n" time-str sender msg-text))))
|
||||
|
||||
(insert "\n")
|
||||
(goto-char (point-max))))
|
||||
@@ -81,7 +84,8 @@
|
||||
(mattermost-channel-load-history))
|
||||
|
||||
(defun mattermost-channel-send-message ()
|
||||
"Send a message to the current channel."
|
||||
"Send a message to the current channel.
|
||||
The message will appear automatically via WebSocket fan-out; no refresh needed."
|
||||
(interactive)
|
||||
(let* ((server-id mattermost--server-id)
|
||||
(channel-id mattermost--channel-id)
|
||||
@@ -97,9 +101,29 @@
|
||||
:message msg)
|
||||
(lambda (response)
|
||||
(if (plist-get response :ok)
|
||||
(progn
|
||||
(message "[MM] Message sent successfully.")
|
||||
(mattermost-channel-refresh))
|
||||
(message "[MM] Message sent. Waiting for WS echo...")
|
||||
(message "[MM] Failed to send message: %S" (plist-get response :error)))))))
|
||||
|
||||
(defun mattermost-channel-on-posted (params)
|
||||
"Handle a 'posted' event. PARAMS should contain the 'post' plist."
|
||||
(let* ((post (plist-get params :post))
|
||||
(channel-id (plist-get post :channel_id))
|
||||
(msg-text (or (plist-get post :message) ""))
|
||||
(create-at (plist-get post :create_at))
|
||||
(time-str (if create-at
|
||||
(format-time-string "%H:%M" (seconds-to-time (/ create-at 1000)))
|
||||
"??:??"))
|
||||
(sender (or (plist-get post :user_display_name)
|
||||
(plist-get post :user_id)
|
||||
"unknown")))
|
||||
(dolist (buf (buffer-list))
|
||||
(with-current-buffer buf
|
||||
(when (and (eq major-mode 'mattermost-channel-mode)
|
||||
(string= mattermost--channel-id channel-id))
|
||||
(let ((inhibit-read-only t))
|
||||
(goto-char (point-max))
|
||||
(insert (format "[%s] %s: %s\n" time-str sender msg-text))
|
||||
(when (= (point) (point-max))
|
||||
(goto-char (point-max)))))))))
|
||||
|
||||
(provide 'mattermost-channel)
|
||||
|
||||
@@ -25,28 +25,21 @@
|
||||
mattermost-bridge-host
|
||||
mattermost-bridge-port))
|
||||
|
||||
;; Очищаем буфер при новом подключении
|
||||
(setq mattermost--input-buffer "")
|
||||
|
||||
(set-process-filter mattermost--process #'mattermost--process-filter)
|
||||
(message "[MM-PROTOCOL] Connected to Mattermost bridge."))
|
||||
|
||||
(defun mattermost--process-filter (_proc output)
|
||||
"Handle OUTPUT from the bridge process with proper chunk buffering."
|
||||
;; Добавляем новые данные к накопителю
|
||||
(setq mattermost--input-buffer (concat mattermost--input-buffer output))
|
||||
|
||||
;; Разбиваем по переносам строк
|
||||
(let ((lines (split-string mattermost--input-buffer "\n" t)))
|
||||
;; Последняя строка может быть неполной — сохраняем её на следующий вызов
|
||||
(if (string-suffix-p "\n" output)
|
||||
(setq mattermost--input-buffer "")
|
||||
(setq mattermost--input-buffer (car (last lines)))
|
||||
;; Убираем последний (неполный) элемент из списка для обработки
|
||||
(setq lines (butlast lines)))
|
||||
|
||||
(dolist (line lines)
|
||||
;; ДОБАВЛЕНО: :array-type 'list чтобы JSON-массивы становились списками, а не векторами
|
||||
(let ((msg (ignore-errors (json-parse-string line :object-type 'plist :array-type 'list))))
|
||||
(if msg
|
||||
(let ((id (plist-get msg :id))
|
||||
@@ -67,13 +60,10 @@
|
||||
(defun mattermost--send-request (method params callback)
|
||||
"Send a request with METHOD and PARAMS, calling CALLBACK on response."
|
||||
(let* ((id (format "req-%d" (random 100000)))
|
||||
;; 1. Создаем реальный payload для отправки
|
||||
(payload (list :id id :type "request" :method method :params params))
|
||||
(json-str (concat (json-encode payload) "\n"))
|
||||
;; 2. Создаем копию ТОЛЬКО для логирования
|
||||
(safe-params (copy-tree params)))
|
||||
|
||||
;; Маскируем пароль в копии для лога
|
||||
(when (plist-get safe-params :password)
|
||||
(plist-put safe-params :password "***MASKED***"))
|
||||
|
||||
@@ -81,12 +71,16 @@
|
||||
(message "[MM-PROTOCOL] Sending request id=%s method=%s" id method))
|
||||
|
||||
(puthash id callback mattermost--pending-requests)
|
||||
;; Отправляем реальный json-str с настоящим паролем!
|
||||
(process-send-string mattermost--process json-str)))
|
||||
|
||||
(defun mattermost--handle-event (_msg)
|
||||
(defun mattermost--handle-event (msg)
|
||||
"Handle an asynchronous event MSG from the bridge."
|
||||
;; TODO: Fan out events to buffers
|
||||
)
|
||||
(let ((method (plist-get msg :method))
|
||||
(params (plist-get msg :params)))
|
||||
(cond
|
||||
((string= method "posted")
|
||||
(mattermost-channel-on-posted params))
|
||||
(t
|
||||
(message "[MM-DEBUG] Unhandled event method: %s" method)))))
|
||||
|
||||
(provide 'mattermost-protocol)
|
||||
|
||||
Reference in New Issue
Block a user