~abyxcos/matt

bf463a2fdb69cc32d100a97b23769c975eb453d3 — abyxcos 2 years ago cb9e07c master
Move access token to the header, and hide some URL noise in to our helper functions. Start hashing out event structures.
1 files changed, 46 insertions(+), 27 deletions(-)

M matt_tricks.go
M matt_tricks.go => matt_tricks.go +46 -27
@@ 9,7 9,6 @@ import (
	"net/url"
)

// Account data structures
type Server struct {
	URL            string
	IdentityServer string


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

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


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

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


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

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


@@ 120,8 119,13 @@ func (account *Account) JoinRoom(room string) (string, error) {
		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)
	// TODO: Create a list of server names to join from.
	// room suffix, inviter's homeserver, your homeserver.
	serverName := "matrix.org"
	reqUrl := fmt.Sprintf("/join/%s?server_name=%s",
		url.QueryEscape(room),
		url.QueryEscape(serverName))
	err := account.post(reqUrl, &resp, nil)
	if err != nil {
		return "", err
	}


@@ 129,24 133,42 @@ func (account *Account) JoinRoom(room string) (string, error) {
	return resp.RoomId, nil
}

type ClientEvent struct {
	//Content		EventContent	`json:"content"`
	EventId		string			`json:"event_id"`
	SentTime		int				`json:"origin_server_ts"`
	RoomId		string			`json:"room_id"`
	Sender		string			`json:"sender"`
	StateKey		string			`json:"state_key"`
	Type			string			`json:"type"`
	Unsigned		UnsignedData	`json:"unsigned"`
}

type UnsignedData struct {
	Age				int				`json:"age"`
	//PrevContent	EventContent	`json:"prev_content"`
	Redeacted		*ClientEvent		`json:"redacted_because"`
	TransactionID	string			`json:"transaction_id"`
}

func (account *Account) GetEvents() ([]ClientEvent, error) {

	return nil, 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

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

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

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 {
func (account *Account) do(method, path string, resp, req interface{}) error {
	var reqBody io.Reader
	if method == "POST" {
		body, err := json.Marshal(req)


@@ 156,21 178,18 @@ func (a *Account) do(method, path string, params url.Values, resp, req interface
		reqBody = bytes.NewReader(body)
	}

	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)
	httpReq, err := http.NewRequest(method,
		account.Server.URL+"/_matrix/client/v3"+path,
		reqBody)
	if err != nil {
		return fmt.Errorf("New request: %w", err)
	}

	reply, err := a.Do(httpReq)
	if account.AccessToken != "" {
		httpReq.Header.Add("Authorization", "Bearer " + account.AccessToken)
	}

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