body를 marshaling하고 클라이언트에서 flatten함
This commit is contained in:
@ -128,14 +128,14 @@ func (sub *subTavern) Invite(w http.ResponseWriter, r *http.Request) {
|
||||
Invitee bson.M `bson:"invitee"`
|
||||
}
|
||||
if err := readBsonDoc(r.Body, &reqdoc); err != nil {
|
||||
logger.Error("Invite failed. readBsonDoc returns err :", err)
|
||||
logger.Println("Invite failed. readBsonDoc returns err :", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := group.Invite(gid, mid, reqdoc.Inviter, reqdoc.Invitee)
|
||||
if err != nil {
|
||||
logger.Error("Invite failed. group.Invite returns err :", err)
|
||||
logger.Println("Invite failed. group.Invite returns err :", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@ -191,7 +191,7 @@ func (sub *subTavern) AcceptInvitation(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
err := group.AcceptInvitation(gid, mid, member)
|
||||
if err != nil {
|
||||
logger.Error("AcceptInvitation failed. group.AcceptInvitation returns err :", err)
|
||||
logger.Println("AcceptInvitation failed. group.AcceptInvitation returns err :", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
@ -21,10 +21,12 @@ import (
|
||||
type accountID = primitive.ObjectID
|
||||
type ticketID = primitive.ObjectID
|
||||
type groupID = primitive.ObjectID
|
||||
type Body = bson.M
|
||||
|
||||
func init() {
|
||||
gob.Register(memberDoc{})
|
||||
gob.Register(groupDoc{})
|
||||
gob.Register(Invitation{})
|
||||
gob.Register(InvitationFail{})
|
||||
}
|
||||
|
||||
func makeTid(gid groupID, in accountID) string {
|
||||
@ -44,7 +46,7 @@ type Invitation struct {
|
||||
|
||||
// 플레이어한테 공유하는 멤버 정보
|
||||
type memberDoc struct {
|
||||
Body `json:",inline"`
|
||||
Body bson.M `json:"_body"`
|
||||
Invite bool `json:"_invite"`
|
||||
InviteExpire int64 `json:"_invite_exp"`
|
||||
}
|
||||
@ -52,25 +54,43 @@ type memberDoc struct {
|
||||
type InvitationFail bson.M
|
||||
|
||||
type groupDoc struct {
|
||||
Body `json:",inline"`
|
||||
Body bson.M `json:"_body"`
|
||||
Members map[string]*memberDoc `json:"_members"`
|
||||
InCharge string `json:"_incharge"`
|
||||
Gid string `json:"_gid"`
|
||||
|
||||
rh *RedisonHandler
|
||||
id groupID
|
||||
idhex string
|
||||
}
|
||||
|
||||
type groupDocWithId struct {
|
||||
*groupDoc `json:",inline"`
|
||||
Gid string `json:"_gid"`
|
||||
func (p groupDoc) MarshalJSON() ([]byte, error) {
|
||||
if len(p.Gid) == 0 {
|
||||
p.Gid = p.id.Hex()
|
||||
}
|
||||
|
||||
// Turn p into a map
|
||||
type groupDoc_ groupDoc // prevent recursion
|
||||
b, _ := json.Marshal(groupDoc_(p))
|
||||
|
||||
var m map[string]json.RawMessage
|
||||
_ = json.Unmarshal(b, &m)
|
||||
|
||||
// Add tags to the map, possibly overriding struct fields
|
||||
for k, v := range p.Body {
|
||||
// if overriding struct fields is not acceptable:
|
||||
// if _, ok := m[k]; ok { continue }
|
||||
b, _ = json.Marshal(v)
|
||||
m[k] = b
|
||||
}
|
||||
|
||||
return json.Marshal(m)
|
||||
}
|
||||
|
||||
func (gd *groupDoc) strid() string {
|
||||
if len(gd.idhex) == 0 {
|
||||
gd.idhex = gd.id.Hex()
|
||||
if len(gd.Gid) == 0 {
|
||||
gd.Gid = gd.id.Hex()
|
||||
}
|
||||
return gd.idhex
|
||||
return gd.Gid
|
||||
}
|
||||
|
||||
func (gd *groupDoc) tid(in accountID) string {
|
||||
@ -282,10 +302,7 @@ func (gm *groupInMemory) Invite(gid groupID, mid accountID, inviterDoc bson.M, i
|
||||
|
||||
gm.sendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: "@" + mid.Hex(),
|
||||
Body: groupDocWithId{
|
||||
groupDoc: gd,
|
||||
Gid: gd.strid(),
|
||||
},
|
||||
Body: gd,
|
||||
Tag: []string{"GroupDocFull"},
|
||||
})
|
||||
}
|
||||
@ -296,11 +313,10 @@ func (gm *groupInMemory) Invite(gid groupID, mid accountID, inviterDoc bson.M, i
|
||||
}
|
||||
|
||||
// 초대 중 표시
|
||||
success, err := gm.rh.SetNX(gm.rh.ctx, targetid.Hex(), mid.Hex(), time.Duration(gm.InviteExpire)*time.Second).Result()
|
||||
_, err = gm.rh.SetNX(gm.rh.ctx, targetid.Hex(), mid.Hex(), time.Duration(gm.InviteExpire)*time.Second).Result()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
logger.Println("invitation key :", targetid.Hex(), success)
|
||||
|
||||
// invitee에게 알림
|
||||
gm.sendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
@ -358,19 +374,17 @@ func (gm *groupInMemory) AcceptInvitation(gid groupID, mid accountID, member bso
|
||||
return err
|
||||
}
|
||||
|
||||
logger.Println(full)
|
||||
var temp []groupDoc
|
||||
var temp []*groupDoc
|
||||
err = json.Unmarshal([]byte(full.(string)), &temp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
test, _ := json.Marshal(temp[0])
|
||||
logger.Println(string(test))
|
||||
gm.sendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: "@" + mid.Hex(),
|
||||
Body: groupDocWithId{
|
||||
groupDoc: &temp[0],
|
||||
Gid: gd.strid(),
|
||||
},
|
||||
Body: temp[0],
|
||||
Tag: []string{"GroupDocFull"},
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user