mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
WTF-758 Open HN comments if there's no external link (#802)
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>
This commit is contained in:
parent
86b32b3f9f
commit
e679a981da
1
go.mod
1
go.mod
@ -62,6 +62,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
|
||||
|
2
go.sum
2
go.sum
@ -377,6 +377,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=
|
||||
|
@ -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()
|
||||
}
|
||||
|
50
modules/hackernews/story_test.go
Normal file
50
modules/hackernews/story_test.go
Normal file
@ -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)
|
||||
})
|
||||
}
|
||||
}
|
@ -99,18 +99,27 @@ func (widget *Widget) content() (string, string, bool) {
|
||||
return title, str, false
|
||||
}
|
||||
|
||||
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)
|
||||
func (widget *Widget) openComments() {
|
||||
story := widget.selectedStory()
|
||||
if story != nil {
|
||||
utils.OpenFile(story.CommentLink())
|
||||
}
|
||||
}
|
||||
|
||||
func (widget *Widget) openComments() {
|
||||
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))
|
||||
func (widget *Widget) openStory() {
|
||||
story := widget.selectedStory()
|
||||
if story != nil {
|
||||
utils.OpenFile(story.Link())
|
||||
}
|
||||
}
|
||||
|
||||
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]
|
||||
}
|
||||
|
||||
return story
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user