차단 목록 관리 추가하고 connection 분리
This commit is contained in:
124
core/blocklist.go
Normal file
124
core/blocklist.go
Normal file
@ -0,0 +1,124 @@
|
||||
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
|
||||
}
|
||||
|
||||
_, 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 != block.Id {
|
||||
// 이미 있다고 봐야지
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
block.Id = newid.(primitive.ObjectID)
|
||||
bl.wsh.SendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: block.To.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{
|
||||
Alias: ctx.CallBy.Alias,
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user