mattermost.el/mattermost.el
2022-05-22 20:09:12 +02:00

142 lines
5.3 KiB
EmacsLisp

;;; mattermost.el --- An Emacs Mattermost Chat Client
;; ToDo: LICENSE HERE
;; Author: Miguel de la Cruz (mgdelacroix@gmail.com)
;; Version: 0.1
;; Package-Requires: ((websocket))
;; Keywords: Mattermost, chat, client, Internet
;;; Code:
(defgroup mattermost nil
"Mattermost chat client"
:prefix "mattermost-"
:group 'applications)
(defcustom mattermost-server-url ""
"The URL of the Mattermost server"
:group 'mattermost
:type 'string)
(defvar mattermost-users-plist '()
"A plist with the server users by id")
(defvar mattermost-token nil
"The user token to access the server")
(defvar mattermost-user-id nil
"The ID of the authenticated user")
(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)
(json-array-type 'list)
(json-key-type 'keyword))
(goto-char url-http-end-of-headers)
(json-read)))
(defun mattermost-print-post (post)
"Prints 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 msg))))
(defun mattermost--get-user (user-id)
"Returns the user from the internal cache or fetches it first"
(let* ((user-id-keyword (mattermost-string->keyword user-id))
(user (plist-get mattermost-users-plist user-id-keyword)))
(if (not user)
(let ((user (mattermost-get-user user-id)))
(setq mattermost-users-plist (plist-put mattermost-users-plist user-id-keyword user)))
user)))
;; ToDo: once mattermost-request parses headers, use it to fetch both
;; the user (set the id to a local var) and the headers (set the
;; token)
(defun mattermost-login (username password)
"Log into the Mattermost Server"
(let ((url-request-method "POST")
(url-request-extra-headers '(("Content-Type" . "application/json")))
(url-request-data (json-serialize (list :login_id username :password password))))
(with-current-buffer (url-retrieve-synchronously (concat mattermost-server-url "/api/v4/users/login"))
(beginning-of-buffer)
(search-forward "Token: ")
(let ((token (word-at-point))
(response (mattermost-parse-json)))
(setq mattermost-token token)
(setq mattermost-user-id (plist-get response :id))
token))))
;; ToDo: update to parse headers as well
(defun mattermost-request (method url &optional body)
"Builds a Mattermost request and returns the JSON response"
(let ((url-request-method method)
(url-request-extra-headers `(("Content-Type" . "application/json")))
(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)
(mattermost-parse-json))))
(defun mattermost-get-user (user-id)
"Returns the user information"
(mattermost-request "GET" (format "/users/%s" user-id)))
(defun mattermost-get-teams ()
"Returns the user's team list"
(mattermost-request "GET" "/teams"))
(defun mattermost-get-channels (team-id)
"Returns the user's channel for a given team"
(let ((url (format "/users/%s/teams/%s/channels" mattermost-user-id team-id)))
(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)
(dolist (team teams)
(let* ((team-id (plist-get team :id))
(team-display-name (plist-get team :display_name))
(channels (mattermost-get-channels team-id)))
(insert (format "%s\n" team-display-name))
(dolist (channel channels)
(let ((channel-id (plist-get channel :id))
(channel-display-name (plist-get channel :display_name))
(channel-name (plist-get channel :name)))
(insert (format "> [%s] %s\n" channel-id (if (string= channel-display-name "")
channel-name
channel-display-name)))))))))
;; ToDo: remove
(setq msgs (mattermost-get-channel-messages "68w17u1da7yg7enayudjjqqwse"))
(car (plist-get msgs :posts))
(with-current-buffer (get-buffer-create "*Town Square*")
(erase-buffer)
(let ((posts (plist-get msgs :posts))
(order (plist-get msgs :order)))
(dolist (msgid order)
(let* ((msgid-keyword (mattermost-string->keyword msgid))
(post (plist-get posts msgid-keyword)))
(mattermost-print-post post)))))
(provide 'mattermost)
;;; mattermost.el ends here