Skip to content

Commit 067cbe5

Browse files
authored
Merge pull request #13 from Hashnode/scriptonist/get-all-responses
Fix to show all responses
2 parents 1c8f318 + 32a72ba commit 067cbe5

File tree

4 files changed

+135
-32
lines changed

4 files changed

+135
-32
lines changed

pkg/posts/hot.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,7 @@ func GetHotPosts() {
7575

7676
func makeRequest(url string) ([]byte, error) {
7777

78-
client := http.Client{
79-
Timeout: time.Duration(1 * time.Minute),
80-
}
78+
client := getHttpClient()
8179
resp, err := client.Get(url)
8280
if err != nil {
8381
return nil, err
@@ -97,6 +95,12 @@ func makeRequest(url string) ([]byte, error) {
9795
return nil, fmt.Errorf("Not Found")
9896

9997
}
98+
func getHttpClient() *http.Client {
99+
100+
return &http.Client{
101+
Timeout: time.Duration(1 * time.Minute),
102+
}
103+
}
100104

101105
// Types
102106

pkg/posts/post.go

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func openPost(app *tview.Application, postcuid string, list *tview.List) {
5151
log.Fatal(err)
5252
}
5353

54-
title := fmt.Sprintf("\nTitle: %s", singlePost.Post.Title)
54+
title := fmt.Sprintf("\nTitle: [::b]%s[-:-:-]", singlePost.Post.Title)
5555
var author string
5656
if singlePost.Post.Author.Name != "" {
5757
author = fmt.Sprintf("Author: %s", singlePost.Post.Author.Name)
@@ -60,12 +60,14 @@ func openPost(app *tview.Application, postcuid string, list *tview.List) {
6060
}
6161

6262
reactions := fmt.Sprintf("Reactions: %d", singlePost.Post.TotalReactions)
63+
responses := fmt.Sprintf("Responses: %d", singlePost.Post.ResponseCount)
6364
ptype := fmt.Sprintf("Type: %s", singlePost.Post.Type)
6465
link := fmt.Sprintf("Link: https://hashnode.com/post/%s", singlePost.Post.Cuid)
6566
writeToTextView(textView,
6667
title,
6768
author,
6869
reactions,
70+
responses,
6971
ptype,
7072
link,
7173
"\n",
@@ -78,34 +80,7 @@ func openPost(app *tview.Application, postcuid string, list *tview.List) {
7880
return ""
7981
}(),
8082
)
81-
noresponse := len(singlePost.Post.Responses)
82-
for ind, response := range singlePost.Post.Responses {
83-
writeToTextView(
84-
textView,
85-
fmt.Sprintf("\n[green]Response %d/%d[white]", ind+1, noresponse),
86-
fmt.Sprintf("[green]--------------[green]"),
87-
renderTerminal(response.ContentMarkdown),
88-
)
89-
if len(response.Replies) > 0 {
90-
writeToTextView(textView,
91-
"\n[yellow]Replies[white]",
92-
"[yellow]=======[white]",
93-
)
94-
noreplies := len(response.Replies)
95-
for indreply, reply := range response.Replies {
96-
writeToTextView(
97-
textView,
98-
fmt.Sprintf("\n[yellow]Reply %d/%d[white]", indreply+1, noreplies),
99-
fmt.Sprintf("[yellow]~~~~~~~~~~~[white]"),
100-
fmt.Sprintf("Author: %s", reply.Author.Name),
101-
indentMarkdown(renderTerminal(reply.ContentMarkdown), "\t"),
102-
)
103-
104-
}
105-
}
106-
107-
}
108-
83+
openResponses(textView, singlePost.Post.ID, singlePost.Post.ResponseCount)
10984
textView.SetDoneFunc(func(key tcell.Key) {
11085
if key == tcell.KeyEscape {
11186
if err := app.SetRoot(list, true).SetFocus(list).Run(); err != nil {

pkg/posts/responses.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package posts
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io/ioutil"
7+
"log"
8+
"net/url"
9+
10+
"github.com/rivo/tview"
11+
)
12+
13+
func openResponses(t *tview.TextView, postcuid string, totalResponses int) {
14+
15+
const defaultSortOrder = "totalReactions"
16+
17+
r, err := getResponses(postcuid, totalResponses, 1, defaultSortOrder)
18+
if err != nil {
19+
log.Println(err)
20+
}
21+
var responsesAPI responsesAPI
22+
err = json.Unmarshal(r, &responsesAPI)
23+
if err != nil {
24+
log.Println(err)
25+
}
26+
27+
noresponse := len(responsesAPI.Responses)
28+
for ind, response := range responsesAPI.Responses {
29+
writeToTextView(
30+
t,
31+
fmt.Sprintf("\n[green]Response %d/%d[white]", ind+1, noresponse),
32+
fmt.Sprintf("[green]--------------[green]"),
33+
renderTerminal(response.ContentMarkdown),
34+
)
35+
if len(response.Replies) > 0 {
36+
writeToTextView(t,
37+
"\n[yellow]Replies[white]",
38+
"[yellow]=======[white]",
39+
)
40+
noreplies := len(response.Replies)
41+
for indreply, reply := range response.Replies {
42+
writeToTextView(
43+
t,
44+
fmt.Sprintf("\n[yellow]Reply %d/%d[white]", indreply+1, noreplies),
45+
fmt.Sprintf("[yellow]~~~~~~~~~~~[white]"),
46+
fmt.Sprintf("Author: %s", reply.Author.Name),
47+
indentMarkdown(renderTerminal(reply.ContentMarkdown), "\t"),
48+
)
49+
50+
}
51+
}
52+
53+
}
54+
55+
}
56+
57+
func getResponses(postID string, perPage, page int, sortOrder string) ([]byte, error) {
58+
const apiURL = "https://hashnode.com/ajax/responses"
59+
u, err := url.Parse(apiURL)
60+
if err != nil {
61+
log.Fatal(err)
62+
}
63+
64+
q := u.Query()
65+
q.Set("post_id", postID)
66+
q.Set("page", fmt.Sprintf("%d", page))
67+
q.Set("per_page", fmt.Sprintf("%d", perPage))
68+
q.Set("sort_order", sortOrder)
69+
u.RawQuery = q.Encode()
70+
71+
client := getHttpClient()
72+
resp, err := client.Get(u.String())
73+
if err != nil {
74+
return nil, err
75+
}
76+
defer resp.Body.Close()
77+
78+
b, err := ioutil.ReadAll(resp.Body)
79+
if err != nil {
80+
return nil, err
81+
}
82+
return b, err
83+
}
84+
85+
type responsesAPI struct {
86+
Pagination struct {
87+
Page string `json:"page"`
88+
PerPage string `json:"per_page"`
89+
Total int `json:"total"`
90+
} `json:"pagination"`
91+
Order string `json:"order"`
92+
Responses []Response
93+
}

pkg/posts/responses_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package posts
2+
3+
import "testing"
4+
5+
func Test_getResponses(t *testing.T) {
6+
type args struct {
7+
postID string
8+
perPage int
9+
page int
10+
sortOrder string
11+
}
12+
tests := []struct {
13+
name string
14+
args args
15+
}{
16+
{
17+
name: "test1",
18+
args: args{
19+
postID: "5c34ffe75aa5738b323c386b",
20+
page: 2,
21+
perPage: 5,
22+
sortOrder: "totalReactions",
23+
},
24+
},
25+
}
26+
for _, tt := range tests {
27+
t.Run(tt.name, func(t *testing.T) {
28+
getResponses(tt.args.postID, tt.args.perPage, tt.args.page, tt.args.sortOrder)
29+
})
30+
}
31+
}

0 commit comments

Comments
 (0)