Correctly print post messages

This commit is contained in:
Miguel de la Cruz 2022-06-01 13:09:28 +02:00
parent 34fa91691c
commit 952fe90817

View file

@ -57,6 +57,7 @@
(let ((json-object-type 'plist) (let ((json-object-type 'plist)
(json-array-type 'list) (json-array-type 'list)
(json-key-type 'keyword)) (json-key-type 'keyword))
(set-buffer-multibyte t)
(goto-char url-http-end-of-headers) (goto-char url-http-end-of-headers)
(json-read))) (json-read)))
@ -69,16 +70,6 @@
(funcall secret) (funcall secret)
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) (defun mattermost--get-user (user-id)
"Returns the user from the internal cache and fills the cache "Returns the user from the internal cache and fills the cache
if the user is not present" if the user is not present"
@ -117,7 +108,7 @@ if the user is not present"
(let ((url-request-method "POST") (let ((url-request-method "POST")
(url-request-extra-headers '(("Content-Type" . "application/json"))) (url-request-extra-headers '(("Content-Type" . "application/json")))
(url-request-data (json-serialize `(:login_id ,username :password ,password)))) (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) (beginning-of-buffer)
(search-forward "Token: ") (search-forward "Token: ")
(let ((token (word-at-point)) (let ((token (word-at-point))
@ -135,7 +126,7 @@ if the user is not present"
(url-request-data (if body (json-serialize body)))) (url-request-data (if body (json-serialize body))))
(if mattermost-token (if mattermost-token
(add-to-list 'url-request-extra-headers `("Authorization" . ,(concat "Bearer " 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)))) (mattermost-read-json))))
(defun mattermost-get-user (user-id) (defun mattermost-get-user (user-id)
@ -165,15 +156,14 @@ if the user is not present"
channel-display-name))) channel-display-name)))
(insert-text-button (format "> %s\n" name) (insert-text-button (format "> %s\n" name)
'action #'(lambda (b) 'action #'(lambda (b)
(let* ((channel (button-get b 'channel)) (let* ((channel (button-get b 'channel)))
(channel-id (plist-get channel :id))) (mattermost-show-channel channel)))
(message "button action! %s" channel-id)))
'channel channel 'channel channel
'read-only t 'read-only t
'rear-sticky t 'rear-sticky t
'front-sticky t))) 'front-sticky t)))
;; ToDo: when root buffer is closed, disconnect
(defun mattermost-show-root () (defun mattermost-show-root ()
"Populates the Mattermost Root buffer and changes to it" "Populates the Mattermost Root buffer and changes to it"
(interactive) (interactive)
@ -206,6 +196,46 @@ if the user is not present"
"Mode to list the Mattermost teams and channels to allow the "Mode to list the Mattermost teams and channels to allow the
user to check their status and select between them") 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 () (defun mattermost ()
"Connect to a Mattermost instance" "Connect to a Mattermost instance"
(interactive) (interactive)