~abyxcos/matt

cb9e07c5718ac388014ccfff9bac0abafce02509 — abyxcos 1 year, 6 months ago f2cc313
Support joining rooms (but not getting events yet) and clean up the http code to URL escape URLs that need to be escaped.
2 files changed, 48 insertions(+), 10 deletions(-)

M main.go
M matt_tricks.go
M main.go => main.go +8 -0
@@ 157,6 157,14 @@ func main() {
					s.Printf("[%s] - %s", room.ID, alias)
				}
			}()
		case "/join":
			s.Printf("Joining %s", command[1])
			roomId, err := account.JoinRoom(command[1])
			if err != nil {
				s.Printf("Error joining %s: %s", command[1], err)
				continue
			}
			s.Printf("Joined %s", roomId)
		default:
			s.Printf("Unknown command, %s", command[0])
		}

M matt_tricks.go => matt_tricks.go +40 -10
@@ 6,6 6,7 @@ import (
	"fmt"
	"io"
	"net/http"
	"net/url"
)

// Account data structures


@@ 67,7 68,7 @@ func (account *Account) Login(password string) error {
		return err
	}

	err = account.post("/_matrix/client/v3/login", &resp, json.RawMessage(requestBody))
	err = account.post("/_matrix/client/v3/login", nil, &resp, requestBody)
	if err != nil {
		return err
	}


@@ 86,7 87,7 @@ func (account *Account) GetRooms() ([]Room, error) {
		JoinedRooms []string `json:"joined_rooms"`
	}

	err := account.get("/_matrix/client/v3/joined_rooms", &resp)
	err := account.get("/_matrix/client/v3/joined_rooms", nil, &resp)
	if err != nil {
		return nil, err
	}


@@ 106,7 107,7 @@ func (account *Account) GetRoomAlias(roomId string) (string, error) {
		Alias string `json:"alias"`
	}

	err := account.get("/_matrix/client/v3/rooms/"+roomId+"/state/m.room.canonical_alias", &resp)
	err := account.get("/_matrix/client/v3/rooms/"+url.QueryEscape(roomId)+"/state/m.room.canonical_alias", nil, &resp)
	if err != nil {
		return "", err
	}


@@ 114,19 115,38 @@ func (account *Account) GetRoomAlias(roomId string) (string, error) {
	return resp.Alias, nil
}

func (account *Account) JoinRoom(room string) (string, error) {
	var resp struct {
		RoomId string `json:"room_id"`
	}

	params := url.Values{"server_name": []string{"matrix.org"}}
	err := account.post("/_matrix/client/v3/join/"+url.QueryEscape(room), params, &resp, nil)
	if err != nil {
		return "", err
	}

	return resp.RoomId, nil
}

// HTTP client helper functions
// Inspiration taken from the Decred wallet http client implementation
// https://github.com/decred/dcrwallet/blob/master/internal/vsp/client.go

func (a *Account) get(path string, resp interface{}) error {
	return a.do("GET", path, resp, nil)
type Params struct{
	key	string
	value	string
}

func (a *Account) post(path string, resp, req interface{}) error {
	return a.do("POST", path, resp, req)
func (a *Account) get(path string, params url.Values, resp interface{}) error {
	return a.do("GET", path, params, resp, nil)
}

func (a *Account) do(method, path string, resp, req interface{}) error {
func (a *Account) post(path string, params url.Values, resp, req interface{}) error {
	return a.do("POST", path, params, resp, req)
}

func (a *Account) do(method, path string, params url.Values, resp, req interface{}) error {
	var reqBody io.Reader
	if method == "POST" {
		body, err := json.Marshal(req)


@@ 135,11 155,21 @@ func (a *Account) do(method, path string, resp, req interface{}) error {
		}
		reqBody = bytes.NewReader(body)
	}
	httpReq, err := http.NewRequest(method, a.Server.URL+path+
		"?access_token="+a.AccessToken, reqBody)

	if params == nil {
		params = url.Values{}
	}

	if a.AccessToken != "" {
		params.Add("access_token", a.AccessToken)
	}

	url := a.Server.URL+path+"?"+params.Encode()
	httpReq, err := http.NewRequest(method, url, reqBody)
	if err != nil {
		return fmt.Errorf("New request: %w", err)
	}

	reply, err := a.Do(httpReq)
	if err != nil {
		return fmt.Errorf("%s %s: %w", method, httpReq.URL.String(), err)