Skip to content

Commit 2dda3c4

Browse files
authored
fix: check if message.parent_id exists (#156)
1 parent 8467205 commit 2dda3c4

3 files changed

Lines changed: 16 additions & 1 deletion

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,9 @@ func (m *messageItem) validateReferences(idx *index) error {
519519
if idx.isReply(m.ParentID) {
520520
return errors.New("only one level thread is supported")
521521
}
522+
if !idx.messageExist(m.ParentID) {
523+
return fmt.Errorf("message parent_id %q doesn't exist", m.ParentID)
524+
}
522525
}
523526
channelID, isDistinct := getChannelID(m.ChannelID, m.ChannelMemberIDs)
524527
if !idx.channelExist(m.ChannelType, channelID) {

pkg/cmd/chat/imports/validator/testdata/invalid-messages.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,17 @@
9292
"deleted_at": "2022-02-14T12:34:30+00:00"
9393
}
9494
},
95+
{
96+
"type": "message",
97+
"item": {
98+
"id": "thread",
99+
"channel_type": "messaging",
100+
"channel_id": "channelA",
101+
"user": "userA",
102+
"text": "reply!",
103+
"parent_id": "parentID"
104+
}
105+
},
95106
{
96107
"type": "channel",
97108
"item": {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func TestValidator_Validate(t *testing.T) {
8181
},
8282
}},
8383
{name: "Invalid messages", filename: "invalid-messages.json", want: &Results{
84-
Stats: map[string]int{"channels": 2, "devices": 0, "members": 2, "messages": 0, "reactions": 0, "users": 1},
84+
Stats: map[string]int{"channels": 2, "devices": 0, "members": 2, "messages": 1, "reactions": 0, "users": 1},
8585
Errors: []error{
8686
errors.New(`validation error: message.id max length exceeded (255)`),
8787
errors.New(`validation error: message.channel_type required`),
@@ -93,6 +93,7 @@ func TestValidator_Validate(t *testing.T) {
9393
errors.New(`reference error: channel ":channelA" doesn't exist`),
9494
errors.New(`reference error: distinct channel with type "messaging" and members:[] doesn't exist`),
9595
errors.New(`reference error: user "" doesn't exist (message_id messageA)`),
96+
errors.New(`reference error: message parent_id "parentID" doesn't exist`),
9697
},
9798
}},
9899
{name: "Invalid devices", filename: "invalid-devices.json", want: &Results{

0 commit comments

Comments
 (0)