作者 yangfu

快讯修改

... ... @@ -55,6 +55,9 @@ service Core {
@doc "用户快讯"
@handler miniUserNews
post /mini/user/news (MiniUserNewsRequest)returns(MiniUserNewsResposne)
@doc "我关注人发布的信息"
@handler miniUserMyFollowingNews
post /mini/user/my_following_news (MiniUserNewsRequest)returns(MiniUserNewsResposne)
@doc "关注我的人"
@handler miniUserFollower
post /mini/user/follower (MiniUserFollowedSearchRequest)returns(MiniUserFollowedSearchResponse)
... ...
... ... @@ -276,6 +276,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
},
{
Method: http.MethodPost,
Path: "/mini/user/my_following_news",
Handler: user.MiniUserMyFollowingNewsHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/mini/user/follower",
Handler: user.MiniUserFollowerHandler(serverCtx),
},
... ...
package user
import (
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/result"
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/logic/user"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
)
func MiniUserMyFollowingNewsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.MiniUserNewsRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := user.NewMiniUserMyFollowingNewsLogic(r.Context(), svcCtx)
resp, err := l.MiniUserMyFollowingNews(&req)
result.HttpResult(r, w, resp, err)
}
}
... ...
package user
import (
"context"
"github.com/samber/lo"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/contextdata"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type MiniUserMyFollowingNewsLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewMiniUserMyFollowingNewsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniUserMyFollowingNewsLogic {
return &MiniUserMyFollowingNewsLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *MiniUserMyFollowingNewsLogic) MiniUserMyFollowingNews(req *types.MiniUserNewsRequest) (resp *types.MiniUserNewsResposne, err error) {
var (
conn = l.svcCtx.DefaultDBConn()
user *domain.User
userToken = contextdata.GetUserTokenFromCtx(l.ctx)
articles []*domain.Article
readArticles = make([]*domain.UserReadArticle, 0)
userMap = make(map[int64]*domain.User)
)
if user, err = l.svcCtx.UserRepository.FindOne(l.ctx, conn, userToken.UserId); err != nil {
return nil, xerr.NewErrMsgErr("用户不存在", err)
}
var (
users = user.Following
)
if req.AuthorId > 0 {
users = []int64{req.AuthorId}
}
if _, articles, err = l.svcCtx.ArticleRepository.FindAuthorsLatestArticle(l.ctx, conn, user.CompanyId, users, user.Id, req.LastArticleId, req.Size); err != nil {
return nil, xerr.NewErrMsgErr("获取快讯异常", err)
}
resp = &types.MiniUserNewsResposne{
List: make([]types.UserNewsItem, 0),
}
articleIds := domain.Values(articles, func(item *domain.Article) int64 {
return item.Id
})
if len(articleIds) > 0 {
resp.LastArticleId = articleIds[len(articleIds)-1]
_, readArticles, _ = l.svcCtx.UserReadArticleRepository.Find(l.ctx, conn, domain.IndexCompanyId(userToken.CompanyId)().WithFindOnly().WithKV("articleIds", articleIds))
}
readArticlesMap := lo.KeyBy(readArticles, func(item *domain.UserReadArticle) int64 {
return item.ArticleId
})
lo.ForEach(articles, func(item *domain.Article, index int) {
newsItem := types.UserNewsItem{
NewsId: item.Id,
Type: "article",
Title: item.Title,
Summary: item.Summary,
Time: item.CreatedAt,
Images: make([]string, 0),
ReadFlag: false,
}
if author, _ := domain.LazyLoad(userMap, l.ctx, conn, item.AuthorId, l.svcCtx.UserRepository.FindOne); author != nil {
newsItem.Author = types.UserItem{
Id: author.Id,
Name: author.Name,
Avatar: lo.ToPtr(author.Avatar),
}
}
for _, img := range item.Images {
newsItem.Images = append(newsItem.Images, img.Url)
}
if _, ok := readArticlesMap[item.Id]; ok {
newsItem.ReadFlag = true
}
resp.List = append(resp.List, newsItem)
})
return
}
... ...
... ... @@ -39,15 +39,7 @@ func (l *MiniUserNewsLogic) MiniUserNews(req *types.MiniUserNewsRequest) (resp *
if user, err = l.svcCtx.UserRepository.FindOne(l.ctx, conn, userToken.UserId); err != nil {
return nil, xerr.NewErrMsgErr("用户不存在", err)
}
var (
users = user.Following
)
if req.AuthorId > 0 {
users = []int64{req.AuthorId}
} else {
users = append(users, user.Id)
}
if _, articles, err = l.svcCtx.ArticleRepository.FindAuthorsLatestArticle(l.ctx, conn, user.CompanyId, users, user.Id, req.LastArticleId, req.Size); err != nil {
if _, articles, err = l.svcCtx.ArticleRepository.FindLatestAccessibleArticle(l.ctx, conn, user.CompanyId, user.Id, req.LastArticleId, req.Size); err != nil {
return nil, xerr.NewErrMsgErr("获取快讯异常", err)
}
resp = &types.MiniUserNewsResposne{
... ... @@ -58,7 +50,7 @@ func (l *MiniUserNewsLogic) MiniUserNews(req *types.MiniUserNewsRequest) (resp *
})
if len(articleIds) > 0 {
resp.LastArticleId = articleIds[len(articleIds)-1]
_, readArticles, _ = l.svcCtx.UserReadArticleRepository.Find(l.ctx, conn, domain.IndexCompanyId(userToken.CompanyId)().WithKV("articleIds", articleIds))
_, readArticles, _ = l.svcCtx.UserReadArticleRepository.Find(l.ctx, conn, domain.IndexCompanyId(userToken.CompanyId)().WithFindOnly().WithKV("articleIds", articleIds))
}
readArticlesMap := lo.KeyBy(readArticles, func(item *domain.UserReadArticle) int64 {
return item.ArticleId
... ...
... ... @@ -164,6 +164,47 @@ func (repository *ArticleRepository) Find(ctx context.Context, conn transaction.
return total, dms, nil
}
// FindLatestAccessibleArticle 最新可访问的文章
func (repository *ArticleRepository) FindLatestAccessibleArticle(ctx context.Context, conn transaction.Conn,
companyId int64, whoRead int64, lastId int64, limit int) (int64, []*domain.Article, error) {
var (
tx = conn.DB()
ms []*models.Article
dms = make([]*domain.Article, 0)
total int64
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(&ms).
Where("company_id=?", companyId).
Where(fmt.Sprintf("author_id = %d or target_user=0 or who_read @>'[%d]'", whoRead, whoRead)).
Where("show = 1")
if lastId > 0 {
tx.Where("id < ?", lastId)
}
tx.Order("id desc")
if limit > 0 {
tx.Limit(limit)
}
if total, tx = transaction.PaginationAndCount(ctx, tx, domain.NewQueryOptions().WithFindOnly(), &ms); tx.Error != nil {
return dms, tx.Error
}
return dms, nil
}
if _, err := repository.Query(queryFunc); err != nil {
return 0, nil, err
}
for _, item := range ms {
if dm, err := repository.ModelToDomainModel(item); err != nil {
return 0, dms, err
} else {
dms = append(dms, dm)
}
}
return total, dms, nil
}
// FindAuthorsLatestArticle 作者最新的文章
func (repository *ArticleRepository) FindAuthorsLatestArticle(ctx context.Context, conn transaction.Conn,
companyId int64, authors []int64, whoRead int64, lastId int64, limit int) (int64, []*domain.Article, error) {
... ...
... ... @@ -43,6 +43,7 @@ type ArticleRepository interface {
FindAuthorsLatestFirstArticle(ctx context.Context, conn transaction.Conn, companyId int64, authors []int64, whoRead int64, limit int) (int64, []*Article, error)
FindAuthorsLatestFirstUnreadArticle(ctx context.Context, conn transaction.Conn, companyId int64, authors []int64, whoRead int64, limit int) (int64, []*Article, error)
FindAuthorsLatestArticle(ctx context.Context, conn transaction.Conn, companyId int64, authors []int64, whoRead int64, lastId int64, limit int) (int64, []*Article, error)
FindLatestAccessibleArticle(ctx context.Context, conn transaction.Conn, companyId int64, whoRead int64, lastId int64, limit int) (int64, []*Article, error)
IncreaseCountLove(ctx context.Context, conn transaction.Conn, incr int, articleId int64) error //点赞数量变动
IncreaseCountComment(ctx context.Context, conn transaction.Conn, incr int, articleId int64) error //评论数量变动
IncreaseCountRead(ctx context.Context, conn transaction.Conn, incr int, articleId int64) error //浏览数量变动
... ...