Skip to content

Commit

Permalink
add thread reply list pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
Southclaws committed Jan 2, 2025
1 parent 2ddc4a4 commit 2c87659
Show file tree
Hide file tree
Showing 30 changed files with 846 additions and 616 deletions.
14 changes: 12 additions & 2 deletions api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1147,7 +1147,9 @@ paths:
created as well as a list of the posts within the thread.
security: []
tags: [threads]
parameters: [$ref: "#/components/parameters/ThreadMarkParam"]
parameters:
- $ref: "#/components/parameters/ThreadMarkParam"
- $ref: "#/components/parameters/PaginationQuery"
responses:
default: { $ref: "#/components/responses/InternalServerError" }
"404": { $ref: "#/components/responses/NotFound" }
Expand Down Expand Up @@ -4577,7 +4579,7 @@ components:
- type: object
required: [replies]
properties:
replies: { $ref: "#/components/schemas/ReplyList" }
replies: { $ref: "#/components/schemas/PaginatedReplyList" }

ThreadInitialProps:
type: object
Expand Down Expand Up @@ -4686,6 +4688,14 @@ components:
- { $ref: "#/components/schemas/PostProps" }
- { $ref: "#/components/schemas/ReplyProps" }

PaginatedReplyList:
allOf:
- $ref: "#/components/schemas/PaginatedResult"
- type: object
required: [replies]
properties:
replies: { $ref: "#/components/schemas/ReplyList" }

