From 1eb75d163c494b3691ec197bc61740d4bcdda4b8 Mon Sep 17 00:00:00 2001 From: Vartan Benohanian Date: Fri, 18 Sep 2020 09:55:28 -0400 Subject: [PATCH] Add embedded urls attribute to live thread updates Signed-off-by: Vartan Benohanian --- reddit/live-thread.go | 43 +++++++++++++++++++++++++++---- reddit/live-thread_test.go | 3 ++- testdata/live-thread/updates.json | 13 +++++++++- 3 files changed, 52 insertions(+), 7 deletions(-) diff --git a/reddit/live-thread.go b/reddit/live-thread.go index f3ef078..b32d13b 100644 --- a/reddit/live-thread.go +++ b/reddit/live-thread.go @@ -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"` @@ -50,10 +50,44 @@ type LiveThreadUpdate struct { Author string `json:"author,omitempty"` Created *Timestamp `json:"created_utc,omitempty"` - Body string `json:"body,omitempty"` - // todo: add "embeds" field? + Body string `json:"body,omitempty"` + EmbeddedURLs []string `json:"embeds,omitempty"` + Stricken bool `json:"stricken"` +} - 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. @@ -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") diff --git a/reddit/live-thread_test.go b/reddit/live-thread_test.go index c68d433..3fb3d2f 100644 --- a/reddit/live-thread_test.go +++ b/reddit/live-thread_test.go @@ -75,7 +75,8 @@ var expectedLiveThreadUpdates = []*LiveThreadUpdate{ Author: "testuser1", Created: &Timestamp{time.Date(2020, 9, 18, 4, 35, 24, 0, time.UTC)}, - Body: "test 2", + Body: "test 2", + EmbeddedURLs: []string{"https://example.com", "https://reddit.com"}, Stricken: true, }, diff --git a/testdata/live-thread/updates.json b/testdata/live-thread/updates.json index 64f82e2..f8bbb04 100644 --- a/testdata/live-thread/updates.json +++ b/testdata/live-thread/updates.json @@ -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": "<div class=\"md\"><p>test 2</p>\n</div>",