Skip to content

Commit

Permalink
feat: add MessageType
Browse files Browse the repository at this point in the history
  • Loading branch information
tomMoulard committed Apr 30, 2024
1 parent 162a8d2 commit ab951b2
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 8 deletions.
4 changes: 2 additions & 2 deletions pkg/message/list_messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type ListMessagesRequest struct {
// MessageType specifies a message type to retrieve. Acceptable values are
// MESG, FILE, and ADMM. If not specified, all messages are retrieved.
// Optional.
MessageType string
MessageType MessageType
// CustomTypes specifies a list of one or more custom message types to
// retrieve. The value set to this parameter can serve as a filter as
// follows:
Expand Down Expand Up @@ -163,7 +163,7 @@ func listMessagesRequestToMap(lmr ListMessagesRequest) map[string]string {
}

if lmr.MessageType != "" {
m["message_type"] = lmr.MessageType
m["message_type"] = string(lmr.MessageType)
}

if len(lmr.CustomTypes) > 0 {
Expand Down
23 changes: 20 additions & 3 deletions pkg/message/send_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package message

import (
"context"
"errors"
"fmt"
)

// SendMessageRequest is the request to send a message.
type SendMessageRequest struct {
// MessageType specifies the type of the message. The value of MESG
// represents a text message.
MessageType string `json:"message_type"`
// 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.
Expand Down Expand Up @@ -91,6 +91,19 @@ type SendMessageRequest struct {
Volume float32 `json:"volume,omitempty"`
}

func (smr *SendMessageRequest) Validate() error {
switch {
case smr.MessageType == "":
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")
}

return nil
}

// SendMessageResponse is the response to send a message.
type SendMessageResponse MessageResource

Expand All @@ -100,6 +113,10 @@ type SendMessageResponse MessageResource
// the URL of the channel.
// See https://sendbird.com/docs/chat/platform-api/v3/message/messaging-basics/send-a-message
func (m *message) SendMessage(ctx context.Context, channelType, channelURL string, sendMessageRequest SendMessageRequest) (*SendMessageResponse, error) {
if err := sendMessageRequest.Validate(); err != nil {
return nil, fmt.Errorf("failed to validate send message request: %w", err)
}

path := fmt.Sprintf("/%s/%s/messages", channelType, channelURL)

smr, err := m.client.Post(ctx, path, sendMessageRequest, &SendMessageResponse{})
Expand Down
58 changes: 58 additions & 0 deletions pkg/message/send_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,64 @@ func TestPtr(t *testing.T) {
assert.Equal(t, "foo", *ptr("foo"))
}

func TestValidateSMR(t *testing.T) {
t.Parallel()

tests := []struct {
name string
smr SendMessageRequest
assertErr assert.ErrorAssertionFunc
}{
{
name: "empty",
smr: SendMessageRequest{},
assertErr: assert.Error,
},
{
name: "Without Message Type",
smr: SendMessageRequest{
UserID: "42",
Message: "Hello, World!",
},
assertErr: assert.Error,
},
{
name: "Without user id",
smr: SendMessageRequest{
MessageType: MessageTypeText,
Message: "Hello, World!",
},
assertErr: assert.Error,
},
{
name: "Without message",
smr: SendMessageRequest{
MessageType: MessageTypeText,
UserID: "42",
},
assertErr: assert.Error,
},
{
name: "Valid",
smr: SendMessageRequest{
MessageType: MessageTypeText,
UserID: "42",
Message: "Hello, World!",
},
assertErr: assert.NoError,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()

err := test.smr.Validate()
test.assertErr(t, err)
})
}
}

func TestSendMessage(t *testing.T) {
t.Parallel()

Expand Down
8 changes: 5 additions & 3 deletions pkg/message/types.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package message

type MessageType string

const (
MessageTypeText string = "MESG"
MessageTypeFile string = "FILE"
MessageTypeAdminMessage string = "ADMM"
MessageTypeText MessageType = "MESG"
MessageTypeFile MessageType = "FILE"
MessageTypeAdminMessage MessageType = "ADMM"
)

const (
Expand Down

0 comments on commit ab951b2

Please sign in to comment.