sneakpeek 함수 추가

This commit is contained in:
2024-01-24 14:33:02 +09:00
parent 3e868ba49f
commit 862d5650a2
3 changed files with 43 additions and 3 deletions

View File

@ -6,6 +6,7 @@ import (
"github.com/gorilla/websocket"
"go.mongodb.org/mongo-driver/bson/primitive"
"repositories.action2quare.com/ayo/gocommon"
"repositories.action2quare.com/ayo/gocommon/wshandler"
)
@ -18,6 +19,7 @@ type connWithFriends struct {
type connections struct {
connLock sync.Mutex
conns map[primitive.ObjectID]*connWithFriends
redison *gocommon.RedisonHandler
}
func (cs *connections) new(accid primitive.ObjectID, conn *websocket.Conn) {
@ -93,14 +95,17 @@ func (cs *connections) writeMessage(acc primitive.ObjectID, src any) {
func (cs *connections) ClientConnected(conn *websocket.Conn, callby *wshandler.Sender) {
cs.new(callby.Accid, conn)
cs.redison.JSONSet(callby.Accid.Hex(), "$", map[string]any{}, gocommon.RedisonSetOptionNX)
}
func (cs *connections) ClientDisconnected(msg string, callby *wshandler.Sender) {
cs.redison.JSONDel(callby.Accid.Hex(), "$")
cs.delete(callby.Accid)
}
func makeConnections() *connections {
func makeConnections(redison *gocommon.RedisonHandler) *connections {
return &connections{
conns: make(map[primitive.ObjectID]*connWithFriends),
conns: make(map[primitive.ObjectID]*connWithFriends),
redison: redison,
}
}

View File

@ -33,6 +33,11 @@ type invitationDoc struct {
Blocked bool `bson:"blocked,omitempty" json:"-"` // From은 To에 의해 차단된 상태를 표시
}
type sneakpeekDoc struct {
From primitive.ObjectID `bson:"from,omitempty" json:"-"`
To primitive.ObjectID `bson:"to,omitempty" json:"-"`
}
func init() {
gob.Register([]invitationDoc{})
}
@ -226,6 +231,36 @@ func (iv *invitation) Trim(ctx wshandler.ApiCallContext) {
}
}
func (iv *invitation) SneakPeekTarget(w http.ResponseWriter, r *http.Request) {
var ivdoc sneakpeekDoc
if err := gocommon.MakeDecoder(r).Decode(&ivdoc); err != nil {
logger.Println("InviteAsFriend failed:", err)
w.WriteHeader(http.StatusBadRequest)
return
}
// ivdoc.To가 invdoc.From을 차단했으면 offline으로 표시
exists, err := iv.mongoClient.Exists(block_collection_name, bson.M{"_id": combineObjectID(ivdoc.To, ivdoc.From)})
if err != nil {
logger.Println("InviteAsFriend failed:", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
enc := gocommon.MakeEncoder(w, r)
if exists {
enc.Encode("offline")
} else {
exists, _ := iv.redison.Exists(iv.redison.Context(), ivdoc.To.Hex()).Result()
if exists == 0 {
enc.Encode("offline")
} else {
enc.Encode("online")
}
}
}
func (iv *invitation) InviteAsFriend(w http.ResponseWriter, r *http.Request) {
// 내 현재 친구 숫자 + 내가 보낸 초대 숫자가 FriendsMax를 넘을 수 없다.
// TODO : 이미 친구면 초대 불가

View File

@ -80,7 +80,7 @@ func (so *Social) prepare(ctx context.Context) error {
so.redison = gocommon.NewRedisonHandler(redisClient.Context(), redisClient)
connections := makeConnections()
connections := makeConnections(so.redison)
so.wsh.AddHandler(wshandler.MakeWebsocketApiHandler(connections, "social"))
so.httpApiBorker.AddHandler(gocommon.MakeHttpApiHandler(connections, "social"))