From 22f8f3addf6905a4de838247f4f0bb7a137280b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guyon=20Mor=C3=A9e?= Date: Wed, 29 Jul 2020 10:42:19 +0200 Subject: [PATCH] User mute expiration (#86) Added options to MuteUser and MuteUsers to support the recently added timeout option for Mute expiration --- query.go | 3 --- stream_chat.go | 4 ++-- stream_chat_easyjson.go | 21 ++++++++++++++++ user.go | 37 ++++++++++++++-------------- user_test.go | 53 ++++++++++++++++++++++++++++++++--------- 5 files changed, 84 insertions(+), 34 deletions(-) diff --git a/query.go b/query.go index 753b1bb..fc4dc6a 100644 --- a/query.go +++ b/query.go @@ -2,7 +2,6 @@ package stream_chat // nolint: golint import ( "encoding/json" - "fmt" "net/http" "net/url" "strings" @@ -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)) diff --git a/stream_chat.go b/stream_chat.go index 3769a7a..4ecea50 100644 --- a/stream_chat.go +++ b/stream_chat.go @@ -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 diff --git a/stream_chat_easyjson.go b/stream_chat_easyjson.go index b52d26d..202e981 100644 --- a/stream_chat_easyjson.go +++ b/stream_chat_easyjson.go @@ -4639,6 +4639,18 @@ func easyjson458e82b7DecodeGithubComGetStreamStreamChatGoV237(in *jlexer.Lexer, if data := in.Raw(); in.Ok() { in.AddError((out.UpdatedAt).UnmarshalJSON(data)) } + case "expires": + if in.IsNull() { + in.Skip() + out.Expires = nil + } else { + if out.Expires == nil { + out.Expires = new(time.Time) + } + if data := in.Raw(); in.Ok() { + in.AddError((*out.Expires).UnmarshalJSON(data)) + } + } default: in.SkipRecursive() } @@ -4673,6 +4685,15 @@ func easyjson458e82b7EncodeGithubComGetStreamStreamChatGoV237(out *jwriter.Write out.RawString(prefix) out.Raw((in.UpdatedAt).MarshalJSON()) } + { + const prefix string = ",\"expires\":" + out.RawString(prefix) + if in.Expires == nil { + out.RawString("null") + } else { + out.Raw((*in.Expires).MarshalJSON()) + } + } out.RawByte('}') } diff --git a/user.go b/user.go index 8e730e2..dd2fbbe 100644 --- a/user.go +++ b/user.go @@ -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. @@ -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. diff --git a/user_test.go b/user_test.go index 35ac214..8c1a781 100644 --- a/user_test.go +++ b/user_test.go @@ -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) {