Skip to content

Commit

Permalink
[CHAT-1159] import channel messages (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
thesyncim authored Sep 24, 2020
1 parent 0d3b322 commit c224872
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
26 changes: 25 additions & 1 deletion channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ func (q queryResponse) updateChannel(ch *Channel) {
}
}

type ImportChannelMessagesResponse struct {
Messages []Message `json:"messages"`
}

// query makes request to channel api and updates channel internal state.
func (ch *Channel) query(options, data map[string]interface{}) (err error) {
payload := map[string]interface{}{
Expand Down Expand Up @@ -212,6 +216,27 @@ func (ch *Channel) RemoveMembers(userIDs []string, message *Message) error {
return nil
}

// ImportMessages is a batch endpoint for inserting multiple messages.
func (ch *Channel) ImportMessages(messages ...*Message) (*ImportChannelMessagesResponse, error) {
for _, m := range messages {
if m.User == nil || m.User.ID == "" {
return nil, errors.New("message.user is a required field")
}
}

p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID), "import")

var resp ImportChannelMessagesResponse
err := ch.client.makeRequest(http.MethodPost, p, nil, map[string]interface{}{
"messages": messages,
}, &resp)
if err != nil {
return nil, err
}

return &resp, nil
}

type queryMembersResponse struct {
Members []*ChannelMember `json:"members"`
}
Expand Down Expand Up @@ -310,7 +335,6 @@ func (ch *Channel) inviteMembers(userIDs []string, msg *Message) error {
}

p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID))

return ch.client.makeRequest(http.MethodPost, p, nil, data, nil)
}

Expand Down
39 changes: 39 additions & 0 deletions channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -90,6 +91,44 @@ func TestChannel_AddMembers(t *testing.T) {
assert.Equal(t, user.ID, ch.Members[0].User.ID, "members contain user id")
}

func TestChannel_ImportChannelMessages(t *testing.T) {
c := initClient(t)

chanID := randomString(12)

ch, err := c.CreateChannel("messaging", chanID, serverUser.ID, nil)
require.NoError(t, err, "create channel")
defer func() {
_ = ch.Delete()
}()

assert.Empty(t, ch.Members, "members are empty")

user := randomUser()

t0 := time.Unix(0, 0).UTC()
t1 := time.Unix(1, 0).UTC()
resp, err := ch.ImportMessages(
&Message{
Text: "hi 1",
User: user,
CreatedAt: &t1,
}, &Message{
Text: "hi 0",
User: user,
CreatedAt: &t0,
})
require.NoError(t, err, "Send bulk messages")
require.Len(t, resp.Messages, 2)
require.Equal(t, &t0, resp.Messages[0].CreatedAt)
require.Equal(t, &t1, resp.Messages[1].CreatedAt)

// get the channel and validate last_message_at
ch, err = c.CreateChannel("messaging", chanID, serverUser.ID, nil)
require.NoError(t, err, "create channel")
require.Equal(t, t1, ch.LastMessageAt)
}

func TestChannel_QueryMembers(t *testing.T) {
c := initClient(t)

Expand Down
1 change: 1 addition & 0 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type Message struct {

CreatedAt *time.Time `json:"created_at,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
DeletedAt *time.Time `json:"deleted_at,omitempty"`

ExtraData map[string]interface{} `json:"-"`
}
Expand Down
2 changes: 2 additions & 0 deletions stream_chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,6 @@ type StreamChannel interface {
DeleteReaction(messageID string, reactionType string, userID string) (*Message, error)
GetReactions(messageID string, options map[string][]string) ([]*Reaction, error)
SendReaction(reaction *Reaction, messageID string, userID string) (*Message, error)

ImportMessages(messages ...*Message) (*ImportChannelMessagesResponse, error)
}

0 comments on commit c224872

Please sign in to comment.