차단 목록 관리 추가하고 connection 분리
This commit is contained in:
@ -14,14 +14,6 @@ import (
|
||||
"repositories.action2quare.com/ayo/gocommon/wshandler"
|
||||
)
|
||||
|
||||
const (
|
||||
invitation_collection_name = gocommon.CollectionName("invitation")
|
||||
)
|
||||
|
||||
var invitation_sent_tag = []string{"social.InvitationsSent"}
|
||||
var invitation_received_tag = []string{"social.InvitationsReceived"}
|
||||
var friends_tag = []string{"social.Friends"}
|
||||
|
||||
type invitation struct {
|
||||
mongoClient gocommon.MongoClient
|
||||
redison *gocommon.RedisonHandler
|
||||
@ -37,6 +29,7 @@ type invitationDoc struct {
|
||||
ToAlias string `bson:"talias,omitempty" json:"to"`
|
||||
Timestamp int64 `bson:"ts" json:"ts"`
|
||||
Deleted bool `bson:"deleted,omitempty" json:"deleted,omitempty"`
|
||||
Blocked bool `bson:"blocked,omitempty" json:"-"` // From은 To에 의해 차단된 상태를 표시
|
||||
}
|
||||
|
||||
func init() {
|
||||
@ -45,14 +38,14 @@ func init() {
|
||||
|
||||
func makeInvitation(ctx context.Context, s *Social, f *friends) (*invitation, error) {
|
||||
if err := s.mongoClient.MakeUniqueIndices(invitation_collection_name, map[string]bson.D{
|
||||
"fromto": {{Key: "from", Value: 1}, {Key: "to", Value: 1}},
|
||||
"fromts": {{Key: "from", Value: 1}, {Key: "ts", Value: -1}},
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 내가 받은거
|
||||
if err := s.mongoClient.MakeIndices(invitation_collection_name, map[string]bson.D{
|
||||
"received": {{Key: "to", Value: 1}, {Key: "ts", Value: -1}},
|
||||
"tots": {{Key: "to", Value: 1}, {Key: "ts", Value: -1}, {Key: "blocked", Value: 1}},
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -65,49 +58,34 @@ func makeInvitation(ctx context.Context, s *Social, f *friends) (*invitation, er
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (iv *invitation) QueryReceivedInvitations(ctx wshandler.ApiCallContext) {
|
||||
func (iv *invitation) QueryInvitations(ctx wshandler.ApiCallContext) {
|
||||
// 내가 받은 초대 목록
|
||||
queryfrom := int64(ctx.Arguments[0].(float64))
|
||||
|
||||
var receives []*invitationDoc
|
||||
|
||||
err := iv.mongoClient.FindAllAs(invitation_collection_name, bson.M{
|
||||
"to": ctx.CallBy.Accid,
|
||||
"ts": bson.M{"$gt": queryfrom},
|
||||
}, &receives)
|
||||
if err != nil {
|
||||
logger.Println("QueryReceivedInvitations failed. FindAllAs err :", err)
|
||||
if err := iv.mongoClient.FindAllAs(invitation_collection_name, bson.M{
|
||||
"to": ctx.CallBy.Accid,
|
||||
"ts": bson.M{"$gt": queryfrom},
|
||||
"blocked": false,
|
||||
}, &receives, options.Find().SetHint("tots")); err != nil {
|
||||
logger.Println("QueryInvitations failed. FindAllAs err :", err)
|
||||
}
|
||||
|
||||
if len(receives) > 0 {
|
||||
var sents []*invitationDoc
|
||||
if err := iv.mongoClient.FindAllAs(invitation_collection_name, bson.M{
|
||||
"from": ctx.CallBy.Accid,
|
||||
"ts": bson.M{"$gt": queryfrom},
|
||||
}, &sents, options.Find().SetHint("fromts")); err != nil {
|
||||
logger.Println("QueryInvitations failed. FindAllAs err :", err)
|
||||
}
|
||||
|
||||
invitations := append(receives, sents...)
|
||||
if len(invitations) > 0 {
|
||||
iv.wsh.SendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: ctx.CallBy.Accid.Hex(),
|
||||
Body: receives,
|
||||
Tag: invitation_received_tag,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (iv *invitation) QuerySentInvitations(ctx wshandler.ApiCallContext) {
|
||||
// 내가 보낸 초대 목록
|
||||
queryfrom := int64(ctx.Arguments[0].(float64))
|
||||
|
||||
var receives []*invitationDoc
|
||||
|
||||
err := iv.mongoClient.FindAllAs(invitation_collection_name, bson.M{
|
||||
"from": ctx.CallBy.Accid,
|
||||
"ts": bson.M{"$gt": queryfrom},
|
||||
"falias": bson.M{"$exists": true},
|
||||
}, &receives)
|
||||
if err != nil {
|
||||
logger.Println("QueryReceivedInvitations failed. FindAllAs err :", err)
|
||||
}
|
||||
|
||||
if len(receives) > 0 {
|
||||
iv.wsh.SendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: ctx.CallBy.Accid.Hex(),
|
||||
Body: receives,
|
||||
Tag: invitation_sent_tag,
|
||||
Body: invitations,
|
||||
Tag: invitations_tag,
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -145,13 +123,13 @@ func (iv *invitation) CancelInvitation(ctx wshandler.ApiCallContext) {
|
||||
iv.wsh.SendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: ivdoc.To.Hex(),
|
||||
Body: []invitationDoc{ivdoc},
|
||||
Tag: invitation_received_tag,
|
||||
Tag: invitations_tag,
|
||||
})
|
||||
|
||||
iv.wsh.SendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: ivdoc.From.Hex(),
|
||||
Body: []invitationDoc{ivdoc},
|
||||
Tag: invitation_sent_tag,
|
||||
Tag: invitations_tag,
|
||||
})
|
||||
}
|
||||
|
||||
@ -263,13 +241,13 @@ func (iv *invitation) DenyInvitation(ctx wshandler.ApiCallContext) {
|
||||
iv.wsh.SendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: ivdoc.To.Hex(),
|
||||
Body: []invitationDoc{ivdoc},
|
||||
Tag: invitation_received_tag,
|
||||
Tag: invitations_tag,
|
||||
})
|
||||
|
||||
iv.wsh.SendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: ivdoc.From.Hex(),
|
||||
Body: []invitationDoc{ivdoc},
|
||||
Tag: invitation_sent_tag,
|
||||
Tag: invitations_tag,
|
||||
})
|
||||
}
|
||||
|
||||
@ -308,76 +286,109 @@ func (iv *invitation) InviteAsFriend(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
ivdoc.Timestamp = time.Now().UTC().Unix()
|
||||
_, newid, err := iv.mongoClient.Update(invitation_collection_name, bson.M{
|
||||
"from": ivdoc.From,
|
||||
"to": ivdoc.To,
|
||||
}, bson.M{
|
||||
"$set": bson.M{
|
||||
"ts": ivdoc.Timestamp,
|
||||
"falias": ivdoc.FromAlias,
|
||||
"talias": ivdoc.ToAlias,
|
||||
},
|
||||
}, options.Update().SetUpsert(true))
|
||||
// ivdoc.To가 invdoc.From을 차단했으면 표시
|
||||
exists, err := iv.mongoClient.Exists(block_collection_name, bson.M{"_id": combineObjectID(ivdoc.To, ivdoc.From)})
|
||||
if err != nil {
|
||||
logger.Println("IniviteAsFriend failed:", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if newid != nil {
|
||||
ivdoc.Id = newid.(primitive.ObjectID)
|
||||
// exists면 차단된 상태
|
||||
ivdoc.Blocked = exists
|
||||
ivdoc.Timestamp = time.Now().UTC().Unix()
|
||||
_, newid, err := iv.mongoClient.Update(invitation_collection_name, bson.M{
|
||||
"_id": combineObjectID(ivdoc.From, ivdoc.To),
|
||||
}, bson.M{"$setOnInsert": ivdoc}, options.Update().SetUpsert(true))
|
||||
if err != nil || newid == nil {
|
||||
logger.Println("IniviteAsFriend failed:", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
ivdoc.Id = newid.(primitive.ObjectID)
|
||||
if !ivdoc.Blocked {
|
||||
iv.wsh.SendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: ivdoc.To.Hex(),
|
||||
Body: []invitationDoc{ivdoc},
|
||||
Tag: invitation_received_tag,
|
||||
})
|
||||
} else {
|
||||
found, _ := iv.mongoClient.FindOne(invitation_collection_name, bson.M{
|
||||
"from": ivdoc.From,
|
||||
"to": ivdoc.To,
|
||||
}, options.FindOne().SetProjection(bson.M{"_id": 1}))
|
||||
|
||||
ivdoc.Id = found["_id"].(primitive.ObjectID)
|
||||
}
|
||||
|
||||
if !ivdoc.Id.IsZero() {
|
||||
iv.wsh.SendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: ivdoc.From.Hex(),
|
||||
Body: []invitationDoc{ivdoc},
|
||||
Tag: invitation_sent_tag,
|
||||
Tag: invitations_tag,
|
||||
})
|
||||
}
|
||||
|
||||
iv.wsh.SendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: ivdoc.From.Hex(),
|
||||
Body: []invitationDoc{ivdoc},
|
||||
Tag: invitations_tag,
|
||||
})
|
||||
}
|
||||
|
||||
func (iv *invitation) Block(w http.ResponseWriter, r *http.Request) {
|
||||
// 초대가 있으면
|
||||
// var bi struct {
|
||||
// From primitive.ObjectID
|
||||
// To primitive.ObjectID
|
||||
// FromAlias string
|
||||
// }
|
||||
// if err := gocommon.MakeDecoder(r).Decode(&bi); err != nil {
|
||||
// logger.Println("invitation.Block failed :", err)
|
||||
// w.WriteHeader(http.StatusBadRequest)
|
||||
// return
|
||||
// }
|
||||
var block blockDoc
|
||||
if err := gocommon.MakeDecoder(r).Decode(&block); err != nil {
|
||||
logger.Println("Block failed:", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// now := time.Now().UTC().Unix()
|
||||
// // From이 To를 block했으므로 To가 From을 초대하는 것을 방지하려면 둘을 뒤집어서 문서를 만들어 놔야 함
|
||||
// // 이미 존재하는 초대일 수도 있다.
|
||||
// _, _, err := iv.mongoClient.Update(invitation_collection_name, bson.M{
|
||||
// "from": bi.To,
|
||||
// "to": bi.From,
|
||||
// }, bson.M{
|
||||
// "$set": invitationDoc{
|
||||
// ToAlias: bi.FromAlias,
|
||||
// Timestamp: now,
|
||||
// },
|
||||
// }, options.Update().SetUpsert(true))
|
||||
// if err != nil {
|
||||
// logger.Println("Block failed:", err)
|
||||
// w.WriteHeader(http.StatusInternalServerError)
|
||||
// return
|
||||
// }
|
||||
// 차단한 상대가 나한테 보낸 초대가 있으면 차단 표시
|
||||
// block.From이 block.To를 차단 -> block.To가 block.From을 초대한게 있나?
|
||||
id := combineObjectID(block.To, block.From)
|
||||
now := time.Now().UTC().Unix()
|
||||
updated, _, err := iv.mongoClient.Update(invitation_collection_name, bson.M{
|
||||
"_id": id,
|
||||
}, bson.M{
|
||||
"$set": bson.M{
|
||||
"blocked": true,
|
||||
"ts": now,
|
||||
},
|
||||
}, options.Update().SetUpsert(false))
|
||||
if err != nil {
|
||||
logger.Println("Block failed:", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if updated {
|
||||
// 초대가 있었다.
|
||||
// 사실은 삭제가 아니지만 초대 삭제 알림. 나중에 쿼리해도 안나옴
|
||||
iv.wsh.SendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: block.From.Hex(),
|
||||
Body: []invitationDoc{{Id: id, Deleted: true, Timestamp: now}},
|
||||
Tag: invitations_tag,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (iv *invitation) Unblock(ctx wshandler.ApiCallContext) {
|
||||
// From이 To를 unblock = to가 from을 block했었다. = from이 to한테 보낸 초대가 있을 수 있다.
|
||||
// invitation key는 to+from이고 block key는 from+to
|
||||
id, _ := primitive.ObjectIDFromHex(ctx.Arguments[0].(string))
|
||||
|
||||
var revertid primitive.ObjectID
|
||||
copy(revertid[:6], id[6:])
|
||||
copy(revertid[6:], id[:6])
|
||||
|
||||
now := time.Now().UTC().Unix()
|
||||
var ivdoc invitationDoc
|
||||
err := iv.mongoClient.FindOneAndUpdateAs(invitation_collection_name, bson.M{
|
||||
"_id": revertid,
|
||||
}, bson.M{
|
||||
"$set": bson.M{
|
||||
"ts": now,
|
||||
},
|
||||
}, &ivdoc, options.FindOneAndUpdate().SetUpsert(false).SetReturnDocument(options.After))
|
||||
if err != nil {
|
||||
logger.Println("Block failed:", err)
|
||||
return
|
||||
}
|
||||
|
||||
if !ivdoc.Id.IsZero() {
|
||||
// 받은 초대가 있었다.
|
||||
// 나한테 알림
|
||||
iv.f.conns.writeMessage(ctx.CallBy.Accid, &wshandler.DownstreamMessage{
|
||||
Alias: ctx.CallBy.Alias,
|
||||
Body: []invitationDoc{ivdoc},
|
||||
Tag: invitations_tag,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user