Skip to content

Commit 3943c6f

Browse files
authored
Merge pull request #26 from GetStream/CHAT-791_system_messages
system message on channel operations
2 parents 9f08049 + 322558d commit 3943c6f

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
@@ -385,6 +395,44 @@ func (ch *Channel) DeleteImage(location string) error {
385395
return ch.client.makeRequest(http.MethodDelete, p, params, nil, nil)
386396
}
387397

398+
func (ch *Channel) AcceptInvite(userID string, message *Message) error {
399+
if userID == "" {
400+
return errors.New("user ID must be not empty")
401+
}
402+
403+
data := map[string]interface{}{
404+
"accept_invite": true,
405+
"user_id": userID,
406+
}
407+
408+
if message != nil {
409+
data["message"] = message
410+
}
411+
412+
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID))
413+
414+
return ch.client.makeRequest(http.MethodPost, p, nil, data, nil)
415+
}
416+
417+
func (ch *Channel) RejectInvite(userID string, message *Message) error {
418+
if userID == "" {
419+
return errors.New("user ID must be not empty")
420+
}
421+
422+
data := map[string]interface{}{
423+
"reject_invite": true,
424+
"user_id": userID,
425+
}
426+
427+
if message != nil {
428+
data["message"] = message
429+
}
430+
431+
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID))
432+
433+
return ch.client.makeRequest(http.MethodPost, p, nil, data, nil)
434+
}
435+
388436
// todo: cleanup this
389437
func (ch *Channel) refresh() error {
390438
options := map[string]interface{}{

channel_test.go

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77

88
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
910
)
1011

1112
func TestClient_CreateChannel(t *testing.T) {
@@ -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
@@ -207,7 +211,10 @@ func TestChannel_RemoveMembers(t *testing.T) {
207211
}()
208212

209213
user := randomUser()
210-
err := ch.RemoveMembers(user.ID)
214+
err := ch.RemoveMembers(
215+
[]string{user.ID},
216+
&Message{Text: "some members", User: &User{ID: user.ID}},
217+
)
211218

212219
mustNoError(t, err, "remove members")
213220

@@ -270,7 +277,12 @@ func TestChannel_Truncate(t *testing.T) {
270277
}
271278

272279
func TestChannel_Update(t *testing.T) {
280+
c := initClient(t)
281+
ch := initChannel(t, c)
273282

283+
err := ch.Update(map[string]interface{}{"color": "blue"},
284+
&Message{Text: "color is blue", User: &User{ID: testUsers[0].ID}})
285+
require.NoError(t, err)
274286
}
275287

276288
func TestChannel_AddModerators(t *testing.T) {
@@ -353,3 +365,45 @@ func TestChannel_SendImage(t *testing.T) {
353365
}
354366
})
355367
}
368+
369+
func TestChannel_AcceptInvite(t *testing.T) {
370+
c := initClient(t)
371+
372+
_, err := c.UpdateUsers(testUsers...)
373+
mustNoError(t, err, "update users")
374+
375+
members := make([]string, 0, len(testUsers))
376+
for i := range testUsers {
377+
members = append(members, testUsers[i].ID)
378+
}
379+
380+
ch, err := c.CreateChannel("team", randomString(12), serverUser.ID, map[string]interface{}{
381+
"members": members,
382+
"invites": []string{members[0]},
383+
})
384+
385+
require.NoError(t, err, "create channel")
386+
err = ch.AcceptInvite(members[0], &Message{Text: "accepted", User: &User{ID: members[0]}})
387+
require.NoError(t, err, "accept invite")
388+
}
389+
390+
func TestChannel_RejectInvite(t *testing.T) {
391+
c := initClient(t)
392+
393+
_, err := c.UpdateUsers(testUsers...)
394+
mustNoError(t, err, "update users")
395+
396+
members := make([]string, 0, len(testUsers))
397+
for i := range testUsers {
398+
members = append(members, testUsers[i].ID)
399+
}
400+
401+
ch, err := c.CreateChannel("team", randomString(12), serverUser.ID, map[string]interface{}{
402+
"members": members,
403+
"invites": []string{members[0]},
404+
})
405+
406+
require.NoError(t, err, "create channel")
407+
err = ch.RejectInvite(members[0], &Message{Text: "rejected", User: &User{ID: members[0]}})
408+
require.NoError(t, err, "reject invite")
409+
}

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,16 +73,16 @@ 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
@@ -91,7 +91,8 @@ type StreamChannel interface {
9191
SendImage(request SendFileRequest) (url string, err error)
9292
DeleteFile(location string) error
9393
DeleteImage(location string) error
94-
94+
AcceptInvite(userID string, message *Message) error
95+
RejectInvite(userID string, message *Message) error
9596
// event.go
9697
SendEvent(event *Event, userID string) error
9798

0 commit comments

Comments
 (0)