Skip to content

Commit

Permalink
[feature] add pic feedback api
Browse files Browse the repository at this point in the history
  • Loading branch information
kyrie committed Jan 28, 2023
1 parent 981b81e commit 840209b
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 1 deletion.
4 changes: 3 additions & 1 deletion database/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,6 @@ create table bilibili_dynamics (
index idx_sent_at(sent_at) comment '发送时间索引'
)Engine=InnoDB comment '动态' charset 'utf8mb4';

alter table bilibili_dynamics add column topic_details json default null comment '动态的#xxx#' after pictures;
alter table bilibili_dynamics add column topic_details json default null comment '动态的#xxx#' after pictures;
alter table bilibili_dynamics add column feedback int default 0 comment '反馈类型' after dynamic_id;
alter table bilibili_dynamics add column verify boolean default false comment '是否审核过' after feedback;
26 changes: 26 additions & 0 deletions internal/app/api/handler/bilbil_picture.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handler

import (
"net/http"
"strconv"

"git.vtb.link/eoefans/internal/app/api/apperrors"
"git.vtb.link/eoefans/internal/app/api/help"
Expand Down Expand Up @@ -52,3 +53,28 @@ func BilibiliRandomPic(s *service.BilbilPicture) func(ctx *gin.Context) {
}
}
}

func BilibiliPicFeedback(s *service.BilbilPicture) func(ctx *gin.Context) {
return func(ctx *gin.Context) {
var req idl.BilibiliPictureFeedbackReq
if err := ctx.ShouldBindJSON(&req); err != nil {
_ = ctx.Error(apperrors.NewValidationError(400, err.Error()).Wrap(err))
return
}
if _, ok := idl.DynamicFeedbackMap[req.FeedBack]; !ok {
_ = ctx.Error(apperrors.NewValidationError(400, "invalid feedback value"))
return
}
dynamicID, err := strconv.ParseUint(req.DynamicIDStr, 10, 64)
if err != nil {
_ = ctx.Error(apperrors.NewValidationError(400, "invalid dynamic_id"))
return
}
if err := s.FeedBack(ctx, req.FeedBack, dynamicID); err != nil {
_ = ctx.Error(err)
return
} else {
ctx.JSON(http.StatusOK, help.SuccessJson(req))
}
}
}
25 changes: 25 additions & 0 deletions internal/app/api/idl/bilibili_picture.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,31 @@ import (
"time"
)

type DynamicFeedback int

const (
Default DynamicFeedback = 0 //无反馈
Unrelated DynamicFeedback = 1 //不相关的图
IncorrectTags DynamicFeedback = 2 //不正确的标签
OnPeriod DynamicFeedback = 3 //不适宜在工作期间观看
Uncomfortable DynamicFeedback = 4 //不适宜的内容
)

var DynamicFeedbackMap = map[DynamicFeedback]struct{}{
Default: {},
Unrelated: {},
IncorrectTags: {},
OnPeriod: {},
Uncomfortable: {},
}

// 图片来源于动态,以动态为单位
type BilibiliDynamic struct {
ID uint64 `gorm:"primarykey"`
UID uint64 `gorm:"column:uid"`
DynamicID uint64 `gorm:"column:dynamic_id"`
Feedback DynamicFeedback `gorm:"column:feedback"`
Verify bool `gorm:"column:verify"`
Pictures BilibiliDynamicPictures `gorm:"column:pictures"`
TopicDetails *BilibiliDynamicTopicDetails `gorm:"topic_details"`
TopicName string `gorm:"column:topic_name"`
Expand Down Expand Up @@ -69,6 +89,10 @@ type BilibiliPictureLatestReq struct {
TopicID int `form:"topic_id"`
}

type BilibiliPictureFeedbackReq struct {
DynamicIDStr string `json:"dynamic_id_str"`
FeedBack DynamicFeedback `json:"feedback"`
}
type BilibiliPictureRecommendReq struct {
Page int `form:"page,default=1" binding:"omitempty,gt=0"`
TopicID int `form:"topic_id"`
Expand Down Expand Up @@ -137,4 +161,5 @@ type BilibiliPictureRepository interface {
Latest(page, size, topicID int) (list []*BilibiliDynamic, err error)
Recommend(from, to time.Time, page, size, topicID int) (list []*BilibiliDynamic, err error)
Random(rand float64) (list []*BilibiliDynamic, err error)
FeedBack(feedback DynamicFeedback, dynamicID uint64) error
}
1 change: 1 addition & 0 deletions internal/app/api/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func InitRouters(
picApi.GET("/latest", handler.BilibiliLatestPics(picService))
picApi.GET("/recommend", handler.BilibiliRecommendPics(picService))
picApi.GET("/random", handler.BilibiliRandomPic(picService))
picApi.POST("/feedback", handler.BilibiliPicFeedback(picService))
}
// Auth相关
authApi := r.Group("/v1/auth")
Expand Down
6 changes: 6 additions & 0 deletions internal/app/api/service/bilbil_picture.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,9 @@ func (service *BilbilPicture) Random(ctx context.Context) (*idl.BilibiliPictureR
resp.DynamicURL = fmt.Sprintf(DynamicURL, pictures[idx].DynamicID)
return resp, nil
}

func (service *BilbilPicture) FeedBack(ctx context.Context, feedback idl.DynamicFeedback, dynamicID uint64) error {
tx := service.db.WithContext(ctx)
picRepository := repository.NewBilibiliPicture(tx)
return picRepository.FeedBack(feedback, dynamicID)
}
16 changes: 16 additions & 0 deletions internal/repository/bilibili_picture.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,19 @@ func (impl *BilibiliPictureMysqlImpl) Random(rand float64) (list []*idl.Bilibili
}
return list, nil
}

func (impl *BilibiliPictureMysqlImpl) FeedBack(feedback idl.DynamicFeedback, dynamicID uint64) error {
var dynamic idl.BilibiliDynamic
err := impl.tx.Table(idl.BilibiliDynamic{}.TableName()).
Where("dynamic_id = ?", dynamicID).
Select("verify").First(&dynamic).Error
if err != nil {
return err
}
if dynamic.Verify {
return nil
}
return impl.tx.Table(idl.BilibiliDynamic{}.TableName()).
Where("dynamic_id = ?", dynamicID).
UpdateColumn("feedback", feedback).Error
}

0 comments on commit 840209b

Please sign in to comment.