From 2d4dededc925e9870c8844bbc501adf752abaefc Mon Sep 17 00:00:00 2001 From: Yassine Ennebati <4570448+yaziine@users.noreply.github.com> Date: Thu, 6 Oct 2022 17:49:53 +0400 Subject: [PATCH] chore: add channel mutes and user mutes (#148) --- docs/imports.md | 8 ++++- pkg/cmd/chat/imports/validator/items.go | 31 ++++++++++++++++ .../testdata/invalid-channel-mutes.json | 36 +++++++++++++++++++ .../testdata/invalid-user-mutes.json | 20 +++++++++++ .../chat/imports/validator/validator_test.go | 12 +++++++ 5 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 pkg/cmd/chat/imports/validator/testdata/invalid-channel-mutes.json create mode 100644 pkg/cmd/chat/imports/validator/testdata/invalid-user-mutes.json diff --git a/docs/imports.md b/docs/imports.md index 1493d04..ce3bffe 100644 --- a/docs/imports.md +++ b/docs/imports.md @@ -24,6 +24,9 @@ list of JSON objects, representing each item to be imported. "role": "user", "teams": [ "A-team" + ], + "channel_mutes": [ + "messaging:HQ" ] } }, @@ -38,7 +41,10 @@ list of JSON objects, representing each item to be imported. "push_notifications": { "disabled": true, "disabled_reason": "doesn't want to be disturbed" - } + }, + "user_mutes": [ + "hannibal" + ] } }, { diff --git a/pkg/cmd/chat/imports/validator/items.go b/pkg/cmd/chat/imports/validator/items.go index 62798c8..58e65dc 100644 --- a/pkg/cmd/chat/imports/validator/items.go +++ b/pkg/cmd/chat/imports/validator/items.go @@ -99,7 +99,10 @@ type userItem struct { UpdatedAt time.Time `json:"updated_at"` DeletedAt *time.Time `json:"deleted_at"` DeactivatedAt *time.Time `json:"deactivated_at"` + Language string `json:"language"` Teams []string `json:"teams"` + ChannelMutes []string `json:"channel_mutes"` + UserMutes []string `json:"user_mutes"` PushNotifications pushNotification `json:"push_notifications"` Custom extraFields } @@ -138,9 +141,37 @@ func (u *userItem) validateReferences(idx *index) error { if !idx.roleExist(u.Role) { return fmt.Errorf("user.role %q doesn't exist (user %q)", u.Role, u.ID) } + + if len(u.ChannelMutes) > 0 { + for _, ch := range u.ChannelMutes { + typ, id, err := splitChannelCID(ch) + if err != nil { + return err + } + if !idx.channelExist(typ, id) { + return fmt.Errorf("muted channel %q by user %q doesn't exist", ch, u.ID) + } + } + } + if len(u.UserMutes) > 0 { + for _, mutedUserID := range u.UserMutes { + if !idx.userExist(mutedUserID) { + return fmt.Errorf("muted user %q by user %q doesn't exist", mutedUserID, u.ID) + } + } + } return nil } +// splitChannelCID returns channel type and channel ID from channel CID +func splitChannelCID(cid string) (string, string, error) { + s := strings.Split(cid, ":") + if len(s) != 2 { + return "", "", fmt.Errorf(`channel %q should have the following format "type:id"`, cid) + } + return s[0], s[1], nil +} + type deviceItem struct { ID string `json:"id"` UserID string `json:"user_id"` diff --git a/pkg/cmd/chat/imports/validator/testdata/invalid-channel-mutes.json b/pkg/cmd/chat/imports/validator/testdata/invalid-channel-mutes.json new file mode 100644 index 0000000..037d461 --- /dev/null +++ b/pkg/cmd/chat/imports/validator/testdata/invalid-channel-mutes.json @@ -0,0 +1,36 @@ +[ + { + "type": "user", + "item": { + "id": "user1", + "role": "user", + "channel_mutes": [ + "messaging:123", + "messaging:456" + ] + } + }, + { + "type": "user", + "item": { + "id": "user2", + "role": "user" + } + }, + { + "type": "channel", + "item": { + "id": "abc", + "type": "messaging", + "created_by": "user2" + } + }, + { + "type": "channel", + "item": { + "id": "123", + "type": "messaging", + "created_by": "user2" + } + } +] diff --git a/pkg/cmd/chat/imports/validator/testdata/invalid-user-mutes.json b/pkg/cmd/chat/imports/validator/testdata/invalid-user-mutes.json new file mode 100644 index 0000000..a355e4f --- /dev/null +++ b/pkg/cmd/chat/imports/validator/testdata/invalid-user-mutes.json @@ -0,0 +1,20 @@ +[ + { + "type": "user", + "item": { + "id": "user1", + "role": "user", + "user_mutes": [ + "user2", + "missing_user" + ] + } + }, + { + "type": "user", + "item": { + "id": "user2", + "role": "user" + } + } +] diff --git a/pkg/cmd/chat/imports/validator/validator_test.go b/pkg/cmd/chat/imports/validator/validator_test.go index 6fcfde1..50a77de 100644 --- a/pkg/cmd/chat/imports/validator/validator_test.go +++ b/pkg/cmd/chat/imports/validator/validator_test.go @@ -48,6 +48,18 @@ func TestValidator_Validate(t *testing.T) { errors.New(`validation error: distinct channel: ["userA"] is missing members: ["userA"]. Please include all members as separate member entries`), }, }}, + {name: "Invalid channel mutes", filename: "invalid-channel-mutes.json", want: &Results{ + Stats: map[string]int{"channels": 2, "devices": 0, "members": 0, "messages": 0, "reactions": 0, "users": 2}, + Errors: []error{ + errors.New(`reference error: muted channel "messaging:456" by user "user1" doesn't exist`), + }, + }}, + {name: "Invalid user mutes", filename: "invalid-user-mutes.json", want: &Results{ + Stats: map[string]int{"channels": 0, "devices": 0, "members": 0, "messages": 0, "reactions": 0, "users": 2}, + Errors: []error{ + errors.New(`reference error: muted user "missing_user" by user "user1" doesn't exist`), + }, + }}, {name: "Invalid members", filename: "invalid-members.json", want: &Results{ Stats: map[string]int{"channels": 4, "devices": 0, "members": 5, "messages": 0, "reactions": 0, "users": 3}, Errors: []error{