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

add custom message type #166

Closed
wants to merge 9 commits into from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
*.o
*.a
*.so
*.env

# Folders
_obj
Expand Down
5 changes: 3 additions & 2 deletions enums.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (s PNChannelMembersInclude) String() string {
return [...]string{"custom", "uuid", "uuid.custom"}[s-1]
}

// PNMessageType is used as an enum to catgorize the Subscribe response.
// PNMessageType is used as an enum to categorize the Subscribe response.
type PNMessageType int

const (
Expand All @@ -136,8 +136,9 @@ const (
)

const (
PNMessageTypeMessage PNMessageType = iota
// PNMessageTypeSignal is to identify Signal the Subscribe response
PNMessageTypeSignal PNMessageType = 1 + iota
PNMessageTypeSignal
// PNMessageTypeObjects is to identify Objects the Subscribe response
PNMessageTypeObjects
// PNMessageTypeMessageActions is to identify Actions the Subscribe response
Expand Down
31 changes: 28 additions & 3 deletions fetch_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ 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
return b
}

// QueryParam accepts a map, the keys and values of the map are passed as the query string parameters of the URL called by the API.
func (b *fetchBuilder) QueryParam(queryParam map[string]string) *fetchBuilder {
b.opts.QueryParam = queryParam
Expand Down Expand Up @@ -126,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 @@ -139,6 +152,8 @@ type fetchOpts struct {
WithMeta bool
WithUUID bool
WithMessageType bool
WithType bool
WithSpaceId bool

// default: 100
Count int
Expand Down Expand Up @@ -214,6 +229,8 @@ 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.WithType))
q.Set("include_space_id", strconv.FormatBool(o.WithSpaceId))
q.Set("include_uuid", strconv.FormatBool(o.WithUUID))

SetQueryParam(q, o.QueryParam)
Expand Down Expand Up @@ -318,6 +335,12 @@ func (o *fetchOpts) fetchMessages(channels map[string]interface{}) map[string][]
}
}
}
if t, ok := histResponse["type"]; ok {
histItem.Type = t.(string)
}
if sid, ok := histResponse["space_id"]; ok {
histItem.SpaceId = SpaceId(sid.(string))
}
if d, ok := histResponse["uuid"]; ok {
histItem.UUID = d.(string)
}
Expand Down Expand Up @@ -395,16 +418,18 @@ type FetchResponseItem struct {
Timetoken string `json:"timetoken"`
UUID string `json:"uuid"`
MessageType int `json:"message_type"`
Type string `json:"custom_message_type"`
SpaceId SpaceId `TODO: remove`
Error error
}

// PNHistoryMessageActionsTypeMap is the struct used in the Fetch request that includes Message Actions
type PNHistoryMessageActionsTypeMap struct {
ActionsTypeValues map[string][]PNHistoryMessageActionTypeVal `json:"-"`
ActionsTypeValues map[string][]PNHistoryMessageActionTypeVal
}

// PNHistoryMessageActionTypeVal is the struct used in the Fetch request that includes Message Actions
type PNHistoryMessageActionTypeVal struct {
UUID string `json:"uuid"`
ActionTimetoken string `json:"actionTimetoken"`
UUID string
ActionTimetoken string
}
24 changes: 23 additions & 1 deletion files_send_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ func (b *sendFileBuilder) Meta(meta interface{}) *sendFileBuilder {
return b
}

func (b *sendFileBuilder) SpaceId(id SpaceId) *sendFileBuilder {
b.opts.SpaceId = id

return b
}

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

return b
}

