Files
full/module/social/feed/internal/logic/post/fetch.go
2026-09-22 21:15:34 +08:00

75 lines
1.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package post
import (
"context"
"bsm/full/module/social/feed/internal/models"
pb "bsm/full/module/social/feed/pb"
"git.apinb.com/bsm-sdk/core/errcode"
"git.apinb.com/bsm-sdk/core/printer"
"git.apinb.com/bsm-sdk/core/service"
)
// maxPageSize 单页最大条数,避免一次请求拉取过多数据
const maxPageSize int64 = 50
// 推文列表
func Fetch(ctx context.Context, in *pb.FetchRequest) (reply *pb.PostListReply, err error) {
auth, err := service.ParseMetaCtx(ctx, nil)
if err != nil {
return nil, err
}
// 归一化分页参数:页码下限 1单页条数上限 maxPageSize
if in.GetPageNo() <= 0 {
in.PageNo = 1
}
if in.GetPageSize() <= 0 {
in.PageSize = 10
}
if in.GetPageSize() > maxPageSize {
in.PageSize = maxPageSize
}
data, cnt, err := models.PostList(in.PageNo, in.PageSize, in.Params["key"], auth.Identity)
if err != nil {
printer.Error(err.Error())
return nil, errcode.ErrDB
}
var res = &pb.PostListReply{Cnt: cnt}
if cnt > 0 {
res.List = AssembleList(data)
}
return res, nil
}
func AssembleList(data []*models.FeedPost) (res []*pb.PostItem) {
for _, val := range data {
post := &pb.PostItem{
Identity: val.Identity,
Content: val.Content,
FeedIdentity: val.Identity,
FeedId: int64(val.ID),
IsOpen: val.IsOpen,
CntUnlike: val.CntUnlike,
CntLike: val.CntLike,
CntComment: val.CntComment,
}
for _, tag := range val.Tags {
post.Tags = append(post.Tags, &pb.TagItem{
Key: tag.Key,
Content: tag.Content,
})
}
for _, attachs := range val.Attachs {
post.Attachs = append(post.Attachs, &pb.AttachItem{
Identity: attachs.Identity,
AttachType: attachs.AttachType,
Url: attachs.Url,
})
}
res = append(res, post)
}
return
}