Fix: Edited messages appear twice in fulltext search (#3363)

As stated in https://github.com/matrix-org/dendrite/issues/3358 the
search response contains both original and edited message.
This PR fixes it by removing of the original message from the fulltext
index after indexing the edit message event.
I also made some cosmetic changes/fixes i found in the code

Signed-off-by: `Alexander Dubovikov <d.lexand@gmail.com>`
This commit is contained in:
Alex 2024-07-27 22:30:17 +02:00 committed by GitHub
parent affb6977e4
commit 9897959731
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 115 additions and 4 deletions

View file

@ -601,9 +601,11 @@ func (s *OutputRoomEventConsumer) writeFTS(ev *rstypes.HeaderedEvent, pduPositio
}
e.SetContentType(ev.Type())
var relatesTo gjson.Result
switch ev.Type() {
case "m.room.message":
e.Content = gjson.GetBytes(ev.Content(), "body").String()
relatesTo = gjson.GetBytes(ev.Content(), "m\\.relates_to")
case spec.MRoomName:
e.Content = gjson.GetBytes(ev.Content(), "name").String()
case spec.MRoomTopic:
@ -622,6 +624,22 @@ func (s *OutputRoomEventConsumer) writeFTS(ev *rstypes.HeaderedEvent, pduPositio
if err := s.fts.Index(e); err != nil {
return err
}
// If the event is an edited message we remove the original event from the index
// to avoid duplicates in the search results.
if relatesTo.Exists() {
relatedData := relatesTo.Map()
if _, ok := relatedData["rel_type"]; ok && relatedData["rel_type"].Str == "m.replace" {
// We remove the original event from the index
if srcEventID, ok := relatedData["event_id"]; ok {
if err := s.fts.Delete(srcEventID.Str); err != nil {
log.WithFields(log.Fields{
"event_id": ev.EventID(),
"src_id": srcEventID.Str,
}).WithError(err).Error("Failed to delete edited message from the fulltext index")
}
}
}
}
}
return nil
}