Skip to content

Commit

Permalink
fix(gql): event attributes to json
Browse files Browse the repository at this point in the history
Signed-off-by: Godefroy Ponsinet <godefroy.ponsinet@outlook.com>
  • Loading branch information
90dy committed Sep 21, 2018
1 parent 2596177 commit eb3d34e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
Expand Up @@ -34,7 +34,7 @@ const Message = props => {
isMyself ? textRight : textLeft,
]}
>
{atob(props.data.attributes).trim()}
{JSON.parse(props.data.attributes).message.text}
</TextNative>
</Flex.Cols>
)
Expand Down
12 changes: 11 additions & 1 deletion core/api/node/graphql/converts.go
Expand Up @@ -184,6 +184,16 @@ func convertBytes(value *[]byte) *string {
return &encoded
}

func convertAttributes(e *p2p.Event) *string {
jsonBytes, err := e.GetJsonAttrs()
if err != nil {
logger().Error(err.Error())
return nil
}
jsonString := string(jsonBytes)
return &jsonString
}

func convertEvent(event *p2p.Event) *model.BertyP2pEvent {
if event == nil {
return &model.BertyP2pEvent{}
Expand All @@ -208,7 +218,7 @@ func convertEvent(event *p2p.Event) *model.BertyP2pEvent {
ReceiverAPIVersion: convertUint32(event.ReceiverAPIVersion),
ReceiverID: &event.ReceiverID,
Kind: convertEventKind(event.Kind),
Attributes: convertBytes(&event.Attributes),
Attributes: convertAttributes(event),
ConversationID: &conversationID,
CreatedAt: &scalar.DateTime{Value: &event.CreatedAt},
UpdatedAt: &scalar.DateTime{Value: &event.UpdatedAt},
Expand Down
20 changes: 19 additions & 1 deletion core/api/p2p/kind.go
@@ -1,6 +1,10 @@
package p2p

import "github.com/gogo/protobuf/proto"
import (
"encoding/json"

"github.com/gogo/protobuf/proto"
)

func (e *Event) SetAttrs(attrs proto.Message) error {
raw, err := proto.Marshal(attrs)
Expand All @@ -10,3 +14,17 @@ func (e *Event) SetAttrs(attrs proto.Message) error {
e.Attributes = raw
return nil
}

func (e *Event) GetJsonAttrs() ([]byte, error) {
attrs, err := e.GetAttrs()
if err != nil {
return nil, err
}

json, err := json.Marshal(attrs)
if err != nil {
return nil, err
}

return json, nil
}

0 comments on commit eb3d34e

Please sign in to comment.