Skip to content

Commit

Permalink
chore: add channel mutes and user mutes (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaziine authored Oct 6, 2022
1 parent b8a102b commit 2d4dede
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 1 deletion.
8 changes: 7 additions & 1 deletion docs/imports.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ list of JSON objects, representing each item to be imported.
"role": "user",
"teams": [
"A-team"
],
"channel_mutes": [
"messaging:HQ"
]
}
},
Expand All @@ -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"
]
}
},
{
Expand Down
31 changes: 31 additions & 0 deletions pkg/cmd/chat/imports/validator/items.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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"`
Expand Down
36 changes: 36 additions & 0 deletions pkg/cmd/chat/imports/validator/testdata/invalid-channel-mutes.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
]
20 changes: 20 additions & 0 deletions pkg/cmd/chat/imports/validator/testdata/invalid-user-mutes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[
{
"type": "user",
"item": {
"id": "user1",
"role": "user",
"user_mutes": [
"user2",
"missing_user"
]
}
},
{
"type": "user",
"item": {
"id": "user2",
"role": "user"
}
}
]
12 changes: 12 additions & 0 deletions pkg/cmd/chat/imports/validator/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 2d4dede

Please sign in to comment.