145 lines
3.7 KiB
Go
145 lines
3.7 KiB
Go
package core
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"time"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
"repositories.action2quare.com/ayo/gocommon"
|
|
"repositories.action2quare.com/ayo/gocommon/logger"
|
|
"repositories.action2quare.com/ayo/gocommon/wshandler"
|
|
)
|
|
|
|
type blockDoc struct {
|
|
Id primitive.ObjectID `bson:"_id,omitempty" json:"_id"`
|
|
From primitive.ObjectID `bson:"from,omitempty" json:"-"`
|
|
To primitive.ObjectID `bson:"to,omitempty" json:"-"`
|
|
ToAlias string `bson:"talias" json:"to"`
|
|
Timestamp int64 `bson:"ts" json:"ts"`
|
|
Deleted bool `bson:"deleted,omitempty" json:"deleted,omitempty"`
|
|
}
|
|
|
|
type blocklist struct {
|
|
mongoClient gocommon.MongoClient
|
|
redison *gocommon.RedisonHandler
|
|
wsh *wshandler.WebsocketHandler
|
|
conns *connections
|
|
}
|
|
|
|
func makeBlocklist(ctx context.Context, so *Social, conns *connections) (*blocklist, error) {
|
|
if err := so.mongoClient.MakeUniqueIndices(block_collection_name, map[string]bson.D{
|
|
"fromto": {{Key: "from", Value: 1}, {Key: "to", Value: 1}},
|
|
}); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &blocklist{
|
|
mongoClient: so.mongoClient,
|
|
redison: so.redison,
|
|
wsh: so.wsh,
|
|
conns: conns,
|
|
}, nil
|
|
}
|
|
|
|
func (bl *blocklist) Block(w http.ResponseWriter, r *http.Request) {
|
|
// 차단목록에 추가
|
|
var block blockDoc
|
|
if err := gocommon.MakeDecoder(r).Decode(&block); err != nil {
|
|
logger.Println("Block failed:", err)
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
block.Timestamp = time.Now().UTC().Unix()
|
|
_, newid, err := bl.mongoClient.Update(block_collection_name, bson.M{
|
|
"_id": combineObjectID(block.From, block.To),
|
|
}, bson.M{
|
|
"$set": block,
|
|
}, options.Update().SetUpsert(true))
|
|
|
|
if err != nil || newid == nil {
|
|
// 이미 있다고 봐야지
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
block.Id = newid.(primitive.ObjectID)
|
|
bl.wsh.SendUpstreamMessage(&wshandler.UpstreamMessage{
|
|
Target: block.From.Hex(),
|
|
Body: []blockDoc{block},
|
|
Tag: blocks_tag,
|
|
})
|
|
}
|
|
|
|
func (bl *blocklist) Unblock(ctx wshandler.ApiCallContext) {
|
|
id, _ := primitive.ObjectIDFromHex(ctx.Arguments[0].(string))
|
|
now := time.Now().UTC().Unix()
|
|
updated, _, err := bl.mongoClient.Update(block_collection_name, bson.M{
|
|
"_id": id,
|
|
}, bson.M{
|
|
"$set": bson.M{
|
|
"deleted": true,
|
|
"ts": now,
|
|
},
|
|
}, options.Update().SetUpsert(false))
|
|
|
|
if err != nil {
|
|
logger.Println("Unblock failed :", err)
|
|
return
|
|
}
|
|
|
|
if updated {
|
|
bl.conns.writeMessage(ctx.CallBy.Accid, &wshandler.DownstreamMessage{
|
|
Body: []blockDoc{{Id: id, Deleted: true, Timestamp: now}},
|
|
Tag: blocks_tag,
|
|
})
|
|
}
|
|
}
|
|
|
|
func (bl *blocklist) QueryBlock(w http.ResponseWriter, r *http.Request) {
|
|
var block blockDoc
|
|
if err := gocommon.MakeDecoder(r).Decode(&block); err != nil {
|
|
logger.Println("Block failed:", err)
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
exists, err := bl.mongoClient.Exists(block_collection_name, bson.M{
|
|
"from": block.From,
|
|
"to": block.To,
|
|
})
|
|
|
|
if err != nil {
|
|
logger.Println("QueryBlock failed :", err)
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
if !exists {
|
|
w.WriteHeader(http.StatusGone)
|
|
}
|
|
}
|
|
|
|
func (bl *blocklist) QueryBlocks(ctx wshandler.ApiCallContext) {
|
|
queryfrom := int64(ctx.Arguments[0].(float64))
|
|
|
|
var myblocks []blockDoc
|
|
err := bl.mongoClient.FindAllAs(block_collection_name, bson.M{
|
|
"from": ctx.CallBy.Accid,
|
|
"ts": bson.M{"$gt": queryfrom},
|
|
}, &myblocks)
|
|
if err != nil {
|
|
logger.Println("QueryBlocks failed. FindAllAs err :", err)
|
|
}
|
|
|
|
if len(myblocks) > 0 {
|
|
bl.conns.writeMessage(ctx.CallBy.Accid, &wshandler.DownstreamMessage{
|
|
Body: myblocks,
|
|
Tag: blocks_tag,
|
|
})
|
|
}
|
|
}
|