From ddf25011ccc36f0ef3cb0cf1ec75e87cecf7c6b6 Mon Sep 17 00:00:00 2001 From: Ryan Stafford Date: Thu, 13 Jul 2023 09:45:51 -0400 Subject: [PATCH 01/37] 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)}} From 2b0eefa1721cc775ddef412b97c64b639c22d3fe Mon Sep 17 00:00:00 2001 From: Ryan Stafford Date: Thu, 13 Jul 2023 16:05:16 -0400 Subject: [PATCH 02/37] fix non local community search --- templates/activities.html | 2 +- templates/post.html | 2 +- templates/sidebar.html | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/templates/activities.html b/templates/activities.html index ac0ea49..12b3284 100644 --- a/templates/activities.html +++ b/templates/activities.html @@ -13,7 +13,7 @@ {{$state.User.PersonView.Person.Name }} {{ end }} in - /c/{{ $activity.Comment.P.Community.Name }} + c/{{ $activity.Comment.P.Community.Name }}
    {{ template "comment.html" $activity.Comment }} diff --git a/templates/post.html b/templates/post.html index e61d304..5bdc596 100644 --- a/templates/post.html +++ b/templates/post.html @@ -43,7 +43,7 @@ by {{ fullname .Creator }} to - {{ fullcname .Community}} + c/{{ fullcname .Community}}
    {{ if .Post.NSFW }}NSFW{{end}} diff --git a/templates/sidebar.html b/templates/sidebar.html index 8054605..e2a2782 100644 --- a/templates/sidebar.html +++ b/templates/sidebar.html @@ -7,7 +7,7 @@ {{ if .User }} {{ else if .Community }} - + {{ end }} {{ end }} From 37e62d2e5f71ce6db38b167c91d4946cc755a2ee Mon Sep 17 00:00:00 2001 From: Ryan Stafford Date: Thu, 13 Jul 2023 18:24:03 -0400 Subject: [PATCH 03/37] remove redundant child counts, only insert new --- public/style.css | 13 ++++++++++--- public/utils.js | 29 +++++++++++++++++++++++++++-- routes.go | 3 +++ state.go | 7 ++++--- templates/comment.html | 6 +++--- templates/frontpage.html | 4 ++-- templates/main.html | 4 ++-- templates/post.html | 4 ++-- 8 files changed, 53 insertions(+), 17 deletions(-) diff --git a/public/style.css b/public/style.css index ea80321..36214aa 100644 --- a/public/style.css +++ b/public/style.css @@ -115,8 +115,8 @@ summary { .title a p { display: inline; } -.post.distinguished .title a, -.dark .post.distinguished .title a:visited { +.post.distinguished .title a, .post.announcement .title a, +.dark .post.distinguished .title a:visited, .post.announcement .title a:visited { color: #228822; font-weight: bold; } @@ -228,6 +228,13 @@ summary { .dark .meta a { color: #6a98af; } +.comment .meta a.distinguished.admin, .post.distinguished a.admin, .post.announcement a.admin { + background-color: #ff0011; + color: white; + font-weight: bold; + border-radius: 3px; + padding: 0px 2px; +} .meta a:hover { text-decoration: underline; } @@ -472,7 +479,7 @@ form.nsfw div { .expando-button:hover{ background-color: #466599; } -.expando-button.hidden, .children.hidden .comment { +.expando-button.hidden, .children.hidden .comment, .children.hidden .morecomments { display: none; } .hidechildren .show { diff --git a/public/utils.js b/public/utils.js index 3c7a935..ae67624 100644 --- a/public/utils.js +++ b/public/utils.js @@ -91,8 +91,7 @@ function commentClick(e) { } return false } - if ((e.target.className.indexOf("loadmore") != -1) || - (e.target.className.indexOf("edit") != -1) || + if ((e.target.className.indexOf("edit") != -1) || (e.target.className.indexOf("source") != -1) || (e.target.className.indexOf("reply") != -1)) { var id = targ.id @@ -104,6 +103,32 @@ function commentClick(e) { }) return false } + if (e.target.className.indexOf("loadmore") != -1) { + var id = targ.id + if (e.target.getAttribute("for") != id) { return } + e.preventDefault() + var comments = targ.getElementsByClassName("comment") + var skip = [] + for (var i = 0; i < comments.length; i++) { + skip.push(comments[i].id) + } + request(e.target.href+"&xhr",false, function(res){ + var parent = e.target.parentNode + parent.innerHTML = res + parent.innerHTML = parent.getElementsByClassName("children")[0].innerHTML + var comments = parent.getElementsByClassName("comment") + for (var i = 0; i < skip.length; i++) { + for (var c = 0; c < comments.length; c++) { + if (skip[i] == comments[c].id) { + comments[c].remove() + } + } + } + parent.outerHTML = parent.innerHTML + setup() + }) + return false + } } function loadMoreComments(e) { diff --git a/routes.go b/routes.go index 24f5385..1507480 100644 --- a/routes.go +++ b/routes.go @@ -165,6 +165,9 @@ var funcMap = template.FuncMap{ "sub": func(a int32, b int) int { return int(a) - b }, + "add": func(a int32, b int) int { + return int(a) + b + }, } func Initialize(Host string, r *http.Request) (State, error) { diff --git a/state.go b/state.go index a36d295..266eddc 100644 --- a/state.go +++ b/state.go @@ -627,19 +627,19 @@ func (state *State) UploadImage(file multipart.File, header *multipart.FileHeade func getChildren(parent *Comment, pool []types.CommentView, postCreatorID int) { var children []Comment - total := -1 + total := int32(0) for _, c := range pool { levels := strings.Split(c.Comment.Path, ".") for i, l := range levels { id, _ := strconv.Atoi(l) if id == parent.P.Comment.ID { - total = total + 1 if i == (len(levels) - 2) { children = append(children, Comment{ P: c, C: children, State: parent.State, }) + total += c.Counts.ChildCount } } @@ -647,7 +647,8 @@ func getChildren(parent *Comment, pool []types.CommentView, postCreatorID int) { } for i, _ := range children { getChildren(&children[i], pool, postCreatorID) + parent.ChildCount += 1 } parent.C = children - parent.ChildCount = total + parent.P.Counts.ChildCount -= total } diff --git a/templates/comment.html b/templates/comment.html index 87481a1..2a740ea 100644 --- a/templates/comment.html +++ b/templates/comment.html @@ -21,7 +21,7 @@ [-] {{ end }} - {{fullname .P.Creator}} + {{fullname .P.Creator}} {{.P.Counts.Score}} points {{ humanize .P.Comment.Published.Time }} {{- if gt .P.Comment.Updated.Time.Unix .P.Comment.Published.Time.Unix -}} * (last edited {{ humanize .P.Comment.Updated.Time }}) @@ -97,8 +97,8 @@ context {{ end }} -{{ if gt .ChildCount 0 }} -
  • hideshow {{ .ChildCount }} child comments
  • +{{ if and .State.PostID (gt (add .P.Counts.ChildCount .ChildCount) 0) }} +
  • hideshow {{add .P.Counts.ChildCount .ChildCount }} child comments
  • {{ end }}
    diff --git a/templates/frontpage.html b/templates/frontpage.html index d27dc5b..ea74320 100644 --- a/templates/frontpage.html +++ b/templates/frontpage.html @@ -2,7 +2,7 @@ {{ if and .Community (ne .Community.CommunityView.Community.Title "")}}{{.Community.CommunityView.Community.Title}}{{else if ne .CommunityName ""}}/c/{{.CommunityName}}{{ else if .User}}overview for {{.User.PersonView.Person.Name}}{{else}}{{ host .Host }}{{end}} - + @@ -56,6 +56,6 @@ {{ template "sidebar.html" . }} {{ end }} - + diff --git a/templates/main.html b/templates/main.html index 37e3139..5b03aa5 100644 --- a/templates/main.html +++ b/templates/main.html @@ -3,7 +3,7 @@ {{if and .Posts .PostID }}{{ (index .Posts 0).Post.Name}} : {{.CommunityName}}{{else if and .Community (ne .Community.CommunityView.Community.Title "")}}{{.Community.CommunityView.Community.Title}}{{else if ne .CommunityName ""}}/c/{{.CommunityName}}{{ else if .User}}overview for {{.User.PersonView.Person.Name}}{{else}}{{ host .Host }}{{end}} - +