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:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user