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"`
|
Invitee bson.M `bson:"invitee"`
|
||||||
}
|
}
|
||||||
if err := readBsonDoc(r.Body, &reqdoc); err != nil {
|
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)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
result, err := group.Invite(gid, mid, reqdoc.Inviter, reqdoc.Invitee)
|
result, err := group.Invite(gid, mid, reqdoc.Inviter, reqdoc.Invitee)
|
||||||
if err != nil {
|
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)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -191,7 +191,7 @@ func (sub *subTavern) AcceptInvitation(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
err := group.AcceptInvitation(gid, mid, member)
|
err := group.AcceptInvitation(gid, mid, member)
|
||||||
if err != nil {
|
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)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@ -21,10 +21,12 @@ import (
|
|||||||
type accountID = primitive.ObjectID
|
type accountID = primitive.ObjectID
|
||||||
type ticketID = primitive.ObjectID
|
type ticketID = primitive.ObjectID
|
||||||
type groupID = primitive.ObjectID
|
type groupID = primitive.ObjectID
|
||||||
type Body = bson.M
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
gob.Register(memberDoc{})
|
gob.Register(memberDoc{})
|
||||||
|
gob.Register(groupDoc{})
|
||||||
|
gob.Register(Invitation{})
|
||||||
|
gob.Register(InvitationFail{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeTid(gid groupID, in accountID) string {
|
func makeTid(gid groupID, in accountID) string {
|
||||||
@ -44,7 +46,7 @@ type Invitation struct {
|
|||||||
|
|
||||||
// 플레이어한테 공유하는 멤버 정보
|
// 플레이어한테 공유하는 멤버 정보
|
||||||
type memberDoc struct {
|
type memberDoc struct {
|
||||||
Body `json:",inline"`
|
Body bson.M `json:"_body"`
|
||||||
Invite bool `json:"_invite"`
|
Invite bool `json:"_invite"`
|
||||||
InviteExpire int64 `json:"_invite_exp"`
|
InviteExpire int64 `json:"_invite_exp"`
|
||||||
}
|
}
|
||||||
@ -52,25 +54,43 @@ type memberDoc struct {
|
|||||||
type InvitationFail bson.M
|
type InvitationFail bson.M
|
||||||
|
|
||||||
type groupDoc struct {
|
type groupDoc struct {
|
||||||
Body `json:",inline"`
|
Body bson.M `json:"_body"`
|
||||||
Members map[string]*memberDoc `json:"_members"`
|
Members map[string]*memberDoc `json:"_members"`
|
||||||
InCharge string `json:"_incharge"`
|
InCharge string `json:"_incharge"`
|
||||||
|
Gid string `json:"_gid"`
|
||||||
|
|
||||||
rh *RedisonHandler
|
rh *RedisonHandler
|
||||||
id groupID
|
id groupID
|
||||||
idhex string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type groupDocWithId struct {
|
func (p groupDoc) MarshalJSON() ([]byte, error) {
|
||||||
*groupDoc `json:",inline"`
|
if len(p.Gid) == 0 {
|
||||||
Gid string `json:"_gid"`
|
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 {
|
func (gd *groupDoc) strid() string {
|
||||||
if len(gd.idhex) == 0 {
|
if len(gd.Gid) == 0 {
|
||||||
gd.idhex = gd.id.Hex()
|
gd.Gid = gd.id.Hex()
|
||||||
}
|
}
|
||||||
return gd.idhex
|
return gd.Gid
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gd *groupDoc) tid(in accountID) string {
|
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{
|
gm.sendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||||
Target: "@" + mid.Hex(),
|
Target: "@" + mid.Hex(),
|
||||||
Body: groupDocWithId{
|
Body: gd,
|
||||||
groupDoc: gd,
|
|
||||||
Gid: gd.strid(),
|
|
||||||
},
|
|
||||||
Tag: []string{"GroupDocFull"},
|
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 {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
logger.Println("invitation key :", targetid.Hex(), success)
|
|
||||||
|
|
||||||
// invitee에게 알림
|
// invitee에게 알림
|
||||||
gm.sendUpstreamMessage(&wshandler.UpstreamMessage{
|
gm.sendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||||
@ -358,19 +374,17 @@ func (gm *groupInMemory) AcceptInvitation(gid groupID, mid accountID, member bso
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Println(full)
|
var temp []*groupDoc
|
||||||
var temp []groupDoc
|
|
||||||
err = json.Unmarshal([]byte(full.(string)), &temp)
|
err = json.Unmarshal([]byte(full.(string)), &temp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test, _ := json.Marshal(temp[0])
|
||||||
|
logger.Println(string(test))
|
||||||
gm.sendUpstreamMessage(&wshandler.UpstreamMessage{
|
gm.sendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||||
Target: "@" + mid.Hex(),
|
Target: "@" + mid.Hex(),
|
||||||
Body: groupDocWithId{
|
Body: temp[0],
|
||||||
groupDoc: &temp[0],
|
|
||||||
Gid: gd.strid(),
|
|
||||||
},
|
|
||||||
Tag: []string{"GroupDocFull"},
|
Tag: []string{"GroupDocFull"},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user