Skip to content

Commit

Permalink
feat: send files in send_messages
Browse files Browse the repository at this point in the history
  • Loading branch information
tomMoulard committed Apr 30, 2024
1 parent 5d8cdec commit 7bbb5ef
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 37 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ issues:
- path: '(.+)_test.go'
linters:
- funlen
- gosec
- path: 'pkg/client/errors_test.go'
text: 'tests: ExampleHandleError refers to unknown identifier: HandleError'
linters:
Expand Down
43 changes: 40 additions & 3 deletions pkg/message/send_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,41 @@ import (
"fmt"
)

// File is a fiel to send.
type File struct {
// URL specifies the URL of the file hosted on the server of your own or
// other third-party companies.
URL string `json:"url"`
// FileName specifies the name of the file. The file_name can be any string
// you've set to this property. If no file_name value is supplied, the
// files.file_name property defaults to an empty string.
FileName string `json:"file_name,omitempty"`
// FileSize specifies the size of the file in bytes. The file_size can be set
// to be any int you've set to this property. If no files.file_size value is
// supplied, it defaults to 0. File size is stored in bytes.
FileSize int `json:"file_size,omitempty"`
// FileType specifies the type of the file. The file_type can be set to any
// string you've set to this property. If no files.file_type value is
// supplied, it defaults to an empty string.
FileType string `json:"file_type,omitempty"`
}

// SendMessageRequest is the request to send a message.
type SendMessageRequest struct {
// MessageType specifies the type of the message.
MessageType MessageType `json:"message_type"`
// UserID specifies the user ID of the sender.
UserID string `json:"user_id"`

// Message specifies the content of the message.
Message string `json:"message"`
Message string `json:"message,omitempty"`
// Files specifies the data of files to upload to the Sendbird server by
// their location.
Files []File `json:"files,omitempty"`

// Thumbnails specifies an array of external thumbnail image URLs to store a
// reference to those images in the Sendbird server.
Thumbnails []string `json:"thumbnails,omitempty"`
// CustomType specifies a custom message type used for message grouping. The
// length is limited to 128 characters.
CustomType string `json:"custom_type,omitempty"`
Expand Down Expand Up @@ -97,8 +123,19 @@ func (smr *SendMessageRequest) Validate() error {
return errors.New("message type is required")
case smr.UserID == "":
return errors.New("user ID is required")
case smr.Message == "":
return errors.New("message is required")
}

switch {
case smr.MessageType == MessageTypeText && smr.Message == "":
return errors.New("message is required for text message")
case smr.MessageType == MessageTypeFile && len(smr.Files) == 0:
return errors.New("files are required for file message")
case smr.MessageType == MessageTypeFile:
for _, file := range smr.Files {
if file.URL == "" {
return errors.New("file URL is required")
}
}
}

return nil
Expand Down
128 changes: 94 additions & 34 deletions pkg/message/send_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,38 +35,67 @@ func TestValidateSMR(t *testing.T) {
assertErr: assert.Error,
},
{
name: "Without Message Type",
name: "without Message Type",
smr: SendMessageRequest{
UserID: "42",
Message: "Hello, World!",
},
assertErr: assert.Error,
},
{
name: "Without user id",
name: "without user id",
smr: SendMessageRequest{
MessageType: MessageTypeText,
Message: "Hello, World!",
},
assertErr: assert.Error,
},
{
name: "Without message",
name: "text message without message",
smr: SendMessageRequest{
MessageType: MessageTypeText,
UserID: "42",
},
assertErr: assert.Error,
},
{
name: "Valid",
name: "file message without files",
smr: SendMessageRequest{
MessageType: MessageTypeFile,
UserID: "42",
},
assertErr: assert.Error,
},
{
name: "file message with files, but not url",
smr: SendMessageRequest{
MessageType: MessageTypeFile,
UserID: "42",
Files: []File{{}},
},
assertErr: assert.Error,
},
{
name: "valid text message",
smr: SendMessageRequest{
MessageType: MessageTypeText,
UserID: "42",
Message: "Hello, World!",
},
assertErr: assert.NoError,
},
{
name: "valid file message",
smr: SendMessageRequest{
MessageType: MessageTypeFile,
UserID: "42",
Files: []File{
{URL: "https://example.com/file1"},
{URL: "https://example.com/file2"},
},
},
assertErr: assert.NoError,
},
}

for _, test := range tests {
Expand All @@ -82,38 +111,69 @@ func TestValidateSMR(t *testing.T) {
func TestSendMessage(t *testing.T) {
t.Parallel()

client := newClientMock(t)
message := NewMessage(client)

sendMessageRequest := SendMessageRequest{
MessageType: MessageTypeText,
UserID: "42",
Message: "Hello, World!",
CustomType: "custom-type",
Data: `{ "key": "value" }`,
SendPush: ptr(true),
PushMessageTemplate: "push-notification-template",
MentionType: MentionTypeChannels,
MentionUserIDs: []string{"mention-user-id"},
IsSilent: ptr(true),
MarkAsRead: ptr(true),
SortedMetaArray: []MetaArray{{Key: "key1", Value: []string{"value1"}}, {Key: "key2", Value: []string{"value2"}}},
CreatedAt: 42,
PollID: 42,
IncludePollDetails: ptr(true),
DedupID: "dedup-id",
ApnsBundleID: "apns-bundle-id",
Sound: "sound",
Volume: 0.5,
tests := []struct {
name string
smrq SendMessageRequest
smrp SendMessageResponse
}{
{
name: "Text Message",
smrq: SendMessageRequest{
MessageType: MessageTypeText,
UserID: "42",
Message: "Hello, World!",
CustomType: "custom-type",
Data: `{ "key": "value" }`,
SendPush: ptr(true),
PushMessageTemplate: "push-notification-template",
MentionType: MentionTypeChannels,
MentionUserIDs: []string{"mention-user-id"},
IsSilent: ptr(true),
MarkAsRead: ptr(true),
SortedMetaArray: []MetaArray{
{Key: "key1", Value: []string{"value1"}},
{Key: "key2", Value: []string{"value2"}},
},
CreatedAt: 42,
PollID: 42,
IncludePollDetails: ptr(true),
DedupID: "dedup-id",
ApnsBundleID: "apns-bundle-id",
Sound: "sound",
Volume: 0.5,
},
smrp: SendMessageResponse{
MessageID: 42,
},
},
{
name: "File Message",
smrq: SendMessageRequest{
MessageType: MessageTypeFile,
UserID: "42",
Files: []File{
{URL: "https://example.com/file1"},
{URL: "https://example.com/file2"},
},
},
smrp: SendMessageResponse{
MessageID: 42,
},
},
}

sendMessageResponse := &SendMessageResponse{
MessageID: 42,
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()

client.OnPost("/group_channels/url/messages", sendMessageRequest, &SendMessageResponse{}).Return(sendMessageResponse, nil)
client := newClientMock(t)
message := NewMessage(client)
client.OnPost("/group_channels/url/messages", test.smrq, &SendMessageResponse{}).Return(&test.smrp, nil)

cur, err := message.SendMessage(context.Background(), ChannelTypeGroup, "url", sendMessageRequest)
require.NoError(t, err)
assert.Equal(t, sendMessageResponse, cur)
cur, err := message.SendMessage(context.Background(), ChannelTypeGroup, "url", test.smrq)
require.NoError(t, err)
require.NotNil(t, cur)
assert.Equal(t, test.smrp, *cur)
})
}
}

0 comments on commit 7bbb5ef

Please sign in to comment.