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 message type and space id in files #144

Merged
merged 4 commits into from
Feb 27, 2023
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
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
* @kleewho @parfeon @mateusz-dahlke
* @kleewho @parfeon @Xavrax
.github/* @parfeon @kleewho
README.md @techwritermat @kazydek
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
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) MessageType(messageType MessageType) *sendFileBuilder {
b.opts.MessageType = messageType

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
MessageType MessageType
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).
MessageType(o.MessageType).
SpaceId(o.SpaceId).
Execute()
if errPubFileResponse != nil {
if tryCount >= maxCount {
pubFileResponseStatus.AdditionalData = file
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/brianolson/cbor_go v1.0.0
github.com/cucumber/godog v0.12.0
github.com/google/uuid v1.3.0
github.com/joho/godotenv v1.5.1 // indirect
github.com/stretchr/testify v1.7.0
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d
golang.org/x/text v0.3.7 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0m
github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
Expand Down
2 changes: 2 additions & 0 deletions listener_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,4 +362,6 @@ type PNFilesEvent struct {
Subscription string
Publisher string
Timetoken int64
MessageType MessageType
SpaceId SpaceId
}
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) MessageType(messageType MessageType) *publishFileMessageBuilder {
b.opts.MessageType = messageType

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
MessageType MessageType
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.MessageType != "" {
q.Set(publishMessageTypeQueryParam, string(o.MessageType))
}

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 TestPublishFileMessageMessageTypeQueryParamIsPassed(t *testing.T) {
a := assert.New(t)
pn := NewPubNub(NewDemoConfig())
expectedMessageType := MessageType("customMessageType")
queryValues, _ := pn.PublishFileMessage().MessageType(expectedMessageType).opts.buildQuery()

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

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

a.Equal("", queryValues.Get(publishMessageTypeQueryParam))
}
1 change: 0 additions & 1 deletion publish_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ func (b *publishBuilder) Meta(meta interface{}) *publishBuilder {

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

return b
}

Expand Down
6 changes: 4 additions & 2 deletions subscription_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ func processNonPresencePayload(m *SubscriptionManager, payload subscribeMessage,

}

pnFilesEvent := createPNFilesEvent(messagePayload, m, actualCh, subscribedCh, channel, subscriptionMatch, payload.IssuingClientID, payload.UserMetadata, timetoken)
pnFilesEvent := createPNFilesEvent(messagePayload, m, actualCh, subscribedCh, channel, subscriptionMatch, payload.IssuingClientID, payload.UserMetadata, timetoken, messageType, payload.SpaceId)
m.pubnub.Config.Log.Println("PNMessageTypeFile:", PNMessageTypeFile)
m.listenerManager.announceFile(pnFilesEvent)
default:
Expand Down Expand Up @@ -725,7 +725,7 @@ func processSubscribePayload(m *SubscriptionManager, payload subscribeMessage) {
}
}

func createPNFilesEvent(filePayload interface{}, m *SubscriptionManager, actualCh, subscribedCh, channel, subscriptionMatch, issuingClientID string, userMetadata interface{}, timetoken int64) *PNFilesEvent {
func createPNFilesEvent(filePayload interface{}, m *SubscriptionManager, actualCh, subscribedCh, channel, subscriptionMatch, issuingClientID string, userMetadata interface{}, timetoken int64, messageType MessageType, spaceId SpaceId) *PNFilesEvent {
var filesPayload map[string]interface{}
var ok bool
if filesPayload, ok = filePayload.(map[string]interface{}); !ok {
Expand Down Expand Up @@ -756,6 +756,8 @@ func createPNFilesEvent(filePayload interface{}, m *SubscriptionManager, actualC
Timetoken: timetoken,
Publisher: issuingClientID,
UserMetadata: userMetadata,
SpaceId: spaceId,
MessageType: messageType,
}
return pnFilesEvent
}
Expand Down
Binary file modified tests/e2e/file_upload_test_output.txt
Binary file not shown.
Loading