diff options
Diffstat (limited to 'backend/notes/note.go')
| -rw-r--r-- | backend/notes/note.go | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/backend/notes/note.go b/backend/notes/note.go index 3b32aa1..591caff 100644 --- a/backend/notes/note.go +++ b/backend/notes/note.go @@ -1,6 +1,7 @@ package notes import ( + "sort" "time" ) @@ -24,3 +25,42 @@ type Facet struct { Type string `json:"type"` Value string `json:"value"` // the link destination, whether url, profile or tag } + +func isChainContained(smallChain, bigChain []string) bool { + if len(smallChain) > len(bigChain) { + return false + } + + // check if smallChain is a suffix of bigChain + offset := len(bigChain) - len(smallChain) + for i, uri := range smallChain { + if bigChain[offset+i] != uri { + return false + } + } + return true +} + +func DeduplicateThreads(posts []Note) []Note { + // sort by parent chain length (longest first) + sort.Slice(posts, func(i, j int) bool { + return len(posts[i].Parents) > len(posts[j].Parents) + }) + + filtered := []Note{} + for _, post := range posts { + isRedundant := false + for _, kept := range filtered { + // check if this post's chain is contained in any kept post + if isChainContained(post.Parents, kept.Parents) && post.URI == kept.URI { + isRedundant = true + break + } + } + if !isRedundant { + filtered = append(filtered, post) + } + } + + return filtered +} |
