From 1c8d411cddd12e1755e0419ec6a4cf580e4b96c0 Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Sat, 28 Dec 2019 11:26:52 -0500 Subject: [PATCH] WTF-758 Open HN comments if there's no external link If an HN story has no external link associated with it, open the HN comments page for the story. Closes #758 Signed-off-by: Chris Cummer --- go.mod | 1 + go.sum | 2 ++ modules/hackernews/story.go | 23 +++++++++++++++ modules/hackernews/story_test.go | 50 ++++++++++++++++++++++++++++++++ modules/hackernews/widget.go | 23 ++++++++++----- 5 files changed, 92 insertions(+), 7 deletions(-) create mode 100644 modules/hackernews/story_test.go diff --git a/go.mod b/go.mod index 1469ce4d2..d898e55b4 100644 --- a/go.mod +++ b/go.mod @@ -60,6 +60,7 @@ require ( gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect gopkg.in/jarcoal/httpmock.v1 v1.0.0-20181110093347-3be5f16b70eb // indirect gopkg.in/yaml.v2 v2.2.7 + gotest.tools v2.2.0+incompatible k8s.io/api v0.0.0-20191010143144-fbf594f18f80 // indirect k8s.io/apimachinery v0.0.0-20191016060620-86f2f1b9c076 k8s.io/client-go v0.0.0-20190620085101-78d2af792bab diff --git a/go.sum b/go.sum index aa20ec99b..cc4981d08 100644 --- a/go.sum +++ b/go.sum @@ -375,6 +375,8 @@ gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo= gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo= +gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a h1:LJwr7TCTghdatWv40WobzlKXc9c4s8oGa7QKJUtHhWA= diff --git a/modules/hackernews/story.go b/modules/hackernews/story.go index bed3e310b..38179e443 100644 --- a/modules/hackernews/story.go +++ b/modules/hackernews/story.go @@ -1,5 +1,12 @@ package hackernews +import "fmt" + +const ( + hnStoryPath = "https://news.ycombinator.com/item?id=" +) + +// Story represents a story submission on HackerNews type Story struct { By string `json:"by"` Descendants int `json:"descendants"` @@ -11,3 +18,19 @@ type Story struct { Type string `json:"type"` URL string `json:"url"` } + +// CommentLink return the link to the HackerNews story comments page +func (story *Story) CommentLink() string { + return fmt.Sprintf("%s%d", hnStoryPath, story.ID) +} + +// Link returns the link to a story. If the story has an external link, that is returned +// If the story has no external link, the HackerNews comments link is returned instead +func (story *Story) Link() string { + if story.URL != "" { + return story.URL + } + + // Fall back to the HackerNews comment link + return story.CommentLink() +} diff --git a/modules/hackernews/story_test.go b/modules/hackernews/story_test.go new file mode 100644 index 000000000..dc927bb8c --- /dev/null +++ b/modules/hackernews/story_test.go @@ -0,0 +1,50 @@ +package hackernews + +import ( + "testing" + + "gotest.tools/assert" +) + +func Test_CommentLink(t *testing.T) { + story := Story{ + ID: 3, + } + + assert.Equal(t, "https://news.ycombinator.com/item?id=3", story.CommentLink()) +} + +func Test_Link(t *testing.T) { + tests := []struct { + name string + id int + url string + expected string + }{ + { + name: "no external link", + id: 1, + url: "", + expected: "https://news.ycombinator.com/item?id=1", + }, + { + name: "with external link", + id: 1, + url: "https://www.link.ca", + expected: "https://www.link.ca", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + story := Story{ + ID: tt.id, + URL: tt.url, + } + + actual := story.Link() + + assert.Equal(t, tt.expected, actual) + }) + } +} diff --git a/modules/hackernews/widget.go b/modules/hackernews/widget.go index 688780edc..52def0e81 100644 --- a/modules/hackernews/widget.go +++ b/modules/hackernews/widget.go @@ -99,18 +99,27 @@ func (widget *Widget) content() (string, string, bool) { return title, str, false } +func (widget *Widget) openComments() { + story := widget.selectedStory() + if story != nil { + utils.OpenFile(story.CommentLink()) + } +} + func (widget *Widget) openStory() { - sel := widget.GetSelected() - if sel >= 0 && widget.stories != nil && sel < len(widget.stories) { - story := &widget.stories[sel] - utils.OpenFile(story.URL) + story := widget.selectedStory() + if story != nil { + utils.OpenFile(story.Link()) } } -func (widget *Widget) openComments() { +func (widget *Widget) selectedStory() *Story { + var story *Story + sel := widget.GetSelected() if sel >= 0 && widget.stories != nil && sel < len(widget.stories) { - story := &widget.stories[sel] - utils.OpenFile(fmt.Sprintf("https://news.ycombinator.com/item?id=%d", story.ID)) + story = &widget.stories[sel] } + + return story }