mirror of
				https://github.com/matrix-org/gomatrix
				synced 2025-11-03 22:08:04 +00:00 
			
		
		
		
	Merge branch 'master' into master
This commit is contained in:
		
						commit
						f42613437d
					
				
					 12 changed files with 318 additions and 50 deletions
				
			
		
							
								
								
									
										21
									
								
								.golangci.yml
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								.golangci.yml
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,21 @@
 | 
			
		|||
run:
 | 
			
		||||
  timeout: 5m
 | 
			
		||||
  linters:
 | 
			
		||||
    enable:
 | 
			
		||||
      - vet
 | 
			
		||||
      - vetshadow
 | 
			
		||||
      - typecheck
 | 
			
		||||
      - deadcode
 | 
			
		||||
      - gocyclo
 | 
			
		||||
      - golint
 | 
			
		||||
      - varcheck
 | 
			
		||||
      - structcheck
 | 
			
		||||
      - maligned
 | 
			
		||||
      - ineffassign
 | 
			
		||||
      - misspell
 | 
			
		||||
      - unparam
 | 
			
		||||
      - goimports
 | 
			
		||||
      - goconst
 | 
			
		||||
      - unconvert
 | 
			
		||||
      - errcheck
 | 
			
		||||
      - interfacer
 | 
			
		||||
| 
						 | 
				
			
			@ -1,9 +1,7 @@
 | 
			
		|||
language: go
 | 
			
		||||
go:
 | 
			
		||||
 - 1.10.x
 | 
			
		||||
 - 1.13.10
 | 
			
		||||
install:
 | 
			
		||||
 - go get github.com/golang/lint/golint
 | 
			
		||||
 - go get github.com/fzipp/gocyclo
 | 
			
		||||
 - go get github.com/client9/misspell/...
 | 
			
		||||
 - go get github.com/gordonklaus/ineffassign
 | 
			
		||||
 - go get github.com/golangci/golangci-lint/cmd/golangci-lint@v1.24.0
 | 
			
		||||
 - go build
 | 
			
		||||
