diff --git a/docs/mvp-checklist.md b/docs/mvp-checklist.md index be0a6f1..e066da9 100644 --- a/docs/mvp-checklist.md +++ b/docs/mvp-checklist.md @@ -95,13 +95,18 @@ - [x] Bridge methods verified via smoke tests. - [x] Post model includes file_ids. -## Phase 4b — Emacs UI (pending) +## Phase 4b — Emacs UI -- [ ] Connect Emacs to bridge over TCP. -- [ ] Open self-DM buffer. -- [ ] Display raw messages. -- [ ] Send message from Emacs. -- [ ] Receive own message online. +- [x] Connect Emacs to bridge over TCP. +- [x] Interactive connection prompt (password in memory only). +- [x] Dashboard buffer with channel list (`mattermost-dashboard-mode`). +- [x] Open channel buffer from dashboard. +- [x] Display raw message history in channel buffer. +- [x] Send message from channel buffer (`C-c C-s`). +- [x] Refresh channel history (`g`). +- [x] Reset connection state (`mattermost-reset-state`). +- [ ] Receive own message online (WebSocket fan-out). +- [ ] Resolve DM channel display names (currently shows empty). ## Phase 5 — WebSocket online updates @@ -127,12 +132,13 @@ ## Phase 7 — channels -- [ ] Implement list_channels. -- [ ] Channel list buffer. -- [ ] Open channel buffer, load history, send message. -- [ ] Channel events online. +- [x] Implement list_channels (Rust backend). +- [x] Channel list buffer (Emacs UI). +- [x] Open channel buffer, load history, send message. +- [ ] Channel events online (WebSocket fan-out). - [ ] Implement mark_read. -- [ ] Forced refresh with g. +- [ ] Resolve DM channel display names. +- [ ] Forced refresh with g (basic refresh works, full recovery pending). ## Phase 8 — TLS/self-signed support diff --git a/lisp/mattermost-channel.el b/lisp/mattermost-channel.el index e69de29..c5a8d51 100644 --- a/lisp/mattermost-channel.el +++ b/lisp/mattermost-channel.el @@ -0,0 +1,105 @@ +;;; 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) diff --git a/lisp/mattermost-dashboard.el b/lisp/mattermost-dashboard.el index 91004f9..9b6a086 100644 --- a/lisp/mattermost-dashboard.el +++ b/lisp/mattermost-dashboard.el @@ -2,6 +2,7 @@ (require 'mattermost-custom) (require 'mattermost-protocol) +(require 'mattermost-channel) (defvar mattermost--current-server-id nil "Current server ID for the dashboard buffer.") @@ -19,9 +20,18 @@ (setq buffer-read-only t) (setq truncate-lines t)) +(defun mattermost-reset-state () + "Reset all connection state and clear pending requests." + (interactive) + (when (and (boundp 'mattermost--process) mattermost--process (process-live-p mattermost--process)) + (delete-process mattermost--process)) + (setq mattermost--process nil) + (setq mattermost--pending-requests (make-hash-table :test 'equal)) + (setq mattermost--input-buffer "") + (message "[MM] State reset complete. Ready to reconnect.")) + (defun mattermost-ensure-connected (callback) - "Ensure connection to the bridge, prompting for missing credentials. -Calls CALLBACK with server-id on success. Password is never saved to disk." + "Ensure connection to the bridge, prompting for missing credentials." (let* ((url (if (string= mattermost-server-url "") (read-string "Mattermost URL (e.g., https://mattermost.local): ") mattermost-server-url)) @@ -31,12 +41,16 @@ Calls CALLBACK with server-id on success. Password is never saved to disk." (password (read-passwd "Password (or PAT token): ")) (server-id mattermost-server-id)) - ;; Сохраняем только не-секретные настройки, если они были пустыми (when (string= mattermost-server-url "") (customize-save-variable 'mattermost-server-url url)) (when (string= mattermost-login-id "") (customize-save-variable 'mattermost-login-id login)) + (unless (and (boundp 'mattermost--process) + mattermost--process + (process-live-p mattermost--process)) + (mattermost-connect-to-bridge)) + (message "[MM-DEBUG] Connecting to %s as %s (password masked)" url login) (mattermost--send-request "connect" @@ -70,21 +84,22 @@ Calls CALLBACK with server-id on success. Password is never saved to disk." "Render CHANNELS in a new Mattermost Dashboard buffer." (let ((buf (get-buffer-create "*Mattermost Dashboard*"))) (with-current-buffer buf - (erase-buffer) - (mattermost-dashboard-mode) - (setq-local mattermost--current-server-id server-id) - (setq-local mattermost--channels channels) - - (insert "Mattermost Channels\n") - (insert "==================\n\n") - - (dolist (ch channels) - (let ((name (or (plist-get ch :display_name) "Unknown")) - (type (or (plist-get ch :channel_type) "?")) - (id (plist-get ch :id))) - (insert (format "[%s] %s (ID: %s)\n" type name id)))) - - (goto-char (point-min))) + (let ((inhibit-read-only t)) + (erase-buffer) + (mattermost-dashboard-mode) + (setq-local mattermost--current-server-id server-id) + (setq-local mattermost--channels channels) + + (insert "Mattermost Channels\n") + (insert "==================\n\n") + + (dolist (ch channels) + (let ((name (or (plist-get ch :display_name) "Unknown")) + (type (or (plist-get ch :channel_type) "?")) + (id (plist-get ch :id))) + (insert (format "[%s] %s (ID: %s)\n" type name id)))) + + (goto-char (point-min)))) (switch-to-buffer buf))) (defun mattermost-dashboard-refresh () @@ -109,8 +124,14 @@ Calls CALLBACK with server-id on success. Password is never saved to disk." (interactive) (let ((line (thing-at-point 'line t))) (if (string-match "ID: \\([a-z0-9]+\\)" line) - (let ((channel-id (match-string 1 line))) - (message "[MM] Opening channel: %s (TODO: implement channel buffer)" channel-id)) + (let* ((channel-id (match-string 1 line)) + (server-id mattermost--current-server-id) + (channel-name (if (string-match "\\] \\(.+\\) (ID:" line) + (match-string 1 line) + "Unknown"))) + (if server-id + (mattermost-channel-open server-id channel-id channel-name) + (error "No server ID associated with this dashboard"))) (message "[MM] No channel ID found on this line.")))) (provide 'mattermost-dashboard) diff --git a/lisp/mattermost-protocol.el b/lisp/mattermost-protocol.el index 4bc3e06..2c564de 100644 --- a/lisp/mattermost-protocol.el +++ b/lisp/mattermost-protocol.el @@ -15,7 +15,7 @@ (defun mattermost-connect-to-bridge () "Connect to the running Rust bridge." (interactive) - (when (and mattermost--process (process-live-p mattermost--process)) + (when (and (boundp 'mattermost--process) mattermost--process (process-live-p mattermost--process)) (delete-process mattermost--process)) (setq mattermost--process @@ -46,7 +46,8 @@ (setq lines (butlast lines))) (dolist (line lines) - (let ((msg (ignore-errors (json-parse-string line :object-type 'plist)))) + ;; ДОБАВЛЕНО: :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)) (type (plist-get msg :type)))