Initial commit

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-15 13:57:10 +08:00
co-authored by Claude Opus 5
commit 8679200f41
1897 changed files with 257900 additions and 0 deletions
+32
View File
@@ -0,0 +1,32 @@
package recommod
import "91porn-server/models/v/vidmod"
// BaseInfo 推荐用户信息
type BaseInfo struct {
UID uint64 `json:"uid"`
Name string `json:"name"`
Gender string `json:"gender"`
Portrait string `json:"portrait"`
HasLocked bool `json:"hasLocked"` //已禁止登陆
HasBanned bool `json:"hasBanned"` //已禁言
CollectionCount int `json:"collectionCount"` //作品总数
}
// UserListResp 主播推荐应答
type UserListResp struct {
List []BaseInfo `json:"list"`
HasNext bool `json:"hasNext"`
}
// VideoListResp 视频推荐应答
type VideoListResp struct {
//视频信息
VInfos []*vidmod.VideoInfo `json:"vInfos"`
//总页数
TotalPages int `json:"totalPages"`
//环形队列非空时始终可继续读取
HasNext bool `json:"hasNext"`
//当前全局推荐队列版本,仅用于排查
QueueVersion string `json:"queueVersion,omitempty"`
}
+10
View File
@@ -0,0 +1,10 @@
package recommod
import "91porn-server/models/v/vidmod"
// SignM3u8 推荐视频列表(/api/app/recommend/vid/list),供 m3u8ticket 零反射签票。
func (r VideoListResp) SignM3u8(s vidmod.M3u8Signer) {
vidmod.SignM3u8Infos(s, r.VInfos)
}
var _ vidmod.M3u8Signable = VideoListResp{}
+201
View File
@@ -0,0 +1,201 @@
package recommod
import (
"fmt"
"time"
"91porn-server/common/db"
"91porn-server/common/log"
"91porn-server/models"
"91porn-server/models/v/vidmod"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var mdb, rdb *db.MongoDB
const table = models.UserRecoRecord
func coll(t *db.MongoTool) *db.MongoTool {
if t == nil {
return mdb.Coll(table)
}
return t.Coll(table)
}
func collRead() *db.MongoTool {
return rdb.Coll(table)
}
// initIndex 设置index
func initIndex() {
many := []mongo.IndexModel{ //batch set indexes //value is the type 1 or -1
{
Keys: bson.D{{Key: "uid", Value: 1}, {Key: "type", Value: 1}, {Key: "mark", Value: 1}},
Options: options.Index().SetUnique(true),
},
}
if _, err := coll(nil).CreateIndex(many); err != nil {
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
}
}
func insertRecoRecord(uid uint64, newsType string, typ string, mark string, line time.Time) error {
now := time.Now()
record := RecoRecord{
UID: uid,
Type: typ,
Mark: mark,
TimeLine: line,
UpdatedAt: now,
CreatedAt: now,
}
if vidmod.IsSP(newsType) {
record.TimeLine = line
}
if vidmod.IsCover(newsType) {
record.CoverTimeLine = line
}
if _, err := coll(nil).InsertOne(&record); err != nil {
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "insertRecoRecord", table, "InsertOne", err),
log.Any("uid", uid),
log.Any("typ", typ),
log.Any("mark", mark),
log.Any("line", line),
log.Any("newsType", newsType),
)
return err
}
return nil
}
func updateRecoRecord(uid uint64, newsType string, typ string, mark string, line time.Time) (int64, error) {
now := time.Now()
cond := bson.M{"uid": uid, "type": typ, "mark": mark}
update := bson.M{}
if vidmod.IsSP(newsType) {
update = bson.M{"$set": bson.M{"timeLine": line, "updatedAt": now}}
}
if vidmod.IsCover(newsType) {
update = bson.M{"$set": bson.M{"coverTimeLine": line, "updatedAt": now}}
}
result, err := coll(nil).UpdateOne(cond, update)
if err != nil {
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "updateRecoRecord", table, "UpdateOne", err),
log.Any("uid", uid),
log.Any("typ", typ),
log.Any("mark", mark),
log.Any("line", line),
log.Any("newsType", newsType),
)
}
if result == nil {
return 0, err
}
return result.MatchedCount, err
}
func findRecoRecord(uid uint64, typs []string) ([]RecoRecord, error) {
var record []RecoRecord
cond := bson.M{"uid": uid, "type": bson.M{"$in": typs}}
if err := coll(nil).Find(&record, cond); err != nil {
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "findRecoRecord", table, "Find", err),
log.Any("uid", uid),
log.Any("typs", typs),
)
return record, err
}
return record, nil
}
func findRecoRecordWithMark(uid uint64, typ string, marks []string) ([]RecoRecord, error) {
var record []RecoRecord
cond := bson.M{"uid": uid, "type": typ, "mark": bson.M{"$in": marks}}
if err := collRead().Find(&record, cond); err != nil {
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "findRecoRecordWithMark", table, "Find", err),
log.Any("uid", uid),
log.Any("typ", typ),
log.Any("marks", marks),
)
return record, err
}
return record, nil
}
// SetRecoTimeLines 刷新维度的推荐时间轴
func SetRecoTimeLines(uid uint64, newsType string, set map[string]time.Time) error {
for typ, line := range set {
if line.IsZero() {
continue
}
matchCnt, err := updateRecoRecord(uid, newsType, typ, "", line)
if err != nil {
return err
}
if matchCnt == 0 {
if err = insertRecoRecord(uid, newsType, typ, "", line); err != nil {
return err
}
}
}
return nil
}
// SetRecoTimeLinesWithMark 刷新维度的推荐时间轴
func SetRecoTimeLinesWithMark(uid uint64, newsType string, typ string, set map[string]time.Time) error {
for mark, line := range set {
if line.IsZero() {
continue
}
matchCnt, err := updateRecoRecord(uid, newsType, typ, mark, line)
if err != nil {
return err
}
if matchCnt == 0 {
if err = insertRecoRecord(uid, newsType, typ, mark, line); err != nil {
return err
}
}
}
return nil
}
// GetRecoTimeLines 获取维度的推荐时间轴
func GetRecoTimeLines(uid uint64, newsType string, typs []string) (map[string]time.Time, error) {
m := make(map[string]time.Time)
records, err := findRecoRecord(uid, typs)
for _, r := range records {
if vidmod.IsSP(newsType) {
m[r.Type] = ReviseTimeline(r.TimeLine)
}
if vidmod.IsCover(newsType) {
m[r.Type] = ReviseTimeline(r.CoverTimeLine)
}
}
return m, err
}
// GetRecoTimeLinesWithMark 获取维度的推荐时间轴
func GetRecoTimeLinesWithMark(uid uint64, newsType, typ string, marks []string) (map[string]time.Time, error) {
m := make(map[string]time.Time)
records, err := findRecoRecordWithMark(uid, typ, marks)
for _, r := range records {
if vidmod.IsSP(newsType) {
m[r.Mark] = ReviseTimeline(r.TimeLine)
}
if vidmod.IsCover(newsType) {
m[r.Mark] = ReviseTimeline(r.CoverTimeLine)
}
}
return m, err
}
func ReviseTimeline(line time.Time) time.Time {
tenMonthsBefore := time.Now().AddDate(0, -10, 0)
if line.Before(tenMonthsBefore) || line.After(time.Now()) {
return tenMonthsBefore
}
return line
}
+24
View File
@@ -0,0 +1,24 @@
package recommod
import (
"time"
"91porn-server/common/db"
)
// RecoRecord 推荐记录
type RecoRecord struct {
UID uint64 `json:"uid" bson:"uid"`
Type string `json:"type" bson:"type"` //记录推荐维度, chosenVideo,tagVideo,sameCityVideo,newVideo,unPopularVideo,pushVideo
Mark string `json:"mark" bson:"mark"` //标示字段,维度下的区分字段
TimeLine time.Time `json:"timeLine" bson:"timeLine"` //推荐进度
CoverTimeLine time.Time `json:"coverTimeLine" bson:"coverTimeLine"`
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"`
CreatedAt time.Time `json:"createdAt" bson:"createdAt"`
}
func Init() {
mdb = db.Init(table)
rdb = db.InitRead(table)
initIndex()
}