From 2bb0261a02d6d95ba9fbbcf16d623c3d89d4902b Mon Sep 17 00:00:00 2001 From: Christopher Brown Date: Mon, 8 Aug 2022 23:22:31 -0400 Subject: [PATCH] remove hardcoded subreddit --- go.mod | 2 +- go.sum | 2 ++ server/index_handler.go | 2 +- server/reddit_comment.go | 15 +++++++++++++-- server/reddit_indexer.go | 3 +++ server/reddit_indexer_test.go | 2 ++ server/reddit_post.go | 3 +++ server/static/index.js | 9 +++++---- 8 files changed, 30 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index 420293b..14c1539 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,7 @@ require ( github.com/hashicorp/hcl v0.0.0-20171017181929-23c074d0eceb github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af github.com/json-iterator/go v1.1.5 - github.com/kevinburke/go-bindata v3.21.0+incompatible // indirect + github.com/kevinburke/go-bindata v3.23.0+incompatible // indirect github.com/labstack/echo v3.2.6+incompatible github.com/labstack/gommon v0.2.2-0.20170925052817-57409ada9da0 github.com/magiconair/properties v1.7.6 diff --git a/go.sum b/go.sum index 46319ee..1bb49eb 100644 --- a/go.sum +++ b/go.sum @@ -29,6 +29,8 @@ github.com/kevinburke/go-bindata v3.16.0+incompatible h1:TFzFZop2KxGhqNwsyjgmIh5 github.com/kevinburke/go-bindata v3.16.0+incompatible/go.mod h1:/pEEZ72flUW2p0yi30bslSp9YqD9pysLxunQDdb2CPM= github.com/kevinburke/go-bindata v3.21.0+incompatible h1:baK7hwFJDlAHrOqmE9U3u8tow1Uc5ihN9E/b7djcK2g= github.com/kevinburke/go-bindata v3.21.0+incompatible/go.mod h1:/pEEZ72flUW2p0yi30bslSp9YqD9pysLxunQDdb2CPM= +github.com/kevinburke/go-bindata v3.23.0+incompatible h1:rqNOXZlqrYhMVVAsQx8wuc+LaA73YcfbQ407wAykyS8= +github.com/kevinburke/go-bindata v3.23.0+incompatible/go.mod h1:/pEEZ72flUW2p0yi30bslSp9YqD9pysLxunQDdb2CPM= github.com/labstack/echo v3.2.6+incompatible h1:28U/uXFFKBIP+VQIqq641LuYhMS7Br9ZlwEXERaohCc= github.com/labstack/echo v3.2.6+incompatible/go.mod h1:0INS7j/VjnFxD4E2wkz67b8cVwCLbBmJyDaka6Cmk1s= github.com/labstack/gommon v0.2.2-0.20170925052817-57409ada9da0 h1:7AIW1qc9sYYTZLamTsRKSmVvJDXkZZrIWXHDK4Gq4X0= diff --git a/server/index_handler.go b/server/index_handler.go index ee7bc72..b08c1f6 100644 --- a/server/index_handler.go +++ b/server/index_handler.go @@ -68,7 +68,7 @@ var index = ` - + ` diff --git a/server/reddit_comment.go b/server/reddit_comment.go index 8de747d..d5bae23 100644 --- a/server/reddit_comment.go +++ b/server/reddit_comment.go @@ -13,6 +13,9 @@ type RedditComment struct { PostId string `json:"post_id"` PostTitle string `json:"post_title"` Time time.Time `json:"time"` + + // Added in August 2022. Comments stored before then may not have this. + Subreddit string `json:"subreddit"` } func (c *RedditComment) ActivityTime() time.Time { @@ -25,9 +28,17 @@ func (c *RedditComment) ActivityKey() uint32 { } func (c *RedditComment) PostURL() string { - return fmt.Sprintf("https://www.reddit.com/r/pathofexile/comments/%v/", c.PostId) + subreddit := "pathofexile" + if c.Subreddit != "" { + subreddit = c.Subreddit + } + return fmt.Sprintf("https://www.reddit.com/r/%v/comments/%v/", subreddit, c.PostId) } func (c *RedditComment) CommentURL() string { - return fmt.Sprintf("https://www.reddit.com/r/pathofexile/comments/%v/-/%v/?context=3", c.PostId, c.Id) + subreddit := "pathofexile" + if c.Subreddit != "" { + subreddit = c.Subreddit + } + return fmt.Sprintf("https://www.reddit.com/r/%v/comments/%v/-/%v/?context=3", subreddit, c.PostId, c.Id) } diff --git a/server/reddit_indexer.go b/server/reddit_indexer.go index 4c6fe04..88f8e52 100644 --- a/server/reddit_indexer.go +++ b/server/reddit_indexer.go @@ -83,6 +83,7 @@ func ParseRedditActivity(b []byte) ([]Activity, string, error) { CreatedUTC float64 `json:"created_utc"` LinkId string `json:"link_id"` LinkTitle string `json:"link_title"` + Subreddit string `json:"subreddit"` } `json:"data"` } `json:"children"` } `json:"data"` @@ -105,6 +106,7 @@ func ParseRedditActivity(b []byte) ([]Activity, string, error) { PostId: strings.TrimPrefix(thing.Data.LinkId, "t3_"), PostTitle: thing.Data.LinkTitle, Time: time.Unix(int64(thing.Data.CreatedUTC), 0), + Subreddit: thing.Data.Subreddit, }) case "t3": activity = append(activity, &RedditPost{ @@ -115,6 +117,7 @@ func ParseRedditActivity(b []byte) ([]Activity, string, error) { Title: thing.Data.Title, URL: thing.Data.URL, Time: time.Unix(int64(thing.Data.CreatedUTC), 0), + Subreddit: thing.Data.Subreddit, }) } } diff --git a/server/reddit_indexer_test.go b/server/reddit_indexer_test.go index 13b1f94..6079a0d 100644 --- a/server/reddit_indexer_test.go +++ b/server/reddit_indexer_test.go @@ -30,6 +30,7 @@ func TestParseRedditComments(t *testing.T) { assert.Equal(t, "/r/pathofexile/comments/5q12qc/another_update_on_singapore_fibre_cuts/", post.Permalink) assert.Equal(t, "Another Update on Singapore Fibre Cuts", post.Title) assert.Equal(t, time.Unix(1485316926, 0), post.Time) + assert.Equal(t, "pathofexile", post.Subreddit) comment, ok := activity[1].(*RedditComment) require.True(t, ok) @@ -37,4 +38,5 @@ func TestParseRedditComments(t *testing.T) { assert.Equal(t, "5plxw0", comment.PostId) assert.Equal(t, "Development Manifesto: Solo Self-Found Support in 2.6.0", comment.PostTitle) assert.Equal(t, time.Unix(1485287813, 0), comment.Time) + assert.Equal(t, "pathofexile", comment.Subreddit) } diff --git a/server/reddit_post.go b/server/reddit_post.go index fa238a2..f910bd8 100644 --- a/server/reddit_post.go +++ b/server/reddit_post.go @@ -13,6 +13,9 @@ type RedditPost struct { BodyHTML string `json:"body_html,omitempty"` Permalink string `json:"permalink"` Time time.Time `json:"time"` + + // Added in August 2022. Comments stored before then may not have this. + Subreddit string `json:"subreddit"` } func (p *RedditPost) ActivityTime() time.Time { diff --git a/server/static/index.js b/server/static/index.js index 3677f7a..4b65dbe 100644 --- a/server/static/index.js +++ b/server/static/index.js @@ -30,6 +30,7 @@ function loadActivity() { for (var i = 0; i < data.activity.length; ++i) { var type = data.activity[i].type; var activity = data.activity[i].data; + var subreddit = activity.subreddit || 'pathofexile'; var $tr = $('').addClass(type == 'forum_post' ? 'forum' : 'reddit').addClass(type.replace('/_/', '-')); var $toggleTD = $(''); @@ -42,7 +43,7 @@ function loadActivity() { )); } else if (type == 'reddit_comment') { $tr.append($('').append($('') - .attr('href', 'https://www.reddit.com/r/pathofexile/comments/' + activity.post_id) + .attr('href', 'https://www.reddit.com/r/' + subreddit + '/comments/' + activity.post_id) .append($('')) )); } else { @@ -64,7 +65,7 @@ function loadActivity() { )); } else if (type == "reddit_comment") { $tr.append($('').append($('') - .attr('href', 'https://www.reddit.com/r/pathofexile/comments/' + activity.post_id + '/-/' + activity.id + '/?context=3') + .attr('href', 'https://www.reddit.com/r/' + subreddit + '/comments/' + activity.post_id + '/-/' + activity.id + '/?context=3') .text(activity.post_title) )); } @@ -90,8 +91,8 @@ function loadActivity() { )); } else { $tr.append($('').append($('') - .attr('href', 'https://www.reddit.com/r/pathofexile') - .text('pathofexile') + .attr('href', 'https://www.reddit.com/r/' + subreddit) + .text(subreddit) )); }