// ShouldStore if true the messages are stored in History
func (b *sendFileBuilder) ShouldStore(store bool) *sendFileBuilder {
b.opts.ShouldStore = store
Expand Down Expand Up @@ -117,6 +129,8 @@ type sendFileOpts struct {
CipherKey string
TTL int
Meta interface{}
SpaceId SpaceId
Type string
ShouldStore bool
QueryParam map[string]string

Expand Down Expand Up @@ -236,7 +250,15 @@ func newPNSendFileResponse(jsonBytes []byte, o *sendFileOpts,
maxCount := o.config().FileMessagePublishRetryLimit
for !sent && tryCount < maxCount {
tryCount++
pubFileMessageResponse, pubFileResponseStatus, errPubFileResponse := o.pubnub.PublishFileMessage().TTL(o.TTL).Meta(o.Meta).ShouldStore(o.ShouldStore).Channel(o.Channel).Message(message).Execute()
pubFileMessageResponse, pubFileResponseStatus, errPubFileResponse := o.pubnub.PublishFileMessage().
TTL(o.TTL).
Meta(o.Meta).
ShouldStore(o.ShouldStore).
Channel(o.Channel).
Message(message).
Type(o.Type).
SpaceId(o.SpaceId).
Execute()
if errPubFileResponse != nil {
if tryCount >= maxCount {
pubFileResponseStatus.AdditionalData = file
Expand Down
4 changes: 4 additions & 0 deletions listener_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@ type PNMessage struct {
Subscription string
Publisher string
Timetoken int64
Type string
SpaceId SpaceId // TODO: remove
Error error
}

Expand Down Expand Up @@ -361,5 +363,7 @@ type PNFilesEvent struct {
Subscription string
Publisher string
Timetoken int64
Type string
SpaceId SpaceId // TODO: remove
Error error
}
22 changes: 22 additions & 0 deletions publish_file_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ func (b *publishFileMessageBuilder) Meta(meta interface{}) *publishFileMessageBu
return b
}

func (b *publishFileMessageBuilder) SpaceId(id SpaceId) *publishFileMessageBuilder {
b.opts.SpaceId = id

return b
}

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

return b
}

// ShouldStore if true the messages are stored in History
func (b *publishFileMessageBuilder) ShouldStore(store bool) *publishFileMessageBuilder {
b.opts.ShouldStore = store
Expand Down Expand Up @@ -129,6 +141,8 @@ type publishFileMessageOpts struct {
UsePost bool
TTL int
Meta interface{}
SpaceId SpaceId
Type string
ShouldStore bool
setTTL bool
setShouldStore bool
Expand Down Expand Up @@ -244,6 +258,14 @@ func (o *publishFileMessageOpts) buildPath() (string, error) {
func (o *publishFileMessageOpts) buildQuery() (*url.Values, error) {
q := defaultQuery(o.pubnub.Config.UUID, o.pubnub.telemetryManager)

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

if o.SpaceId != "" {
q.Set(publishSpaceIdQueryParam, string(o.SpaceId))
}

SetQueryParam(q, o.QueryParam)

return q, nil
Expand Down
34 changes: 34 additions & 0 deletions publish_file_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,37 @@ func TestPublishFileMessageResponseValuePass(t *testing.T) {

assert.Nil(err)
}

func TestPublishFileMessageSpaceIdQueryParamIsPassed(t *testing.T) {
a := assert.New(t)
pn := NewPubNub(NewDemoConfig())
expectedSpaceId := SpaceId("spaceId")
queryValues, _ := pn.PublishFileMessage().SpaceId(expectedSpaceId).opts.buildQuery()

a.Equal(expectedSpaceId, SpaceId(queryValues.Get(publishSpaceIdQueryParam)))
}

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

a.Equal("", queryValues.Get(publishSpaceIdQueryParam))
}

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

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(publishTypeQueryParam))
}
24 changes: 24 additions & 0 deletions publish_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@ const publishPostPath = "/publish/%s/%s/0/%s/%s"

var emptyPublishResponse *PublishResponse

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

type publishOpts struct {
endpointOpts

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

UsePost bool
ShouldStore bool
Expand Down Expand Up @@ -125,6 +130,17 @@ func (b *publishBuilder) Meta(meta interface{}) *publishBuilder {
return b
}

func (b *publishBuilder) SpaceId(id SpaceId) *publishBuilder {
b.opts.SpaceId = id
return b
}

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

return b
}

// UsePost sends the Publish request using HTTP POST.
func (b *publishBuilder) UsePost(post bool) *publishBuilder {
b.opts.UsePost = post
Expand Down Expand Up @@ -306,6 +322,14 @@ func (o *publishOpts) buildQuery() (*url.Values, error) {
q.Set("meta", string(meta))
}

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

if o.SpaceId != "" {
q.Set(publishSpaceIdQueryParam, string(o.SpaceId))
}

if o.setShouldStore {
if o.ShouldStore {
q.Set("store", "1")
Expand Down
Loading
Loading