Skip to content

Commit

Permalink
Merge pull request #13 from Hashnode/scriptonist/get-all-responses
Browse files Browse the repository at this point in the history
Fix to show all responses
  • Loading branch information
scriptonist authored Jan 22, 2019
2 parents 1c8f318 + 32a72ba commit 067cbe5
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 32 deletions.
10 changes: 7 additions & 3 deletions pkg/posts/hot.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ func GetHotPosts() {

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

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

}
func getHttpClient() *http.Client {

return &http.Client{
Timeout: time.Duration(1 * time.Minute),
}
}

// Types

Expand Down
33 changes: 4 additions & 29 deletions pkg/posts/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func openPost(app *tview.Application, postcuid string, list *tview.List) {
log.Fatal(err)
}

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

reactions := fmt.Sprintf("Reactions: %d", singlePost.Post.TotalReactions)
responses := fmt.Sprintf("Responses: %d", singlePost.Post.ResponseCount)
ptype := fmt.Sprintf("Type: %s", singlePost.Post.Type)
link := fmt.Sprintf("Link: https://hashnode.com/post/%s", singlePost.Post.Cuid)
writeToTextView(textView,
title,
author,
reactions,
responses,
ptype,
link,
"\n",
Expand All @@ -78,34 +80,7 @@ func openPost(app *tview.Application, postcuid string, list *tview.List) {
return ""
}(),
)
noresponse := len(singlePost.Post.Responses)
for ind, response := range singlePost.Post.Responses {
writeToTextView(
textView,
fmt.Sprintf("\n[green]Response %d/%d[white]", ind+1, noresponse),
fmt.Sprintf("[green]--------------[green]"),
renderTerminal(response.ContentMarkdown),
)
if len(response.Replies) > 0 {
writeToTextView(textView,
"\n[yellow]Replies[white]",
"[yellow]=======[white]",
)
noreplies := len(response.Replies)
for indreply, reply := range response.Replies {
writeToTextView(
textView,
fmt.Sprintf("\n[yellow]Reply %d/%d[white]", indreply+1, noreplies),
fmt.Sprintf("[yellow]~~~~~~~~~~~[white]"),
fmt.Sprintf("Author: %s", reply.Author.Name),
indentMarkdown(renderTerminal(reply.ContentMarkdown), "\t"),
)

}
}

}

openResponses(textView, singlePost.Post.ID, singlePost.Post.ResponseCount)
textView.SetDoneFunc(func(key tcell.Key) {
if key == tcell.KeyEscape {
if err := app.SetRoot(list, true).SetFocus(list).Run(); err != nil {
Expand Down
93 changes: 93 additions & 0 deletions pkg/posts/responses.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package posts

import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/url"

"github.com/rivo/tview"
)

func openResponses(t *tview.TextView, postcuid string, totalResponses int) {

const defaultSortOrder = "totalReactions"

r, err := getResponses(postcuid, totalResponses, 1, defaultSortOrder)
if err != nil {
log.Println(err)
}
var responsesAPI responsesAPI
err = json.Unmarshal(r, &responsesAPI)
if err != nil {
log.Println(err)
}

noresponse := len(responsesAPI.Responses)
for ind, response := range responsesAPI.Responses {
writeToTextView(
t,
fmt.Sprintf("\n[green]Response %d/%d[white]", ind+1, noresponse),
fmt.Sprintf("[green]--------------[green]"),
renderTerminal(response.ContentMarkdown),
)
if len(response.Replies) > 0 {
writeToTextView(t,
"\n[yellow]Replies[white]",
"[yellow]=======[white]",
)
noreplies := len(response.Replies)
for indreply, reply := range response.Replies {
writeToTextView(
t,
fmt.Sprintf("\n[yellow]Reply %d/%d[white]", indreply+1, noreplies),
fmt.Sprintf("[yellow]~~~~~~~~~~~[white]"),
fmt.Sprintf("Author: %s", reply.Author.Name),
indentMarkdown(renderTerminal(reply.ContentMarkdown), "\t"),
)

}
}

}

}

func getResponses(postID string, perPage, page int, sortOrder string) ([]byte, error) {
const apiURL = "https://hashnode.com/ajax/responses"
u, err := url.Parse(apiURL)
if err != nil {
log.Fatal(err)
}

q := u.Query()
q.Set("post_id", postID)
q.Set("page", fmt.Sprintf("%d", page))
q.Set("per_page", fmt.Sprintf("%d", perPage))
q.Set("sort_order", sortOrder)
u.RawQuery = q.Encode()

client := getHttpClient()
resp, err := client.Get(u.String())
if err != nil {
return nil, err
}
defer resp.Body.Close()

b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return b, err
}

type responsesAPI struct {
Pagination struct {
Page string `json:"page"`
PerPage string `json:"per_page"`
Total int `json:"total"`
} `json:"pagination"`
Order string `json:"order"`
Responses []Response
}
31 changes: 31 additions & 0 deletions pkg/posts/responses_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package posts

import "testing"

func Test_getResponses(t *testing.T) {
type args struct {
postID string
perPage int
page int
sortOrder string
}
tests := []struct {
name string
args args
}{
{
name: "test1",
args: args{
postID: "5c34ffe75aa5738b323c386b",
page: 2,
perPage: 5,
sortOrder: "totalReactions",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
getResponses(tt.args.postID, tt.args.perPage, tt.args.page, tt.args.sortOrder)
})
}
}

0 comments on commit 067cbe5

Please sign in to comment.