@@ -0,0 +1,53 @@
|
||||
package reptmod
|
||||
|
||||
import (
|
||||
"91porn-server/common/pageopt"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
type Matcher = pageopt.Matcher
|
||||
|
||||
// InObjTypeMatch
|
||||
type InObjTypeMatch struct {
|
||||
Typs []ReportObjType
|
||||
}
|
||||
|
||||
func (i *InObjTypeMatch) New() Matcher {
|
||||
return pageopt.NewInMatch("objType", i.Typs)
|
||||
}
|
||||
|
||||
// TypesMatch
|
||||
type TypesMatch struct {
|
||||
Types *string
|
||||
}
|
||||
|
||||
func (t *TypesMatch) New() Matcher {
|
||||
return pageopt.NewAssignMatch("types", t.Types)
|
||||
}
|
||||
|
||||
// CreatedAtGTEAndLTMatch
|
||||
type CreatedAtGTEAndLTMatch = pageopt.CreatedAtGTEAndLTMatch
|
||||
|
||||
func List(sort bson.D, skip, limit int64, matchers ...pageopt.Matcher) ([]Report, error) {
|
||||
filter := pageopt.MergeM(matchers)
|
||||
opt := (&options.FindOptions{}).
|
||||
SetSort(sort).
|
||||
SetSkip(skip).
|
||||
SetLimit(limit)
|
||||
reportList := make([]Report, 0, limit)
|
||||
if err := coll(nil).Find(&reportList, filter, opt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return reportList, nil
|
||||
}
|
||||
|
||||
func Count(matchers ...pageopt.Matcher) (int64, error) {
|
||||
filter := pageopt.MergeM(matchers)
|
||||
count, err := coll(nil).Count(filter)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
package reptmod
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"91porn-server/common"
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/common/log"
|
||||
"91porn-server/models"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
var mdb *db.MongoDB
|
||||
|
||||
const table = models.Report
|
||||
|
||||
func coll(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(table)
|
||||
}
|
||||
return t.Coll(table)
|
||||
}
|
||||
|
||||
func initIndex() {
|
||||
many := []mongo.IndexModel{ //batch set indexes //value is the type 1 or -1
|
||||
{
|
||||
Keys: bson.D{{Key: "uid", Value: 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "objType", Value: 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "objID", Value: 1}},
|
||||
Options: options.Index().SetSparse(true),
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "objUID", Value: -1}},
|
||||
Options: options.Index().SetSparse(true),
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "createdAt", Value: -1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "status", Value: 1}},
|
||||
Options: options.Index().SetSparse(true),
|
||||
},
|
||||
}
|
||||
if _, err := coll(nil).CreateIndex(many); err != nil {
|
||||
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
||||
}
|
||||
}
|
||||
|
||||
// setOnInsert SetOnInsert 未匹配到文档则插入, 否则不做任何操作
|
||||
// @return true:已经举报过 false:未举报过
|
||||
func setOnInsert(update ReportDoc, repeatTime time.Duration) (bool, error) {
|
||||
filterDoc := ReportDoc{
|
||||
UID: update.UID,
|
||||
ObjType: update.ObjType,
|
||||
ObjID: update.ObjID,
|
||||
ObjUID: update.ObjUID,
|
||||
}
|
||||
filter, err := common.ToBsonM(filterDoc)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("coll:%s SetOnInsert filter ToBsonM fail, error:%+v ", table, err)
|
||||
}
|
||||
now := time.Now()
|
||||
filter["createdAt"] = bson.M{
|
||||
"$gte": now.Add(-1 * repeatTime),
|
||||
}
|
||||
update.CreatedAt = &now
|
||||
updateM, err := common.ToBsonM(update)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("coll:%s SetOnInsert update ToBsonM fail, error:%+v ", table, err)
|
||||
}
|
||||
result, err := coll(nil).UpsertOne(filter, bson.M{
|
||||
"$setOnInsert": updateM,
|
||||
})
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "findRecoRecordWithMark", table, "Find", err),
|
||||
log.Any("update", update),
|
||||
log.Any("repeatTime", repeatTime),
|
||||
)
|
||||
return false, err
|
||||
}
|
||||
return result.MatchedCount != 0, err
|
||||
}
|
||||
|
||||
// 举报
|
||||
func Do(uid uint64, objType ReportObjType, objID *primitive.ObjectID, objUID *uint64, types string, repeatTime time.Duration) (bool, error) {
|
||||
doc := ReportDoc{
|
||||
UID: &uid,
|
||||
ObjType: &objType,
|
||||
Types: &types,
|
||||
}
|
||||
if objType == User {
|
||||
doc.ObjUID = objUID //缺省字段
|
||||
} else {
|
||||
doc.ObjID = objID
|
||||
}
|
||||
return setOnInsert(doc, repeatTime)
|
||||
}
|
||||
|
||||
// 举报已处理
|
||||
func SetStatus(id primitive.ObjectID, status bool) error {
|
||||
filter := bson.M{"_id": id}
|
||||
update := bson.M{"$set": bson.M{"status": status}}
|
||||
if !status {
|
||||
update = bson.M{"$unset": bson.M{"status": 1}}
|
||||
}
|
||||
if _, err := coll(nil).UpdateOne(filter, update); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "findRecoRecordWithMark", table, "Find", err),
|
||||
log.Any("id", id),
|
||||
log.Any("status", status),
|
||||
)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package reptmod
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"91porn-server/common/db"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
type ReportObjType string
|
||||
|
||||
const (
|
||||
Video ReportObjType = "video"
|
||||
Comment ReportObjType = "comment"
|
||||
User ReportObjType = "user"
|
||||
)
|
||||
|
||||
// Report 举报
|
||||
type Report struct {
|
||||
ID primitive.ObjectID `bson:"_id,omitempty"`
|
||||
UID uint64 `bson:"uid"` // 举报者uid
|
||||
ObjType ReportObjType `bson:"objType"` // 举报对象类型: video、comment、user
|
||||
ObjID *primitive.ObjectID `bson:"objID,omitempty"` // 举报对象类型非user,缺省参数
|
||||
ObjUID *uint64 `bson:"objUID,omitempty"` // 举报对象类型是user,缺省参数
|
||||
Types string `bson:"types"` // 类型:内容违规/账号违规/侵权/其他
|
||||
Status bool `bson:"status"` // 举报已经处理
|
||||
CreatedAt time.Time `bson:"createdAt"` // 创建时间
|
||||
}
|
||||
|
||||
type ReportDoc struct {
|
||||
ID *primitive.ObjectID `bson:"_id,omitempty"`
|
||||
UID *uint64 `bson:"uid,omitempty"` // 举报者uid
|
||||
ObjType *ReportObjType `bson:"objType,omitempty"` // 举报对象类型 video、comment、user
|
||||
ObjID *primitive.ObjectID `bson:"objID,omitempty"` // 举报对象类型非user,选用
|
||||
ObjUID *uint64 `bson:"objUID,omitempty"` // 举报对象类型是user,选用
|
||||
Types *string `bson:"types,omitempty"` // 类型:内容违规/账号违规/侵权/其他
|
||||
IsDone *bool `bson:"done,omitempty"` // 举报已经处理
|
||||
CreatedAt *time.Time `bson:"createdAt,omitempty"` // 创建时间
|
||||
}
|
||||
|
||||
func Init() {
|
||||
mdb = db.Init(table)
|
||||
initIndex()
|
||||
}
|
||||
Reference in New Issue
Block a user