Skip to content

Commit

Permalink
WTF-758 Open HN comments if there's no external link
Browse files Browse the repository at this point in the history
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 <chriscummer@me.com>
  • Loading branch information
senorprogrammer committed Dec 28, 2019
1 parent ac28176 commit 1c8d411
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 7 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
23 changes: 23 additions & 0 deletions modules/hackernews/story.go
Original file line number Diff line number Diff line change
@@ -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"`
Expand All @@ -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()
}
50 changes: 50 additions & 0 deletions modules/hackernews/story_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
23 changes: 16 additions & 7 deletions modules/hackernews/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 1c8d411

Please sign in to comment.