diff --git a/mattermost.el b/mattermost.el index b3f0067..fcbbaea 100644 --- a/mattermost.el +++ b/mattermost.el @@ -57,6 +57,7 @@ (let ((json-object-type 'plist) (json-array-type 'list) (json-key-type 'keyword)) + (set-buffer-multibyte t) (goto-char url-http-end-of-headers) (json-read))) @@ -69,16 +70,6 @@ (funcall secret) secret))))) -(defun mattermost-insert-post (post) - "Inserts the post in the current buffer" - (let* ((msg (plist-get post :message)) - ;; ToDo: convert to a readable date - ;; (update-at (plist-get post :update_at)) - (user-id (plist-get post :user_id)) - (user (mattermost--get-user user-id)) - (username (plist-get user :username))) - (insert (format "[%s] %s\n" username (encode-coding-string msg 'utf-8))))) - (defun mattermost--get-user (user-id) "Returns the user from the internal cache and fills the cache if the user is not present" @@ -117,7 +108,7 @@ if the user is not present" (let ((url-request-method "POST") (url-request-extra-headers '(("Content-Type" . "application/json"))) (url-request-data (json-serialize `(:login_id ,username :password ,password)))) - (with-current-buffer (url-retrieve-synchronously (concat mattermost-server-url "/api/v4/users/login")) + (with-current-buffer (url-retrieve-synchronously (concat "https://" mattermost-host "/api/v4/users/login")) (beginning-of-buffer) (search-forward "Token: ") (let ((token (word-at-point)) @@ -135,7 +126,7 @@ if the user is not present" (url-request-data (if body (json-serialize body)))) (if mattermost-token (add-to-list 'url-request-extra-headers `("Authorization" . ,(concat "Bearer " mattermost-token)))) - (with-current-buffer (url-retrieve-synchronously (concat mattermost-server-url "/api/v4" url) t) + (with-current-buffer (url-retrieve-synchronously (concat "https://" mattermost-host "/api/v4" url) t) (mattermost-read-json)))) (defun mattermost-get-user (user-id) @@ -165,15 +156,14 @@ if the user is not present" channel-display-name))) (insert-text-button (format "> %s\n" name) 'action #'(lambda (b) - (let* ((channel (button-get b 'channel)) - (channel-id (plist-get channel :id))) - - (message "button action! %s" channel-id))) + (let* ((channel (button-get b 'channel))) + (mattermost-show-channel channel))) 'channel channel 'read-only t 'rear-sticky t 'front-sticky t))) +;; ToDo: when root buffer is closed, disconnect (defun mattermost-show-root () "Populates the Mattermost Root buffer and changes to it" (interactive) @@ -206,6 +196,46 @@ if the user is not present" "Mode to list the Mattermost teams and channels to allow the user to check their status and select between them") +(defun mattermost-insert-post (post) + "Inserts a post message in the current buffer" + (let* ((user-id (plist-get post :user_id)) + (user (mattermost--get-user user-id)) + (username (plist-get user :username)) + (msg (plist-get post :message))) + (insert-text-button (format "[%s] %s\n" username msg) + 'action #'(lambda (b) + (message "button action! %s" (button-get b 'post))) + 'post post + 'read-only t + 'rear-sticky t + 'front-sticky t))) + +;; ToDo: should we receive just a channel id and fetch the channel +;; data? +(defun mattermost-show-channel (channel) + "Populates the channel buffer with posts and the prompt" + (let* ((id (plist-get channel :id)) + (display-name (plist-get channel :display_name)) + (name (plist-get channel :name)) + (cname (if (string-empty-p display-name) name display-name)) + (chanb (get-buffer-create (format "> %s <" cname)))) + + ;; ToDo: somehow check if the buffer already exists to populate it + ;; only if it doesn't + + (with-current-buffer chanb + (erase-buffer) + (let* ((inhibit-read-only t) + (resp (mattermost-get-channel-messages id)) + (order (seq-reverse (plist-get resp :order))) + (posts (plist-get resp :posts))) + (dolist (post-id order) + (let ((post (plist-get posts (mattermost-string->keyword post-id)))) + (mattermost-insert-post post))))) + (switch-to-buffer chanb) + ;; ToDo: here enable mode + (end-of-buffer))) + (defun mattermost () "Connect to a Mattermost instance" (interactive)