diff --git a/appview/issues/issues.go b/appview/issues/issues.go --- a/appview/issues/issues.go +++ b/appview/issues/issues.go @@ -135,7 +135,7 @@ LoggedInUser: user, RepoInfo: rp.repoResolver.GetRepoInfo(r, user), Issue: issue, - CommentList: issue.CommentList(), + CommentList: models.NewCommentList(issue.Comments), Backlinks: backlinks, Reactions: reactionMap, UserReacted: userReactions, diff --git a/appview/models/comment.go b/appview/models/comment.go --- a/appview/models/comment.go +++ b/appview/models/comment.go @@ -2,6 +2,7 @@ import ( "fmt" + "sort" "strings" "time" @@ -155,4 +156,75 @@ ReplyTo: record.ReplyTo, PullRoundIdx: pullRoundIdx, }, nil +} + +type CommentListItem struct { + Self *Comment + Replies []*Comment +} + +func (it *CommentListItem) Participants() []syntax.DID { + participantSet := make(map[syntax.DID]struct{}) + participants := []syntax.DID{} + + addParticipant := func(did syntax.DID) { + if _, exists := participantSet[did]; !exists { + participantSet[did] = struct{}{} + participants = append(participants, did) + } + } + + addParticipant(syntax.DID(it.Self.Did)) + + for _, c := range it.Replies { + addParticipant(syntax.DID(c.Did)) + } + + return participants +} + +func NewCommentList(comments []Comment) []CommentListItem { + // Create a map to quickly find comments by their aturi + toplevel := make(map[syntax.ATURI]*CommentListItem) + var replies []*Comment + + // collect top level comments into the map + for _, comment := range comments { + if comment.IsTopLevel() { + toplevel[comment.AtUri()] = &CommentListItem{ + Self: &comment, + } + } else { + replies = append(replies, &comment) + } + } + + for _, r := range replies { + if r.ReplyTo == nil { + continue + } + if parent, exists := toplevel[syntax.ATURI(r.ReplyTo.Uri)]; exists { + parent.Replies = append(parent.Replies, r) + } + } + + var listing []CommentListItem + for _, v := range toplevel { + listing = append(listing, *v) + } + + // sort everything + sortFunc := func(a, b *Comment) bool { + return a.Created.Before(b.Created) + } + sort.Slice(listing, func(i, j int) bool { + return sortFunc(listing[i].Self, listing[j].Self) + }) + for _, r := range listing { + sort.Slice(r.Replies, func(i, j int) bool { + return sortFunc(r.Replies[i], r.Replies[j]) + }) + } + + return listing } diff --git a/appview/models/issue.go b/appview/models/issue.go --- a/appview/models/issue.go +++ b/appview/models/issue.go @@ -2,7 +2,6 @@ import ( "fmt" - "sort" "time" "github.com/bluesky-social/indigo/atproto/syntax" @@ -64,77 +63,6 @@ return "open" } return "closed" -} - -type CommentListItem struct { - Self *Comment - Replies []*Comment -} - -func (it *CommentListItem) Participants() []syntax.DID { - participantSet := make(map[syntax.DID]struct{}) - participants := []syntax.DID{} - - addParticipant := func(did syntax.DID) { - if _, exists := participantSet[did]; !exists { - participantSet[did] = struct{}{} - participants = append(participants, did) - } - } - - addParticipant(syntax.DID(it.Self.Did)) - - for _, c := range it.Replies { - addParticipant(syntax.DID(c.Did)) - } - - return participants -} - -func (i *Issue) CommentList() []CommentListItem { - // Create a map to quickly find comments by their aturi - toplevel := make(map[syntax.ATURI]*CommentListItem) - var replies []*Comment - - // collect top level comments into the map - for _, comment := range i.Comments { - if comment.IsTopLevel() { - toplevel[comment.AtUri()] = &CommentListItem{ - Self: &comment, - } - } else { - replies = append(replies, &comment) - } - } - - for _, r := range replies { - if r.ReplyTo == nil { - continue - } - if parent, exists := toplevel[syntax.ATURI(r.ReplyTo.Uri)]; exists { - parent.Replies = append(parent.Replies, r) - } - } - - var listing []CommentListItem - for _, v := range toplevel { - listing = append(listing, *v) - } - - // sort everything - sortFunc := func(a, b *Comment) bool { - return a.Created.Before(b.Created) - } - sort.Slice(listing, func(i, j int) bool { - return sortFunc(listing[i].Self, listing[j].Self) - }) - for _, r := range listing { - sort.Slice(r.Replies, func(i, j int) bool { - return sortFunc(r.Replies[i], r.Replies[j]) - }) - } - - return listing } func (i *Issue) Participants() []string { diff --git a/appview/notify/db/db.go b/appview/notify/db/db.go --- a/appview/notify/db/db.go +++ b/appview/notify/db/db.go @@ -121,7 +121,7 @@ parent := *comment.ReplyTo // find the parent thread, and add all DIDs from here to the recipient list - for _, t := range issue.CommentList() { + for _, t := range models.NewCommentList(issue.Comments) { if t.Self.AtUri() == syntax.ATURI(parent.Uri) { for _, p := range t.Participants() { recipients.Insert(p) diff --git a/appview/pages/templates/fragments/comment/commentList.html b/appview/pages/templates/fragments/comment/commentList.html new file mode 100644 --- /dev/null +++ b/appview/pages/templates/fragments/comment/commentList.html @@ -0,0 +1,70 @@ +{{ define "fragments/comment/commentList" }} +
+ {{ range $item := .CommentList }} + {{ template "commentListItem" (list $ .) }} + {{ end }} +
+{{ end }} + +{{ define "commentListItem" }} + {{ $root := index . 0 }} + {{ $item := index . 1 }} + +
+ {{ template "topLevelComment" + (dict + "LoggedInUser" $root.LoggedInUser + "Comment" $item.Self) }} + +
+ {{ range $index, $reply := $item.Replies }} +
+ {{ + template "replyComment" + (dict + "LoggedInUser" $root.LoggedInUser + "Comment" $reply) + }} +
+ {{ end }} +
+ +
+ + + + + {{ if $item.Self.IsLegacy }} +
+ Can't reply to legacy comment. +
+ {{ else }} + {{ template "fragments/comment/replyPlaceholder" (dict "LoggedInUser" $root.LoggedInUser) }} + {{ end }} +
+
+{{ end }} + +{{ define "topLevelComment" }} +
+
+ {{ template "user/fragments/picLink" (list .Comment.Did.String "size-8 mr-1") }} +
+
+ {{ template "fragments/comment/commentHeader" . }} + {{ template "fragments/comment/commentBody" . }} +
+
+{{ end }} + +{{ define "replyComment" }} +
+
+ {{ template "user/fragments/picLink" (list .Comment.Did.String "size-8 mr-1") }} +
+
+ {{ template "fragments/comment/commentHeader" . }} + {{ template "fragments/comment/commentBody" . }} +
+
+{{ end }} diff --git a/appview/pages/templates/repo/issues/issue.html b/appview/pages/templates/repo/issues/issue.html --- a/appview/pages/templates/repo/issues/issue.html +++ b/appview/pages/templates/repo/issues/issue.html @@ -113,12 +113,10 @@ {{ define "repoAfter" }}
{{ - template "repo/issues/fragments/commentList" + template "fragments/comment/commentList" (dict - "RepoInfo" $.RepoInfo "LoggedInUser" $.LoggedInUser - "Issue" $.Issue - "CommentList" $.Issue.CommentList) + "CommentList" $.CommentList) }} {{ template "repo/issues/fragments/newComment" . }} diff --git a/appview/pages/templates/repo/issues/fragments/commentList.html b/appview/pages/templates/repo/issues/fragments/commentList.html deleted file mode 100644 --- a/appview/pages/templates/repo/issues/fragments/commentList.html +++ /dev/null @@ -1,71 +0,0 @@ -{{ define "repo/issues/fragments/commentList" }} -
- {{ range $item := .CommentList }} - {{ template "commentListItem" (list $ .) }} - {{ end }} -
-{{ end }} - -{{ define "commentListItem" }} - {{ $root := index . 0 }} - {{ $item := index . 1 }} - {{ $params := - (dict - "LoggedInUser" $root.LoggedInUser - "Comment" $item.Self) }} - -
- {{ template "topLevelComment" $params }} - -
- {{ range $index, $reply := $item.Replies }} -
- {{ - template "replyComment" - (dict - "LoggedInUser" $root.LoggedInUser - "Comment" $reply) - }} -
- {{ end }} -
- -
- - - - - {{ if $item.Self.IsLegacy }} -
- Can't reply to legacy comment. -
- {{ else }} - {{ template "fragments/comment/replyPlaceholder" (dict "LoggedInUser" $root.LoggedInUser) }} - {{ end }} -
-
-{{ end }} - -{{ define "topLevelComment" }} -
-
- {{ template "user/fragments/picLink" (list .Comment.Did.String "size-8 mr-1") }} -
-
- {{ template "fragments/comment/commentHeader" . }} - {{ template "fragments/comment/commentBody" . }} -
-
-{{ end }} - -{{ define "replyComment" }} -
-
- {{ template "user/fragments/picLink" (list .Comment.Did.String "size-8 mr-1") }} -
-
- {{ template "fragments/comment/commentHeader" . }} - {{ template "fragments/comment/commentBody" . }} -
-
-{{ end }}