Skip to content

Commit

Permalink
User mute expiration (#86)
Browse files Browse the repository at this point in the history
Added options to MuteUser and MuteUsers to support the recently added timeout option for Mute expiration
  • Loading branch information
gumuz authored Jul 29, 2020
1 parent ae94037 commit 22f8f3a
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 34 deletions.
3 changes: 0 additions & 3 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package stream_chat // nolint: golint

import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -81,8 +80,6 @@ func (c *Client) QueryUsers(q *QueryOption, sorters ...*SortOption) ([]*User, er
return nil, err
}

fmt.Println(string(data))

values := make(url.Values)
values.Set("payload", string(data))

Expand Down
4 changes: 2 additions & 2 deletions stream_chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ type StreamClient interface {
DeleteUser(targetID string, options map[string][]string) error
ExportUser(targetID string, options map[string][]string) (user *User, err error)
FlagUser(targetID string, options map[string]interface{}) error
MuteUser(targetID string, userID string) error
MuteUsers(targetIDs []string, userID string) error
MuteUser(targetID string, userID string, options map[string]interface{}) error
MuteUsers(targetIDs []string, userID string, options map[string]interface{}) error
UnBanUser(targetID string, options map[string]string) error
UnFlagUser(targetID string, options map[string]interface{}) error
UnmuteUser(targetID string, userID string) error
Expand Down
21 changes: 21 additions & 0 deletions stream_chat_easyjson.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 19 additions & 18 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ import (

// Mute represents a user mute.
type Mute struct {
User User `json:"user"`
Target User `json:"target"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
User User `json:"user"`
Target User `json:"target"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Expires *time.Time `json:"expires"`
}

// ChannelMute represents a channel mute.
Expand Down Expand Up @@ -77,39 +78,39 @@ func (u User) MarshalUnknowns(out *jwriter.Writer, first bool) {
// MuteUser creates a mute.
// targetID: the user getting muted.
// userID: the user is muting the target.
func (c *Client) MuteUser(targetID, userID string) error {
func (c *Client) MuteUser(targetID, userID string, options map[string]interface{}) error {
switch {
case targetID == "":
return errors.New("target ID is empty")
case userID == "":
return errors.New("user ID is empty")
case options == nil:
options = map[string]interface{}{}
}

data := map[string]interface{}{
"target_id": targetID,
"user_id": userID,
}
options["target_id"] = targetID
options["user_id"] = userID

return c.makeRequest(http.MethodPost, "moderation/mute", nil, data, nil)
return c.makeRequest(http.MethodPost, "moderation/mute", nil, options, nil)
}

// MuteUsers creates a mute.
// targetID: the user getting muted.
// MuteUsers creates mutes for multiple users.
// targetIDs: the users getting muted.
// userID: the user is muting the target.
func (c *Client) MuteUsers(targetIDs []string, userID string) error {
func (c *Client) MuteUsers(targetIDs []string, userID string, options map[string]interface{}) error {
switch {
case len(targetIDs) == 0:
return errors.New("target IDs are empty")
case userID == "":
return errors.New("user ID is empty")
case options == nil:
options = map[string]interface{}{}
}

data := map[string]interface{}{
"target_ids": targetIDs,
"user_id": userID,
}
options["target_ids"] = targetIDs
options["user_id"] = userID

return c.makeRequest(http.MethodPost, "moderation/mute", nil, data, nil)
return c.makeRequest(http.MethodPost, "moderation/mute", nil, options, nil)
}

// UnmuteUser removes a mute.
Expand Down
53 changes: 42 additions & 11 deletions user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,64 @@ func TestClient_MuteUser(t *testing.T) {
c := initClient(t)
initChannel(t, c)

user := randomUser()
require.NotEmptyf(t, testUsers, "there should be at least 1 test user: %+v", testUsers)
user := testUsers[0]

err := c.MuteUser(user.ID, serverUser.ID)
require.NoError(t, err, "mute user")
err := c.MuteUser(user.ID, serverUser.ID, nil)
require.NoError(t, err, "MuteUser should not return an error")

users, err := c.QueryUsers(&QueryOption{
Filter: map[string]interface{}{
"id": map[string]string{"$eq": serverUser.ID},
}})
require.NoError(t, err, "QueryUsers should not return an error")
require.NotEmptyf(t, users, "QueryUsers should return a user: %+v", users)
require.NotEmptyf(t, users[0].Mutes, "user should have Mutes: %+v", users[0])

require.NoError(t, err, "query users")
mute := users[0].Mutes[0]
assert.NotEmpty(t, mute.User, "mute should have a User")
assert.NotEmpty(t, mute.Target, "mute should have a Target")
assert.Empty(t, mute.Expires, "mute should have no Expires")

assert.Lenf(t, users[0].Mutes, 1, "user mutes exists: %+v", users[0])
// when timeout is given, expiration field should be set on mute
err = c.MuteUser(user.ID, serverUser.ID, map[string]interface{}{"timeout": 60})
require.NoError(t, err, "MuteUser should not return an error")

mute := users[0].Mutes[0]
assert.NotEmpty(t, mute.User, "mute has user")
assert.NotEmpty(t, mute.Target, "mute has target")
users, err = c.QueryUsers(&QueryOption{
Filter: map[string]interface{}{
"id": map[string]string{"$eq": serverUser.ID},
}})
require.NoError(t, err, "QueryUsers should not return an error")
require.NotEmptyf(t, users, "QueryUsers should return a user: %+v", users)
require.NotEmptyf(t, users[0].Mutes, "user should have Mutes: %+v", users[0])

mute = users[0].Mutes[0]
assert.NotEmpty(t, mute.User, "mute should have a User")
assert.NotEmpty(t, mute.Target, "mute should have a Target")
assert.NotEmpty(t, mute.Expires, "mute should have Expires")
}

func TestClient_MuteUsers(t *testing.T) {
c := initClient(t)
initChannel(t, c)

users := []string{randomUser().ID, randomUser().ID}
require.GreaterOrEqualf(t, len(testUsers), 2, "there should be at least 2 test users: %+v", testUsers)
usernames := []string{testUsers[0].ID, testUsers[1].ID}

err := c.MuteUsers(users, serverUser.ID)
require.NoError(t, err, "mute user")
err := c.MuteUsers(usernames, serverUser.ID, map[string]interface{}{"timeout": 60})
require.NoError(t, err, "MuteUsers should not return an error")

users, err := c.QueryUsers(&QueryOption{
Filter: map[string]interface{}{
"id": map[string]string{"$eq": serverUser.ID},
}})
require.NoError(t, err, "QueryUsers should not return an error")
require.NotEmptyf(t, users, "QueryUsers should return a user: %+v", users)
require.NotEmptyf(t, users[0].Mutes, "user should have Mutes: %+v", users[0])

for _, mute := range users[0].Mutes {
assert.NotEmpty(t, mute.Expires, "mute should have Expires")
}
}

func TestClient_UnBanUser(t *testing.T) {
Expand Down

0 comments on commit 22f8f3a

Please sign in to comment.