diff --git a/src/idl/feed.proto b/src/idl/feed.proto index 0f0c5e7..1d4a9e6 100644 --- a/src/idl/feed.proto +++ b/src/idl/feed.proto @@ -22,7 +22,7 @@ message ListFeedRequest { message ListFeedResponse { int32 status_code = 1; string status_msg = 2; - optional uint32 next_time = 3; + optional uint64 next_time = 3; repeated Video video_list = 4; } diff --git a/src/models/message.go b/src/models/message.go index 2add457..815b3cf 100644 --- a/src/models/message.go +++ b/src/models/message.go @@ -8,7 +8,7 @@ import ( type Message struct { ID uint32 `gorm:"not null;primarykey;autoIncrement"` - ToUserId uint32 `gorm:"not null" ` + ToUserId uint32 `gorm:"not null"` FromUserId uint32 `gorm:"not null"` ConversationId string `gorm:"not null" index:"conversationid"` Content string `gorm:"not null"` diff --git a/src/rpc/feed/feed.pb.go b/src/rpc/feed/feed.pb.go index f19c729..4801b1d 100644 --- a/src/rpc/feed/feed.pb.go +++ b/src/rpc/feed/feed.pb.go @@ -186,7 +186,7 @@ type ListFeedResponse struct { StatusCode int32 `protobuf:"varint,1,opt,name=status_code,json=statusCode,proto3" json:"status_code,omitempty"` StatusMsg string `protobuf:"bytes,2,opt,name=status_msg,json=statusMsg,proto3" json:"status_msg,omitempty"` - NextTime *uint32 `protobuf:"varint,3,opt,name=next_time,json=nextTime,proto3,oneof" json:"next_time,omitempty"` + NextTime *uint64 `protobuf:"varint,3,opt,name=next_time,json=nextTime,proto3,oneof" json:"next_time,omitempty"` VideoList []*Video `protobuf:"bytes,4,rep,name=video_list,json=videoList,proto3" json:"video_list,omitempty"` } @@ -236,7 +236,7 @@ func (x *ListFeedResponse) GetStatusMsg() string { return "" } -func (x *ListFeedResponse) GetNextTime() uint32 { +func (x *ListFeedResponse) GetNextTime() uint64 { if x != nil && x.NextTime != nil { return *x.NextTime } @@ -638,7 +638,7 @@ var file_feed_proto_rawDesc = []byte{ 0x0a, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x43, 0x6f, 0x64, 0x65, 0x12, 0x1d, 0x0a, 0x0a, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x5f, 0x6d, 0x73, 0x67, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x4d, 0x73, 0x67, 0x12, 0x20, 0x0a, 0x09, 0x6e, 0x65, - 0x78, 0x74, 0x5f, 0x74, 0x69, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0d, 0x48, 0x00, 0x52, + 0x78, 0x74, 0x5f, 0x74, 0x69, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x04, 0x48, 0x00, 0x52, 0x08, 0x6e, 0x65, 0x78, 0x74, 0x54, 0x69, 0x6d, 0x65, 0x88, 0x01, 0x01, 0x12, 0x2e, 0x0a, 0x0a, 0x76, 0x69, 0x64, 0x65, 0x6f, 0x5f, 0x6c, 0x69, 0x73, 0x74, 0x18, 0x04, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x72, 0x70, 0x63, 0x2e, 0x66, 0x65, 0x65, 0x64, 0x2e, 0x56, 0x69, 0x64, 0x65, diff --git a/src/services/feed/handler.go b/src/services/feed/handler.go index 2933f4d..7fe2b49 100644 --- a/src/services/feed/handler.go +++ b/src/services/feed/handler.go @@ -127,11 +127,11 @@ func (s FeedServiceImpl) ListVideosByRecommend(ctx context.Context, request *fee defer span.End() logger := logging.LogService("FeedService.ListVideos").WithContext(ctx) - now := time.Now().Unix() + now := time.Now().UnixMilli() latestTime := now if request.LatestTime != nil && *request.LatestTime != "" { // Check if request.LatestTime is a timestamp - t, ok := isUnixTimestamp(*request.LatestTime) + t, ok := isUnixMilliTimestamp(*request.LatestTime) if ok { latestTime = t } else { @@ -163,7 +163,7 @@ func (s FeedServiceImpl) ListVideosByRecommend(ctx context.Context, request *fee recommendVideoId := recommendResponse.VideoList find, err := findRecommendVideos(ctx, recommendVideoId) - nextTimeStamp := uint32(latestTime) + nextTimeStamp := uint64(latestTime) if err != nil { logger.WithFields(logrus.Fields{ "find": find, @@ -236,11 +236,11 @@ func (s FeedServiceImpl) ListVideos(ctx context.Context, request *feed.ListFeedR defer span.End() logger := logging.LogService("FeedService.ListVideos").WithContext(ctx) - now := time.Now().Unix() + now := time.Now().UnixMilli() latestTime := now if request.LatestTime != nil && *request.LatestTime != "" { // Check if request.LatestTime is a timestamp - t, ok := isUnixTimestamp(*request.LatestTime) + t, ok := isUnixMilliTimestamp(*request.LatestTime) if ok { latestTime = t } else { @@ -252,7 +252,7 @@ func (s FeedServiceImpl) ListVideos(ctx context.Context, request *feed.ListFeedR } find, nextTime, err := findVideos(ctx, latestTime) - nextTimeStamp := uint32(nextTime.Unix()) + nextTimeStamp := uint64(nextTime.UnixMilli()) if err != nil { logger.WithFields(logrus.Fields{ "find": find, @@ -444,10 +444,10 @@ func (s FeedServiceImpl) QueryVideoSummaryAndKeywords(ctx context.Context, req * func findVideos(ctx context.Context, latestTime int64) ([]*models.Video, time.Time, error) { logger := logging.LogService("ListVideos.findVideos").WithContext(ctx) - nextTime := time.Unix(latestTime, 0) + nextTime := time.UnixMilli(latestTime) var videos []*models.Video - result := database.Client.Where("created_at < ?", time.Unix(latestTime, 0)). + result := database.Client.Where("created_at < ?", nextTime). Order("created_at DESC"). Limit(VideoCount). Find(&videos) @@ -464,7 +464,7 @@ func findVideos(ctx context.Context, latestTime int64) ([]*models.Video, time.Ti } logger.WithFields(logrus.Fields{ - "latestTime": time.Unix(latestTime, 0), + "latestTime": time.UnixMilli(latestTime), "VideosCount": len(videos), "NextTime": nextTime, }).Debugf("Find videos") @@ -645,7 +645,7 @@ func query(ctx context.Context, logger *logrus.Entry, actorId uint32, videoIds [ return queryDetailed(ctx, logger, actorId, videos), nil } -func isUnixTimestamp(s string) (int64, bool) { +func isUnixMilliTimestamp(s string) (int64, bool) { timestamp, err := strconv.ParseInt(s, 10, 64) if err != nil { return 0, false @@ -654,7 +654,7 @@ func isUnixTimestamp(s string) (int64, bool) { startTime := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC) endTime := time.Now().AddDate(100, 0, 0) - t := time.Unix(timestamp, 0) + t := time.UnixMilli(timestamp) res := t.After(startTime) && t.Before(endTime) return timestamp, res diff --git a/src/services/message/handler.go b/src/services/message/handler.go index 20af58a..e9e2da3 100644 --- a/src/services/message/handler.go +++ b/src/services/message/handler.go @@ -18,6 +18,7 @@ import ( "fmt" "github.com/go-redis/redis_rate/v10" "github.com/robfig/cron/v3" + "gorm.io/gorm" "time" "github.com/sirupsen/logrus" @@ -50,8 +51,8 @@ func (c MessageServiceImpl) New() { chatClient = chat.NewChatServiceClient(chatRpcConn) cronRunner := cron.New(cron.WithSeconds()) - _, err := cronRunner.AddFunc("0 0 18 * * *", sendMagicMessage) // execute every 18:00 - //_, err := cronRunner.AddFunc("@every 1m", sendMagicMessage) // execute every minute [for test] + //_, err := cronRunner.AddFunc("0 0 18 * * *", sendMagicMessage) // execute every 18:00 + _, err := cronRunner.AddFunc("@every 2m", sendMagicMessage) // execute every minute [for test] if err != nil { logging.Logger.WithFields(logrus.Fields{ @@ -196,10 +197,19 @@ func (c MessageServiceImpl) Chat(ctx context.Context, request *chat.ChatRequest) //TO DO 看怎么需要改一下 var pMessageList []models.Message - result := database.Client.WithContext(ctx). - Where("conversation_id=?", conversationId). - Order("created_at desc"). - Find(&pMessageList) + var result *gorm.DB + if request.PreMsgTime == 0 { + result = database.Client.WithContext(ctx). + Where("conversation_id=?", conversationId). + Order("created_at"). + Find(&pMessageList) + } else { + result = database.Client.WithContext(ctx). + Where("conversation_id=?", conversationId). + Where("created_at > ?", time.UnixMilli(int64(request.PreMsgTime)).Add(100*time.Millisecond)). + Order("created_at"). + Find(&pMessageList) + } if result.Error != nil { logger.WithFields(logrus.Fields{ @@ -222,7 +232,7 @@ func (c MessageServiceImpl) Chat(ctx context.Context, request *chat.ChatRequest) rMessageList = append(rMessageList, &chat.Message{ Id: pMessage.ID, Content: pMessage.Content, - CreateTime: uint64(pMessage.CreatedAt.UnixMicro()), + CreateTime: uint64(pMessage.CreatedAt.UnixMilli()), FromUserId: &pMessage.FromUserId, ToUserId: &pMessage.ToUserId, }) diff --git a/src/web/feed/handler.go b/src/web/feed/handler.go index 2c3a9f3..aef903a 100644 --- a/src/web/feed/handler.go +++ b/src/web/feed/handler.go @@ -39,10 +39,19 @@ func ListVideosByRecommendHandle(c *gin.Context) { latestTime := req.LatestTime actorId := uint32(req.ActorId) - res, err := Client.ListVideosByRecommend(c.Request.Context(), &feed.ListFeedRequest{ - LatestTime: &latestTime, - ActorId: &actorId, - }) + var res *feed.ListFeedResponse + var err error + if actorId == 0 { + res, err = Client.ListVideos(c.Request.Context(), &feed.ListFeedRequest{ + LatestTime: &latestTime, + ActorId: &actorId, + }) + } else { + res, err = Client.ListVideosByRecommend(c.Request.Context(), &feed.ListFeedRequest{ + LatestTime: &latestTime, + ActorId: &actorId, + }) + } if err != nil { logger.WithFields(logrus.Fields{ "LatestTime": latestTime, diff --git a/src/web/message/handler.go b/src/web/message/handler.go index 1b71321..3b1d509 100644 --- a/src/web/message/handler.go +++ b/src/web/message/handler.go @@ -119,7 +119,7 @@ func ListMessageHandler(c *gin.Context) { logger.WithFields(logrus.Fields{ "ActorId": req.ActorId, "user_id": req.ToUserId, - }).Infof("List comment success") + }).Infof("List message success") c.Render(http.StatusOK, utils.CustomJSON{Data: res, Context: c}) } diff --git a/src/web/middleware/authenticate.go b/src/web/middleware/authenticate.go index b44a553..fb714a0 100644 --- a/src/web/middleware/authenticate.go +++ b/src/web/middleware/authenticate.go @@ -18,7 +18,13 @@ var client auth.AuthServiceClient func TokenAuthMiddleware() gin.HandlerFunc { return func(c *gin.Context) { - if c.Request.URL.Path == "/douyin/user/login/" || c.Request.URL.Path == "/douyin/user/register/" { + if c.Request.URL.Path == "/douyin/user/login/" || + c.Request.URL.Path == "/douyin/user/register/" || + c.Request.URL.Path == "/douyin/comment/list/" || + c.Request.URL.Path == "/douyin/relation/follow/list/" || + c.Request.URL.Path == "/douyin/publish/list/" || + c.Request.URL.Path == "/douyin/favorite/list/" || + c.Request.URL.Path == "/douyin/relation/follower/list/" { c.Next() return } diff --git a/src/web/models/Comment.go b/src/web/models/Comment.go index ccdccd7..7bc9956 100644 --- a/src/web/models/Comment.go +++ b/src/web/models/Comment.go @@ -18,7 +18,7 @@ type ActionCommentRes struct { } type ListCommentReq struct { - Token string `form:"token" binding:"required"` + Token string `form:"token"` ActorId int `form:"actor_id"` VideoId int `form:"video_id" binding:"-"` } @@ -30,7 +30,7 @@ type ListCommentRes struct { } type CountCommentReq struct { - Token string `form:"token" binding:"required"` + Token string `form:"token"` ActorId int `form:"actor_id"` VideoId int `form:"video_id" binding:"-"` } diff --git a/src/web/models/Favorite.go b/src/web/models/Favorite.go index d3a8f8c..2828b20 100644 --- a/src/web/models/Favorite.go +++ b/src/web/models/Favorite.go @@ -17,8 +17,8 @@ type ActionFavoriteRes struct { } type ListFavoriteReq struct { - Token string `form:"token" binding:"required"` - ActorId int `form:"actor_id" binding:"required"` + Token string `form:"token"` + ActorId int `form:"actor_id"` UserId int `form:"user_id" binding:"required"` } diff --git a/src/web/models/Relation.go b/src/web/models/Relation.go index dcc82e3..3412d0b 100644 --- a/src/web/models/Relation.go +++ b/src/web/models/Relation.go @@ -22,7 +22,7 @@ type RelationActionRes struct { } type FollowListReq struct { - Token string `form:"token" binding:"required"` + Token string `form:"token"` ActorId int `form:"actor_id"` UserId int `form:"user_id"` } @@ -45,7 +45,7 @@ type CountFollowListRes struct { } type FollowerListReq struct { - Token string `form:"token" binding:"required"` + Token string `form:"token"` ActorId int `form:"actor_id"` UserId int `form:"user_id"` } @@ -57,7 +57,7 @@ type FollowerListRes struct { } type CountFollowerListReq struct { - Token string `form:"token" binding:"required"` + Token string `form:"token"` UserId int `form:"user_id"` } @@ -68,7 +68,7 @@ type CountFollowerListRes struct { } type FriendListReq struct { - Token string `form:"token" binding:"required"` + Token string `form:"token"` ActorId int `form:"actor_id"` UserId int `form:"user_id"` }