Plenty of stuff

This commit is contained in:
Miguel de la Cruz 2022-06-03 13:05:46 +02:00
parent ac63ec4580
commit afc79685b8

View file

@ -52,7 +52,7 @@
;; ToDo: probably not the best way to get a keyword from a string ;; ToDo: probably not the best way to get a keyword from a string
(defun mattermost-string->keyword (str) (defun mattermost-string->keyword (str)
"Returns a keyword from a string" "Returns a keyword from a string"
(read (concat ":" str))) (intern (concat ":" str)))
(defun mattermost-json-parse-string (string) (defun mattermost-json-parse-string (string)
"Parses a JSON string" "Parses a JSON string"
@ -67,7 +67,8 @@
(json-array-type 'list) (json-array-type 'list)
(json-key-type 'keyword)) (json-key-type 'keyword))
(set-buffer-multibyte t) (set-buffer-multibyte t)
(goto-char url-http-end-of-headers) (if (boundp 'url-http-end-of-headers)
(goto-char url-http-end-of-headers))
(json-read))) (json-read)))
(defun mattermost-read-password () (defun mattermost-read-password ()
@ -96,16 +97,40 @@ if the user is not present"
:action "authentication_challenge" :action "authentication_challenge"
:data (:token ,mattermost-token)))) :data (:token ,mattermost-token))))
(defun mattermost--ws-posted-to-post (msg)
"Extracts the post from a ws posted frame and transforms it to
conform to a post plist"
(let* ((post (plist-get (plist-get msg :data) :post)))
(mattermost-json-parse-string post)))
(defun mattermost--process-ws-posted (msg)
"Processes a websocket posted message"
(let* ((channel-name (plist-get (plist-get msg :data) :channel_name))
(channel-display-name (plist-get (plist-get msg :data) :channel_display_name))
(chanb (or (get-buffer (format "> %s <" channel-name))
(get-buffer (format "> %s <" channel-display-name))))
(post (mattermost--ws-posted-to-post msg)))
(if (not (null chanb))
(with-current-buffer chanb
(save-excursion
(goto-char mattermost-insert-marker)
(mattermost-insert-post post))))))
(defun mattermost--process-ws-frame (_ws frame)
"Processes a websocket frame"
(let* ((text (websocket-frame-text frame))
(msg (mattermost-json-parse-string text))
(event (plist-get msg :event)))
(cond ((string= event "posted") (mattermost--process-ws-posted msg))
(t (lwarn 'mattermost :debug (format "INCOMING> %s" msg))))))
(defun mattermost--connect-websocket () (defun mattermost--connect-websocket ()
"Returns a websocket configured to connect to the Mattermost "Returns a websocket configured to connect to the Mattermost
host and to dispatch and process the incoming messages" host and to dispatch and process the incoming messages"
(websocket-open (format "wss://%s/api/v4/websocket" mattermost-host) (websocket-open (format "wss://%s/api/v4/websocket" mattermost-host)
:on-open (lambda (ws) :on-open (lambda (ws)
(websocket-send-text ws (mattermost--get-auth-challenge))) (websocket-send-text ws (mattermost--get-auth-challenge)))
:on-message (lambda (ws frame) :on-message 'mattermost--process-ws-frame
(let* ((text (websocket-frame-text frame))
(msg (json-parse-string text)))
(lwarn 'mattermost :debug (format "INCOMING> %s" msg))))
:on-close (lambda (ws) :on-close (lambda (ws)
(lwarn 'mattermost :error "websocket connection closed")))) (lwarn 'mattermost :error "websocket connection closed"))))
@ -124,7 +149,7 @@ if the user is not present"
(response (mattermost-read-json))) (response (mattermost-read-json)))
(setq mattermost-token token) (setq mattermost-token token)
(setq mattermost-user-id (plist-get response :id)) (setq mattermost-user-id (plist-get response :id))
(mattermost--connect-websocket) (setq mattermost-websocket (mattermost--connect-websocket))
token)))) token))))
;; ToDo: update to parse headers as well ;; ToDo: update to parse headers as well
@ -169,13 +194,22 @@ if the user is not present"
"Creates a post on a channel" "Creates a post on a channel"
(mattermost-request "POST" "/posts" `(:channel_id ,channel-id :message ,message))) (mattermost-request "POST" "/posts" `(:channel_id ,channel-id :message ,message)))
(defun mattermost--get-channel-display-name (channel)
"Returns the string to be used as channel display name"
(let ((channel-display-name (plist-get channel :display_name))
(channel-name (plist-get channel :name))
(channel-type (plist-get channel :type)))
(cond ((string= channel-type "D")
(let* ((direct-message-user-id (car (split-string channel-name "__")))
(user (mattermost--get-user direct-message-user-id)))
(format "@%s" (plist-get user :username))))
(t (if (string-empty-p channel-display-name)
channel-name
channel-display-name)))))
(defun mattermost-insert-channel (channel) (defun mattermost-insert-channel (channel)
"Inserts a channel button in the current buffer" "Inserts a channel button in the current buffer"
(let* ((channel-display-name (plist-get channel :display_name)) (let* ((name (mattermost--get-channel-display-name channel)))
(channel-name (plist-get channel :name))
(name (if (string-empty-p channel-display-name)
channel-name
channel-display-name)))
(insert-text-button (format "> %s\n" name) (insert-text-button (format "> %s\n" name)
:type 'mattermost-channel :type 'mattermost-channel
'channel channel))) 'channel channel)))
@ -249,16 +283,21 @@ user to check their status and select between them")
(defun mattermost-insert-post (post) (defun mattermost-insert-post (post)
"Inserts a post message in the current buffer" "Inserts a post message in the current buffer"
(let* ((user-id (plist-get post :user_id)) (let* ((inhibit-read-only t)
(user-id (plist-get post :user_id))
(user (mattermost--get-user user-id)) (user (mattermost--get-user user-id))
(username (plist-get user :username)) (username (plist-get user :username))
(msg (plist-get post :message))) (msg (plist-get post :message))
(insert-text-button (format "[%s]" username) (username-text (format "[%s]" username))
:type 'mattermost-username (post-text (format " %s\n" msg)))
'user user) (insert-before-markers username-text)
(insert-text-button (format " %s\n" msg) (make-text-button (- (point) (length username-text)) (point)
:type 'mattermost-post :type 'mattermost-username
'post post))) 'user user)
(insert-before-markers post-text)
(make-text-button (- (point) (length post-text)) (point)
:type 'mattermost-post
'post post)))
(defun mattermost-prompt () (defun mattermost-prompt ()
"Shows the message prompt" "Shows the message prompt"
@ -301,10 +340,8 @@ user to check their status and select between them")
"Populates the channel buffer with posts and the prompt" "Populates the channel buffer with posts and the prompt"
(let* ((inhibit-read-only t) (let* ((inhibit-read-only t)
(id (plist-get channel :id)) (id (plist-get channel :id))
(display-name (plist-get channel :display_name)) (name (mattermost--get-channel-display-name channel))
(name (plist-get channel :name)) (chanb (get-buffer-create (format "> %s <" 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 ;; ToDo: somehow check if the buffer already exists to populate it
;; only if it doesn't ;; only if it doesn't