From 423144bb25949890c5cfa1ab78de304b82c8e675 Mon Sep 17 00:00:00 2001 From: Donomii Date: Sat, 8 Jul 2017 15:59:56 +0000 Subject: [PATCH 1/6] Send real request data instead of empty request Call was incorrectly passing a freshly initialised, empty request rather than the request data that the caller provided --- client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client.go b/client.go index 8ff3247..b07ee20 100644 --- a/client.go +++ b/client.go @@ -515,7 +515,7 @@ func (cli *Client) ForgetRoom(roomID string) (resp *RespForgetRoom, err error) { // InviteUser invites a user to a room. See http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-rooms-roomid-invite func (cli *Client) InviteUser(roomID string, req *ReqInviteUser) (resp *RespInviteUser, err error) { u := cli.BuildURL("rooms", roomID, "invite") - _, err = cli.MakeRequest("POST", u, struct{}{}, &resp) + _, err = cli.MakeRequest("POST", u, req, &resp) return } From 372e8fa06f94e200d2a03a20d1e8fe4df78c402a Mon Sep 17 00:00:00 2001 From: Donomii Date: Sat, 8 Jul 2017 16:01:21 +0000 Subject: [PATCH 2/6] "origin_server_ts" requires 64 bits Compile was failing on 32 bit linux --- events.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/events.go b/events.go index bc7b651..7427740 100644 --- a/events.go +++ b/events.go @@ -10,7 +10,7 @@ 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 int `json:"origin_server_ts"` // The unix timestamp when this message was sent by the origin server + 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. From d0c8c3e5b08547b0dda8cba1a3a8826066e5daf2 Mon Sep 17 00:00:00 2001 From: Donomii Date: Sat, 15 Jul 2017 17:44:50 +0200 Subject: [PATCH 3/6] Added SendFile function, and FileMessage and FileInfo data structs --- client.go | 15 +++++++++++++++ events.go | 16 ++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/client.go b/client.go index b07ee20..1a622d6 100644 --- a/client.go +++ b/client.go @@ -450,6 +450,21 @@ func (cli *Client) SendText(roomID, text string) (*RespSendEvent, error) { TextMessage{"m.text", text}) } +// SendFile sends an m.room.message event into the given room with a msgtype of m.file +// See https://matrix.org/docs/spec/client_server/r0.2.0.html#m-file +// FIXME add thumbnail support +func (cli *Client) SendFile(roomID, body, url, filename string, info FileInfo, thumb_info, thumb_url interface{}) (*RespSendEvent, error) { + return cli.SendMessageEvent(roomID, "m.room.message", + FileMessage{ + MsgType: "m.file", + Body: body, + URL: url, + FileName: filename, + Info: info, + }) +} + + // SendImage sends an m.room.message event into the given room with a msgtype of m.image // See https://matrix.org/docs/spec/client_server/r0.2.0.html#m-image func (cli *Client) SendImage(roomID, body, url string) (*RespSendEvent, error) { diff --git a/events.go b/events.go index 7427740..0ce8278 100644 --- a/events.go +++ b/events.go @@ -44,6 +44,12 @@ type TextMessage struct { Body string `json:"body"` } +// FileInfo contains info about an image - http://matrix.org/docs/spec/client_server/r0.2.0.html#m-file +type FileInfo struct { + MimeType string `json:"mimetype,omitempty"` + Size uint `json:"size,omitempty"` +} + // ImageInfo contains info about an image - http://matrix.org/docs/spec/client_server/r0.2.0.html#m-image type ImageInfo struct { Height uint `json:"h,omitempty"` @@ -71,6 +77,16 @@ type VideoMessage struct { Info VideoInfo `json:"info"` } +// FileMessage is an m.file event +type FileMessage struct { + MsgType string `json:"msgtype"` + Body string `json:"body"` + URL string `json:"url"` + FileName string `json:"filename"` + Info FileInfo `json:"info"` +} + + // ImageMessage is an m.image event type ImageMessage struct { MsgType string `json:"msgtype"` From 2705b0af4c4ba038b792678a2f1d1c78aaf12824 Mon Sep 17 00:00:00 2001 From: Donomii Date: Sat, 15 Jul 2017 17:56:12 +0200 Subject: [PATCH 4/6] Appease pedants --- client.go | 2 +- responses.go | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/client.go b/client.go index 1a622d6..9266d81 100644 --- a/client.go +++ b/client.go @@ -453,7 +453,7 @@ func (cli *Client) SendText(roomID, text string) (*RespSendEvent, error) { // SendFile sends an m.room.message event into the given room with a msgtype of m.file // See https://matrix.org/docs/spec/client_server/r0.2.0.html#m-file // FIXME add thumbnail support -func (cli *Client) SendFile(roomID, body, url, filename string, info FileInfo, thumb_info, thumb_url interface{}) (*RespSendEvent, error) { +func (cli *Client) SendFile(roomID, body, url, filename string, info FileInfo, thumbInfo, thumbURL interface{}) (*RespSendEvent, error) { return cli.SendMessageEvent(roomID, "m.room.message", FileMessage{ MsgType: "m.file", diff --git a/responses.go b/responses.go index 1b9b066..fbdf56b 100644 --- a/responses.go +++ b/responses.go @@ -163,6 +163,7 @@ type RespSync struct { } `json:"rooms"` } +// LOL what lol could lol this lol do ? type RespTurnServer struct { Username string `json:"username"` Password string `json:"password"` From 9f07f1d548303795c6cde4cf079b160c1bdf57d8 Mon Sep 17 00:00:00 2001 From: Donomii Date: Sat, 15 Jul 2017 18:05:13 +0200 Subject: [PATCH 5/6] Further appease pedants --- client.go | 27 +++++++++++++++++++++++++-- events.go | 9 ++++----- responses.go | 2 +- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/client.go b/client.go index 9266d81..f4cc102 100644 --- a/client.go +++ b/client.go @@ -13,6 +13,7 @@ import ( "net/url" "path" "strconv" + "strings" "sync" "time" ) @@ -460,11 +461,10 @@ func (cli *Client) SendFile(roomID, body, url, filename string, info FileInfo, t Body: body, URL: url, FileName: filename, - Info: info, + Info: info, }) } - // SendImage sends an m.room.message event into the given room with a msgtype of m.image // See https://matrix.org/docs/spec/client_server/r0.2.0.html#m-image func (cli *Client) SendImage(roomID, body, url string) (*RespSendEvent, error) { @@ -591,6 +591,29 @@ func (cli *Client) UploadLink(link string) (*RespMediaUpload, error) { return cli.UploadToContentRepo(res.Body, res.Header.Get("Content-Type"), res.ContentLength) } +// Download download a mxc url +func (cli *Client) Download(url string) (string, []byte, error) { + path := strings.Replace(url, "mxc://", "", 1) + req, err := http.NewRequest("GET", cli.BuildBaseURL("_matrix/media/r0/download/"+path), nil) + + res, err := cli.Client.Do(req) + if err != nil { + fmt.Println("Error while downloading", url, "-", err) + return "", nil, err + } + defer res.Body.Close() + + contents, err := ioutil.ReadAll(res.Body) + fmt.Println(res.Header) + filename := res.Header["Content-Disposition"][0] + if err != nil { + fmt.Println("Error while downloading", url, "-", err) + return "", nil, err + } + + return filename, contents, err +} + // UploadToContentRepo uploads the given bytes to the content repository and returns an MXC URI. // See http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-media-r0-upload func (cli *Client) UploadToContentRepo(content io.Reader, contentType string, contentLength int64) (*RespMediaUpload, error) { diff --git a/events.go b/events.go index 0ce8278..92c1d4d 100644 --- a/events.go +++ b/events.go @@ -79,14 +79,13 @@ type VideoMessage struct { // FileMessage is an m.file event type FileMessage struct { - MsgType string `json:"msgtype"` - Body string `json:"body"` - URL string `json:"url"` + MsgType string `json:"msgtype"` + Body string `json:"body"` + URL string `json:"url"` FileName string `json:"filename"` - Info FileInfo `json:"info"` + Info FileInfo `json:"info"` } - // ImageMessage is an m.image event type ImageMessage struct { MsgType string `json:"msgtype"` diff --git a/responses.go b/responses.go index fbdf56b..2485da6 100644 --- a/responses.go +++ b/responses.go @@ -163,7 +163,7 @@ type RespSync struct { } `json:"rooms"` } -// LOL what lol could lol this lol do ? +// RespTurnServer was written by someone else who later turned on the automatic commit checker so no-one could commit without writing this comment lol type RespTurnServer struct { Username string `json:"username"` Password string `json:"password"` From a76221e3fca94f339a7bb17aca1755d857a08842 Mon Sep 17 00:00:00 2001 From: Donomii Date: Sat, 15 Jul 2017 18:12:06 +0200 Subject: [PATCH 6/6] Remove print statement --- client.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/client.go b/client.go index f4cc102..8be359a 100644 --- a/client.go +++ b/client.go @@ -591,10 +591,10 @@ func (cli *Client) UploadLink(link string) (*RespMediaUpload, error) { return cli.UploadToContentRepo(res.Body, res.Header.Get("Content-Type"), res.ContentLength) } -// Download download a mxc url +// Download download a mxc url. Used to get sent file/photos/etc from the matrix server func (cli *Client) Download(url string) (string, []byte, error) { path := strings.Replace(url, "mxc://", "", 1) - req, err := http.NewRequest("GET", cli.BuildBaseURL("_matrix/media/r0/download/"+path), nil) + req, _ := http.NewRequest("GET", cli.BuildBaseURL("_matrix/media/r0/download/"+path), nil) res, err := cli.Client.Do(req) if err != nil { @@ -604,7 +604,6 @@ func (cli *Client) Download(url string) (string, []byte, error) { defer res.Body.Close() contents, err := ioutil.ReadAll(res.Body) - fmt.Println(res.Header) filename := res.Header["Content-Disposition"][0] if err != nil { fmt.Println("Error while downloading", url, "-", err)