diff --git a/core/connection.go b/core/connection.go index b20a954..78f44bf 100644 --- a/core/connection.go +++ b/core/connection.go @@ -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, } } diff --git a/core/invitation.go b/core/invitation.go index 7e2948c..6d51dd6 100644 --- a/core/invitation.go +++ b/core/invitation.go @@ -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 : 이미 친구면 초대 불가 diff --git a/core/social.go b/core/social.go index e540dfb..a2760da 100644 --- a/core/social.go +++ b/core/social.go @@ -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"))