7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1 | // Copyright 2024 The Matrix.org Foundation C.I.C. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 2 | // |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 4 | // you may not use this file except in compliance with the License. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 5 | // You may obtain a copy of the License at |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 6 | // |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 8 | // |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 9 | // Unless required by applicable law or agreed to in writing, software |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 12 | // See the License for the specific language governing permissions and |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 13 | // limitations under the License. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 14 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 15 | use ruma::events::room::message::{MessageType, OriginalSyncRoomMessageEvent, Relation}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 16 | use tantivy::{ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 17 | DateTime, TantivyDocument, doc, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 18 | schema::{DateOptions, DateTimePrecision, Field, INDEXED, STORED, STRING, Schema, TEXT}, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 19 | }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 20 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 21 | use crate::error::{IndexError, IndexSchemaError}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 22 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 23 | pub(crate) trait MatrixSearchIndexSchema { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 24 | fn new() -> Self; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 25 | fn default_search_fields(&self) -> Vec<Field>; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 26 | fn primary_key(&self) -> Field; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 27 | fn deletion_key(&self) -> Field; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 28 | fn get_field_name(&self, field: Field) -> &str; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 29 | fn as_tantivy_schema(&self) -> Schema; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 30 | fn make_doc(&self, event: OriginalSyncRoomMessageEvent) -> Result<TantivyDocument, IndexError>; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 31 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 32 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 33 | #[derive(Debug, Clone)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 34 | pub(crate) struct RoomMessageSchema { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 35 | inner: Schema, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 36 | /// The event id of this event (primary key). |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 37 | event_id_field: Field, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 38 | /// The event id of the event that this event affects. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 39 | /// Used by edits to refer to the event they edited (deletion key). |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 40 | original_event_id_field: Field, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 41 | body_field: Field, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 42 | date_field: Field, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 43 | sender_field: Field, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 44 | default_search_fields: Vec<Field>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 45 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 46 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 47 | impl MatrixSearchIndexSchema for RoomMessageSchema { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 48 | fn new() -> Self { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 49 | let mut schema = Schema::builder(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 50 | let event_id_field = schema.add_text_field("event_id", STORED | STRING); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 51 | let original_event_id_field = schema.add_text_field("original_event_id", STRING); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 52 | let body_field = schema.add_text_field("body", TEXT); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 53 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 54 | let date_options = |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 55 | DateOptions::from(INDEXED).set_fast().set_precision(DateTimePrecision::Seconds); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 56 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 57 | let date_field = schema.add_date_field("date", date_options); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 58 | let sender_field = schema.add_text_field("sender", STRING); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 59 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 60 | let default_search_fields = vec![body_field]; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 61 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 62 | let schema = schema.build(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 63 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 64 | Self { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 65 | inner: schema, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 66 | event_id_field, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 67 | original_event_id_field, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 68 | body_field, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 69 | date_field, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 70 | sender_field, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 71 | default_search_fields, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 72 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 73 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 74 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 75 | fn default_search_fields(&self) -> Vec<Field> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 76 | self.default_search_fields.clone() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 77 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 78 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 79 | fn primary_key(&self) -> Field { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 80 | self.event_id_field |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 81 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 82 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 83 | fn deletion_key(&self) -> Field { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 84 | self.original_event_id_field |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 85 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 86 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 87 | fn get_field_name(&self, field: Field) -> &str { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 88 | self.inner.get_field_name(field) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 89 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 90 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 91 | fn as_tantivy_schema(&self) -> Schema { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 92 | self.inner.clone() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 93 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 94 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 95 | /// Given an [`OriginalSyncRoomMessageEvent`] return a |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 96 | /// [`TantivyDocument`]. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 97 | fn make_doc(&self, event: OriginalSyncRoomMessageEvent) -> Result<TantivyDocument, IndexError> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 98 | let body = match &event.content.msgtype { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 99 | MessageType::Text(content) => Ok(content.body.clone()), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 100 | _ => Err(IndexError::MessageTypeNotSupported), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 101 | }?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 102 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 103 | let mut document = doc!( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 104 | self.event_id_field => event.event_id.to_string(), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 105 | self.body_field => body, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 106 | self.date_field => |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 107 | DateTime::from_timestamp_millis( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 108 | event.origin_server_ts.get().into()), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 109 | self.sender_field => event.sender.to_string(), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 110 | ); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 111 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 112 | if let Some(Relation::Replacement(replacement_data)) = &event.content.relates_to { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 113 | document.add_text(self.original_event_id_field, replacement_data.event_id.clone()); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 114 | } else { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 115 | document.add_text(self.original_event_id_field, event.event_id); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 116 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 117 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 118 | Ok(document) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 119 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 120 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 121 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 122 | impl TryFrom<Schema> for RoomMessageSchema { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 123 | type Error = IndexSchemaError; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 124 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 125 | fn try_from(schema: Schema) -> Result<RoomMessageSchema, Self::Error> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 126 | let event_id_field = schema.get_field("event_id")?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 127 | let original_event_id_field = schema.get_field("original_event_id")?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 128 | let body_field = schema.get_field("body")?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 129 | let date_field = schema.get_field("date")?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 130 | let sender_field = schema.get_field("sender")?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 131 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 132 | let default_search_fields = vec![body_field]; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 133 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 134 | Ok(Self { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 135 | inner: schema, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 136 | event_id_field, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 137 | original_event_id_field, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 138 | body_field, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 139 | date_field, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 140 | sender_field, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 141 | default_search_fields, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 142 | }) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 143 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 144 | } |