正在显示
7 个修改的文件
包含
167 行增加
和
10 行删除
@@ -55,6 +55,9 @@ service Core { | @@ -55,6 +55,9 @@ service Core { | ||
55 | @doc "用户快讯" | 55 | @doc "用户快讯" |
56 | @handler miniUserNews | 56 | @handler miniUserNews |
57 | post /mini/user/news (MiniUserNewsRequest)returns(MiniUserNewsResposne) | 57 | post /mini/user/news (MiniUserNewsRequest)returns(MiniUserNewsResposne) |
58 | + @doc "我关注人发布的信息" | ||
59 | + @handler miniUserMyFollowingNews | ||
60 | + post /mini/user/my_following_news (MiniUserNewsRequest)returns(MiniUserNewsResposne) | ||
58 | @doc "关注我的人" | 61 | @doc "关注我的人" |
59 | @handler miniUserFollower | 62 | @handler miniUserFollower |
60 | post /mini/user/follower (MiniUserFollowedSearchRequest)returns(MiniUserFollowedSearchResponse) | 63 | post /mini/user/follower (MiniUserFollowedSearchRequest)returns(MiniUserFollowedSearchResponse) |
@@ -276,6 +276,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { | @@ -276,6 +276,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { | ||
276 | }, | 276 | }, |
277 | { | 277 | { |
278 | Method: http.MethodPost, | 278 | Method: http.MethodPost, |
279 | + Path: "/mini/user/my_following_news", | ||
280 | + Handler: user.MiniUserMyFollowingNewsHandler(serverCtx), | ||
281 | + }, | ||
282 | + { | ||
283 | + Method: http.MethodPost, | ||
279 | Path: "/mini/user/follower", | 284 | Path: "/mini/user/follower", |
280 | Handler: user.MiniUserFollowerHandler(serverCtx), | 285 | Handler: user.MiniUserFollowerHandler(serverCtx), |
281 | }, | 286 | }, |
1 | +package user | ||
2 | + | ||
3 | +import ( | ||
4 | + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/result" | ||
5 | + "net/http" | ||
6 | + | ||
7 | + "github.com/zeromicro/go-zero/rest/httpx" | ||
8 | + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/logic/user" | ||
9 | + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc" | ||
10 | + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types" | ||
11 | +) | ||
12 | + | ||
13 | +func MiniUserMyFollowingNewsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { | ||
14 | + return func(w http.ResponseWriter, r *http.Request) { | ||
15 | + var req types.MiniUserNewsRequest | ||
16 | + if err := httpx.Parse(r, &req); err != nil { | ||
17 | + httpx.ErrorCtx(r.Context(), w, err) | ||
18 | + return | ||
19 | + } | ||
20 | + | ||
21 | + l := user.NewMiniUserMyFollowingNewsLogic(r.Context(), svcCtx) | ||
22 | + resp, err := l.MiniUserMyFollowingNews(&req) | ||
23 | + result.HttpResult(r, w, resp, err) | ||
24 | + } | ||
25 | +} |
1 | +package user | ||
2 | + | ||
3 | +import ( | ||
4 | + "context" | ||
5 | + "github.com/samber/lo" | ||
6 | + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain" | ||
7 | + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/contextdata" | ||
8 | + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr" | ||
9 | + | ||
10 | + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc" | ||
11 | + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types" | ||
12 | + | ||
13 | + "github.com/zeromicro/go-zero/core/logx" | ||
14 | +) | ||
15 | + | ||
16 | +type MiniUserMyFollowingNewsLogic struct { | ||
17 | + logx.Logger | ||
18 | + ctx context.Context | ||
19 | + svcCtx *svc.ServiceContext | ||
20 | +} | ||
21 | + | ||
22 | +func NewMiniUserMyFollowingNewsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniUserMyFollowingNewsLogic { | ||
23 | + return &MiniUserMyFollowingNewsLogic{ | ||
24 | + Logger: logx.WithContext(ctx), | ||
25 | + ctx: ctx, | ||
26 | + svcCtx: svcCtx, | ||
27 | + } | ||
28 | +} | ||
29 | + | ||
30 | +func (l *MiniUserMyFollowingNewsLogic) MiniUserMyFollowingNews(req *types.MiniUserNewsRequest) (resp *types.MiniUserNewsResposne, err error) { | ||
31 | + var ( | ||
32 | + conn = l.svcCtx.DefaultDBConn() | ||
33 | + user *domain.User | ||
34 | + userToken = contextdata.GetUserTokenFromCtx(l.ctx) | ||
35 | + articles []*domain.Article | ||
36 | + readArticles = make([]*domain.UserReadArticle, 0) | ||
37 | + userMap = make(map[int64]*domain.User) | ||
38 | + ) | ||
39 | + if user, err = l.svcCtx.UserRepository.FindOne(l.ctx, conn, userToken.UserId); err != nil { | ||
40 | + return nil, xerr.NewErrMsgErr("用户不存在", err) | ||
41 | + } | ||
42 | + var ( | ||
43 | + users = user.Following | ||
44 | + ) | ||
45 | + if req.AuthorId > 0 { | ||
46 | + users = []int64{req.AuthorId} | ||
47 | + } | ||
48 | + if _, articles, err = l.svcCtx.ArticleRepository.FindAuthorsLatestArticle(l.ctx, conn, user.CompanyId, users, user.Id, req.LastArticleId, req.Size); err != nil { | ||
49 | + return nil, xerr.NewErrMsgErr("获取快讯异常", err) | ||
50 | + } | ||
51 | + resp = &types.MiniUserNewsResposne{ | ||
52 | + List: make([]types.UserNewsItem, 0), | ||
53 | + } | ||
54 | + articleIds := domain.Values(articles, func(item *domain.Article) int64 { | ||
55 | + return item.Id | ||
56 | + }) | ||
57 | + if len(articleIds) > 0 { | ||
58 | + resp.LastArticleId = articleIds[len(articleIds)-1] | ||
59 | + _, readArticles, _ = l.svcCtx.UserReadArticleRepository.Find(l.ctx, conn, domain.IndexCompanyId(userToken.CompanyId)().WithFindOnly().WithKV("articleIds", articleIds)) | ||
60 | + } | ||
61 | + readArticlesMap := lo.KeyBy(readArticles, func(item *domain.UserReadArticle) int64 { | ||
62 | + return item.ArticleId | ||
63 | + }) | ||
64 | + lo.ForEach(articles, func(item *domain.Article, index int) { | ||
65 | + newsItem := types.UserNewsItem{ | ||
66 | + NewsId: item.Id, | ||
67 | + Type: "article", | ||
68 | + Title: item.Title, | ||
69 | + Summary: item.Summary, | ||
70 | + Time: item.CreatedAt, | ||
71 | + Images: make([]string, 0), | ||
72 | + ReadFlag: false, | ||
73 | + } | ||
74 | + if author, _ := domain.LazyLoad(userMap, l.ctx, conn, item.AuthorId, l.svcCtx.UserRepository.FindOne); author != nil { | ||
75 | + newsItem.Author = types.UserItem{ | ||
76 | + Id: author.Id, | ||
77 | + Name: author.Name, | ||
78 | + Avatar: lo.ToPtr(author.Avatar), | ||
79 | + } | ||
80 | + } | ||
81 | + for _, img := range item.Images { | ||
82 | + newsItem.Images = append(newsItem.Images, img.Url) | ||
83 | + } | ||
84 | + if _, ok := readArticlesMap[item.Id]; ok { | ||
85 | + newsItem.ReadFlag = true | ||
86 | + } | ||
87 | + resp.List = append(resp.List, newsItem) | ||
88 | + }) | ||
89 | + return | ||
90 | +} |
@@ -39,15 +39,7 @@ func (l *MiniUserNewsLogic) MiniUserNews(req *types.MiniUserNewsRequest) (resp * | @@ -39,15 +39,7 @@ func (l *MiniUserNewsLogic) MiniUserNews(req *types.MiniUserNewsRequest) (resp * | ||
39 | if user, err = l.svcCtx.UserRepository.FindOne(l.ctx, conn, userToken.UserId); err != nil { | 39 | if user, err = l.svcCtx.UserRepository.FindOne(l.ctx, conn, userToken.UserId); err != nil { |
40 | return nil, xerr.NewErrMsgErr("用户不存在", err) | 40 | return nil, xerr.NewErrMsgErr("用户不存在", err) |
41 | } | 41 | } |
42 | - var ( | ||
43 | - users = user.Following | ||
44 | - ) | ||
45 | - if req.AuthorId > 0 { | ||
46 | - users = []int64{req.AuthorId} | ||
47 | - } else { | ||
48 | - users = append(users, user.Id) | ||
49 | - } | ||
50 | - if _, articles, err = l.svcCtx.ArticleRepository.FindAuthorsLatestArticle(l.ctx, conn, user.CompanyId, users, user.Id, req.LastArticleId, req.Size); err != nil { | 42 | + if _, articles, err = l.svcCtx.ArticleRepository.FindLatestAccessibleArticle(l.ctx, conn, user.CompanyId, user.Id, req.LastArticleId, req.Size); err != nil { |
51 | return nil, xerr.NewErrMsgErr("获取快讯异常", err) | 43 | return nil, xerr.NewErrMsgErr("获取快讯异常", err) |
52 | } | 44 | } |
53 | resp = &types.MiniUserNewsResposne{ | 45 | resp = &types.MiniUserNewsResposne{ |
@@ -58,7 +50,7 @@ func (l *MiniUserNewsLogic) MiniUserNews(req *types.MiniUserNewsRequest) (resp * | @@ -58,7 +50,7 @@ func (l *MiniUserNewsLogic) MiniUserNews(req *types.MiniUserNewsRequest) (resp * | ||
58 | }) | 50 | }) |
59 | if len(articleIds) > 0 { | 51 | if len(articleIds) > 0 { |
60 | resp.LastArticleId = articleIds[len(articleIds)-1] | 52 | resp.LastArticleId = articleIds[len(articleIds)-1] |
61 | - _, readArticles, _ = l.svcCtx.UserReadArticleRepository.Find(l.ctx, conn, domain.IndexCompanyId(userToken.CompanyId)().WithKV("articleIds", articleIds)) | 53 | + _, readArticles, _ = l.svcCtx.UserReadArticleRepository.Find(l.ctx, conn, domain.IndexCompanyId(userToken.CompanyId)().WithFindOnly().WithKV("articleIds", articleIds)) |
62 | } | 54 | } |
63 | readArticlesMap := lo.KeyBy(readArticles, func(item *domain.UserReadArticle) int64 { | 55 | readArticlesMap := lo.KeyBy(readArticles, func(item *domain.UserReadArticle) int64 { |
64 | return item.ArticleId | 56 | return item.ArticleId |
@@ -164,6 +164,47 @@ func (repository *ArticleRepository) Find(ctx context.Context, conn transaction. | @@ -164,6 +164,47 @@ func (repository *ArticleRepository) Find(ctx context.Context, conn transaction. | ||
164 | return total, dms, nil | 164 | return total, dms, nil |
165 | } | 165 | } |
166 | 166 | ||
167 | +// FindLatestAccessibleArticle 最新可访问的文章 | ||
168 | +func (repository *ArticleRepository) FindLatestAccessibleArticle(ctx context.Context, conn transaction.Conn, | ||
169 | + companyId int64, whoRead int64, lastId int64, limit int) (int64, []*domain.Article, error) { | ||
170 | + var ( | ||
171 | + tx = conn.DB() | ||
172 | + ms []*models.Article | ||
173 | + dms = make([]*domain.Article, 0) | ||
174 | + total int64 | ||
175 | + ) | ||
176 | + queryFunc := func() (interface{}, error) { | ||
177 | + tx = tx.Model(&ms). | ||
178 | + Where("company_id=?", companyId). | ||
179 | + Where(fmt.Sprintf("author_id = %d or target_user=0 or who_read @>'[%d]'", whoRead, whoRead)). | ||
180 | + Where("show = 1") | ||
181 | + if lastId > 0 { | ||
182 | + tx.Where("id < ?", lastId) | ||
183 | + } | ||
184 | + tx.Order("id desc") | ||
185 | + if limit > 0 { | ||
186 | + tx.Limit(limit) | ||
187 | + } | ||
188 | + if total, tx = transaction.PaginationAndCount(ctx, tx, domain.NewQueryOptions().WithFindOnly(), &ms); tx.Error != nil { | ||
189 | + return dms, tx.Error | ||
190 | + } | ||
191 | + return dms, nil | ||
192 | + } | ||
193 | + | ||
194 | + if _, err := repository.Query(queryFunc); err != nil { | ||
195 | + return 0, nil, err | ||
196 | + } | ||
197 | + | ||
198 | + for _, item := range ms { | ||
199 | + if dm, err := repository.ModelToDomainModel(item); err != nil { | ||
200 | + return 0, dms, err | ||
201 | + } else { | ||
202 | + dms = append(dms, dm) | ||
203 | + } | ||
204 | + } | ||
205 | + return total, dms, nil | ||
206 | +} | ||
207 | + | ||
167 | // FindAuthorsLatestArticle 作者最新的文章 | 208 | // FindAuthorsLatestArticle 作者最新的文章 |
168 | func (repository *ArticleRepository) FindAuthorsLatestArticle(ctx context.Context, conn transaction.Conn, | 209 | func (repository *ArticleRepository) FindAuthorsLatestArticle(ctx context.Context, conn transaction.Conn, |
169 | companyId int64, authors []int64, whoRead int64, lastId int64, limit int) (int64, []*domain.Article, error) { | 210 | companyId int64, authors []int64, whoRead int64, lastId int64, limit int) (int64, []*domain.Article, error) { |
@@ -43,6 +43,7 @@ type ArticleRepository interface { | @@ -43,6 +43,7 @@ type ArticleRepository interface { | ||
43 | FindAuthorsLatestFirstArticle(ctx context.Context, conn transaction.Conn, companyId int64, authors []int64, whoRead int64, limit int) (int64, []*Article, error) | 43 | FindAuthorsLatestFirstArticle(ctx context.Context, conn transaction.Conn, companyId int64, authors []int64, whoRead int64, limit int) (int64, []*Article, error) |
44 | FindAuthorsLatestFirstUnreadArticle(ctx context.Context, conn transaction.Conn, companyId int64, authors []int64, whoRead int64, limit int) (int64, []*Article, error) | 44 | FindAuthorsLatestFirstUnreadArticle(ctx context.Context, conn transaction.Conn, companyId int64, authors []int64, whoRead int64, limit int) (int64, []*Article, error) |
45 | FindAuthorsLatestArticle(ctx context.Context, conn transaction.Conn, companyId int64, authors []int64, whoRead int64, lastId int64, limit int) (int64, []*Article, error) | 45 | FindAuthorsLatestArticle(ctx context.Context, conn transaction.Conn, companyId int64, authors []int64, whoRead int64, lastId int64, limit int) (int64, []*Article, error) |
46 | + FindLatestAccessibleArticle(ctx context.Context, conn transaction.Conn, companyId int64, whoRead int64, lastId int64, limit int) (int64, []*Article, error) | ||
46 | IncreaseCountLove(ctx context.Context, conn transaction.Conn, incr int, articleId int64) error //点赞数量变动 | 47 | IncreaseCountLove(ctx context.Context, conn transaction.Conn, incr int, articleId int64) error //点赞数量变动 |
47 | IncreaseCountComment(ctx context.Context, conn transaction.Conn, incr int, articleId int64) error //评论数量变动 | 48 | IncreaseCountComment(ctx context.Context, conn transaction.Conn, incr int, articleId int64) error //评论数量变动 |
48 | IncreaseCountRead(ctx context.Context, conn transaction.Conn, incr int, articleId int64) error //浏览数量变动 | 49 | IncreaseCountRead(ctx context.Context, conn transaction.Conn, incr int, articleId int64) error //浏览数量变动 |
-
请 注册 或 登录 后发表评论