fix(ui): repair dashboard parsing and group channels by team

- Read channel type from :type (matches Rust serde rename), not :channel_type
- Group channels by team with Direct messages section last
- Use text properties instead of regex to resolve channel under point
- Fallback "Other" section when bridge omits teams
This commit is contained in:
Ivan I. Ovchinnikov
2026-11-21 11:21:06 +03:00
parent f2f4f62cd6
commit 628df77588
2 changed files with 114 additions and 81 deletions
+9 -3
View File
@@ -159,8 +159,13 @@ pub async fn handle_list_channels(
Err(e) => return protocol::Response::err(request.id, e), Err(e) => return protocol::Response::err(request.id, e),
}; };
match client.get_my_channels(&token).await { // Получаем каналы и команды параллельно
Ok(mut channels) => { let channels_result = client.get_my_channels(&token).await;
let teams_result = client.get_teams(&token).await;
match (channels_result, teams_result) {
(Ok(mut channels), Ok(teams)) => {
// Резолвим имена для DM-каналов
for ch in &mut channels { for ch in &mut channels {
if ch.channel_type == "D" && ch.display_name.is_empty() { if ch.channel_type == "D" && ch.display_name.is_empty() {
let parts: Vec<&str> = ch.name.split("__").collect(); let parts: Vec<&str> = ch.name.split("__").collect();
@@ -183,10 +188,11 @@ pub async fn handle_list_channels(
serde_json::json!({ serde_json::json!({
"server_id": server_id, "server_id": server_id,
"channels": channels, "channels": channels,
"teams": teams,
}), }),
) )
} }
Err(e) => protocol::Response::err(request.id, e.into()), (Err(e), _) | (_, Err(e)) => protocol::Response::err(request.id, e.into()),
} }
} }
+97 -70
View File
@@ -5,133 +5,160 @@
(require 'mattermost-channel) (require 'mattermost-channel)
(defvar mattermost--current-server-id nil (defvar mattermost--current-server-id nil
"Current server ID for the dashboard buffer.") "Server id owning the current dashboard buffer.")
(defvar mattermost--channels nil
"Last channel list rendered in this buffer.")
(defvar mattermost--teams nil
"Last team list rendered in this buffer.")
(defvar mattermost-dashboard-mode-map (defvar mattermost-dashboard-mode-map
(let ((map (make-sparse-keymap))) (let ((map (make-sparse-keymap)))
(define-key map (kbd "RET") #'mattermost-dashboard-open-channel) (define-key map (kbd "RET") #'mattermost-dashboard-open-channel)
(define-key map (kbd "g") #'mattermost-dashboard-refresh) (define-key map (kbd "g") #'mattermost-dashboard-refresh)
map) map)
"Keymap for `mattermost-dashboard-mode`.") "Keymap for `mattermost-dashboard-mode'.")
(define-derived-mode mattermost-dashboard-mode special-mode "Mattermost-Dashboard" (define-derived-mode mattermost-dashboard-mode special-mode "Mattermost"
"Major mode for viewing Mattermost channels list. "Major mode for the Mattermost channel list.
\\{mattermost-dashboard-mode-map}" \\{mattermost-dashboard-mode-map}"
(setq buffer-read-only t) (setq buffer-read-only t)
(setq truncate-lines t)) (setq truncate-lines t))
(defun mattermost-reset-state () (defun mattermost-reset-state ()
"Reset all connection state and clear pending requests." "Reset bridge connection state and pending requests."
(interactive) (interactive)
(when (and (boundp 'mattermost--process) mattermost--process (process-live-p mattermost--process)) (when (and mattermost--process (process-live-p mattermost--process))
(delete-process mattermost--process)) (delete-process mattermost--process))
(setq mattermost--process nil) (setq mattermost--process nil)
(setq mattermost--pending-requests (make-hash-table :test 'equal)) (setq mattermost--pending-requests (make-hash-table :test 'equal))
(setq mattermost--input-buffer "") (setq mattermost--input-buffer "")
(message "[MM] State reset complete. Ready to reconnect.")) (message "[MM] State reset complete."))
(defun mattermost-ensure-connected (callback) (defun mattermost-ensure-connected (callback)
"Ensure connection to the bridge, prompting for missing credentials." "Connect to bridge and Mattermost, then call CALLBACK with server-id."
(let* ((url (if (string= mattermost-server-url "") (let* ((url (if (string-empty-p mattermost-server-url)
(read-string "Mattermost URL (e.g., https://mattermost.local): ") (read-string "Mattermost URL: ")
mattermost-server-url)) mattermost-server-url))
(login (if (string= mattermost-login-id "") (login (if (string-empty-p mattermost-login-id)
(read-string "Login ID or Email: ") (read-string "Login ID or email: ")
mattermost-login-id)) mattermost-login-id))
(password (read-passwd "Password (or PAT token): ")) (password (read-passwd "Password: "))
(server-id mattermost-server-id)) (server-id mattermost-server-id))
(when (string-empty-p mattermost-server-url)
(when (string= mattermost-server-url "")
(customize-save-variable 'mattermost-server-url url)) (customize-save-variable 'mattermost-server-url url))
(when (string= mattermost-login-id "") (when (string-empty-p mattermost-login-id)
(customize-save-variable 'mattermost-login-id login)) (customize-save-variable 'mattermost-login-id login))
(unless (and mattermost--process (process-live-p mattermost--process))
(unless (and (boundp 'mattermost--process)
mattermost--process
(process-live-p mattermost--process))
(mattermost-connect-to-bridge)) (mattermost-connect-to-bridge))
(message "[MM-DEBUG] Connecting to %s as %s (password ***MASKED***)" url login)
(message "[MM-DEBUG] Connecting to %s as %s (password masked)" url login)
(mattermost--send-request (mattermost--send-request
"connect" "connect"
(list :server_id server-id (list :server_id server-id :url url :login_id login :password password)
:url url
:login_id login
:password password)
(lambda (response) (lambda (response)
(if (plist-get response :ok) (if (plist-get response :ok)
(progn (progn
(message "[MM] Connected successfully.") (message "[MM] Connected.")
(funcall callback server-id)) (funcall callback server-id))
(message "[MM] Connect failed: %S" (plist-get response :error))))))) (message "[MM] Connect failed: %S" (plist-get response :error)))))))
(defun mattermost-dashboard () (defun mattermost-dashboard ()
"Open the Mattermost dashboard showing available channels." "Open the Mattermost dashboard."
(interactive) (interactive)
(mattermost-ensure-connected (mattermost-ensure-connected #'mattermost--request-channels))
(lambda (server-id)
(defun mattermost--request-channels (server-id)
"Fetch channels and teams for SERVER-ID, then render the dashboard."
(message "[MM-DEBUG] Requesting list_channels for %s" server-id)
(mattermost--send-request (mattermost--send-request
"list_channels" "list_channels"
(list :server_id server-id) (list :server_id server-id)
(lambda (response) (lambda (response)
(if (plist-get response :ok) (if (plist-get response :ok)
(let ((result (plist-get response :result)))
(mattermost--render-dashboard (mattermost--render-dashboard
server-id server-id
(plist-get (plist-get response :result) :channels)) (plist-get result :channels)
(message "[MM] Failed to list channels: %S" (plist-get response :error)))))))) (plist-get result :teams)))
(message "[MM] list_channels failed: %S" (plist-get response :error))))))
(defun mattermost--render-dashboard (server-id channels) (defun mattermost--insert-team-header (title)
"Render CHANNELS in a new Mattermost Dashboard buffer." "Insert a section header TITLE."
(let ((buf (get-buffer-create "*Mattermost Dashboard*"))) (insert (propertize (format "%s\n" title) 'face 'mattermost-sender-face)))
(defun mattermost--insert-channel-line (ch)
"Insert one selectable line for channel CH."
(let* ((id (plist-get ch :id))
(type (or (plist-get ch :type) "?"))
(name (or (plist-get ch :display_name) ""))
(label (if (string-empty-p name) (or (plist-get ch :name) id) name))
(line (format " [%s] %s\n" type label)))
(insert (propertize line
'mm-channel-id id
'mm-channel-name label
'mouse-face 'highlight))))
(defun mattermost--render-dashboard (server-id channels teams)
"Render CHANNELS grouped by TEAMS for SERVER-ID."
(let ((buf (get-buffer-create "*Mattermost Dashboard*"))
(by-team (make-hash-table :test 'equal))
(dms nil))
(dolist (ch channels)
(let ((type (plist-get ch :type)))
(if (member type '("D" "G"))
(push ch dms)
(let* ((tid (or (plist-get ch :team_id) ""))
(prev (gethash tid by-team)))
(puthash tid (cons ch prev) by-team)))))
(with-current-buffer buf (with-current-buffer buf
(let ((inhibit-read-only t)) (let ((inhibit-read-only t))
(erase-buffer)
(mattermost-dashboard-mode) (mattermost-dashboard-mode)
(erase-buffer)
(setq-local mattermost--current-server-id server-id) (setq-local mattermost--current-server-id server-id)
(setq-local mattermost--channels channels) (setq-local mattermost--channels channels)
(setq-local mattermost--teams teams)
(insert "Mattermost Channels\n") (insert "Mattermost channels RET open · g refresh\n\n")
(insert "==================\n\n") (let ((rendered (make-hash-table :test 'equal)))
(dolist (team teams)
(dolist (ch channels) (let* ((tid (plist-get team :id))
(let ((name (or (plist-get ch :display_name) "Unknown")) (chs (gethash tid by-team)))
(type (or (plist-get ch :channel_type) "?")) (when chs
(id (plist-get ch :id))) (puthash tid t rendered)
(insert (format "[%s] %s (ID: %s)\n" type name id)))) (mattermost--insert-team-header (plist-get team :display_name))
(dolist (ch (nreverse chs))
(mattermost--insert-channel-line ch))
(insert "\n"))))
(maphash
(lambda (tid chs)
(unless (gethash tid rendered)
(mattermost--insert-team-header (if (string-empty-p tid) "Other" tid))
(dolist (ch (nreverse chs))
(mattermost--insert-channel-line ch))
(insert "\n")))
by-team))
(when dms
(mattermost--insert-team-header "Direct messages")
(dolist (ch (nreverse dms))
(mattermost--insert-channel-line ch))
(insert "\n"))
(goto-char (point-min)))) (goto-char (point-min))))
(switch-to-buffer buf))) (switch-to-buffer buf)))
(defun mattermost-dashboard-refresh () (defun mattermost-dashboard-refresh ()
"Refresh the channel list in the current dashboard buffer." "Refresh the channel list."
(interactive) (interactive)
(let ((server-id mattermost--current-server-id)) (unless mattermost--current-server-id
(unless server-id
(error "No server ID associated with this buffer")) (error "No server ID associated with this buffer"))
(message "[MM-DEBUG] Refreshing channels...") (mattermost--request-channels mattermost--current-server-id))
(mattermost--send-request
"list_channels"
(list :server_id server-id)
(lambda (response)
(if (plist-get response :ok)
(mattermost--render-dashboard
server-id
(plist-get (plist-get response :result) :channels))
(message "[MM] Refresh failed: %S" (plist-get response :error)))))))
(defun mattermost-dashboard-open-channel () (defun mattermost-dashboard-open-channel ()
"Open the channel under point." "Open the channel under point."
(interactive) (interactive)
(let ((line (thing-at-point 'line t))) (let ((id (get-text-property (point) 'mm-channel-id))
(if (string-match "ID: \\([a-z0-9]+\\)" line) (name (get-text-property (point) 'mm-channel-name)))
(let* ((channel-id (match-string 1 line)) (cond
(server-id mattermost--current-server-id) ((not id) (message "[MM] No channel on this line."))
(channel-name (if (string-match "\\] \\(.+\\) (ID:" line) ((not mattermost--current-server-id)
(match-string 1 line) (error "No server ID associated with this dashboard"))
"Unknown"))) (t (mattermost-channel-open mattermost--current-server-id id name)))))
(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) (provide 'mattermost-dashboard)