diff --git a/client.go b/client.go index d0bb0c5..6cb811a 100644 --- a/client.go +++ b/client.go @@ -776,6 +776,89 @@ func (cli *Client) TurnServer() (resp *RespTurnServer, err error) { return } +// WhoAmI Gets information about the owner of a given access token. +// See https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-account-whoami +func (cli *Client) WhoAmI() (resp *RespWhoAmI, err error) { + u := cli.BuildURL("account", "whoami") + err = cli.MakeRequest("GET", u, struct{}{}, &resp) + return +} + +// RoomAlias requests that the server resolve a room alias to a room ID. +// See https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-directory-room-roomalias +func (cli *Client) RoomAlias(roomAlias string) (resp *RespRoomAlias, err error) { + u := cli.BuildURL("directory", "room", roomAlias) + err = cli.MakeRequest("GET", u, struct{}{}, &resp) + return +} + +// EmailRequestToken requests email from homeserver so that it email be bound to existing account after validation. +// See https://matrix.org/docs/spec/client_server/r0.6.1#post-matrix-client-r0-account-3pid-email-requesttoken +func (cli *Client) Account3PidEmailRequestToken(req ReqEmailRequestToken) (resp *RespEmailRequestToken, err error) { + u := cli.BuildURL("account", "3pid", "email", "requestToken") + err = cli.MakeRequest("POST", u, req, &resp) + return +} + +// GetAccountData gets some account_data for the client. +// See https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-user-userid-account-data-type +func (cli *Client) GetAccountData(req ReqGetAccountData) (resp RespAccountData, err error) { + resp = make(RespAccountData) + u := cli.BuildURL("user", cli.UserID, "account_data", req.Type) + err = cli.MakeRequest("GET", u, nil, &resp) + return +} + +// PutAccountData sets some account_data for the client. +// See https://matrix.org/docs/spec/client_server/r0.6.1#put-matrix-client-r0-user-userid-account-data-type +func (cli *Client) PutAccountData(req ReqPutAccountData) (err error) { + u := cli.BuildURL("user", cli.UserID, "account_data", req.Type) + err = cli.MakeRequest("PUT", u, req.Data, nil) + return +} + +// GetDevices gets information about all devices for the current user. +// See https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-devices +func (cli *Client) GetDevices() (resp RespGetDevices, err error) { + resp.Devices = make([]Device, 0) + u := cli.BuildURL("devices") + err = cli.MakeRequest("GET", u, nil, &resp) + return +} + +// GetThreePID gets a list of the third party identifiers that the homeserver has associated with the user's account. +// See https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-account-3pid +func (cli *Client) GetThreePID() (resp RespGetThreePID, err error) { + u := cli.BuildURL("account", "3pid") + err = cli.MakeRequest("GET", u, nil, &resp) + return +} + +// Available checks to see if a username is available, and valid, for the server. +// See https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-register-available +func (cli *Client) Available(username string) (err error) { + u := cli.BuildURLWithQuery( + []string{"register", "available"}, + map[string]string{ + "username": username, + }) + err = cli.MakeRequest("GET", u, nil, nil) + return +} + +// PowerLevels gets most recent m.room.power_levels event. +// See https://matrix.org/docs/spec/client_server/r0.6.1#m-room-power-levels +func (cli *Client) PowerLevels(roomID string) (resp PowerLevels, err error) { + err = cli.StateEvent(roomID, "m.room.power_levels", "", &resp) + return +} + +// SendPowerLevels sends m.room.power_levels event. +// See https://matrix.org/docs/spec/client_server/r0.6.1#m-room-power-levels +func (cli *Client) SendPowerLevels(roomID string, pl PowerLevels) (*RespSendEvent, error) { + return cli.SendStateEvent(roomID, "m.room.power_levels", "", pl) +} + func txnID() string { return "go" + strconv.FormatInt(time.Now().UnixNano(), 10) } diff --git a/events.go b/events.go index cbc70a8..2d90c93 100644 --- a/events.go +++ b/events.go @@ -143,6 +143,24 @@ type AudioMessage struct { Info AudioInfo `json:"info,omitempty"` } +// PowerLevels is and m.room.power_levels event - https://matrix.org/docs/spec/client_server/r0.6.1#m-room-power-levels +type PowerLevels struct { + Ban int `json:"ban"` + Invite int `json:"invite"` + Kick int `json:"kick"` + Redact int `json:"redact"` + Events map[string]int `json:"events"` + Users map[string]int `json:"users"` + Notifications NotificationPowerLevel `json:"notifications"` + EventsDefault int `json:"events_default"` + StateDefault int `json:"state_default"` + UsersDefault int `json:"users_default"` +} + +type NotificationPowerLevel struct { + Room int `json:"room"` +} + var htmlRegex = regexp.MustCompile("<[^<]+?>") // GetHTMLMessage returns an HTMLMessage with the body set to a stripped version of the provided HTML, in addition diff --git a/requests.go b/requests.go index 31c426d..069c4ee 100644 --- a/requests.go +++ b/requests.go @@ -77,3 +77,27 @@ type ReqTyping struct { Typing bool `json:"typing"` Timeout int64 `json:"timeout"` } + +// ReqGetAccountData is the JSON request for https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-user-userid-account-data-type +type ReqGetAccountData struct { + Type string +} + +// ReqPutAccountData is the JSON request for https://matrix.org/docs/spec/client_server/r0.6.1#put-matrix-client-r0-user-userid-account-data-type +type ReqPutAccountData struct { + ReqGetAccountData + Data map[string]interface{} +} + +// ReqEmailRequestToken is the JSON request for +// https://matrix.org/docs/spec/client_server/r0.6.1#post-matrix-client-r0-register-email-requesttoken +// https://matrix.org/docs/spec/client_server/r0.6.1#post-matrix-client-r0-account-password-email-requesttoken +// https://matrix.org/docs/spec/client_server/r0.6.1#post-matrix-client-r0-account-3pid-email-requesttoken +type ReqEmailRequestToken struct { + IdServer string `json:"id_server,omitempty"` + IdAccessToken string `json:"id_access_token,omitempty"` + Secret string `json:"client_secret"` + Email string `json:"email"` + SendAttempt int `json:"send_attempt"` + NextLink string `json:"next_link,omitempty"` +} diff --git a/responses.go b/responses.go index f488e69..fb4e608 100644 --- a/responses.go +++ b/responses.go @@ -208,3 +208,49 @@ type RespTurnServer struct { TTL int `json:"ttl"` URIs []string `json:"uris"` } + +// RespWhoAmI is JSON response for https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-account-whoami +type RespWhoAmI struct { + UserId string `json:"user_id"` +} + +// RespRoomAlias is JSON response for https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-directory-room-roomalias +type RespRoomAlias struct { + RoomID string `json:"room_id"` + Servers []string `json:"servers"` +} + +// RespGetDevices is JSON response for https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-devices +type RespGetDevices struct { + Devices []Device `json:"devices"` +} + +type Device struct { + DeviceId string `json:"device_id" example:"l4kRnv3u"` + DisplayName string `json:"display_name" example:"web"` + LastSeenTs int `json:"last_seen_ts" example:"1620644706232"` +} + +// RespGetThreePID is JSON response for https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-account-3pid +type RespGetThreePID struct { + ThreePIDs []ThreePID `json:"threepids"` +} + +type ThreePID struct { + AddedAt int `json:"added_at"` + Address string `json:"address"` + Medium string `json:"medium"` + ValidatedAt int `json:"validated_at"` +} + +// RespAccountData is JSON response for https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-user-userid-account-data-type +type RespAccountData map[string]interface{} + +// RespEmailRequestToken is JSON response for +// https://matrix.org/docs/spec/client_server/r0.6.1#post-matrix-client-r0-register-email-requesttoken +// https://matrix.org/docs/spec/client_server/r0.6.1#post-matrix-client-r0-account-password-email-requesttoken +// https://matrix.org/docs/spec/client_server/r0.6.1#post-matrix-client-r0-account-3pid-email-requesttoken +type RespEmailRequestToken struct { + Sid string `json:"sid"` + SumbitURL string `json:"submit_url"` +}