script: ./hooks/pre-commit
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										102
									
								
								client.go
									
										
									
									
									
								
							
							
						
						
									
										102
									
								
								client.go
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -55,10 +55,7 @@ func (e HTTPError) Error() string {
 | 
			
		|||
 | 
			
		||||
// BuildURL builds a URL with the Client's homserver/prefix/access_token set already.
 | 
			
		||||
func (cli *Client) BuildURL(urlPath ...string) string {
 | 
			
		||||
	ps := []string{cli.Prefix}
 | 
			
		||||
	for _, p := range urlPath {
 | 
			
		||||
		ps = append(ps, p)
 | 
			
		||||
	}
 | 
			
		||||
	ps := append([]string{cli.Prefix}, urlPath...)
 | 
			
		||||
	return cli.BuildBaseURL(ps...)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -357,7 +354,7 @@ func (cli *Client) Login(req *ReqLogin) (resp *RespLogin, err error) {
 | 
			
		|||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Logout the current user. See http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-logout
 | 
			
		||||
// Logout the current user. See http://matrix.org/docs/spec/client_server/r0.6.0.html#post-matrix-client-r0-logout
 | 
			
		||||
// This does not clear the credentials from the client instance. See ClearCredentials() instead.
 | 
			
		||||
func (cli *Client) Logout() (resp *RespLogout, err error) {
 | 
			
		||||
	urlPath := cli.BuildURL("logout")
 | 
			
		||||
| 
						 | 
				
			
			@ -365,6 +362,14 @@ func (cli *Client) Logout() (resp *RespLogout, err error) {
 | 
			
		|||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// LogoutAll logs the current user out on all devices. See https://matrix.org/docs/spec/client_server/r0.6.0#post-matrix-client-r0-logout-all
 | 
			
		||||
// This does not clear the credentials from the client instance. See ClearCredentails() instead.
 | 
			
		||||
func (cli *Client) LogoutAll() (resp *RespLogoutAll, err error) {
 | 
			
		||||
	urlPath := cli.BuildURL("logout/all")
 | 
			
		||||
	err = cli.MakeRequest("POST", urlPath, nil, &resp)
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Versions returns the list of supported Matrix versions on this homeserver. See http://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-client-versions
 | 
			
		||||
func (cli *Client) Versions() (resp *RespVersions, err error) {
 | 
			
		||||
	urlPath := cli.BuildBaseURL("_matrix", "client", "versions")
 | 
			
		||||
| 
						 | 
				
			
			@ -372,6 +377,53 @@ func (cli *Client) Versions() (resp *RespVersions, err error) {
 | 
			
		|||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// PublicRooms returns the list of public rooms on target server. See https://matrix.org/docs/spec/client_server/r0.6.0#get-matrix-client-unstable-publicrooms
 | 
			
		||||
func (cli *Client) PublicRooms(limit int, since string, server string) (resp *RespPublicRooms, err error) {
 | 
			
		||||
	args := map[string]string{}
 | 
			
		||||
 | 
			
		||||
	if limit != 0 {
 | 
			
		||||
		args["limit"] = strconv.Itoa(limit)
 | 
			
		||||
	}
 | 
			
		||||
	if since != "" {
 | 
			
		||||
		args["since"] = since
 | 
			
		||||
	}
 | 
			
		||||
	if server != "" {
 | 
			
		||||
		args["server"] = server
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	urlPath := cli.BuildURLWithQuery([]string{"publicRooms"}, args)
 | 
			
		||||
	err = cli.MakeRequest("GET", urlPath, nil, &resp)
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// PublicRoomsFiltered returns a subset of PublicRooms filtered server side.
 | 
			
		||||
// See https://matrix.org/docs/spec/client_server/r0.6.0#post-matrix-client-unstable-publicrooms
 | 
			
		||||
func (cli *Client) PublicRoomsFiltered(limit int, since string, server string, filter string) (resp *RespPublicRooms, err error) {
 | 
			
		||||
	content := map[string]string{}
 | 
			
		||||
 | 
			
		||||
	if limit != 0 {
 | 
			
		||||
		content["limit"] = strconv.Itoa(limit)
 | 
			
		||||
	}
 | 
			
		||||
	if since != "" {
 | 
			
		||||
		content["since"] = since
 | 
			
		||||
	}
 | 
			
		||||
	if filter != "" {
 | 
			
		||||
		content["filter"] = filter
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var urlPath string
 | 
			
		||||
	if server == "" {
 | 
			
		||||
		urlPath = cli.BuildURL("publicRooms")
 | 
			
		||||
	} else {
 | 
			
		||||
		urlPath = cli.BuildURLWithQuery([]string{"publicRooms"}, map[string]string{
 | 
			
		||||
			"server": server,
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	err = cli.MakeRequest("POST", urlPath, content, &resp)
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// JoinRoom joins the client to a room ID or alias. See http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-join-roomidoralias
 | 
			
		||||
//
 | 
			
		||||
// If serverName is specified, this will be added as a query param to instruct the homeserver to join via that server. If content is specified, it will
 | 
			
		||||
| 
						 | 
				
			
			@ -442,6 +494,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) {
 | 
			
		||||
| 
						 | 
				
			
			@ -463,7 +538,14 @@ func (cli *Client) SendStateEvent(roomID, eventType, stateKey string, contentJSO
 | 
			
		|||
// See http://matrix.org/docs/spec/client_server/r0.2.0.html#m-text
 | 
			
		||||
func (cli *Client) SendText(roomID, text string) (*RespSendEvent, error) {
 | 
			
		||||
	return cli.SendMessageEvent(roomID, "m.room.message",
 | 
			
		||||
		TextMessage{"m.text", text})
 | 
			
		||||
		TextMessage{MsgType: "m.text", Body: text})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// SendFormattedText sends an m.room.message event into the given room with a msgtype of m.text, supports a subset of HTML for formatting.
 | 
			
		||||
// See https://matrix.org/docs/spec/client_server/r0.6.0#m-text
 | 
			
		||||
func (cli *Client) SendFormattedText(roomID, text, formattedText string) (*RespSendEvent, error) {
 | 
			
		||||
	return cli.SendMessageEvent(roomID, "m.room.message",
 | 
			
		||||
		TextMessage{MsgType: "m.text", Body: text, FormattedBody: formattedText, Format: "org.matrix.custom.html"})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// SendImage sends an m.room.message event into the given room with a msgtype of m.image
 | 
			
		||||
| 
						 | 
				
			
			@ -492,7 +574,7 @@ func (cli *Client) SendVideo(roomID, body, url string) (*RespSendEvent, error) {
 | 
			
		|||
// See http://matrix.org/docs/spec/client_server/r0.2.0.html#m-notice
 | 
			
		||||
func (cli *Client) SendNotice(roomID, text string) (*RespSendEvent, error) {
 | 
			
		||||
	return cli.SendMessageEvent(roomID, "m.room.message",
 | 
			
		||||
		TextMessage{"m.notice", text})
 | 
			
		||||
		TextMessage{MsgType: "m.notice", Body: text})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// RedactEvent redacts the given event. See http://matrix.org/docs/spec/client_server/r0.2.0.html#put-matrix-client-r0-rooms-roomid-redact-eventid-txnid
 | 
			
		||||
| 
						 | 
				
			
			@ -503,6 +585,12 @@ func (cli *Client) RedactEvent(roomID, eventID string, req *ReqRedact) (resp *Re
 | 
			
		|||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// MarkRead marks eventID in roomID as read, signifying the event, and all before it have been read. See https://matrix.org/docs/spec/client_server/r0.6.0#post-matrix-client-r0-rooms-roomid-receipt-receipttype-eventid
 | 
			
		||||
func (cli *Client) MarkRead(roomID, eventID string) error {
 | 
			
		||||
	urlPath := cli.BuildURL("rooms", roomID, "receipt", "m.read", eventID)
 | 
			
		||||
	return cli.MakeRequest("POST", urlPath, nil, nil)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CreateRoom creates a new Matrix room. See https://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-createroom
 | 
			
		||||
//  resp, err := cli.CreateRoom(&gomatrix.ReqCreateRoom{
 | 
			
		||||
//  	Preset: "public_chat",
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -46,7 +46,7 @@ func Example_customInterfaces() {
 | 
			
		|||
	cli.Client = http.DefaultClient
 | 
			
		||||
 | 
			
		||||
	// Once you call a function, you can't safely change the interfaces.
 | 
			
		||||
	cli.SendText("!foo:bar", "Down the rabbit hole")
 | 
			
		||||
	_, _ = cli.SendText("!foo:bar", "Down the rabbit hole")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func ExampleClient_BuildURLWithQuery() {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -84,6 +84,55 @@ func TestClient_StateEvent(t *testing.T) {
 | 
			
		|||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestClient_PublicRooms(t *testing.T) {
 | 
			
		||||
	cli := mockClient(func(req *http.Request) (*http.Response, error) {
 | 
			
		||||
		if req.Method == "GET" && req.URL.Path == "/_matrix/client/r0/publicRooms" {
 | 
			
		||||
			return &http.Response{
 | 
			
		||||
				StatusCode: 200,
 | 
			
		||||
				Body: ioutil.NopCloser(bytes.NewBufferString(`{
 | 
			
		||||
  "chunk": [
 | 
			
		||||
    {
 | 
			
		||||
      "aliases": [
 | 
			
		||||
        "#murrays:cheese.bar"
 | 
			
		||||
      ],
 | 
			
		||||
      "avatar_url": "mxc://bleeker.street/CHEDDARandBRIE",
 | 
			
		||||
      "guest_can_join": false,
 | 
			
		||||
      "name": "CHEESE",
 | 
			
		||||
      "num_joined_members": 37,
 | 
			
		||||
      "room_id": "!ol19s:bleecker.street",
 | 
			
		||||
      "topic": "Tasty tasty cheese",
 | 
			
		||||
      "world_readable": true
 | 
			
		||||
    }
 | 
			
		||||
  ],
 | 
			
		||||
  "next_batch": "p190q",
 | 
			
		||||
  "prev_batch": "p1902",
 | 
			
		||||
  "total_room_count_estimate": 115
 | 
			
		||||
}`)),
 | 
			
		||||
			}, nil
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return nil, fmt.Errorf("unhandled URL: %s", req.URL.Path)
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
	publicRooms, err := cli.PublicRooms(0, "", "")
 | 
			
		||||
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Fatalf("PublicRooms: error, got %s", err.Error())
 | 
			
		||||
	}
 | 
			
		||||
	if publicRooms.TotalRoomCountEstimate != 115 {
 | 
			
		||||
		t.Fatalf("PublicRooms: got %d, want %d", publicRooms.TotalRoomCountEstimate, 115)
 | 
			
		||||
	}
 | 
			
		||||
	if len(publicRooms.Chunk) != 1 {
 | 
			
		||||
		t.Fatalf("PublicRooms: got %d, want %d", len(publicRooms.Chunk), 1)
 | 
			
		||||
	}
 | 
			
		||||
	if publicRooms.Chunk[0].Name != "CHEESE" {
 | 
			
		||||
		t.Fatalf("PublicRooms: got %s, want %s", publicRooms.Chunk[0].Name, "CHEESE")
 | 
			
		||||
	}
 | 
			
		||||
	if publicRooms.Chunk[0].NumJoinedMembers != 37 {
 | 
			
		||||
		t.Fatalf("PublicRooms: got %d, want %d", publicRooms.Chunk[0].NumJoinedMembers, 37)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func mockClient(fn func(*http.Request) (*http.Response, error)) *Client {
 | 
			
		||||
	mrt := MockRoundTripper{
 | 
			
		||||
		RT: fn,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										25
									
								
								events.go
									
										
									
									
									
								
							
							
						
						
									
										25
									
								
								events.go
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -7,15 +7,16 @@ import (
 | 
			
		|||
 | 
			
		||||
// Event represents a single Matrix event.
 | 
			
		||||
type Event struct {
 | 
			
		||||
	StateKey  *string                `json:"state_key,omitempty"` // The state key for the event. Only present on State Events.
 | 
			
		||||
	Sender    string                 `json:"sender"`              // The user ID of the sender of the event
 | 
			
		||||
	Type      string                 `json:"type"`                // The event type
 | 
			
		||||
	Timestamp int64                  `json:"origin_server_ts"`    // The unix timestamp when this message was sent by the origin server
 | 
			
		||||
	ID        string                 `json:"event_id"`            // The unique ID of this event
 | 
			
		||||
	RoomID    string                 `json:"room_id"`             // The room the event was sent to. May be nil (e.g. for presence)
 | 
			
		||||
	Content   map[string]interface{} `json:"content"`             // The JSON content of the event.
 | 
			
		||||
	Redacts   string                 `json:"redacts,omitempty"`   // The event ID that was redacted if a m.room.redaction event
 | 
			
		||||
	Unsigned  map[string]interface{} `json:"unsigned"`            // The unsigned portions of the event, such as age and prev_content
 | 
			
		||||
	StateKey    *string                `json:"state_key,omitempty"`    // The state key for the event. Only present on State Events.
 | 
			
		||||
	Sender      string                 `json:"sender"`                 // The user ID of the sender of the event
 | 
			
		||||
	Type        string                 `json:"type"`                   // The event type
 | 
			
		||||
	Timestamp   int64                  `json:"origin_server_ts"`       // The unix timestamp when this message was sent by the origin server
 | 
			
		||||
	ID          string                 `json:"event_id"`               // The unique ID of this event
 | 
			
		||||
	RoomID      string                 `json:"room_id"`                // The room the event was sent to. May be nil (e.g. for presence)
 | 
			
		||||
	Redacts     string                 `json:"redacts,omitempty"`      // The event ID that was redacted if a m.room.redaction event
 | 
			
		||||
	Unsigned    map[string]interface{} `json:"unsigned"`               // The unsigned portions of the event, such as age and prev_content
 | 
			
		||||
	Content     map[string]interface{} `json:"content"`                // The JSON content of the event.
 | 
			
		||||
	PrevContent map[string]interface{} `json:"prev_content,omitempty"` // The JSON prev_content of the event.
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Body returns the value of the "body" key in the event content if it is
 | 
			
		||||
| 
						 | 
				
			
			@ -42,8 +43,10 @@ func (event *Event) MessageType() (msgtype string, ok bool) {
 | 
			
		|||
 | 
			
		||||
// TextMessage is the contents of a Matrix formated message event.
 | 
			
		||||
type TextMessage struct {
 | 
			
		||||
	MsgType string `json:"msgtype"`
 | 
			
		||||
	Body    string `json:"body"`
 | 
			
		||||
	MsgType       string `json:"msgtype"`
 | 
			
		||||
	Body          string `json:"body"`
 | 
			
		||||
	FormattedBody string `json:"formatted_body"`
 | 
			
		||||
	Format        string `json:"format"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ThumbnailInfo contains info about an thumbnail image - http://matrix.org/docs/spec/client_server/r0.2.0.html#m-image
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										3
									
								
								go.mod
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								go.mod
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,3 @@
 | 
			
		|||
module github.com/matrix-org/gomatrix
 | 
			
		||||
 | 
			
		||||
go 1.12
 | 
			
		||||
| 
						 | 
				
			
			@ -2,9 +2,6 @@
 | 
			
		|||
 | 
			
		||||
set -eu
 | 
			
		||||
 | 
			
		||||
golint
 | 
			
		||||
misspell --error .
 | 
			
		||||
 | 
			
		||||
# gofmt doesn't exit with an error code if the files don't match the expected
 | 
			
		||||
# format. So we have to run it and see if it outputs anything.
 | 
			
		||||
if gofmt -l -s . 2>&1 | read
 | 
			
		||||
| 
						 | 
				
			
			@ -18,9 +15,5 @@ then
 | 
			
		|||
    exit 1
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
ineffassign .
 | 
			
		||||
 | 
			
		||||
go fmt
 | 
			
		||||
go tool vet --all --shadow .
 | 
			
		||||
gocyclo -over 12 .
 | 
			
		||||
go test -timeout 5s -test.v
 | 
			
		||||
golangci-lint run
 | 
			
		||||
go test -timeout 5s . ./...
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										69
									
								
								identifier.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										69
									
								
								identifier.go
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,69 @@
 | 
			
		|||
package gomatrix
 | 
			
		||||
 | 
			
		||||
// Identifier is the interface for https://matrix.org/docs/spec/client_server/r0.6.0#identifier-types
 | 
			
		||||
type Identifier interface {
 | 
			
		||||
	// Returns the identifier type
 | 
			
		||||
	// https://matrix.org/docs/spec/client_server/r0.6.0#identifier-types
 | 
			
		||||
	Type() string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// UserIdentifier is the Identifier for https://matrix.org/docs/spec/client_server/r0.6.0#matrix-user-id
 | 
			
		||||
type UserIdentifier struct {
 | 
			
		||||
	IDType string `json:"type"` // Set by NewUserIdentifer
 | 
			
		||||
	User   string `json:"user"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Type implements the Identifier interface
 | 
			
		||||
func (i UserIdentifier) Type() string {
 | 
			
		||||
	return "m.id.user"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// NewUserIdentifier creates a new UserIdentifier with IDType set to "m.id.user"
 | 
			
		||||
func NewUserIdentifier(user string) UserIdentifier {
 | 
			
		||||
	return UserIdentifier{
 | 
			
		||||
		IDType: "m.id.user",
 | 
			
		||||
		User:   user,
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ThirdpartyIdentifier is the Identifier for https://matrix.org/docs/spec/client_server/r0.6.0#third-party-id
 | 
			
		||||
type ThirdpartyIdentifier struct {
 | 
			
		||||
	IDType  string `json:"type"` // Set by NewThirdpartyIdentifier
 | 
			
		||||
	Medium  string `json:"medium"`
 | 
			
		||||
	Address string `json:"address"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Type implements the Identifier interface
 | 
			
		||||
func (i ThirdpartyIdentifier) Type() string {
 | 
			
		||||
	return "m.id.thirdparty"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// NewThirdpartyIdentifier creates a new UserIdentifier with IDType set to "m.id.user"
 | 
			
		||||
func NewThirdpartyIdentifier(medium, address string) ThirdpartyIdentifier {
 | 
			
		||||
	return ThirdpartyIdentifier{
 | 
			
		||||
		IDType:  "m.id.thirdparty",
 | 
			
		||||
		Medium:  medium,
 | 
			
		||||
		Address: address,
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// PhoneIdentifier is the Identifier for https://matrix.org/docs/spec/client_server/r0.6.0#phone-number
 | 
			
		||||
type PhoneIdentifier struct {
 | 
			
		||||
	IDType  string `json:"type"` // Set by NewPhoneIdentifier
 | 
			
		||||
	Country string `json:"country"`
 | 
			
		||||
	Phone   string `json:"phone"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Type implements the Identifier interface
 | 
			
		||||
func (i PhoneIdentifier) Type() string {
 | 
			
		||||
	return "m.id.phone"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// NewPhoneIdentifier creates a new UserIdentifier with IDType set to "m.id.user"
 | 
			
		||||
func NewPhoneIdentifier(country, phone string) PhoneIdentifier {
 | 
			
		||||
	return PhoneIdentifier{
 | 
			
		||||
		IDType:  "m.id.phone",
 | 
			
		||||
		Country: country,
 | 
			
		||||
		Phone:   phone,
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										19
									
								
								requests.go
									
										
									
									
									
								
							
							
						
						
									
										19
									
								
								requests.go
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -10,16 +10,17 @@ type ReqRegister struct {
 | 
			
		|||
	Auth                     interface{} `json:"auth,omitempty"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ReqLogin is the JSON request for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-login
 | 
			
		||||
// ReqLogin is the JSON request for http://matrix.org/docs/spec/client_server/r0.6.0.html#post-matrix-client-r0-login
 | 
			
		||||
type ReqLogin struct {
 | 
			
		||||
	Type                     string `json:"type"`
 | 
			
		||||
	Password                 string `json:"password,omitempty"`
 | 
			
		||||
	Medium                   string `json:"medium,omitempty"`
 | 
			
		||||
	User                     string `json:"user,omitempty"`
 | 
			
		||||
	Address                  string `json:"address,omitempty"`
 | 
			
		||||
	Token                    string `json:"token,omitempty"`
 | 
			
		||||
	DeviceID                 string `json:"device_id,omitempty"`
 | 
			
		||||
	InitialDeviceDisplayName string `json:"initial_device_display_name,omitempty"`
 | 
			
		||||
	Type                     string     `json:"type"`
 | 
			
		||||
	Identifier               Identifier `json:"identifier,omitempty"`
 | 
			
		||||
	Password                 string     `json:"password,omitempty"`
 | 
			
		||||
	Medium                   string     `json:"medium,omitempty"`
 | 
			
		||||
	User                     string     `json:"user,omitempty"`
 | 
			
		||||
	Address                  string     `json:"address,omitempty"`
 | 
			
		||||
	Token                    string     `json:"token,omitempty"`
 | 
			
		||||
	DeviceID                 string     `json:"device_id,omitempty"`
 | 
			
		||||
	InitialDeviceDisplayName string     `json:"initial_device_display_name,omitempty"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ReqCreateRoom is the JSON request for https://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-createroom
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										42
									
								
								responses.go
									
										
									
									
									
								
							
							
						
						
									
										42
									
								
								responses.go
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -22,6 +22,14 @@ type RespVersions struct {
 | 
			
		|||
	Versions []string `json:"versions"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// RespPublicRooms is the JSON response for http://matrix.org/speculator/spec/HEAD/client_server/unstable.html#get-matrix-client-unstable-publicrooms
 | 
			
		||||
type RespPublicRooms struct {
 | 
			
		||||
	TotalRoomCountEstimate int          `json:"total_room_count_estimate"`
 | 
			
		||||
	PrevBatch              string       `json:"prev_batch"`
 | 
			
		||||
	NextBatch              string       `json:"next_batch"`
 | 
			
		||||
	Chunk                  []PublicRoom `json:"chunk"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// RespJoinRoom is the JSON response for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-rooms-roomid-join
 | 
			
		||||
type RespJoinRoom struct {
 | 
			
		||||
	RoomID string `json:"room_id"`
 | 
			
		||||
| 
						 | 
				
			
			@ -105,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"`
 | 
			
		||||
| 
						 | 
				
			
			@ -114,17 +130,31 @@ type RespRegister struct {
 | 
			
		|||
	UserID       string `json:"user_id"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// RespLogin is the JSON response for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-login
 | 
			
		||||
// RespLogin is the JSON response for http://matrix.org/docs/spec/client_server/r0.6.0.html#post-matrix-client-r0-login
 | 
			
		||||
type RespLogin struct {
 | 
			
		||||
	AccessToken string `json:"access_token"`
 | 
			
		||||
	DeviceID    string `json:"device_id"`
 | 
			
		||||
	HomeServer  string `json:"home_server"`
 | 
			
		||||
	UserID      string `json:"user_id"`
 | 
			
		||||
	AccessToken string               `json:"access_token"`
 | 
			
		||||
	DeviceID    string               `json:"device_id"`
 | 
			
		||||
	HomeServer  string               `json:"home_server"`
 | 
			
		||||
	UserID      string               `json:"user_id"`
 | 
			
		||||
	WellKnown   DiscoveryInformation `json:"well_known"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// RespLogout is the JSON response for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-logout
 | 
			
		||||
// DiscoveryInformation is the JSON Response for https://matrix.org/docs/spec/client_server/r0.6.0#get-well-known-matrix-client and a part of the JSON Response for https://matrix.org/docs/spec/client_server/r0.6.0#post-matrix-client-r0-login
 | 
			
		||||
type DiscoveryInformation struct {
 | 
			
		||||
	Homeserver struct {
 | 
			
		||||
		BaseURL string `json:"base_url"`
 | 
			
		||||
	} `json:"m.homeserver"`
 | 
			
		||||
	IdentityServer struct {
 | 
			
		||||
		BaseURL string `json:"base_url"`
 | 
			
		||||
	} `json:"m.identitiy_server"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// RespLogout is the JSON response for http://matrix.org/docs/spec/client_server/r0.6.0.html#post-matrix-client-r0-logout
 | 
			
		||||
type RespLogout struct{}
 | 
			
		||||
 | 
			
		||||
// RespLogoutAll is the JSON response for https://matrix.org/docs/spec/client_server/r0.6.0#post-matrix-client-r0-logout-all
 | 
			
		||||
type RespLogoutAll struct{}
 | 
			
		||||
 | 
			
		||||
// RespCreateRoom is the JSON response for https://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-createroom
 | 
			
		||||
type RespCreateRoom struct {
 | 
			
		||||
	RoomID string `json:"room_id"`
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										17
									
								
								room.go
									
										
									
									
									
								
							
							
						
						
									
										17
									
								
								room.go
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -6,6 +6,19 @@ type Room struct {
 | 
			
		|||
	State map[string]map[string]*Event
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// PublicRoom represents the information about a public room obtainable from the room directory
 | 
			
		||||
type PublicRoom struct {
 | 
			
		||||
	CanonicalAlias   string   `json:"canonical_alias"`
 | 
			
		||||
	Name             string   `json:"name"`
 | 
			
		||||
	WorldReadable    bool     `json:"world_readable"`
 | 
			
		||||
	Topic            string   `json:"topic"`
 | 
			
		||||
	NumJoinedMembers int      `json:"num_joined_members"`
 | 
			
		||||
	AvatarURL        string   `json:"avatar_url"`
 | 
			
		||||
	RoomID           string   `json:"room_id"`
 | 
			
		||||
	GuestCanJoin     bool     `json:"guest_can_join"`
 | 
			
		||||
	Aliases          []string `json:"aliases"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// UpdateState updates the room's current state with the given Event. This will clobber events based
 | 
			
		||||
// on the type/state_key combination.
 | 
			
		||||
func (room Room) UpdateState(event *Event) {
 | 
			
		||||
| 
						 | 
				
			
			@ -18,8 +31,8 @@ func (room Room) UpdateState(event *Event) {
 | 
			
		|||
 | 
			
		||||
// GetStateEvent returns the state event for the given type/state_key combo, or nil.
 | 
			
		||||
func (room Room) GetStateEvent(eventType string, stateKey string) *Event {
 | 
			
		||||
	stateEventMap, _ := room.State[eventType]
 | 
			
		||||
	event, _ := stateEventMap[stateKey]
 | 
			
		||||
	stateEventMap := room.State[eventType]
 | 
			
		||||
	event := stateEventMap[stateKey]
 | 
			
		||||
	return event
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue