Skip to content

Commit

Permalink
Add: (ement-invite, ement-create-room, ement-put-account-data)
Browse files Browse the repository at this point in the history
  • Loading branch information
alphapapa committed Mar 22, 2022
1 parent 5bbc9ac commit b09e8ca
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ If you want to install it manually, it's simple enough, but you should know what
3. Use these commands:
- ~ement-list-rooms~ to view the list of joined rooms.
- ~ement-view-room~ to view a room's buffer, selected with completion.
- ~ement-create-room~ to create a new room.
- ~ement-invite~ to invite a user to a room.
- ~ement-join-room~ to join a room.
- ~ement-leave-room~ to leave a room.
- ~ement-room-edit-message~ to edit a message at point.
Expand Down
72 changes: 72 additions & 0 deletions ement.el
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,67 @@ to be selected as well."
(selected-user (completing-read "User: " (mapcar #'car display-to-id))))
(or (alist-get selected-user display-to-id nil nil #'equal)
selected-user)))

(cl-defun ement-create-room
(session &key name alias topic invite direct-p (visibility 'private)
(then (lambda (data)
(message "Created new room: %s" (alist-get 'room_id data)))))
"Create new room on SESSION with given arguments."
;; TODO: Document other arguments.
;; SPEC: 10.1.1.
(declare (indent defun))
(interactive (list (ement-complete-session)
:name (read-string "New room name: ")
:alias (read-string "New room alias (e.g. \"foo\" for \"#foo:matrix.org\"): ")
:topic (read-string "New room topic: ")
:visibility (completing-read "New room type: " '(private public))))
(cl-labels ((given-p
(var) (and var (not (string-empty-p var))))
(put-direct
(data) (let ((room-id (alist-get 'room_id data))
(users-to-room (make-hash-table)))
(cl-loop for user-id in invite
do (puthash user-id (vector room-id) users-to-room))
(ement-put-account-data session "m.direct" users-to-room)
(ement-debug "Marked room as direct: %s" room-id))))
(pcase-let* ((endpoint "createRoom")
(data (ement-aprog1
(ement-alist "visibility" visibility)
(when (given-p alias)
(push (cons "room_alias_name" alias) it))
(when (given-p name)
(push (cons "name" name) it))
(when (given-p topic)
(push (cons "topic" topic) it))
(when invite
(push (cons "invite" invite) it))
(when direct-p
(push (cons "is_direct" t) it)))))
(ement-api session endpoint :method 'post :data (json-encode data)
:then (if direct-p
(lambda (data)
(put-direct data)
(funcall then data))
then)))))

(defun ement-invite (user-id room session)
"Invite USER-ID to ROOM on SESSION."
;; SPEC: 10.4.2.1.
(interactive
(let* ((session (ement-complete-session))
(user-id (ement-complete-user-id))
(room (car (ement-complete-room session))))
(list user-id room session)))
(pcase-let* ((endpoint (format "rooms/%s/invite"
(url-hexify-string (ement-room-id room))))
(data (ement-alist "user_id" user-id) ))
(ement-api session endpoint :method 'post :data (json-encode data)
;; TODO: Handle error codes.
:then (lambda (_data)
(message "User %s invited to room \"%s\" (%s)" user-id
(ement-room-display-name room)
(ement-room-id room))))))

;;;; Functions

(defun ement-view-initial-rooms (session)
Expand Down Expand Up @@ -816,6 +877,17 @@ IMAGE should be one as created by, e.g. `create-image'."
(lambda (a b)
(> (latest-event-in a) (latest-event-in b))))))))

(defun ement-put-account-data (session type data)
"Put account data of TYPE with DATA on SESSION."
(declare (indent defun))
(let ((endpoint (format "user/%s/account_data/%s"
(url-hexify-string (ement-user-id (ement-session-user session)))
type)))
(ement-api session endpoint :data (json-encode data)
:then (lambda (_data)
(ement-debug "Account data put on session %s: TYPE:%S DATA:%S"
(ement-user-id (ement-session-user session)) type (json-encode data))))))

;;;;; Reading/writing sessions

(defun ement--read-sessions ()
Expand Down

0 comments on commit b09e8ca

Please sign in to comment.