Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error when sending a message with no RCS data #10

Merged
merged 1 commit into from
Aug 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions ocs/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package ocs

import (
"encoding/json"
"strings"
)

Expand All @@ -41,6 +42,11 @@ type TalkRoomMessageData struct {
SystemMessage string `json:"systemMessage"`
Timestamp int `json:"timestamp"`
MessageType MessageType `json:"messageType"`
MessageParameters map[string]RichObjectString `json:"-"`
}

// talkRoomMessageParameters is used to unmarshal only MessageParameters
type talkRoomMessageParameters struct {
MessageParameters map[string]RichObjectString `json:"messageParameters"`
}

Expand All @@ -59,12 +65,78 @@ func (m *TalkRoomMessageData) PlainMessage() string {

// TalkRoomMessage describes an ocs response for a Talk room message
type TalkRoomMessage struct {
OCS talkRoomMessage `json:"ocs"`
}

type talkRoomMessage struct {
ocs
TalkRoomMessage []TalkRoomMessageData `json:"data"`
}

// TalkRoomMessageDataUnmarshal unmarshals given ocs request data and returns a TalkRoomMessageData
func TalkRoomMessageDataUnmarshal(data *[]byte) (*TalkRoomMessage, error) {
message := &TalkRoomMessage{}
err := json.Unmarshal(*data, message)
if err != nil {
return nil, err
}

// Get RCS
var rcs struct {
OCS struct {
ocs
TalkRoomMessage []talkRoomMessageParameters `json:"data"`
} `json:"ocs"`
}
err = json.Unmarshal(*data, &rcs)
// There is no RCS data
if err != nil {
for i := range message.OCS.TalkRoomMessage {
message.OCS.TalkRoomMessage[i].MessageParameters = map[string]RichObjectString{}
}
return message, nil
}

// There is RCS data
for i := range message.OCS.TalkRoomMessage {
message.OCS.TalkRoomMessage[i].MessageParameters = rcs.OCS.TalkRoomMessage[i].MessageParameters
}
return message, nil
}

// TalkRoomSentResponse describes an ocs response for what is returned when a message is sent
type TalkRoomSentResponse struct {
OCS talkRoomSentResponse `json:"ocs"`
}

type talkRoomSentResponse struct {
ocs
TalkRoomMessage TalkRoomMessageData `json:"data"`
}

// TalkRoomSentResponseUnmarshal unmarshals given ocs request data and returns a TalkRoomMessageData
func TalkRoomSentResponseUnmarshal(data *[]byte) (*TalkRoomSentResponse, error) {
message := &TalkRoomSentResponse{}
err := json.Unmarshal(*data, message)
if err != nil {
return nil, err
}

// Get RCS
var rcs struct {
OCS struct {
ocs
TalkRoomMessage talkRoomMessageParameters `json:"data"`
} `json:"ocs"`
}
err = json.Unmarshal(*data, &rcs)
// There is no RCS data
if err != nil {
message.OCS.TalkRoomMessage.MessageParameters = map[string]RichObjectString{}
return message, nil
}

// There is RCS data
message.OCS.TalkRoomMessage.MessageParameters = rcs.OCS.TalkRoomMessage.MessageParameters
return message, nil
}
12 changes: 4 additions & 8 deletions room/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package room

import (
"context"
"encoding/json"
"errors"
"io/ioutil"
"time"
Expand Down Expand Up @@ -79,10 +78,10 @@ func (t *TalkRoom) SendMessage(msg string) (*ocs.TalkRoomMessageData, error) {
if res.StatusCode() != 201 {
return nil, ErrUnexpectedReturnCode
}
var msgInfo struct {
OCS ocs.TalkRoomSentResponse `json:"ocs"`
msgInfo, err := ocs.TalkRoomSentResponseUnmarshal(&res.Data)
if err != nil {
return nil, err
}
err = json.Unmarshal(res.Data, &msgInfo)
return &msgInfo.OCS.TalkRoomMessage, err
}

Expand Down Expand Up @@ -129,14 +128,11 @@ func (t *TalkRoom) ReceiveMessages(ctx context.Context) (chan ocs.TalkRoomMessag
}
if res.StatusCode == 200 {
lastKnown = res.Header.Get("X-Chat-Last-Given")
var message struct {
OCS ocs.TalkRoomMessage `json:"ocs"`
}
data, err := ioutil.ReadAll(res.Body)
if err != nil {
continue
}
err = json.Unmarshal(data, &message)
message, err := ocs.TalkRoomMessageDataUnmarshal(&data)
if err != nil {
continue
}
Expand Down