Skip to content

Commit

Permalink
Add wrapper for preview_url endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Feb 15, 2022
1 parent 46c5271 commit 441b081
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
12 changes: 12 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1246,6 +1246,18 @@ func (cli *Client) UploadMedia(data ReqUploadMedia) (*RespMediaUpload, error) {
return &m, err
}

// GetURLPreview asks the homeserver to fetch a preview for a given URL.
//
// See https://spec.matrix.org/v1.2/client-server-api/#get_matrixmediav3preview_url
func (cli *Client) GetURLPreview(url string) (*RespPreviewURL, error) {
reqURL := cli.BuildBaseURLWithQuery(URLPath{"_matrix", "media", "r0", "preview_url"}, map[string]string{
"url": url,
})
var output RespPreviewURL
_, err := cli.MakeRequest(http.MethodGet, reqURL, nil, &output)
return &output, err
}

// JoinedMembers returns a map of joined room members. See https://matrix.org/docs/spec/client_server/r0.4.0.html#get-matrix-client-r0-joined-rooms
//
// In general, usage of this API is discouraged in favour of /sync, as calling this API can race with incoming membership changes.
Expand Down
15 changes: 15 additions & 0 deletions responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,21 @@ type RespMediaUpload struct {
ContentURI id.ContentURI `json:"content_uri"`
}

// RespPreviewURL is the JSON response for https://spec.matrix.org/v1.2/client-server-api/#get_matrixmediav3preview_url
type RespPreviewURL struct {
CanonicalURL string `json:"og:url,omitempty"`
Title string `json:"og:title,omitempty"`
Type string `json:"og:type,omitempty"`
Description string `json:"og:description,omitempty"`

ImageURL id.ContentURIString `json:"og:image,omitempty"`

ImageSize int `json:"matrix:image:size,omitempty"`
ImageWidth int `json:"og:image:width,omitempty"`
ImageHeight int `json:"og:image:height,omitempty"`
ImageType string `json:"og:image:type,omitempty"`
}

// RespUserInteractive is the JSON response for https://matrix.org/docs/spec/client_server/r0.2.0.html#user-interactive-authentication-api
type RespUserInteractive struct {
Flows []struct {
Expand Down

0 comments on commit 441b081

Please sign in to comment.