diff --git a/bridge/crates/mm-bridge/src/app/handlers.rs b/bridge/crates/mm-bridge/src/app/handlers.rs index af12a2d..b85b1b2 100644 --- a/bridge/crates/mm-bridge/src/app/handlers.rs +++ b/bridge/crates/mm-bridge/src/app/handlers.rs @@ -159,8 +159,13 @@ pub async fn handle_list_channels( 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 { if ch.channel_type == "D" && ch.display_name.is_empty() { let parts: Vec<&str> = ch.name.split("__").collect(); @@ -183,10 +188,11 @@ pub async fn handle_list_channels( serde_json::json!({ "server_id": server_id, "channels": channels, + "teams": teams, }), ) } - Err(e) => protocol::Response::err(request.id, e.into()), + (Err(e), _) | (_, Err(e)) => protocol::Response::err(request.id, e.into()), } } diff --git a/lisp/mattermost-dashboard.el b/lisp/mattermost-dashboard.el index 9b6a086..3edc2be 100644 --- a/lisp/mattermost-dashboard.el +++ b/lisp/mattermost-dashboard.el @@ -5,133 +5,160 @@ (require 'mattermost-channel) (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 (let ((map (make-sparse-keymap))) (define-key map (kbd "RET") #'mattermost-dashboard-open-channel) (define-key map (kbd "g") #'mattermost-dashboard-refresh) map) - "Keymap for `mattermost-dashboard-mode`.") + "Keymap for `mattermost-dashboard-mode'.") -(define-derived-mode mattermost-dashboard-mode special-mode "Mattermost-Dashboard" - "Major mode for viewing Mattermost channels list. +(define-derived-mode mattermost-dashboard-mode special-mode "Mattermost" + "Major mode for the Mattermost channel list. \\{mattermost-dashboard-mode-map}" (setq buffer-read-only t) (setq truncate-lines t)) (defun mattermost-reset-state () - "Reset all connection state and clear pending requests." + "Reset bridge connection state and pending requests." (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)) (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.")) + (message "[MM] State reset complete.")) (defun mattermost-ensure-connected (callback) - "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): ") + "Connect to bridge and Mattermost, then call CALLBACK with server-id." + (let* ((url (if (string-empty-p mattermost-server-url) + (read-string "Mattermost URL: ") mattermost-server-url)) - (login (if (string= mattermost-login-id "") - (read-string "Login ID or Email: ") + (login (if (string-empty-p mattermost-login-id) + (read-string "Login ID or email: ") mattermost-login-id)) - (password (read-passwd "Password (or PAT token): ")) + (password (read-passwd "Password: ")) (server-id mattermost-server-id)) - - (when (string= mattermost-server-url "") + (when (string-empty-p mattermost-server-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)) - - (unless (and (boundp 'mattermost--process) - mattermost--process - (process-live-p mattermost--process)) + (unless (and mattermost--process (process-live-p mattermost--process)) (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 "connect" - (list :server_id server-id - :url url - :login_id login - :password password) + (list :server_id server-id :url url :login_id login :password password) (lambda (response) (if (plist-get response :ok) (progn - (message "[MM] Connected successfully.") + (message "[MM] Connected.") (funcall callback server-id)) (message "[MM] Connect failed: %S" (plist-get response :error))))))) (defun mattermost-dashboard () - "Open the Mattermost dashboard showing available channels." + "Open the Mattermost dashboard." (interactive) - (mattermost-ensure-connected - (lambda (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] Failed to list channels: %S" (plist-get response :error)))))))) + (mattermost-ensure-connected #'mattermost--request-channels)) -(defun mattermost--render-dashboard (server-id channels) - "Render CHANNELS in a new Mattermost Dashboard buffer." - (let ((buf (get-buffer-create "*Mattermost Dashboard*"))) +(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 + "list_channels" + (list :server_id server-id) + (lambda (response) + (if (plist-get response :ok) + (let ((result (plist-get response :result))) + (mattermost--render-dashboard + server-id + (plist-get result :channels) + (plist-get result :teams))) + (message "[MM] list_channels failed: %S" (plist-get response :error)))))) + +(defun mattermost--insert-team-header (title) + "Insert a section header TITLE." + (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 (let ((inhibit-read-only t)) - (erase-buffer) (mattermost-dashboard-mode) + (erase-buffer) (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)))) - + (setq-local mattermost--teams teams) + (insert "Mattermost channels RET open · g refresh\n\n") + (let ((rendered (make-hash-table :test 'equal))) + (dolist (team teams) + (let* ((tid (plist-get team :id)) + (chs (gethash tid by-team))) + (when chs + (puthash tid t rendered) + (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)))) (switch-to-buffer buf))) (defun mattermost-dashboard-refresh () - "Refresh the channel list in the current dashboard buffer." + "Refresh the channel list." (interactive) - (let ((server-id mattermost--current-server-id)) - (unless server-id - (error "No server ID associated with this buffer")) - (message "[MM-DEBUG] Refreshing channels...") - (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))))))) + (unless mattermost--current-server-id + (error "No server ID associated with this buffer")) + (mattermost--request-channels mattermost--current-server-id)) (defun mattermost-dashboard-open-channel () "Open the channel under point." (interactive) - (let ((line (thing-at-point 'line t))) - (if (string-match "ID: \\([a-z0-9]+\\)" line) - (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.")))) + (let ((id (get-text-property (point) 'mm-channel-id)) + (name (get-text-property (point) 'mm-channel-name))) + (cond + ((not id) (message "[MM] No channel on this line.")) + ((not mattermost--current-server-id) + (error "No server ID associated with this dashboard")) + (t (mattermost-channel-open mattermost--current-server-id id name))))) (provide 'mattermost-dashboard)