From ce0951fc453dff1360c8ce2c1420f53cb73409e4 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Sun, 22 May 2022 18:02:11 +0200 Subject: [PATCH] Get the last messages from a channel and print them on a buffer --- mattermost.el | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/mattermost.el b/mattermost.el index 653d612..c59e25c 100644 --- a/mattermost.el +++ b/mattermost.el @@ -21,6 +21,11 @@ (setq mattermost-server-url "https://chat.ctrlz.es") +;; ToDo: probably not the best way to get a keyword from a string +(defun mattermost-string->keyword (str) + "Returns a keyword from a string" + (read (concat ":" str))) + (defun mattermost-parse-json () "Parses the JSON in the current buffer after a url-request" (let ((json-object-type 'plist) @@ -68,6 +73,11 @@ (message "Channels url: %s" url) (mattermost-request "GET" url))) +(defun mattermost-get-channel-messages (channel-id &optional page per-page) + "Returns a list of posts for a given channel" + (let ((url (format "/channels/%s/posts" channel-id))) + (mattermost-request "GET" url))) + (with-current-buffer (get-buffer-create "*Mattermost Teams*") (let ((teams (mattermost-get-teams))) (erase-buffer) @@ -80,9 +90,23 @@ (let ((channel-id (plist-get channel :id)) (channel-display-name (plist-get channel :display_name)) (channel-name (plist-get channel :name))) - (insert (format "> %s\n" (if (string= channel-display-name "") - channel-name - channel-display-name))))))))) + (insert (format "> [%s] %s\n" channel-id (if (string= channel-display-name "") + channel-name + channel-display-name))))))))) + +(setq msgs (mattermost-get-channel-messages "68w17u1da7yg7enayudjjqqwse")) + +(car (plist-get msgs :posts)) + +(with-current-buffer (get-buffer-create "*Town Square*") + (erase-buffer) + (dolist (msgid (plist-get msgs :order)) + (let* ((posts (plist-get msgs :posts)) + (post (plist-get posts (mattermost-string->keyword msgid))) + (user-id (plist-get post :user_id)) + (message (plist-get post :message)) + (update-at (plist-get post :update_at))) + (insert (format "[%s %s] %s\n" user-id update-at message))))) (provide 'mattermost) ;;; mattermost.el ends here