Skip to content

Commit

Permalink
Change message type to type. Type do not have fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
kleewho committed Mar 24, 2023
1 parent f87a80a commit a25f2ba
Show file tree
Hide file tree
Showing 22 changed files with 160 additions and 170 deletions.
17 changes: 0 additions & 17 deletions enums.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,23 +123,6 @@ func (s PNChannelMembersInclude) String() string {
// PNMessageType is used as an enum to categorize the Subscribe response.
type PNMessageType int

func (pnMessageType PNMessageType) toMessageType() MessageType {
switch pnMessageType {
case PNMessageTypeObjects:
return "pn_object"
case PNMessageTypeFile:
return "pn_file"
case PNMessageTypeSignal:
return "pn_signal"
case PNMessageTypeMessageActions:
return "pn_messageAction"
case PNMessageTypeMessage:
fallthrough
default:
return "pn_message"
}
}

const (
// PNNonePolicy is to be used when selecting the no Reconnection Policy
// ReconnectionPolicy is set in the config.
Expand Down
32 changes: 23 additions & 9 deletions fetch_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"reflect"
"strconv"

"github.com/pubnub/go/v7/pnerr"
Expand Down Expand Up @@ -95,6 +96,12 @@ func (b *fetchBuilder) IncludeMessageType(withMessageType bool) *fetchBuilder {
return b
}

// IncludeType fetches the Message Type associated with the message
func (b *fetchBuilder) IncludeType(withType bool) *fetchBuilder {
b.opts.WithType = withType
return b
}

// IncludeSpaceId fetches the Space Id associated with the message
func (b *fetchBuilder) IncludeSpaceId(withSpaceId bool) *fetchBuilder {
b.opts.WithSpaceId = withSpaceId
Expand Down Expand Up @@ -131,6 +138,7 @@ func newFetchOpts(pubnub *PubNub, ctx Context, opts fetchOpts) *fetchOpts {
}
opts.WithUUID = true
opts.WithMessageType = true
opts.WithType = true
return &opts
}

Expand All @@ -144,6 +152,7 @@ type fetchOpts struct {
WithMeta bool
WithUUID bool
WithMessageType bool
WithType bool
WithSpaceId bool

// default: 100
Expand Down Expand Up @@ -220,7 +229,7 @@ func (o *fetchOpts) buildQuery() (*url.Values, error) {
q.Set("reverse", strconv.FormatBool(o.Reverse))
q.Set("include_meta", strconv.FormatBool(o.WithMeta))
q.Set("include_message_type", strconv.FormatBool(o.WithMessageType))
q.Set("include_type", strconv.FormatBool(o.WithMessageType))
q.Set("include_type", strconv.FormatBool(o.WithType))
q.Set("include_space_id", strconv.FormatBool(o.WithSpaceId))
q.Set("include_uuid", strconv.FormatBool(o.WithUUID))

Expand Down Expand Up @@ -286,7 +295,7 @@ func (o *fetchOpts) parseMessageActions(actions interface{}) map[string]PNHistor
return resp
}

//{"status": 200, "error": false, "error_message": "", "channels": {"ch1":[{"message_type": "", "message": {"text": "hey"}, "timetoken": "15959610984115342", "meta": "", "uuid": "db9c5e39-7c95-40f5-8d71-125765b6f561"}]}}
// {"status": 200, "error": false, "error_message": "", "channels": {"ch1":[{"message_type": "", "message": {"text": "hey"}, "timetoken": "15959610984115342", "meta": "", "uuid": "db9c5e39-7c95-40f5-8d71-125765b6f561"}]}}
func (o *fetchOpts) fetchMessages(channels map[string]interface{}) map[string][]FetchResponseItem {
messages := make(map[string][]FetchResponseItem, len(channels))

Expand All @@ -308,22 +317,26 @@ func (o *fetchOpts) fetchMessages(channels map[string]interface{}) map[string][]
if d, ok := histResponse["message_type"]; ok {
switch v := d.(type) {
case float64:
histItem.MessageType = PNMessageType(int(v)).toMessageType()
histItem.MessageType = int(v)
case string:
t, err := strconv.ParseInt(v, 10, 64)
if err == nil {
histItem.MessageType = PNMessageType(int(t)).toMessageType()
histItem.MessageType = int(t)
} else {
o.pubnub.Config.Log.Printf("MessageType conversion error.")
o.pubnub.Config.Log.Printf("Type conversion error.")
}
default:
if o.WithMessageType {
histItem.MessageType = PNMessageTypeMessage.toMessageType()
o.pubnub.Config.Log.Printf("histResponse message_type type %vv", d)
if v != nil {
o.pubnub.Config.Log.Printf("histResponse message_type type %vv", reflect.TypeOf(v).Kind())
} else {
histItem.MessageType = ""
o.pubnub.Config.Log.Printf("histResponse message_type nil")
}
}
}
if t, ok := histResponse["type"]; ok {
histItem.Type = t.(string)
}
if sid, ok := histResponse["space_id"]; ok {
histItem.SpaceId = SpaceId(sid.(string))
}
Expand Down Expand Up @@ -402,7 +415,8 @@ type FetchResponseItem struct {
File PNFileDetails
Timetoken string
UUID string
MessageType MessageType
MessageType int
Type string
SpaceId SpaceId
}

Expand Down
2 changes: 1 addition & 1 deletion fetch_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ func FetchResponseCommonForMessageTypeAndUUID(t *testing.T, withCipher bool) {
if m0 != nil {
assert.Equal("my-message", m0[0].Message)
assert.Equal("15959610984115342", m0[0].Timetoken)
assert.Equal(MessageType("pn_file"), m0[0].MessageType)
assert.Equal(4, m0[0].MessageType)
assert.Equal("db9c5e39-7c95-40f5-8d71-125765b6f561", m0[0].UUID)
} else {
assert.Fail("m0 nil")
Expand Down
8 changes: 4 additions & 4 deletions files_send_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ func (b *sendFileBuilder) SpaceId(id SpaceId) *sendFileBuilder {
return b
}

func (b *sendFileBuilder) MessageType(messageType MessageType) *sendFileBuilder {
b.opts.MessageType = messageType
func (b *sendFileBuilder) Type(typ string) *sendFileBuilder {
b.opts.Type = typ

return b
}
Expand Down Expand Up @@ -130,7 +130,7 @@ type sendFileOpts struct {
TTL int
Meta interface{}
SpaceId SpaceId
MessageType MessageType
Type string
ShouldStore bool
QueryParam map[string]string

Expand Down Expand Up @@ -256,7 +256,7 @@ func newPNSendFileResponse(jsonBytes []byte, o *sendFileOpts,
ShouldStore(o.ShouldStore).
Channel(o.Channel).
Message(message).
MessageType(o.MessageType).
Type(o.Type).
SpaceId(o.SpaceId).
Execute()
if errPubFileResponse != nil {
Expand Down
4 changes: 2 additions & 2 deletions listener_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ type PNMessage struct {
Subscription string
Publisher string
Timetoken int64
MessageType MessageType
Type string
SpaceId SpaceId
}

Expand Down Expand Up @@ -362,6 +362,6 @@ type PNFilesEvent struct {
Subscription string
Publisher string
Timetoken int64
MessageType MessageType
Type string
SpaceId SpaceId
}
10 changes: 5 additions & 5 deletions publish_file_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func (b *publishFileMessageBuilder) SpaceId(id SpaceId) *publishFileMessageBuild
return b
}

func (b *publishFileMessageBuilder) MessageType(messageType MessageType) *publishFileMessageBuilder {
b.opts.MessageType = messageType
func (b *publishFileMessageBuilder) Type(typ string) *publishFileMessageBuilder {
b.opts.Type = typ

return b
}
Expand Down Expand Up @@ -142,7 +142,7 @@ type publishFileMessageOpts struct {
TTL int
Meta interface{}
SpaceId SpaceId
MessageType MessageType
Type string
ShouldStore bool
setTTL bool
setShouldStore bool
Expand Down Expand Up @@ -258,8 +258,8 @@ func (o *publishFileMessageOpts) buildPath() (string, error) {
func (o *publishFileMessageOpts) buildQuery() (*url.Values, error) {
q := defaultQuery(o.pubnub.Config.UUID, o.pubnub.telemetryManager)

if o.MessageType != "" {
q.Set(publishMessageTypeQueryParam, string(o.MessageType))
if o.Type != "" {
q.Set(publishTypeQueryParam, o.Type)
}

if o.SpaceId != "" {
Expand Down
10 changes: 5 additions & 5 deletions publish_file_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,19 +316,19 @@ func TestPublishFileMessageMissingSpaceIdQueryParamIsNotSet(t *testing.T) {
a.Equal("", queryValues.Get(publishSpaceIdQueryParam))
}

func TestPublishFileMessageMessageTypeQueryParamIsPassed(t *testing.T) {
func TestPublishFileMessageTypeQueryParamIsPassed(t *testing.T) {
a := assert.New(t)
pn := NewPubNub(NewDemoConfig())
expectedMessageType := MessageType("customMessageType")
queryValues, _ := pn.PublishFileMessage().MessageType(expectedMessageType).opts.buildQuery()
expectedType := "customMessageType"
queryValues, _ := pn.PublishFileMessage().Type(expectedType).opts.buildQuery()

a.Equal(expectedMessageType, MessageType(queryValues.Get(publishMessageTypeQueryParam)))
a.Equal(expectedType, queryValues.Get(publishTypeQueryParam))
}

func TestPublishFileMessageMissingMessageTypeQueryParamIsNotSet(t *testing.T) {
a := assert.New(t)
pn := NewPubNub(NewDemoConfig())
queryValues, _ := pn.PublishFileMessage().opts.buildQuery()

a.Equal("", queryValues.Get(publishMessageTypeQueryParam))
a.Equal("", queryValues.Get(publishTypeQueryParam))
}
24 changes: 11 additions & 13 deletions publish_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,18 @@ const publishPostPath = "/publish/%s/%s/0/%s/%s"

var emptyPublishResponse *PublishResponse

type MessageType string

const publishSpaceIdQueryParam = "space-id"
const publishMessageTypeQueryParam = "type"
const publishTypeQueryParam = "type"

type publishOpts struct {
endpointOpts

TTL int
Channel string
Message interface{}
Meta interface{}
SpaceId SpaceId
MessageType MessageType
TTL int
Channel string
Message interface{}
Meta interface{}
SpaceId SpaceId
Type string

UsePost bool
ShouldStore bool
Expand Down Expand Up @@ -137,8 +135,8 @@ func (b *publishBuilder) SpaceId(id SpaceId) *publishBuilder {
return b
}

func (b *publishBuilder) MessageType(messageType MessageType) *publishBuilder {
b.opts.MessageType = messageType
func (b *publishBuilder) Type(typ string) *publishBuilder {
b.opts.Type = typ

return b
}
Expand Down Expand Up @@ -324,8 +322,8 @@ func (o *publishOpts) buildQuery() (*url.Values, error) {
q.Set("meta", string(meta))
}

if o.MessageType != "" {
q.Set(publishMessageTypeQueryParam, string(o.MessageType))
if o.Type != "" {
q.Set(publishTypeQueryParam, o.Type)
}

if o.SpaceId != "" {
Expand Down
8 changes: 4 additions & 4 deletions publish_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,16 +528,16 @@ func TestPublishMissingSpaceIdQueryParamIsNotSet(t *testing.T) {
func TestPublishMessageTypeQueryParamIsPassed(t *testing.T) {
a := assert.New(t)
pn := NewPubNub(NewDemoConfig())
expectedMessageType := MessageType("customMessageType")
queryValues, _ := pn.Publish().MessageType(expectedMessageType).opts.buildQuery()
expectedType := "customMessageType"
queryValues, _ := pn.Publish().Type(expectedType).opts.buildQuery()

a.Equal(expectedMessageType, MessageType(queryValues.Get(publishMessageTypeQueryParam)))
a.Equal(expectedType, queryValues.Get(publishTypeQueryParam))
}

func TestPublishMissingMessageTypeQueryParamIsNotSet(t *testing.T) {
a := assert.New(t)
pn := NewPubNub(NewDemoConfig())
queryValues, _ := pn.Publish().opts.buildQuery()

a.Equal("", queryValues.Get(publishMessageTypeQueryParam))
a.Equal("", queryValues.Get(publishTypeQueryParam))
}
22 changes: 11 additions & 11 deletions signal_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ func (b *signalBuilder) SpaceId(id SpaceId) *signalBuilder {
return b
}

func (b *signalBuilder) MessageType(messageType MessageType) *signalBuilder {
b.opts.MessageType = messageType
func (b *signalBuilder) Type(typ string) *signalBuilder {
b.opts.Type = typ

return b
}
Expand All @@ -93,13 +93,13 @@ func (b *signalBuilder) Execute() (*SignalResponse, StatusResponse, error) {

type signalOpts struct {
endpointOpts
Message interface{}
Channel string
UsePost bool
QueryParam map[string]string
SpaceId SpaceId
MessageType MessageType
Transport http.RoundTripper
Message interface{}
Channel string
UsePost bool
QueryParam map[string]string
SpaceId SpaceId
Type string
Transport http.RoundTripper
}

func (o *signalOpts) validate() error {
Expand Down Expand Up @@ -144,8 +144,8 @@ func (o *signalOpts) buildQuery() (*url.Values, error) {

SetQueryParam(q, o.QueryParam)

if o.MessageType != "" {
q.Set(publishMessageTypeQueryParam, string(o.MessageType))
if o.Type != "" {
q.Set(publishTypeQueryParam, o.Type)
}

if o.SpaceId != "" {
Expand Down
12 changes: 8 additions & 4 deletions signal_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,20 @@ func TestSignalMissingSpaceIdQueryParamIsNotSet(t *testing.T) {
func TestSignalMessageTypeQueryParamIsPassed(t *testing.T) {
a := assert.New(t)
pn := NewPubNub(NewDemoConfig())
expectedMessageType := MessageType("customMessageType")
queryValues, _ := pn.Signal().MessageType(expectedMessageType).opts.buildQuery()
expectedType := "customMessageType"
queryValues, _ := pn.Signal().Type(expectedType).opts.buildQuery()

a.Equal(expectedMessageType, MessageType(queryValues.Get(publishMessageTypeQueryParam)))
a.Equal(expectedType, queryValues.Get(publishTypeQueryParam))
}

func MessageType(s string) {
panic("unimplemented")
}

func TestSignalMissingMessageTypeQueryParamIsNotSet(t *testing.T) {
a := assert.New(t)
pn := NewPubNub(NewDemoConfig())
queryValues, _ := pn.Signal().opts.buildQuery()

a.Equal("", queryValues.Get(publishMessageTypeQueryParam))
a.Equal("", queryValues.Get(publishTypeQueryParam))
}
Loading

0 comments on commit a25f2ba

Please sign in to comment.