feature/fanout #3

Merged
daniel.w merged 11 commits from feature/fanout into main 2024-09-03 11:45:06 +00:00
31 changed files with 124 additions and 50 deletions
Showing only changes of commit ba437e8352 - Show all commits

View File

@ -117,6 +117,14 @@ issues:
- gocognit
- contextcheck
exclude-dirs:
- internal/model
exclude-files:
- .*_test.go
linters-settings:
gci:
sections:

View File

@ -18,6 +18,7 @@ test: # 進行測試
fmt: # 格式優化
$(GOFMT) -w $(GOFILES)
goimports -w ./
golangci-lint run
.PHONY: gen-rpc
gen-rpc: # 建立 rpc code

View File

@ -1,7 +1,6 @@
package domain
import (
"fmt"
"strings"
ers "code.30cm.net/digimon/library-go/errs"
@ -51,9 +50,7 @@ const (
)
func CommentError(ec ErrorCode, s ...string) *ers.LibError {
return ers.NewError(code.CloudEPTweeting, code.DBError,
ec.ToUint32(),
fmt.Sprintf("%s", strings.Join(s, " ")))
return ers.NewError(code.CloudEPTweeting, code.DBError, ec.ToUint32(), strings.Join(s, " "))
}
func CommentErrorL(ec ErrorCode,

View File

@ -37,7 +37,7 @@ func NewNeo4J(conf *Config, opts ...Option) *Client {
// Conn initiates connection to the database and returns a Neo4j driver instance.
func (c *Client) Conn() (neo4j.DriverWithContext, error) {
auth := neo4j.BasicAuth(c.serviceConf.Username, c.serviceConf.Password, "")
driver, err := neo4j.NewDriverWithContext(c.serviceConf.URI, auth, func(c *n4Cfg.Config) {})
driver, err := neo4j.NewDriverWithContext(c.serviceConf.URI, auth, func(_ *n4Cfg.Config) {})
if err != nil {
return nil, fmt.Errorf("neo4j driver initialization error: %w", err)
}

View File

@ -37,6 +37,7 @@ func (l *DeleteCommentLogic) DeleteComment(in *tweeting.DeleteCommentReq) (*twee
{Key: "err", Value: err},
},
"failed to del comment").Wrap(err)
return nil, e
}

View File

@ -41,8 +41,8 @@ func convertToCommentDetailItem(item *model.Comment) *tweeting.CommentDetail {
Uid: item.UID,
Content: item.Content,
CreatedAt: item.CreateAt,
LikeCount: int64(item.LikeCount),
DislikeCount: int64(item.DisLikeCount),
LikeCount: item.LikeCount,
DislikeCount: item.DisLikeCount,
}
}
@ -82,6 +82,7 @@ func (l *GetCommentsLogic) GetComments(in *tweeting.GetCommentsReq) (*tweeting.G
{Key: "err", Value: err},
},
"failed to find comment").Wrap(err)
return nil, e
}

View File

@ -27,16 +27,16 @@ func NewUpdateCommentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Upd
}
}
type checkCommentId struct {
CommentId string `validate:"required"`
type checkCommentID struct {
CommentID string `validate:"required"`
Content string `json:"content,omitempty" validate:"lte=500"`
}
// UpdateComment 更新評論
func (l *UpdateCommentLogic) UpdateComment(in *tweeting.UpdateCommentReq) (*tweeting.OKResp, error) {
// 驗證資料
if err := l.svcCtx.Validate.ValidateAll(&checkCommentId{
CommentId: in.GetCommentId(),
if err := l.svcCtx.Validate.ValidateAll(&checkCommentID{
CommentID: in.GetCommentId(),
Content: in.GetContent(),
}); err != nil {
// 錯誤代碼 05-011-00
@ -75,6 +75,7 @@ func (l *UpdateCommentLogic) UpdateComment(in *tweeting.UpdateCommentReq) (*twee
{Key: "err", Value: err},
},
"failed to update comment:", in.CommentId).Wrap(err)
return nil, e
}

View File

@ -32,7 +32,7 @@ type newTweetingReq struct {
UID string `json:"uid" validate:"required"`
Content string `json:"content" validate:"required,lte=500"` // 貼文限制 500 字內
Tags []string `json:"tags"`
MediaUrl []string `json:"media_url"`
MediaURL []string `json:"media_url"`
IsAd bool `json:"is_ad"` // default false
}
@ -85,6 +85,7 @@ func (l *CreatePostLogic) CreatePost(in *tweeting.NewPostReq) (*tweeting.PostRes
{Key: "err", Value: err},
},
"failed to add new post").Wrap(err)
return nil, e
}

View File

@ -37,6 +37,7 @@ func (l *DeletePostLogic) DeletePost(in *tweeting.DeletePostsReq) (*tweeting.OKR
{Key: "err", Value: err},
},
"failed to del post").Wrap(err)
return nil, e
}

