From 6166ab33b3e0277ab5f0ccfbf4b6d24349144da9 Mon Sep 17 00:00:00 2001
From: TheDiscordian <therealdiscordian@gmail.com>
Date: Fri, 15 May 2020 19:10:21 -0400
Subject: [PATCH] Status functions for interacting with the r0.6.0
 presence/status API

---
 client.go    | 23 +++++++++++++++++++++++
 responses.go |  8 ++++++++
 2 files changed, 31 insertions(+)

diff --git a/client.go b/client.go
index 68a7bdd..632b751 100644
--- a/client.go
+++ b/client.go
@@ -486,6 +486,29 @@ func (cli *Client) SetAvatarURL(url string) error {
 	return nil
 }
 
+// GetStatus returns the status of the user from the specified MXID. See https://matrix.org/docs/spec/client_server/r0.6.0#get-matrix-client-r0-presence-userid-status
+func (cli *Client) GetStatus(mxid string) (resp *RespUserStatus, err error) {
+	urlPath := cli.BuildURL("presence", mxid, "status")
+	err = cli.MakeRequest("GET", urlPath, nil, &resp)
+	return
+}
+
+// GetOwnStatus returns the user's status. See https://matrix.org/docs/spec/client_server/r0.6.0#get-matrix-client-r0-presence-userid-status
+func (cli *Client) GetOwnStatus() (resp *RespUserStatus, err error) {
+	return cli.GetStatus(cli.UserID)
+}
+
+// SetStatus sets the user's status. See https://matrix.org/docs/spec/client_server/r0.6.0#put-matrix-client-r0-presence-userid-status
+func (cli *Client) SetStatus(presence, status string) (err error) {
+	urlPath := cli.BuildURL("presence", cli.UserID, "status")
+	s := struct {
+		Presence  string `json:"presence"`
+		StatusMsg string `json:"status_msg"`
+	}{presence, status}
+	err = cli.MakeRequest("PUT", urlPath, &s, nil)
+	return
+}
+
 // SendMessageEvent sends a message event into a room. See http://matrix.org/docs/spec/client_server/r0.2.0.html#put-matrix-client-r0-rooms-roomid-send-eventtype-txnid
 // contentJSON should be a pointer to something that can be encoded as JSON using json.Marshal.
 func (cli *Client) SendMessageEvent(roomID string, eventType string, contentJSON interface{}) (resp *RespSendEvent, err error) {
diff --git a/responses.go b/responses.go
index effb609..6213ac3 100644
--- a/responses.go
+++ b/responses.go
@@ -113,6 +113,14 @@ type RespUserDisplayName struct {
 	DisplayName string `json:"displayname"`
 }
 
+// RespUserStatus is the JSON response for https://matrix.org/docs/spec/client_server/r0.6.0#get-matrix-client-r0-presence-userid-status
+type RespUserStatus struct {
+	Presence        string `json:"presence"`
+	StatusMsg       string `json:"status_msg"`
+	lastActiveAgo   int    `json:"last_active_ago"`
+	currentlyActive bool   `json:"currently_active"`
+}
+
 // RespRegister is the JSON response for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-register
 type RespRegister struct {
 	AccessToken  string `json:"access_token"`