From ec5428c6d577c50b1a4d7182bd11890c78d6b8b4 Mon Sep 17 00:00:00 2001 From: Umputun Date: Fri, 18 May 2018 16:08:59 -0500 Subject: [PATCH] hide deleted tree #39 --- app/rest/tree.go | 31 ++++++++++++++++++------------- app/rest/tree_test.go | 4 ++++ 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/app/rest/tree.go b/app/rest/tree.go index 797e3189..56e915c3 100644 --- a/app/rest/tree.go +++ b/app/rest/tree.go @@ -20,9 +20,10 @@ type Node struct { ts time.Time } -// timeStamp wraps time.Time to simplify pointer ops -type timeStamp struct { - time.Time +// recurData wraps all fileds used in recursive processing as intermediate results +type recurData struct { + ts time.Time + visible bool } // MakeTree gets unsorted list of comments and produces Tree @@ -34,9 +35,10 @@ func MakeTree(comments []store.Comment, sortType string) *Tree { for _, rootComment := range topComments { node := Node{Comment: rootComment} - ts := timeStamp{} - commentsTree, t := res.proc(comments, &node, &ts, rootComment.ID) - if rootComment.Deleted && len(commentsTree.Replies) == 0 { // skip deleted with no subcomments + rd := recurData{} + commentsTree, t := res.proc(comments, &node, &rd, rootComment.ID) + // skip deleted with no sub-comments ar all sub-comments deleted + if rootComment.Deleted && (len(commentsTree.Replies) == 0 || !rd.visible) { continue } commentsTree.ts = t @@ -48,26 +50,29 @@ func MakeTree(comments []store.Comment, sortType string) *Tree { } // proc makes tree for one top-level comment recursively -func (t *Tree) proc(comments []store.Comment, node *Node, ts *timeStamp, parentID string) (*Node, time.Time) { +func (t *Tree) proc(comments []store.Comment, node *Node, rd *recurData, parentID string) (*Node, time.Time) { - if ts.IsZero() { - ts.Time = node.Comment.Timestamp + if rd.ts.IsZero() { + rd.ts = node.Comment.Timestamp } repComments := t.filter(comments, parentID) for _, rc := range repComments { - if rc.Timestamp.After(ts.Time) { - ts.Time = rc.Timestamp + if rc.Timestamp.After(rd.ts) { + rd.ts = rc.Timestamp + } + if !rc.Deleted { + rd.visible = true } rnode := &Node{Comment: rc, Replies: []*Node{}} node.Replies = append(node.Replies, rnode) - t.proc(comments, rnode, ts, rc.ID) + t.proc(comments, rnode, rd, rc.ID) } // replies always sorted by time sort.Slice(node.Replies, func(i, j int) bool { return node.Replies[i].Comment.Timestamp.Before(node.Replies[j].Comment.Timestamp) }) - return node, ts.Time + return node, rd.ts } // filter returns comments for parentID diff --git a/app/rest/tree_test.go b/app/rest/tree_test.go index 13681f93..8bb1c605 100644 --- a/app/rest/tree_test.go +++ b/app/rest/tree_test.go @@ -29,6 +29,10 @@ func TestMakeTree(t *testing.T) { {ID: "4", Timestamp: time.Date(2017, 12, 25, 19, 47, 22, 0, time.UTC)}, {ID: "3", Timestamp: time.Date(2017, 12, 25, 19, 47, 22, 0, time.UTC)}, {ID: "5", Deleted: true}, + {ID: "6", Deleted: true}, + {ID: "61", ParentID: "6", Deleted: true}, + {ID: "62", ParentID: "6", Deleted: true}, + {ID: "611", ParentID: "61", Deleted: true}, } res := MakeTree(comments, "time")