Initial commit that fills up a buffer with teams and channels
This commit is contained in:
commit
15348b0999
1 changed files with 88 additions and 0 deletions
88
mattermost.el
Normal file
88
mattermost.el
Normal file
|
@ -0,0 +1,88 @@
|
||||||
|
;;; 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)
|
||||||
|
|
||||||
|
(setq mattermost-server-url "https://chat.ctrlz.es")
|
||||||
|
|
||||||
|
(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)))
|
||||||
|
|
||||||
|
;; 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)))
|
||||||
|
;; ToDo: define these as local vars somehow
|
||||||
|
(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 mattemrost 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 (cons "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-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)))
|
||||||
|
|
||||||
|
(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\n" (if (string= channel-display-name "")
|
||||||
|
channel-name
|
||||||
|
channel-display-name)))))))))
|
||||||
|
|
||||||
|
(provide 'mattermost)
|
||||||
|
;;; mattermost.el ends here
|
Loading…
Reference in a new issue