124 lines
3.1 KiB
Go
124 lines
3.1 KiB
Go
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
|
|
}
|