612456334f
- mattermost-dashboard-mode: list channels from bridge - mattermost-channel-mode: view history and send messages - Interactive connection prompt (password in memory only) - Fix JSON array parsing (vectors → lists) in protocol layer - Add mattermost-reset-state for clean reconnects - Add dev-check.sh for quick Rust validation - Verified end-to-end: Emacs → Bridge → Mattermost → back
106 lines
3.8 KiB
EmacsLisp
106 lines
3.8 KiB
EmacsLisp
;;; mattermost-channel.el --- Channel buffer UI -*- lexical-binding: t; -*-
|
|
|
|
(require 'mattermost-custom)
|
|
(require 'mattermost-protocol)
|
|
|
|
(defvar mattermost-channel-mode-map
|
|
(let ((map (make-sparse-keymap)))
|
|
(define-key map (kbd "C-c C-s") #'mattermost-channel-send-message)
|
|
(define-key map (kbd "g") #'mattermost-channel-refresh)
|
|
map)
|
|
"Keymap for `mattermost-channel-mode`.")
|
|
|
|
(define-derived-mode mattermost-channel-mode special-mode "Mattermost-Channel"
|
|
"Major mode for viewing a Mattermost channel.
|
|
\\{mattermost-channel-mode-map}"
|
|
(setq buffer-read-only t)
|
|
(setq truncate-lines t))
|
|
|
|
(defvar-local mattermost--channel-id nil
|
|
"Channel ID for the current buffer.")
|
|
|
|
(defvar-local mattermost--server-id nil
|
|
"Server ID for the current buffer.")
|
|
|
|
(defvar-local mattermost--channel-name nil
|
|
"Display name of the current channel.")
|
|
|
|
(defun mattermost-channel-open (server-id channel-id channel-name)
|
|
"Open a buffer for CHANNEL-ID on SERVER-ID with CHANNEL-NAME."
|
|
(let ((buf-name (format "*MM: %s*" channel-name)))
|
|
(let ((buf (get-buffer-create buf-name)))
|
|
(with-current-buffer buf
|
|
(mattermost-channel-mode)
|
|
(setq-local mattermost--server-id server-id)
|
|
(setq-local mattermost--channel-id channel-id)
|
|
(setq-local mattermost--channel-name channel-name)
|
|
(mattermost-channel-load-history))
|
|
(switch-to-buffer buf))))
|
|
|
|
(defun mattermost-channel-load-history ()
|
|
"Load and display message history for the current channel."
|
|
(let ((server-id mattermost--server-id)
|
|
(channel-id mattermost--channel-id)
|
|
(channel-name mattermost--channel-name))
|
|
(message "[MM-DEBUG] Loading history for channel %s..." channel-id)
|
|
(mattermost--send-request
|
|
"get_history"
|
|
(list :server_id server-id
|
|
:channel_id channel-id
|
|
:limit 50)
|
|
(lambda (response)
|
|
(if (plist-get response :ok)
|
|
(mattermost--render-channel-history
|
|
channel-name
|
|
(plist-get (plist-get response :result) :posts))
|
|
(message "[MM] Failed to load history: %S" (plist-get response :error)))))))
|
|
|
|
(defun mattermost--render-channel-history (channel-name posts)
|
|
"Render POSTS in the current channel buffer with CHANNEL-NAME."
|
|
(let ((inhibit-read-only t))
|
|
(erase-buffer)
|
|
(insert (format "Channel: %s\n" channel-name))
|
|
(insert "========================================\n\n")
|
|
|
|
(dolist (post posts)
|
|
(let* ((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)))
|
|
"??:??"))
|
|
(user-id (or (plist-get post :user_id) "unknown")))
|
|
(insert (format "[%s] %s: %s\n" time-str user-id msg-text))))
|
|
|
|
(insert "\n")
|
|
(goto-char (point-max))))
|
|
|
|
(defun mattermost-channel-refresh ()
|
|
"Reload message history for the current channel."
|
|
(interactive)
|
|
(message "[MM-DEBUG] Refreshing channel...")
|
|
(mattermost-channel-load-history))
|
|
|
|
(defun mattermost-channel-send-message ()
|
|
"Send a message to the current channel."
|
|
(interactive)
|
|
(let* ((server-id mattermost--server-id)
|
|
(channel-id mattermost--channel-id)
|
|
(msg (read-string "Message: ")))
|
|
(when (string-empty-p msg)
|
|
(error "Message cannot be empty"))
|
|
|
|
(message "[MM-DEBUG] Sending message to channel %s..." channel-id)
|
|
(mattermost--send-request
|
|
"send_post"
|
|
(list :server_id server-id
|
|
:channel_id channel-id
|
|
:message msg)
|
|
(lambda (response)
|
|
(if (plist-get response :ok)
|
|
(progn
|
|
(message "[MM] Message sent successfully.")
|
|
(mattermost-channel-refresh))
|
|
(message "[MM] Failed to send message: %S" (plist-get response :error)))))))
|
|
|
|
(provide 'mattermost-channel)
|