ReplyList:
type: array
items: { $ref: "#/components/schemas/Reply" }
Expand Down
3 changes: 2 additions & 1 deletion app/resources/datagraph/hydrate/hydrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/Southclaws/storyden/app/resources/datagraph"
"github.com/Southclaws/storyden/app/resources/library"
"github.com/Southclaws/storyden/app/resources/library/node_querier"
"github.com/Southclaws/storyden/app/resources/pagination"
"github.com/Southclaws/storyden/app/resources/post"
"github.com/Southclaws/storyden/app/resources/post/reply"
"github.com/Southclaws/storyden/app/resources/post/thread"
Expand Down Expand Up @@ -74,7 +75,7 @@ func (h *Hydrator) Hydrate(ctx context.Context, refs ...*datagraph.Ref) (datagra

case datagraph.KindThread:
items, err = dt.MapErr(v, func(r *datagraph.Ref) (withRelevance, error) {
i, err := h.threads.Get(ctx, post.ID(r.ID), nil)
i, err := h.threads.Get(ctx, post.ID(r.ID), pagination.Parameters{}, nil)
return withRelevance{Item: i, r: r.Relevance}, err
})

Expand Down
52 changes: 36 additions & 16 deletions app/resources/post/thread/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ import (

"github.com/Southclaws/storyden/app/resources/account"
"github.com/Southclaws/storyden/app/resources/collection/collection_item_status"
"github.com/Southclaws/storyden/app/resources/pagination"
"github.com/Southclaws/storyden/app/resources/post"
"github.com/Southclaws/storyden/app/resources/post/category"
"github.com/Southclaws/storyden/app/resources/post/reply"
"github.com/Southclaws/storyden/internal/ent"
"github.com/Southclaws/storyden/internal/ent/asset"
"github.com/Southclaws/storyden/internal/ent/collection"
Expand Down Expand Up @@ -284,6 +286,19 @@ func (d *database) List(
}, nil
}

const repliesCountQuery = `select
p.id post_id, -- post ID
count(r.id) replies, -- number of replies,
count(a.id) replied -- has this account replied
from
posts p
inner join posts r on r.root_post_id = p.id and r.deleted_at is null
left join accounts a on a.id = r.account_posts and a.id = $2
where
p.id = $1 or p.root_post_id = $1
group by p.id
`

const likesCountQuery = `select
p.id post_id, -- the post (thread or reply) ID
count(*) likes, -- number of likes
Expand Down Expand Up @@ -312,9 +327,15 @@ where
group by p.id
`

func (d *database) Get(ctx context.Context, threadID post.ID, accountID opt.Optional[account.AccountID]) (*Thread, error) {
func (d *database) Get(ctx context.Context, threadID post.ID, pageParams pagination.Parameters, accountID opt.Optional[account.AccountID]) (*Thread, error) {
var replyStats post.PostRepliesResults
err := d.raw.SelectContext(ctx, &replyStats, repliesCountQuery, threadID.String(), accountID.String())
if err != nil {
return nil, fault.Wrap(err, fctx.With(ctx))
}

var likes post.PostLikesResults
err := d.raw.SelectContext(ctx, &likes, likesCountQuery, threadID.String(), accountID.String())
err = d.raw.SelectContext(ctx, &likes, likesCountQuery, threadID.String(), accountID.String())
if err != nil {
return nil, fault.Wrap(err, fctx.With(ctx))
}
Expand Down Expand Up @@ -354,6 +375,8 @@ func (d *database) Get(ctx context.Context, threadID post.ID, accountID opt.Opti
lq.WithFaviconImage().WithPrimaryImage()
lq.WithAssets().Order(link.ByCreatedAt(sql.OrderDesc()))
}).
Limit(pageParams.Limit()).
Offset(pageParams.Offset()).
Order(ent.Asc(ent_post.FieldCreatedAt))
}).
WithAuthor(func(aq *ent.AccountQuery) {
Expand Down Expand Up @@ -382,27 +405,24 @@ func (d *database) Get(ctx context.Context, threadID post.ID, accountID opt.Opti
return nil, fault.Wrap(err, fctx.With(ctx), ftag.With(ftag.Internal))
}

replies := post.PostRepliesMap{
xid.ID(threadID): post.PostRepliesResult{
PostID: xid.ID(threadID),
Count: len(r.Edges.Posts),
Replied: opt.Map(accountID, func(a account.AccountID) (replied int) {
for _, p := range r.Edges.Posts {
if p.Edges.Author.ID == xid.ID(a) {
replied++
}
}
return
}).OrZero(),
},
replies, err := dt.MapErr(r.Edges.Posts, reply.FromModel(likes.Map()))
if err != nil {
return nil, fault.Wrap(err, fctx.With(ctx))
}

mapper := FromModel(likes.Map(), collections.Map(), replies)
replyStatsMap := replyStats.Map()
totalReplies := replyStatsMap[r.ID].Count

repliesPage := pagination.NewPageResult(pageParams, totalReplies, replies)

mapper := FromModel(likes.Map(), collections.Map(), replyStatsMap)
p, err := mapper(r)
if err != nil {
return nil, fault.Wrap(err, fctx.With(ctx))
}

p.Replies = repliesPage

return p, nil
}

Expand Down
3 changes: 2 additions & 1 deletion app/resources/post/thread/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/Southclaws/storyden/app/resources/account"
"github.com/Southclaws/storyden/app/resources/asset"
"github.com/Southclaws/storyden/app/resources/datagraph"
"github.com/Southclaws/storyden/app/resources/pagination"
"github.com/Southclaws/storyden/app/resources/post"
"github.com/Southclaws/storyden/app/resources/post/category"
"github.com/Southclaws/storyden/app/resources/tag/tag_ref"
Expand Down Expand Up @@ -53,7 +54,7 @@ type Repository interface {

// GetPostCounts(ctx context.Context) (map[string]int, error)

Get(ctx context.Context, threadID post.ID, accountID opt.Optional[account.AccountID]) (*Thread, error)
Get(ctx context.Context, threadID post.ID, pageParams pagination.Parameters, accountID opt.Optional[account.AccountID]) (*Thread, error)

Delete(ctx context.Context, id post.ID) error
}
Expand Down
9 changes: 2 additions & 7 deletions app/resources/post/thread/thread.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/Southclaws/storyden/app/resources/collection/collection_item_status"
"github.com/Southclaws/storyden/app/resources/datagraph"
"github.com/Southclaws/storyden/app/resources/link/link_ref"
"github.com/Southclaws/storyden/app/resources/pagination"
"github.com/Southclaws/storyden/app/resources/post"
"github.com/Southclaws/storyden/app/resources/post/category"
"github.com/Southclaws/storyden/app/resources/post/reaction"
Expand All @@ -30,7 +31,7 @@ type Thread struct {
Pinned bool

ReplyStatus post.ReplyStatus
Replies []*reply.Reply
Replies pagination.Result[*reply.Reply]
Category category.Category
Visibility visibility.Visibility
Tags tag_ref.Tags
Expand Down Expand Up @@ -65,11 +66,6 @@ func FromModel(ls post.PostLikesMap, cs collection_item_status.CollectionStatusM
return nil, fault.Wrap(err)
}

replies, err := dt.MapErr(m.Edges.Posts, reply.FromModel(ls))
if err != nil {
return nil, fault.Wrap(err)
}

link := opt.Map(opt.NewPtr(m.Edges.Link), func(in ent.Link) link_ref.LinkRef {
return *link_ref.Map(&in)
})
Expand Down Expand Up @@ -110,7 +106,6 @@ func FromModel(ls post.PostLikesMap, cs collection_item_status.CollectionStatusM
Pinned: m.Pinned,

ReplyStatus: rs.Status(m.ID),
Replies: replies,
Category: *category,
Visibility: visibility.NewVisibilityFromEnt(m.Visibility),
Tags: tags,
Expand Down
Loading

0 comments on commit 2c87659

Please sign in to comment.