Skip to content

Commit

Permalink
Channel Mutes & Cleanup Query Options
Browse files Browse the repository at this point in the history
* implement channel mute
* cleanup query options

Co-Authored-By: Tabitha <[email protected]>
Co-authored-by: Marcelo Pires <[email protected]>
  • Loading branch information
keyneston and thesyncim authored Apr 30, 2020
1 parent 0da60f7 commit eae21db
Show file tree
Hide file tree
Showing 6 changed files with 1,344 additions and 918 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ import (
stream "github.com/GetStream/stream-chat-go/v2"
)

var APIKey = os.Getenv("STREAM_API_KEY")
var APISecret = os.Getenv("STREAM_API_SECRET")
var APIKey = os.Getenv("STREAM_CHAT_API_KEY")
var APISecret = os.Getenv("STREAM_CHAT_API_SECRET")
var userID = "" // your server user id

func main() {
Expand Down
35 changes: 35 additions & 0 deletions channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,41 @@ func (ch *Channel) RejectInvite(userID string, message *Message) error {
return ch.client.makeRequest(http.MethodPost, p, nil, data, nil)
}

func (ch *Channel) Mute(userID string, expiration *time.Duration) (*ChannelMuteResponse, error) {
if userID == "" {
return nil, errors.New("user ID must be not empty")
}

data := map[string]interface{}{
"user_id": userID,
"channel_cid": ch.CID,
}
if expiration != nil {
data["expiration"] = int(expiration.Seconds())
}

mute := &ChannelMuteResponse{}
err := ch.client.makeRequest(http.MethodPost, "moderation/mute/channel", nil, data, mute)
if err != nil {
return nil, err
}

return mute, nil
}

func (ch *Channel) Unmute(userID string) error {
if userID == "" {
return errors.New("user ID must be not empty")
}

data := map[string]interface{}{
"user_id": userID,
"channel_cid": ch.CID,
}

return ch.client.makeRequest(http.MethodPost, "moderation/unmute/channel", nil, data, nil)
}

//nolint: godox
// todo: cleanup this
func (ch *Channel) refresh() error {
Expand Down
50 changes: 50 additions & 0 deletions channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,56 @@ func TestChannel_RejectInvite(t *testing.T) {
require.NoError(t, err, "reject invite")
}

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

_, err := c.UpdateUsers(testUsers...)
mustNoError(t, err, "update users")

members := make([]string, 0, len(testUsers))
for i := range testUsers {
members = append(members, testUsers[i].ID)
}

ch, err := c.CreateChannel("messaging", randomString(12), serverUser.ID, map[string]interface{}{
"members": members,
})
require.NoError(t, err, "create channel")

// mute the channel
mute, err := ch.Mute(members[0], nil)
require.NoError(t, err, "mute channel")

require.Equal(t, ch.CID, mute.ChannelMute.Channel.CID)
require.Equal(t, members[0], mute.ChannelMute.User.ID)
// query for muted the channel
channels, err := c.QueryChannels(&QueryOption{
UserID: members[0],
Filter: map[string]interface{}{
"muted": true,
"cid": ch.CID,
},
})
require.NoError(t, err, "query muted channel")
require.Len(t, channels, 1)
require.Equal(t, channels[0].CID, ch.CID)

// unmute the channel
err = ch.Unmute(members[0])
require.NoError(t, err, "mute channel")

// query for unmuted the channel should return 1 results
channels, err = c.QueryChannels(&QueryOption{
UserID: members[0],
Filter: map[string]interface{}{
"muted": false,
"cid": ch.CID,
},
})
require.NoError(t, err, "query muted channel")
require.Len(t, channels, 1)
}

func ExampleChannel_Update() {
// https://getstream.io/chat/docs/channel_permissions/?language=python
client := &Client{}
Expand Down
46 changes: 25 additions & 21 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import (

type QueryOption struct {
// https://getstream.io/chat/docs/#query_syntax
Filter map[string]interface{} `json:"-"`
Filter map[string]interface{} `json:"filter_conditions,omitempty"`
Sort []*SortOption `json:"sort,omitempty"`

Limit int `json:"limit,omitempty"` // pagination option: limit number of results
Offset int `json:"offset,omitempty"` // pagination option: offset to return items from
UserID string `json:"user_id,omitempty"`
Limit int `json:"limit,omitempty"` // pagination option: limit number of results
Offset int `json:"offset,omitempty"` // pagination option: offset to return items from
}

// UnmarshalUnknown implements the `easyjson.UnknownsUnmarshaler` interface.
Expand Down Expand Up @@ -46,9 +48,17 @@ type SortOption struct {
Direction int `json:"direction"` // [-1, 1]
}

type queryUsersRequest struct {
FilterConditions *QueryOption `json:"filter_conditions,omitempty"`
Sort []*SortOption `json:"sort,omitempty"`
type queryRequest struct {
Watch bool `json:"watch"`
State bool `json:"state"`
Presence bool `json:"presence"`

UserID string `json:"user_id,omitempty"`
Limit int `json:"limit,omitempty"`
Offset int `json:"offset,omitempty"`

FilterConditions map[string]interface{} `json:"filter_conditions,omitempty"`
Sort []*SortOption `json:"sort,omitempty"`
}

type queryUsersResponse struct {
Expand All @@ -58,12 +68,12 @@ type queryUsersResponse struct {
// QueryUsers returns list of users that match QueryOption.
// If any number of SortOption are set, result will be sorted by field and direction in oder of sort options.
func (c *Client) QueryUsers(q *QueryOption, sort ...*SortOption) ([]*User, error) {
qp := queryUsersRequest{
FilterConditions: q,
qp := queryRequest{
FilterConditions: q.Filter,
Sort: sort,
}

data, err := easyjson.Marshal(&qp)
data, err := json.Marshal(&qp)
if err != nil {
return nil, err
}
Expand All @@ -77,15 +87,6 @@ func (c *Client) QueryUsers(q *QueryOption, sort ...*SortOption) ([]*User, error
return resp.Users, err
}

type queryChannelRequest struct {
Watch bool `json:"watch"`
State bool `json:"state"`
Presence bool `json:"presence"`

FilterConditions *QueryOption `json:"filter_conditions,omitempty"`
Sort []*SortOption `json:"sort,omitempty"`
}

type queryChannelResponse struct {
Channels []queryChannelResponseData `json:"channels"`
}
Expand All @@ -100,13 +101,16 @@ type queryChannelResponseData struct {
// QueryChannels returns list of channels with members and messages, that match QueryOption.
// If any number of SortOption are set, result will be sorted by field and direction in oder of sort options.
func (c *Client) QueryChannels(q *QueryOption, sort ...*SortOption) ([]*Channel, error) {
qp := queryChannelRequest{
qp := queryRequest{
State: true,
FilterConditions: q,
FilterConditions: q.Filter,
Sort: sort,
UserID: q.UserID,
Limit: q.Limit,
Offset: q.Offset,
}

data, err := easyjson.Marshal(&qp)
data, err := json.Marshal(&qp)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit eae21db

Please sign in to comment.