From ddf25011ccc36f0ef3cb0cf1ec75e87cecf7c6b6 Mon Sep 17 00:00:00 2001 From: Ryan Stafford Date: Thu, 13 Jul 2023 09:45:51 -0400 Subject: [PATCH] context and parent comment links --- public/style.css | 5 +++-- public/utils.js | 3 ++- routes.go | 4 ++++ state.go | 37 ++++++++++++++++++++++++++++++++++++- templates/activities.html | 4 +++- templates/comment.html | 12 +++++++++++- templates/main.html | 2 +- 7 files changed, 60 insertions(+), 7 deletions(-) diff --git a/public/style.css b/public/style.css index 7d6debf..ea80321 100644 --- a/public/style.css +++ b/public/style.css @@ -115,7 +115,8 @@ summary { .title a p { display: inline; } -.post.distinguished .title a { +.post.distinguished .title a, +.dark .post.distinguished .title a:visited { color: #228822; font-weight: bold; } @@ -218,7 +219,7 @@ summary { font-size: 10px; margin-bottom: 6px; } -.meta a { +.meta a, .activity .meta a { color: #369; text-decoration: none; font-size: 10px; diff --git a/public/utils.js b/public/utils.js index afd224d..3c7a935 100644 --- a/public/utils.js +++ b/public/utils.js @@ -307,7 +307,8 @@ function setup() { hidechildren.addEventListener("click", hideAllChildComments) } if (lmc = document.getElementById("lmc")){ - if (pager = document.getElementsByClassName("pager")){ + var pager = document.getElementsByClassName("pager") + if (pager.length) { pager[0].style.display = "none"; } lmc.addEventListener("click", loadMoreComments) diff --git a/routes.go b/routes.go index 96cef97..24f5385 100644 --- a/routes.go +++ b/routes.go @@ -414,6 +414,10 @@ func GetComment(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { if len(m["source"]) > 0 { state.Op = "source" } + if len(m["context"]) > 0 { + ctx, _ := strconv.Atoi(m["context"][0]) + state.Context = ctx + } commentid, _ := strconv.Atoi(ps.ByName("commentid")) state.GetComment(commentid) state.GetPost(state.PostID) diff --git a/state.go b/state.go index 33bc5b9..a36d295 100644 --- a/state.go +++ b/state.go @@ -33,6 +33,12 @@ func (c *Comment) Submitter() bool { return c.P.Comment.CreatorID == c.P.Post.CreatorID } +func (c *Comment) ParentID() int { + path := strings.Split(c.P.Comment.Path, ".") + id, _ := strconv.Atoi(path[len(path)-2]) + return id +} + type Person struct { types.PersonViewSafe } @@ -78,6 +84,7 @@ type State struct { CommentCount int PostID int CommentID int + Context int UserName string User *types.GetPersonDetailsResponse Now int64 @@ -258,6 +265,30 @@ func (state *State) GetComment(commentid int) { state.Comments = append(state.Comments, comment) } } + ctx, err := state.GetContext(state.Context, state.Comments[0]) + if err != nil { + fmt.Println(err) + } else { + state.Comments = []Comment{ctx} + } +} +func (state *State) GetContext(depth int, comment Comment) (ctx Comment, err error) { + if depth < 1 || comment.ParentID() == 0 { + return comment, nil + } + cresp, err := state.Client.Comment(context.Background(), types.GetComment{ + ID: comment.ParentID(), + }) + if err != nil { + return + } + ctx, err = state.GetContext(depth-1, Comment{ + P: cresp.CommentView, + State: state, + C: []Comment{comment}, + ChildCount: comment.ChildCount + 1, + }) + return } func (state *State) GetComments() { if state.Sort != "Hot" && state.Sort != "Top" && state.Sort != "Old" && state.Sort != "New" { @@ -504,9 +535,13 @@ func (state *State) Search(searchtype string) { }) } for _, c := range resp.Comments { - state.Comments = append(state.Comments, Comment{ + comment := Comment{ P: c, State: state, + } + state.Activities = append(state.Activities, Activity{ + Timestamp: c.Comment.Published.Time, + Comment: &comment, }) } state.Communities = resp.Communities diff --git a/templates/activities.html b/templates/activities.html index 6d57d09..ac0ea49 100644 --- a/templates/activities.html +++ b/templates/activities.html @@ -3,16 +3,18 @@
{{ if $activity.Comment }}
- {{ if not $state.User }} + {{ if and (not $state.User) (not $state.Query) }} comment on {{ end }} {{ $activity.Comment.P.Post.Name}} + {{ if $state.User}} by {{$state.User.PersonView.Person.Name }} {{ end }} in /c/{{ $activity.Comment.P.Community.Name }} +
{{ template "comment.html" $activity.Comment }} {{ else if $activity.Post }} diff --git a/templates/comment.html b/templates/comment.html index 956445f..87481a1 100644 --- a/templates/comment.html +++ b/templates/comment.html @@ -87,6 +87,16 @@ {{ end }} +{{ if and .ParentID .State.CommentID }} +
  • + parent +
  • +{{ end }} +{{ if and .ParentID (or .State.Activities .State.Query) }} +
  • + context +
  • +{{ end }} {{ if gt .ChildCount 0 }}
  • hideshow {{ .ChildCount }} child comments
  • {{ end }} @@ -105,7 +115,7 @@ {{ end}} {{ range $ci, $child := .C }}{{ template "comment.html" $child }}{{end}} -{{ if ne .P.Counts.ChildCount .ChildCount}} +{{ if and (ne .P.Counts.ChildCount .ChildCount) (not .State.Activities) (not .State.Query) }}
    load more comments ({{ sub .P.Counts.ChildCount .ChildCount}} replies) diff --git a/templates/main.html b/templates/main.html index 7bae19d..37e3139 100644 --- a/templates/main.html +++ b/templates/main.html @@ -108,7 +108,7 @@ {{ template "comment.html" $comment }} {{ end }} - {{ if (and .Comments (gt (index .Posts 0).Counts.Comments .CommentCount)) }} + {{ if and .Comments .Posts (gt (index .Posts 0).Counts.Comments .CommentCount) (not .CommentID)}}