Skip to content

Commit dd39bc1

Browse files
committed
add message to several channel operations, fix Message and User json tags
1 parent b81d2ba commit dd39bc1

File tree

6 files changed

+454
-250
lines changed

6 files changed

+454
-250
lines changed

channel.go

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,13 @@ func (ch *Channel) query(options map[string]interface{}, data map[string]interfa
111111
//
112112
// options: the object to update the custom properties of this channel with
113113
// message: optional update message
114-
func (ch *Channel) Update(options map[string]interface{}, message string) error {
114+
func (ch *Channel) Update(options map[string]interface{}, message *Message) error {
115115
payload := map[string]interface{}{
116-
"data": options,
117-
"message": message,
116+
"data": options,
117+
}
118+
119+
if message != nil {
120+
payload["message"] = message
118121
}
119122

120123
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID))
@@ -137,7 +140,7 @@ func (ch *Channel) Truncate() error {
137140
}
138141

139142
// AddMembers adds members with given user IDs to the channel
140-
func (ch *Channel) AddMembers(userIDs ...string) error {
143+
func (ch *Channel) AddMembers(userIDs []string, message *Message) error {
141144
if len(userIDs) == 0 {
142145
return errors.New("user IDs are empty")
143146
}
@@ -146,13 +149,17 @@ func (ch *Channel) AddMembers(userIDs ...string) error {
146149
"add_members": userIDs,
147150
}
148151

152+
if message != nil {
153+
data["message"] = message
154+
}
155+
149156
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID))
150157

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

154161
// RemoveMembers deletes members with given IDs from the channel
155-
func (ch *Channel) RemoveMembers(userIDs ...string) error {
162+
func (ch *Channel) RemoveMembers(userIDs []string, message *Message) error {
156163
if len(userIDs) == 0 {
157164
return errors.New("user IDs are empty")
158165
}
@@ -161,6 +168,9 @@ func (ch *Channel) RemoveMembers(userIDs ...string) error {
161168
"remove_members": userIDs,
162169
}
163170

171+
if message != nil {
172+
data["message"] = message
173+
}
164174
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID))
165175

166176
var resp queryResponse
@@ -370,6 +380,44 @@ func (ch *Channel) DeleteImage(location string) error {
370380
return ch.client.makeRequest(http.MethodDelete, p, params, nil, nil)
371381
}
372382

383+
func (ch *Channel) AcceptInvite(userID string, message *Message) error {
384+
if userID == "" {
385+
return errors.New("user ID must be not empty")
386+
}
387+
388+
data := map[string]interface{}{
389+
"accept_invite": true,
390+
"user_id": userID,
391+
}
392+
393+
if message != nil {
394+
data["message"] = message
395+
}
396+
397+
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID))
398+
399+
return ch.client.makeRequest(http.MethodPost, p, nil, data, nil)
400+
}
401+
402+
func (ch *Channel) RejectInvite(userID string, message *Message) error {
403+
if userID == "" {
404+
return errors.New("user ID must be not empty")
405+
}
406+
407+
data := map[string]interface{}{
408+
"reject_invite": true,
409+
"user_id": userID,
410+
}
411+
412+
if message != nil {
413+
data["message"] = message
414+
}
415+
416+
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID))
417+
418+
return ch.client.makeRequest(http.MethodPost, p, nil, data, nil)
419+
}
420+
373421
// todo: cleanup this
374422
func (ch *Channel) refresh() error {
375423
options := map[string]interface{}{

channel_test.go

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package stream_chat // nolint: golint
22

33
import (
4+
"github.com/stretchr/testify/require"
45
"os"
56
"path"
67
"testing"
@@ -73,7 +74,10 @@ func TestChannel_AddMembers(t *testing.T) {
7374

7475
user := randomUser()
7576

76-
err = ch.AddMembers(user.ID)
77+
err = ch.AddMembers(
78+
[]string{user.ID},
79+
&Message{Text: "some members", User: &User{ID: user.ID}},
80+
)
7781
mustNoError(t, err, "add members")
7882

7983
// refresh channel state
@@ -180,7 +184,10 @@ func TestChannel_RemoveMembers(t *testing.T) {
180184
}()
181185

182186
user := randomUser()
183-
err := ch.RemoveMembers(user.ID)
187+
err := ch.RemoveMembers(
188+
[]string{user.ID},
189+
&Message{Text: "some members", User: &User{ID: user.ID}},
190+
)
184191

185192
mustNoError(t, err, "remove members")
186193

@@ -243,7 +250,12 @@ func TestChannel_Truncate(t *testing.T) {
243250
}
244251

245252
func TestChannel_Update(t *testing.T) {
253+
c := initClient(t)
254+
ch := initChannel(t, c)
246255

256+
err := ch.Update(map[string]interface{}{"color": "blue"},
257+
&Message{Text: "color is blue", User: &User{ID: testUsers[0].ID}})
258+
require.NoError(t, err)
247259
}
248260

249261
func TestChannel_AddModerators(t *testing.T) {
@@ -326,3 +338,45 @@ func TestChannel_SendImage(t *testing.T) {
326338
}
327339
})
328340
}
341+
342+
func TestChannel_AcceptInvite(t *testing.T) {
343+
c := initClient(t)
344+
345+
_, err := c.UpdateUsers(testUsers...)
346+
mustNoError(t, err, "update users")
347+
348+
members := make([]string, 0, len(testUsers))
349+
for i := range testUsers {
350+
members = append(members, testUsers[i].ID)
351+
}
352+
353+
ch, err := c.CreateChannel("team", randomString(12), serverUser.ID, map[string]interface{}{
354+
"members": members,
355+
"invites": []string{members[0]},
356+
})
357+
358+
require.NoError(t, err, "create channel")
359+
err = ch.AcceptInvite(members[0], &Message{Text: "accepted", User: &User{ID: members[0]}})
360+
require.NoError(t, err, "accept invite")
361+
}
362+
363+
func TestChannel_RejectInvite(t *testing.T) {
364+
c := initClient(t)
365+
366+
_, err := c.UpdateUsers(testUsers...)
367+
mustNoError(t, err, "update users")
368+
369+
members := make([]string, 0, len(testUsers))
370+
for i := range testUsers {
371+
members = append(members, testUsers[i].ID)
372+
}
373+
374+
ch, err := c.CreateChannel("team", randomString(12), serverUser.ID, map[string]interface{}{
375+
"members": members,
376+
"invites": []string{members[0]},
377+
})
378+
379+
require.NoError(t, err, "create channel")
380+
err = ch.RejectInvite(members[0], &Message{Text: "rejected", User: &User{ID: members[0]}})
381+
require.NoError(t, err, "reject invite")
382+
}

message.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type Message struct {
2424
Text string `json:"text"`
2525
HTML string `json:"html"`
2626

27-
Type MessageType `json:"type"` // one of MessageType* constants
27+
Type MessageType `json:"type,omitempty"` // one of MessageType* constants
2828

2929
User *User `json:"user"`
3030
Attachments []*Attachment `json:"attachments"`
@@ -35,12 +35,12 @@ type Message struct {
3535
ParentID string `json:"parent_id"` // id of parent message if it's reply
3636
ShowInChannel bool `json:"show_in_channel"` // show reply message also in channel
3737

38-
ReplyCount int `json:"reply_count"`
38+
ReplyCount int `json:"reply_count,omitempty"`
3939

4040
MentionedUsers []*User `json:"mentioned_users"`
4141

42-
CreatedAt time.Time `json:"created_at"`
43-
UpdatedAt time.Time `json:"updated_at"`
42+
CreatedAt *time.Time `json:"created_at,omitempty"`
43+
UpdatedAt *time.Time `json:"updated_at,omitempty"`
4444

4545
// any other fields the user wants to attach a message
4646
ExtraData map[string]interface{}

stream_chat.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,24 +73,25 @@ type StreamClient interface {
7373
// StreamChannel is a channel of communication
7474
type StreamChannel interface {
7575
// channel.go
76-
AddMembers(userIDs ...string) error
76+
AddMembers(userIDs []string, message *Message) error
7777
AddModerators(userIDs ...string) error
7878
BanUser(targetID string, userID string, options map[string]interface{}) error
7979
Delete() error
8080
DemoteModerators(userIDs ...string) error
8181
MarkRead(userID string, options map[string]interface{}) error
82-
RemoveMembers(userIDs ...string) error
82+
RemoveMembers(userIDs []string, message *Message) error
8383
Truncate() error
8484
UnBanUser(targetID string, options map[string]string) error
85-
Update(options map[string]interface{}, message string) error
85+
Update(options map[string]interface{}, message *Message) error
8686
Query(data map[string]interface{}) error
8787
Show(userID string) error
8888
Hide(userID string) error
8989
SendFile(request SendFileRequest) (url string, err error)
9090
SendImage(request SendFileRequest) (url string, err error)
9191
DeleteFile(location string) error
9292
DeleteImage(location string) error
93-
93+
AcceptInvite(userID string, message *Message) error
94+
RejectInvite(userID string, message *Message) error
9495
// event.go
9596
SendEvent(event *Event, userID string) error
9697

0 commit comments

Comments
 (0)