Add embedded urls attribute to live thread updates

Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
Vartan Benohanian 2020-09-18 09:55:28 -04:00
parent e73b89f0b8
commit 1eb75d163c
3 changed files with 52 additions and 7 deletions

View file

@ -36,7 +36,7 @@ type LiveThread struct {
ViewerCount int `json:"viewer_count"`
ViewerCountFuzzed bool `json:"viewer_count_fuzzed"`
// Empty when a list thread has ended.
// Empty when a live thread has ended.
WebSocketURL string `json:"websocket_url,omitempty"`
Announcement bool `json:"is_announcement"`
@ -51,11 +51,45 @@ type LiveThreadUpdate struct {
Created *Timestamp `json:"created_utc,omitempty"`
Body string `json:"body,omitempty"`
// todo: add "embeds" field?
EmbeddedURLs []string `json:"embeds,omitempty"`
Stricken bool `json:"stricken"`
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (u *LiveThreadUpdate) UnmarshalJSON(b []byte) error {
root := new(struct {
ID string `json:"id"`
FullID string `json:"name"`
Author string `json:"author"`
Created *Timestamp `json:"created_utc"`
Body string `json:"body"`
EmbeddedURLs []struct {
URL string `json:"url"`
} `json:"embeds"`
Stricken bool `json:"stricken"`
})
err := json.Unmarshal(b, root)
if err != nil {
return err
}
u.ID = root.ID
u.FullID = root.FullID
u.Author = root.Author
u.Created = root.Created
u.Body = root.Body
u.Stricken = root.Stricken
for _, eu := range root.EmbeddedURLs {
u.EmbeddedURLs = append(u.EmbeddedURLs, eu.URL)
}
return nil
}
// LiveThreadCreateOrUpdateRequest represents a request to create/update a live thread.
type LiveThreadCreateOrUpdateRequest struct {
// No longer than 120 characters.
@ -432,7 +466,6 @@ func (s *LiveThreadService) Accept(ctx context.Context, id string) (*Response, e
}
// Leave the live thread by abdicating your status as contributor.
// todo: test as the author who leaves the thread.
func (s *LiveThreadService) Leave(ctx context.Context, id string) (*Response, error) {
form := url.Values{}
form.Set("api_type", "json")

View file

@ -76,6 +76,7 @@ var expectedLiveThreadUpdates = []*LiveThreadUpdate{
Created: &Timestamp{time.Date(2020, 9, 18, 4, 35, 24, 0, time.UTC)},
Body: "test 2",
EmbeddedURLs: []string{"https://example.com", "https://reddit.com"},
Stricken: true,
},

View file

@ -11,7 +11,18 @@
"name": "LiveUpdate_5e46cd94-f968-11ea-9a6a-0e1933241e7d",
"mobile_embeds": [],
"author": "testuser1",
"embeds": [],
"embeds": [
{
"url": "https://example.com",
"width": 485,
"height": 0
},
{
"url": "https://reddit.com",
"width": 485,
"height": 0
}
],
"created": 1600432524.0,
"created_utc": 1600403724.0,
"body_html": "&lt;div class=\"md\"&gt;&lt;p&gt;test 2&lt;/p&gt;\n&lt;/div&gt;",