Skip to content

Commit

Permalink
Fix #50, implementation of nested replies
Browse files Browse the repository at this point in the history
  • Loading branch information
mrusme committed Aug 1, 2024
1 parent 683f07f commit 386adc8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
2 changes: 2 additions & 0 deletions models/reply/reply.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ type Reply struct {
Replies []Reply

SysIDX int

MetaPath []string
}
33 changes: 31 additions & 2 deletions system/lemmy/lemmy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"net/url"
"strconv"
"strings"
"time"

"github.com/mrusme/neonmodem/models/author"
Expand Down Expand Up @@ -281,11 +282,11 @@ func (sys *System) LoadPost(p *post.Post) error {
return err
}

p.Replies = []reply.Reply{}
replies := []reply.Reply{}
for _, i := range resp.Comments {
createdAt := i.Comment.Published

p.Replies = append(p.Replies, reply.Reply{
replies = append(replies, reply.Reply{
ID: strconv.FormatInt(i.Comment.ID, 10),

InReplyTo: p.ID,
Expand All @@ -299,12 +300,40 @@ func (sys *System) LoadPost(p *post.Post) error {
Name: i.Creator.Name,
},

Replies: []reply.Reply{},

SysIDX: sys.ID,

MetaPath: strings.Split(i.Comment.Path, "."),
})
}

findRepliesFor(&replies, &p.Replies, 0)

return nil
}

func findRepliesFor(prv *[]reply.Reply, nxt *[]reply.Reply, lvl int) {
if lvl >= 8 {
return
}

for i := 0; i < len(*prv); i++ {
if len((*prv)[i].MetaPath) == (lvl + 2) {
*nxt = append(*nxt, (*prv)[i])
}
}
nextLayer := []reply.Reply{}
findRepliesFor(prv, &nextLayer, lvl+1)
for j := 0; j < len(nextLayer); j++ {
for k := 0; k < len(*nxt); k++ {
if (*nxt)[k].ID == nextLayer[j].MetaPath[(lvl+1)] {
(*nxt)[k].Replies = append((*nxt)[k].Replies, nextLayer[j])
}
}
}
}

func (sys *System) CreatePost(p *post.Post) error {
communityID, err := strconv.ParseInt(p.Forum.ID, 10, 64)
if err != nil {
Expand Down

0 comments on commit 386adc8

Please sign in to comment.