-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package message | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
type TextMessage struct { | ||
// UserID specifies the user ID of the sender - required. | ||
UserID string `json:"user_id"` | ||
|
||
// MessageType specifies the type of the message - required. | ||
MessageType MessageType `json:"message_type"` | ||
|
||
// Message specifies the content of the message - required. | ||
Message string `json:"message,omitempty"` | ||
|
||
// Timestamp specifies the time when the message was sent in Unix milliseconds format - required. | ||
Timestamp int64 `json:"timestamp,omitempty"` | ||
|
||
// CustomType specifies a custom message type used for message grouping. The | ||
// length is limited to 128 characters - optional. | ||
CustomType string `json:"custom_type,omitempty"` | ||
|
||
// MentionUserIDs specifies an array of IDs of the users to mention in the | ||
// message. This property is used only when mention_type is users - optional. | ||
MentionUserIDs []string `json:"mentioned_user_ids,omitempty"` | ||
Check failure on line 28 in pkg/message/migrate_messages.go GitHub Actions / analyze (go)
Check failure on line 28 in pkg/message/migrate_messages.go GitHub Actions / analyze (go)
Check failure on line 28 in pkg/message/migrate_messages.go GitHub Actions / ci (ubuntu-latest)
|
||
|
||
// Data specifies additional message information. This property serves as a | ||
// container for a long text of any type of characters which can also be a | ||
// JSON-formatted string like {"font-size": "24px"} - optional. | ||
Data string `json:"data,omitempty"` | ||
|
||
// DedupID specifies a unique ID for the message created by another system. | ||
// In general, this property is used to prevent the same message data from | ||
// getting inserted when migrating messages from another system to the | ||
// Sendbird server. If specified, the server performs a duplicate check using | ||
// the property value - optional. | ||
DedupID string `json:"dedup_id,omitempty"` | ||
} | ||
|
||
// MigrateMessagesRequest is the request to migrate messages to a channel. | ||
type MigrateMessagesRequest struct { | ||
Messages []TextMessage `json:"messages"` | ||
} | ||
|
||
func (smr *MigrateMessagesRequest) Validate() error { | ||
if len(smr.Messages) == 0 { | ||
return errors.New("messages cannot be empty") | ||
} | ||
for _, message := range smr.Messages { | ||
Check failure on line 52 in pkg/message/migrate_messages.go GitHub Actions / analyze (go)
Check failure on line 52 in pkg/message/migrate_messages.go GitHub Actions / analyze (go)
Check failure on line 52 in pkg/message/migrate_messages.go GitHub Actions / ci (ubuntu-latest)
|
||
if message.UserID == "" { | ||
return errors.New("user_id cannot be empty") | ||
} | ||
if message.MessageType == "" { | ||
Check failure on line 56 in pkg/message/migrate_messages.go GitHub Actions / analyze (go)
Check failure on line 56 in pkg/message/migrate_messages.go GitHub Actions / analyze (go)
Check failure on line 56 in pkg/message/migrate_messages.go GitHub Actions / ci (ubuntu-latest)
|
||
return errors.New("message_type cannot be empty") | ||
} | ||
if message.Message == "" { | ||
Check failure on line 59 in pkg/message/migrate_messages.go GitHub Actions / analyze (go)
Check failure on line 59 in pkg/message/migrate_messages.go GitHub Actions / ci (ubuntu-latest)
|
||
return errors.New("message cannot be empty") | ||
} | ||
if message.Timestamp == 0 { | ||
Check failure on line 62 in pkg/message/migrate_messages.go GitHub Actions / analyze (go)
Check failure on line 62 in pkg/message/migrate_messages.go GitHub Actions / ci (ubuntu-latest)
|
||
return errors.New("timestamp cannot be empty") | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// MigrateMessages migrates messages to a channel. | ||
// See https://sendbird.com/docs/chat/platform-api/v3/message/migration/migrate-messages | ||
func (m *message) MigrateMessages(ctx context.Context, channelURL string, migrateMessagesRequest MigrateMessagesRequest) error { | ||
if err := migrateMessagesRequest.Validate(); err != nil { | ||
return fmt.Errorf("failed to validate migrate messages request: %w", err) | ||
} | ||
|
||
path := fmt.Sprintf("/migration/%s", channelURL) | ||
Check failure on line 77 in pkg/message/migrate_messages.go GitHub Actions / analyze (go)
Check failure on line 77 in pkg/message/migrate_messages.go GitHub Actions / analyze (go)
Check failure on line 77 in pkg/message/migrate_messages.go GitHub Actions / ci (ubuntu-latest)
|
||
|
||
_, err := m.client.Post(ctx, path, migrateMessagesRequest, nil) | ||
if err != nil { | ||
return fmt.Errorf("failed to migrate messages: %w", err) | ||
} | ||
|
||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package message | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/tomMoulard/sendbird-go/pkg/client" | ||
) | ||
|
||
func TestValidateMMR(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
request MigrateMessagesRequest | ||
assertErr assert.ErrorAssertionFunc | ||
}{ | ||
{ | ||
name: "invalid request - empty messages", | ||
request: MigrateMessagesRequest{ | ||
Messages: []TextMessage{}, | ||
}, | ||
assertErr: assert.Error, | ||
}, | ||
{ | ||
name: "invalid request - missing user_id", | ||
request: MigrateMessagesRequest{ | ||
Messages: []TextMessage{ | ||
{ | ||
MessageType: MessageTypeText, | ||
Message: "Hello, World!", | ||
Timestamp: 1609459200000, | ||
}, | ||
}, | ||
}, | ||
assertErr: assert.Error, | ||
}, | ||
{ | ||
name: "invalid request - missing message_type", | ||
request: MigrateMessagesRequest{ | ||
Messages: []TextMessage{ | ||
{ | ||
UserID: "42", | ||
Message: "Hello, World!", | ||
Timestamp: 1609459200000, | ||
}, | ||
}, | ||
}, | ||
assertErr: assert.Error, | ||
}, | ||
{ | ||
name: "invalid request - missing message", | ||
request: MigrateMessagesRequest{ | ||
Messages: []TextMessage{ | ||
{ | ||
UserID: "42", | ||
MessageType: MessageTypeText, | ||
Timestamp: 1609459200000, | ||
}, | ||
}, | ||
}, | ||
assertErr: assert.Error, | ||
}, | ||
{ | ||
name: "invalid request - missing timestamp", | ||
request: MigrateMessagesRequest{ | ||
Messages: []TextMessage{ | ||
{ | ||
UserID: "42", | ||
MessageType: MessageTypeText, | ||
Message: "Hello, World!", | ||
}, | ||
}, | ||
}, | ||
assertErr: assert.Error, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
err := test.request.Validate() | ||
test.assertErr(t, err) | ||
}) | ||
} | ||
} | ||
|
||
func TestMigrateMessages(t *testing.T) { | ||
t.Parallel() | ||
|
||
validRequest := MigrateMessagesRequest{ | ||
Messages: []TextMessage{ | ||
{ | ||
UserID: "42", | ||
MessageType: MessageTypeText, | ||
Message: "Hello, World!", | ||
Timestamp: 1609459200000, | ||
CustomType: "greeting", | ||
Data: `{ "emotion": "happy" }`, | ||
DedupID: "unique123", | ||
}, | ||
}, | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
request MigrateMessagesRequest | ||
assertErr assert.ErrorAssertionFunc | ||
}{ | ||
{ | ||
name: "valid request", | ||
request: validRequest, | ||
assertErr: assert.NoError, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
client := client.NewClientMock(t). | ||
OnPost("/migration/channel_url", test.request, nil).TypedReturns(nil, nil).Once(). | ||
Parent | ||
message := NewMessage(client) | ||
|
||
err := message.MigrateMessages(context.Background(), "channel_url", test.request) | ||
test.assertErr(t, err) | ||
}) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.