View File

@ -54,8 +54,8 @@ func convertToPostDetailItem(item *model.Post) *tweeting.PostDetailItem {
IsAd: item.IsAd,
CreatedAt: item.CreateAt,
UpdateAt: item.UpdateAt,
LikeCount: int64(item.Like),
DislikeCount: int64(item.DisLike),
LikeCount: item.Like,
DislikeCount: item.DisLike,
}
}
@ -102,6 +102,7 @@ func (l *ListPostsLogic) ListPosts(in *tweeting.QueryPostsReq) (*tweeting.ListPo
{Key: "err", Value: err},
},
"failed to find posts").Wrap(err)
return nil, e
}

View File

@ -28,7 +28,7 @@ func NewUpdatePostLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Update
}
}
type checkPostId struct {
type checkPostID struct {
PostID string `validate:"required"`
Content string `json:"content,omitempty" validate:"lte=500"`
}
@ -36,7 +36,7 @@ type checkPostId struct {
// UpdatePost 更新貼文
func (l *UpdatePostLogic) UpdatePost(in *tweeting.UpdatePostReq) (*tweeting.OKResp, error) {
// 驗證資料
if err := l.svcCtx.Validate.ValidateAll(&checkPostId{
if err := l.svcCtx.Validate.ValidateAll(&checkPostID{
PostID: in.GetPostId(),
Content: in.GetContent(),
}); err != nil {
@ -53,7 +53,7 @@ func (l *UpdatePostLogic) UpdatePost(in *tweeting.UpdatePostReq) (*tweeting.OKRe
update.ID = oid
update.Tags = in.GetTags()
// 將 Media 存入
var media []model.Media
media := make([]model.Media, 0, len(in.GetMedia()))
for _, item := range in.GetMedia() {
media = append(media, model.Media{
Links: item.Url,
@ -88,6 +88,7 @@ func (l *UpdatePostLogic) UpdatePost(in *tweeting.UpdatePostReq) (*tweeting.OKRe
{Key: "err", Value: err},
},
"failed to update post", in.PostId).Wrap(err)
return nil, e
}

View File

@ -2,9 +2,10 @@ package socialnetworkservicelogic
import (
"app-cloudep-tweeting-service/internal/domain"
ers "code.30cm.net/digimon/library-go/errs"
"context"
ers "code.30cm.net/digimon/library-go/errs"
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
"app-cloudep-tweeting-service/internal/svc"

View File

@ -3,9 +3,10 @@ package socialnetworkservicelogic
import (
"app-cloudep-tweeting-service/internal/domain"
"app-cloudep-tweeting-service/internal/domain/repository"
ers "code.30cm.net/digimon/library-go/errs"
"context"
ers "code.30cm.net/digimon/library-go/errs"
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
"app-cloudep-tweeting-service/internal/svc"

View File

@ -2,9 +2,10 @@ package socialnetworkservicelogic
import (
"app-cloudep-tweeting-service/internal/domain"
ers "code.30cm.net/digimon/library-go/errs"
"context"
ers "code.30cm.net/digimon/library-go/errs"
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
"app-cloudep-tweeting-service/internal/svc"

View File

@ -3,9 +3,10 @@ package socialnetworkservicelogic
import (
"app-cloudep-tweeting-service/internal/domain"
"app-cloudep-tweeting-service/internal/domain/repository"
ers "code.30cm.net/digimon/library-go/errs"
"context"
ers "code.30cm.net/digimon/library-go/errs"
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
"app-cloudep-tweeting-service/internal/svc"

View File

@ -2,9 +2,10 @@ package socialnetworkservicelogic
import (
"app-cloudep-tweeting-service/internal/domain"
ers "code.30cm.net/digimon/library-go/errs"
"context"
ers "code.30cm.net/digimon/library-go/errs"
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
"app-cloudep-tweeting-service/internal/svc"

View File

@ -2,9 +2,10 @@ package socialnetworkservicelogic
import (
"app-cloudep-tweeting-service/internal/domain"
ers "code.30cm.net/digimon/library-go/errs"
"context"
ers "code.30cm.net/digimon/library-go/errs"
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
"app-cloudep-tweeting-service/internal/svc"

View File

@ -69,6 +69,7 @@ func (l *AddPostLogic) AddPost(in *tweeting.AddPostToTimelineReq) (*tweeting.OKR
{Key: "err", Value: err},
},
"failed to insert timeline repo :", in.GetUid()).Wrap(err)
return nil, e
}

View File

@ -52,6 +52,7 @@ func (l *ClearNoMoreDataFlagLogic) ClearNoMoreDataFlag(in *tweeting.DoNoMoreData
{Key: "err", Value: err},
},
"failed to clear no more data flag timeline repo :", in.GetUid()).Wrap(err)
return nil, e
}

View File

@ -61,6 +61,7 @@ func (l *FetchTimelineLogic) FetchTimeline(in *tweeting.GetTimelineReq) (*tweeti
{Key: "err", Value: err},
},
"failed to fetch timeline repo :", in.GetUid()).Wrap(err)
return nil, e
}

View File

@ -52,6 +52,7 @@ func (l *HasNoMoreDataLogic) HasNoMoreData(in *tweeting.DoNoMoreDataReq) (*tweet
{Key: "err", Value: err},
},
"failed to get no more data flag:", in.GetUid()).Wrap(err)
return nil, e
}

View File

@ -52,6 +52,7 @@ func (l *SetNoMoreDataFlagLogic) SetNoMoreDataFlag(in *tweeting.DoNoMoreDataReq)
{Key: "err", Value: err},
},
"failed to set no more data flag:", in.GetUid()).Wrap(err)
return nil, e
}

View File

@ -6,6 +6,9 @@ import (
client4J "app-cloudep-tweeting-service/internal/lib/neo4j"
"context"
"fmt"
"github.com/zeromicro/go-zero/core/logx"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
)
@ -27,6 +30,7 @@ func MustSocialNetworkRepository(param SocialNetworkParam) repository.SocialNetw
}
func (s *SocialNetworkRepository) CreateUserNode(ctx context.Context, uid string) error {
//nolint:contextcheck
session, err := s.neo4jClient.Conn()
if err != nil {
return err
@ -53,6 +57,7 @@ func (s *SocialNetworkRepository) CreateUserNode(ctx context.Context, uid string
}
func (s *SocialNetworkRepository) MarkFollowerRelation(ctx context.Context, fromUID, toUID string) error {
//nolint:contextcheck
session, err := s.neo4jClient.Conn()
if err != nil {
return err
@ -88,6 +93,7 @@ func (s *SocialNetworkRepository) MarkFollowerRelation(ctx context.Context, from
}
func (s *SocialNetworkRepository) GetFollower(ctx context.Context, req repository.FollowReq) (repository.FollowResp, error) {
//nolint:contextcheck
session, err := s.neo4jClient.Conn()
if err != nil {
return repository.FollowResp{}, err
@ -113,11 +119,17 @@ func (s *SocialNetworkRepository) GetFollower(ctx context.Context, req repositor
return repository.FollowResp{}, err
}
var uids []string
var uidList []string
for run.Next(ctx) {
record := run.Record()
if uid, ok := record.Get("uid"); ok {
uids = append(uids, uid.(string))
uid, ok := record.Get("uid")
if ok {
if uidStr, ok := uid.(string); ok {
uidList = append(uidList, uidStr)
} else {
// TODO 可以印 log
continue
}
}
}
@ -127,12 +139,13 @@ func (s *SocialNetworkRepository) GetFollower(ctx context.Context, req repositor
}
return repository.FollowResp{
UIDs: uids,
UIDs: uidList,
Total: total,
}, nil
}
func (s *SocialNetworkRepository) GetFollowee(ctx context.Context, req repository.FollowReq) (repository.FollowResp, error) {
//nolint:contextcheck
session, err := s.neo4jClient.Conn()
if err != nil {
return repository.FollowResp{}, err
@ -158,11 +171,17 @@ func (s *SocialNetworkRepository) GetFollowee(ctx context.Context, req repositor
return repository.FollowResp{}, err
}
var uids []string
var uidList []string
for run.Next(ctx) {
record := run.Record()
if uid, ok := record.Get("uid"); ok {
uids = append(uids, uid.(string))
uid, ok := record.Get("uid")
if ok {
if uidStr, ok := uid.(string); ok {
uidList = append(uidList, uidStr)
} else {
// 可以印 log
continue
}
}
}
@ -172,12 +191,13 @@ func (s *SocialNetworkRepository) GetFollowee(ctx context.Context, req repositor
}
return repository.FollowResp{
UIDs: uids,
UIDs: uidList,
Total: total,
}, nil
}
func (s *SocialNetworkRepository) GetFollowerCount(ctx context.Context, uid string) (int64, error) {
//nolint:contextcheck
session, err := s.neo4jClient.Conn()
if err != nil {
return 0, err
@ -204,7 +224,11 @@ func (s *SocialNetworkRepository) GetFollowerCount(ctx context.Context, uid stri
if run.Next(ctx) {
record := run.Record()
if followerCount, ok := record.Get("followerCount"); ok {
count = followerCount.(int64)
if dc, ok := followerCount.(int64); ok {
count = dc
} else {
logx.Info("followerCount error")
}
}
}
@ -212,6 +236,7 @@ func (s *SocialNetworkRepository) GetFollowerCount(ctx context.Context, uid stri
}
func (s *SocialNetworkRepository) GetFolloweeCount(ctx context.Context, uid string) (int64, error) {
//nolint:contextcheck
session, err := s.neo4jClient.Conn()
if err != nil {
return 0, err
@ -238,7 +263,11 @@ func (s *SocialNetworkRepository) GetFolloweeCount(ctx context.Context, uid stri
if run.Next(ctx) {
record := run.Record()
if followeeCount, ok := record.Get("followeeCount"); ok {
count = followeeCount.(int64)
if dc, ok := followeeCount.(int64); ok {
count = dc
} else {
logx.Info("followeeCount error")
}
}
}
@ -246,6 +275,7 @@ func (s *SocialNetworkRepository) GetFolloweeCount(ctx context.Context, uid stri
}
func (s *SocialNetworkRepository) RemoveFollowerRelation(ctx context.Context, fromUID, toUID string) error {
//nolint:contextcheck
session, err := s.neo4jClient.Conn()
if err != nil {
return err
@ -274,6 +304,7 @@ func (s *SocialNetworkRepository) RemoveFollowerRelation(ctx context.Context, fr
// GetDegreeBetweenUsers 取得這兩個點之間的度數 (最短路徑長度)
func (s *SocialNetworkRepository) GetDegreeBetweenUsers(ctx context.Context, uid1, uid2 string) (int64, error) {
//nolint:contextcheck
session, err := s.neo4jClient.Conn()
if err != nil {
return 0, err
@ -302,7 +333,11 @@ func (s *SocialNetworkRepository) GetDegreeBetweenUsers(ctx context.Context, uid
if run.Next(ctx) {
record := run.Record()
if deg, ok := record.Get("degree"); ok {
degree = deg.(int64)
if degreeValue, ok := deg.(int64); ok {
degree = degreeValue
} else {
logx.Info("degree error")
}
}
}
@ -311,6 +346,7 @@ func (s *SocialNetworkRepository) GetDegreeBetweenUsers(ctx context.Context, uid
// GetUIDsWithinNDegrees 取得某個節點在 n 度內關係所有 UID
func (s *SocialNetworkRepository) GetUIDsWithinNDegrees(ctx context.Context, uid string, degrees, pageSize, pageIndex int64) ([]string, int64, error) {
//nolint:contextcheck
session, err := s.neo4jClient.Conn()
if err != nil {
return nil, 0, err
@ -339,11 +375,17 @@ func (s *SocialNetworkRepository) GetUIDsWithinNDegrees(ctx context.Context, uid
return nil, 0, fmt.Errorf("failed to get uids within %d degrees of user: %w", degrees, err)
}
var uids []string
var uidList []string
for run.Next(ctx) {
record := run.Record()
if uid, ok := record.Get("uid"); ok {
uids = append(uids, uid.(string))
uid, ok := record.Get("uid")
if ok {
if uidStr, ok := uid.(string); ok {
uidList = append(uidList, uidStr)
} else {
// 可以印 log
continue
}
}
}
@ -353,10 +395,11 @@ func (s *SocialNetworkRepository) GetUIDsWithinNDegrees(ctx context.Context, uid
return nil, 0, err
}
return uids, totalCount, nil
return uidList, totalCount, nil
}
func (s *SocialNetworkRepository) getTotalUIDsWithinNDegrees(ctx context.Context, uid string, degrees int64) (int64, error) {
//nolint:contextcheck
session, err := s.neo4jClient.Conn()
if err != nil {
return 0, err
@ -384,7 +427,11 @@ func (s *SocialNetworkRepository) getTotalUIDsWithinNDegrees(ctx context.Context
if run.Next(ctx) {
record := run.Record()
if count, ok := record.Get("totalCount"); ok {
totalCount = count.(int64)
if countV, ok := count.(int64); ok {
totalCount = countV
} else {
logx.Info("totalCount error")
}
}
}

View File

@ -139,5 +139,6 @@ func (t *TimelineRepository) ClearNoMoreDataFlag(ctx context.Context, uid string
key := domain.TimelineRedisKey.With(uid).ToString()
// 移除 "NoMoreData" 標誌
_, err := t.redis.ZremCtx(ctx, key, domain.LastOfTimelineFlag)
return err
}

View File

@ -7,7 +7,7 @@ import (
"context"
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
"app-cloudep-tweeting-service/internal/logic/commentservice"
commentservicelogic "app-cloudep-tweeting-service/internal/logic/commentservice"
"app-cloudep-tweeting-service/internal/svc"
)

View File

@ -7,7 +7,7 @@ import (
"context"
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
"app-cloudep-tweeting-service/internal/logic/postservice"
postservicelogic "app-cloudep-tweeting-service/internal/logic/postservice"
"app-cloudep-tweeting-service/internal/svc"
)

View File

@ -7,7 +7,7 @@ import (
"context"
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
"app-cloudep-tweeting-service/internal/logic/socialnetworkservice"
socialnetworkservicelogic "app-cloudep-tweeting-service/internal/logic/socialnetworkservice"
"app-cloudep-tweeting-service/internal/svc"
)

View File

@ -7,7 +7,7 @@ import (
"context"
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
"app-cloudep-tweeting-service/internal/logic/timelineservice"
timelineservicelogic "app-cloudep-tweeting-service/internal/logic/timelineservice"
"app-cloudep-tweeting-service/internal/svc"
)

View File

@ -6,7 +6,7 @@ import (
"fmt"
)
func mustMongoConnectUrl(c config.Config) string {
func mustMongoConnectURL(c config.Config) string {
return fmt.Sprintf("%s://%s:%s",
c.Mongo.Schema,
c.Mongo.Host,
@ -18,10 +18,12 @@ func mustMongoConnectUrl(c config.Config) string {
func MustPostModel(c config.Config) model.PostModel {
postCollection := model.Post{}
return model.NewPostModel(mustMongoConnectUrl(c), c.Mongo.Database, postCollection.CollectionName())
return model.NewPostModel(mustMongoConnectURL(c), c.Mongo.Database, postCollection.CollectionName())
}
func MustCommentModel(c config.Config) model.CommentModel {
m := model.Comment{}
return model.NewCommentModel(mustMongoConnectUrl(c), c.Mongo.Database, m.CollectionName())
return model.NewCommentModel(mustMongoConnectURL(c), c.Mongo.Database, m.CollectionName())
}

View File

@ -6,6 +6,7 @@ import (
"app-cloudep-tweeting-service/internal/lib/neo4j"
model "app-cloudep-tweeting-service/internal/model/mongo"
"app-cloudep-tweeting-service/internal/repository"
"github.com/zeromicro/go-zero/core/stores/redis"
vi "code.30cm.net/digimon/library-go/validator"

View File

@ -1,9 +1,6 @@
package main
import (
"flag"
"fmt"
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
"app-cloudep-tweeting-service/internal/config"
commentserviceServer "app-cloudep-tweeting-service/internal/server/commentservice"
@ -11,6 +8,8 @@ import (
socialnetworkserviceServer "app-cloudep-tweeting-service/internal/server/socialnetworkservice"
timelineserviceServer "app-cloudep-tweeting-service/internal/server/timelineservice"
"app-cloudep-tweeting-service/internal/svc"
"flag"
"log"
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/core/service"
@ -40,6 +39,6 @@ func main() {
})
defer s.Stop()
fmt.Printf("Starting rpc server at %s...\n", c.ListenOn)
log.Printf("Starting rpc server at %s...\n", c.ListenOn)
s.Start()
}