src.dualinventive.com/go/dinet/rpc/objects_message.go

139 lines
5.5 KiB
Go

package rpc
import (
"encoding/json"
)
// MessageSms describes the param message:sms structure
type MessageSms struct {
Destinations []string `json:"dest" msgpack:"dest"`
Message string `json:"message" msgpack:"message"`
SenderID string `json:"sender_id" msgpack:"sender_id"`
}
// MessagePush describes the param message:push structure
type MessagePush struct {
Destinations []uint `json:"dest" msgpack:"dest"`
Title string `json:"title" msgpack:"title"`
TitleKey string `json:"title_key" msgpack:"title_key"`
TitleParams map[string]interface{} `json:"title_params" msgpack:"title_params"`
Message string `json:"message" msgpack:"message"`
MessageKey string `json:"message_key" msgpack:"message_key"`
MessageParams map[string]interface{} `json:"message_params" msgpack:"message_params"`
Icon string `json:"icon" msgpack:"icon"`
Sound string `json:"sound" msgpack:"sound"`
Tag string `json:"tag" msgpack:"tag"`
ClickAction string `json:"action" msgpack:"action"`
CustomData interface{} `json:"data" msgpack:"data"`
Collapsekey string `json:"collapsekey" msgpack:"collapsekey"`
Priority string `json:"priority" msgpack:"priority"`
Badge *int `json:"badge" msgpack:"badge"`
SenderID string `json:"sender_id" msgpack:"sender_id"`
}
// MessagePushRegister describes the param message:push:register structure
type MessagePushRegister struct {
UserID uint `json:"user:id" msgpack:"user:id"`
Platform string `json:"platform" msgpack:"platform"`
Token string `json:"token" msgpack:"token"`
DeviceUUID string `json:"device:uuid" msgpack:"device:uuid"`
}
// MessageDeliveryType describes the type of delivery of a message
type MessageDeliveryType string
const (
// MessageDeliveryTypeSMS indicates that the message is delivered by SMS
MessageDeliveryTypeSMS MessageDeliveryType = "sms"
// MessageDeliveryTypePush indicates that the message is delivered by Push
MessageDeliveryTypePush MessageDeliveryType = "push"
// MessageDeliveryTypeFailed indicates that the message could not be delivered by SMS or Push
MessageDeliveryTypeFailed MessageDeliveryType = "failed"
)
// MessageStatus describes the result message:status structure
type MessageStatus struct {
Title string `json:"title" msgpack:"title"`
Message string `json:"message" msgpack:"message"`
Phone string `json:"phone" msgpack:"phone"`
Status MessageDeliveryType `json:"status" msgpack:"status"`
}
// UnmarshalJSON marshal a MessageSms in json format
func (m *MessageSms) UnmarshalJSON(data []byte) error {
type multiMessage MessageSms
type singleMessage struct {
Destination string `json:"dest" msgpack:"dest"`
Message string `json:"message" msgpack:"message"`
SenderID string `json:"sender_id" msgpack:"sender_id"`
}
singlePayload := singleMessage{}
if err := json.Unmarshal(data, &singlePayload); err == nil {
// The message had a single destination
*m = MessageSms{[]string{singlePayload.Destination}, singlePayload.Message, singlePayload.SenderID}
return nil
}
multiPayload := multiMessage{}
if err := json.Unmarshal(data, &multiPayload); err != nil {
return err
}
*m = MessageSms(multiPayload)
return nil
}
// UnmarshalJSON marshal a MessagePush in json format
func (m *MessagePush) UnmarshalJSON(data []byte) error {
type multiMessage MessagePush
type singleMessage struct {
Destination uint `json:"dest" msgpack:"dest"`
Title string `json:"title" msgpack:"title"`
TitleKey string `json:"title_key" msgpack:"title_key"`
TitleParams map[string]interface{} `json:"title_params" msgpack:"title_params"`
Message string `json:"message" msgpack:"message"`
MessageKey string `json:"message_key" msgpack:"message_key"`
MessageParams map[string]interface{} `json:"message_params" msgpack:"message_params"`
Icon string `json:"icon" msgpack:"icon"`
Sound string `json:"sound" msgpack:"sound"`
Tag string `json:"tag" msgpack:"tag"`
ClickAction string `json:"action" msgpack:"action"`
CustomData *interface{} `json:"data" msgpack:"data"`
Collapsekey string `json:"collapsekey" msgpack:"collapsekey"`
Priority string `json:"priority" msgpack:"priority"`
Badge *int `json:"badge" msgpack:"badge"`
SenderID string `json:"sender_id" msgpack:"sender_id"`
}
singlePayload := singleMessage{}
if err := json.Unmarshal(data, &singlePayload); err == nil {
// The message had a single destination
*m = MessagePush{[]uint{singlePayload.Destination},
singlePayload.Title,
singlePayload.TitleKey,
singlePayload.TitleParams,
singlePayload.Message,
singlePayload.MessageKey,
singlePayload.MessageParams,
singlePayload.Icon,
singlePayload.Sound,
singlePayload.Tag,
singlePayload.ClickAction,
singlePayload.CustomData,
singlePayload.Collapsekey,
singlePayload.Priority,
singlePayload.Badge,
singlePayload.SenderID}
return nil
}
multiPayload := multiMessage{}
if err := json.Unmarshal(data, &multiPayload); err != nil {
return err
}
*m = MessagePush(multiPayload)
return nil
}