Skip to content

Commit

Permalink
Merge pull request #199 from Maple-pro/dev
Browse files Browse the repository at this point in the history
fix bug
  • Loading branch information
liaosunny123 authored Aug 29, 2023
2 parents ac9a7b3 + d55e840 commit 6517610
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/idl/feed.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/models/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
6 changes: 3 additions & 3 deletions src/rpc/feed/feed.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 11 additions & 11 deletions src/services/feed/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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")
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
24 changes: 17 additions & 7 deletions src/services/message/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand All @@ -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,
})
Expand Down
17 changes: 13 additions & 4 deletions src/web/feed/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/web/message/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
}
8 changes: 7 additions & 1 deletion src/web/middleware/authenticate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions src/web/models/Comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:"-"`
}
Expand All @@ -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:"-"`
}
Expand Down
4 changes: 2 additions & 2 deletions src/web/models/Favorite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down
8 changes: 4 additions & 4 deletions src/web/models/Relation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand All @@ -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"`
}
Expand All @@ -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"`
}

Expand All @@ -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"`
}
Expand Down

0 comments on commit 6517610

Please sign in to comment.