Skip to content

Commit 2d4dede

Browse files
authored
chore: add channel mutes and user mutes (#148)
1 parent b8a102b commit 2d4dede

File tree

5 files changed

+106
-1
lines changed

5 files changed

+106
-1
lines changed

docs/imports.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ list of JSON objects, representing each item to be imported.
2424
"role": "user",
2525
"teams": [
2626
"A-team"
27+
],
28+
"channel_mutes": [
29+
"messaging:HQ"
2730
]
2831
}
2932
},
@@ -38,7 +41,10 @@ list of JSON objects, representing each item to be imported.
3841
"push_notifications": {
3942
"disabled": true,
4043
"disabled_reason": "doesn't want to be disturbed"
41-
}
44+
},
45+
"user_mutes": [
46+
"hannibal"
47+
]
4248
}
4349
},
4450
{

pkg/cmd/chat/imports/validator/items.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ type userItem struct {
9999
UpdatedAt time.Time `json:"updated_at"`
100100
DeletedAt *time.Time `json:"deleted_at"`
101101
DeactivatedAt *time.Time `json:"deactivated_at"`
102+
Language string `json:"language"`
102103
Teams []string `json:"teams"`
104+
ChannelMutes []string `json:"channel_mutes"`
105+
UserMutes []string `json:"user_mutes"`
103106
PushNotifications pushNotification `json:"push_notifications"`
104107
Custom extraFields
105108
}
@@ -138,9 +141,37 @@ func (u *userItem) validateReferences(idx *index) error {
138141
if !idx.roleExist(u.Role) {
139142
return fmt.Errorf("user.role %q doesn't exist (user %q)", u.Role, u.ID)
140143
}
144+
145+
if len(u.ChannelMutes) > 0 {
146+
for _, ch := range u.ChannelMutes {
147+
typ, id, err := splitChannelCID(ch)
148+
if err != nil {
149+
return err
150+
}
151+
if !idx.channelExist(typ, id) {
152+
return fmt.Errorf("muted channel %q by user %q doesn't exist", ch, u.ID)
153+
}
154+
}
155+
}
156+
if len(u.UserMutes) > 0 {
157+
for _, mutedUserID := range u.UserMutes {
158+
if !idx.userExist(mutedUserID) {
159+
return fmt.Errorf("muted user %q by user %q doesn't exist", mutedUserID, u.ID)
160+
}
161+
}
162+
}
141163
return nil
142164
}
143165

166+
// splitChannelCID returns channel type and channel ID from channel CID
167+
func splitChannelCID(cid string) (string, string, error) {
168+
s := strings.Split(cid, ":")
169+
if len(s) != 2 {
170+
return "", "", fmt.Errorf(`channel %q should have the following format "type:id"`, cid)
171+
}
172+
return s[0], s[1], nil
173+
}
174+
144175
type deviceItem struct {
145176
ID string `json:"id"`
146177
UserID string `json:"user_id"`
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[
2+
{
3+
"type": "user",
4+
"item": {
5+
"id": "user1",
6+
"role": "user",
7+
"channel_mutes": [
8+
"messaging:123",
9+
"messaging:456"
10+
]
11+
}
12+
},
13+
{
14+
"type": "user",
15+
"item": {
16+
"id": "user2",
17+
"role": "user"
18+
}
19+
},
20+
{
21+
"type": "channel",
22+
"item": {
23+
"id": "abc",
24+
"type": "messaging",
25+
"created_by": "user2"
26+
}
27+
},
28+
{
29+
"type": "channel",
30+
"item": {
31+
"id": "123",
32+
"type": "messaging",
33+
"created_by": "user2"
34+
}
35+
}
36+
]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[
2+
{
3+
"type": "user",
4+
"item": {
5+
"id": "user1",
6+
"role": "user",
7+
"user_mutes": [
8+
"user2",
9+
"missing_user"
10+
]
11+
}
12+
},
13+
{
14+
"type": "user",
15+
"item": {
16+
"id": "user2",
17+
"role": "user"
18+
}
19+
}
20+
]

pkg/cmd/chat/imports/validator/validator_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ func TestValidator_Validate(t *testing.T) {
4848
errors.New(`validation error: distinct channel: ["userA"] is missing members: ["userA"]. Please include all members as separate member entries`),
4949
},
5050
}},
51+
{name: "Invalid channel mutes", filename: "invalid-channel-mutes.json", want: &Results{
52+
Stats: map[string]int{"channels": 2, "devices": 0, "members": 0, "messages": 0, "reactions": 0, "users": 2},
53+
Errors: []error{
54+
errors.New(`reference error: muted channel "messaging:456" by user "user1" doesn't exist`),
55+
},
56+
}},
57+
{name: "Invalid user mutes", filename: "invalid-user-mutes.json", want: &Results{
58+
Stats: map[string]int{"channels": 0, "devices": 0, "members": 0, "messages": 0, "reactions": 0, "users": 2},
59+
Errors: []error{
60+
errors.New(`reference error: muted user "missing_user" by user "user1" doesn't exist`),
61+
},
62+
}},
5163
{name: "Invalid members", filename: "invalid-members.json", want: &Results{
5264
Stats: map[string]int{"channels": 4, "devices": 0, "members": 5, "messages": 0, "reactions": 0, "users": 3},
5365
Errors: []error{

0 commit comments

Comments
 (0)