@@ -53,6 +53,7 @@ use crate::{
5353mod keys {
5454 // Tables
5555 pub const LINKED_CHUNKS : & str = "linked_chunks" ;
56+ pub const EVENTS : & str = "events" ;
5657}
5758
5859/// The database name.
@@ -63,7 +64,7 @@ const DATABASE_NAME: &str = "matrix-sdk-event-cache.sqlite3";
6364/// This is used to figure whether the SQLite database requires a migration.
6465/// Every new SQL migration should imply a bump of this number, and changes in
6566/// the [`run_migrations`] function.
66- const DATABASE_VERSION : u8 = 11 ;
67+ const DATABASE_VERSION : u8 = 12 ;
6768
6869/// The string used to identify a chunk of type events, in the `type` field in
6970/// the database.
@@ -472,6 +473,16 @@ async fn run_migrations(conn: &SqliteAsyncConn, version: u8) -> Result<()> {
472473 . await ?;
473474 }
474475
476+ if version < 12 {
477+ conn. with_transaction ( |txn| {
478+ txn. execute_batch ( include_str ! (
479+ "../migrations/event_cache_store/012_store_event_type.sql"
480+ ) ) ?;
481+ txn. set_db_version ( 12 )
482+ } )
483+ . await ?;
484+ }
485+
475486 Ok ( ( ) )
476487}
477488
@@ -632,7 +643,7 @@ impl EventCacheStore for SqliteEventCacheStore {
632643 // deduplicated and moved to another position; or because it was inserted
633644 // outside the context of a linked chunk (e.g. pinned event).
634645 let mut content_statement = txn. prepare (
635- "INSERT OR REPLACE INTO events(room_id, event_id, content, relates_to, rel_type) VALUES (?, ?, ?, ?, ?)"
646+ "INSERT OR REPLACE INTO events(room_id, event_id, event_type, session_id, content, relates_to, rel_type) VALUES (?, ?, ?, ?, ?, ?, ?)"
636647 ) ?;
637648
638649 let invalid_event = |event : TimelineEvent | {
@@ -641,20 +652,28 @@ impl EventCacheStore for SqliteEventCacheStore {
641652 return None ;
642653 } ;
643654
644- Some ( ( event_id. to_string ( ) , event) )
655+ let Some ( event_type) = event. kind . event_type ( ) else {
656+ error ! ( %event_id, "Trying to save an event with no event type" ) ;
657+ return None ;
658+ } ;
659+
660+ Some ( ( event_id. to_string ( ) , event_type, event) )
645661 } ;
646662
647663 let room_id = linked_chunk_id. room_id ( ) ;
648664 let hashed_room_id = this. encode_key ( keys:: LINKED_CHUNKS , room_id) ;
649665
650- for ( i, ( event_id, event) ) in items. into_iter ( ) . filter_map ( invalid_event) . enumerate ( ) {
666+ for ( i, ( event_id, event_type , event) ) in items. into_iter ( ) . filter_map ( invalid_event) . enumerate ( ) {
651667 // Insert the location information into the database.
652668 let index = at. index ( ) + i;
653669 chunk_statement. execute ( ( chunk_id, & hashed_linked_chunk_id, & event_id, index) ) ?;
654670
671+ let session_id = event. kind . session_id ( ) . map ( |s| this. encode_key ( keys:: EVENTS , s) ) ;
672+ let event_type = this. encode_key ( keys:: EVENTS , event_type) ;
673+
655674 // Now, insert the event content into the database.
656675 let encoded_event = this. encode_event ( & event) ?;
657- content_statement. execute ( ( & hashed_room_id, event_id, encoded_event. content , encoded_event. relates_to , encoded_event. rel_type ) ) ?;
676+ content_statement. execute ( ( & hashed_room_id, event_id, event_type , session_id , encoded_event. content , encoded_event. relates_to , encoded_event. rel_type ) ) ?;
658677 }
659678 }
660679
@@ -671,15 +690,23 @@ impl EventCacheStore for SqliteEventCacheStore {
671690 continue ;
672691 } ;
673692
693+ let Some ( event_type) = event. kind . event_type ( ) else {
694+ error ! ( %event_id, "Trying to save an event with no event type" ) ;
695+ continue ;
696+ } ;
697+
698+ let session_id = event. kind . session_id ( ) . map ( |s| this. encode_key ( keys:: EVENTS , s) ) ;
699+ let event_type = this. encode_key ( keys:: EVENTS , event_type) ;
700+
674701 // Replace the event's content. Really we'd like to update, but in case the
675702 // event id changed, we are a bit lenient here and will allow an insertion
676703 // of the new event.
677704 let encoded_event = this. encode_event ( & event) ?;
678705 let room_id = linked_chunk_id. room_id ( ) ;
679706 let hashed_room_id = this. encode_key ( keys:: LINKED_CHUNKS , room_id) ;
680707 txn. execute (
681- "INSERT OR REPLACE INTO events(room_id, event_id, content, relates_to, rel_type) VALUES (?, ?, ?, ?, ?)"
682- , ( & hashed_room_id, & event_id, encoded_event. content , encoded_event. relates_to , encoded_event. rel_type ) ) ?;
708+ "INSERT OR REPLACE INTO events(room_id, event_id, event_type, session_id, content, relates_to, rel_type) VALUES (?, ?, ?, ?, ?, ?, ?)" ,
709+ ( & hashed_room_id, & event_id, event_type , session_id , encoded_event. content , encoded_event. relates_to , encoded_event. rel_type ) ) ?;
683710
684711 // Replace the event id in the linked chunk, in case it changed.
685712 txn. execute (
@@ -1336,6 +1363,14 @@ impl EventCacheStore for SqliteEventCacheStore {
13361363 return Ok ( ( ) ) ;
13371364 } ;
13381365
1366+ let Some ( event_type) = event. kind . event_type ( ) else {
1367+ error ! ( %event_id, "Trying to save an event with no event type" ) ;
1368+ return Ok ( ( ) ) ;
1369+ } ;
1370+
1371+ let event_type = self . encode_key ( keys:: EVENTS , event_type) ;
1372+ let session_id = event. kind . session_id ( ) . map ( |s| self . encode_key ( keys:: EVENTS , s) ) ;
1373+
13391374 let hashed_room_id = self . encode_key ( keys:: LINKED_CHUNKS , room_id) ;
13401375 let event_id = event_id. to_string ( ) ;
13411376 let encoded_event = self . encode_event ( & event) ?;
@@ -1344,8 +1379,8 @@ impl EventCacheStore for SqliteEventCacheStore {
13441379 . await ?
13451380 . with_transaction ( move |txn| -> Result < _ > {
13461381 txn. execute (
1347- "INSERT OR REPLACE INTO events(room_id, event_id, content, relates_to, rel_type) VALUES (?, ?, ?, ?, ?)"
1348- , ( & hashed_room_id, & event_id, encoded_event. content , encoded_event. relates_to , encoded_event. rel_type ) ) ?;
1382+ "INSERT OR REPLACE INTO events(room_id, event_id, event_type, session_id, content, relates_to, rel_type) VALUES (?, ?, ?, ?, ?, ?, ?)" ,
1383+ ( & hashed_room_id, & event_id, event_type , session_id , encoded_event. content , encoded_event. relates_to , encoded_event. rel_type ) ) ?;
13491384
13501385 Ok ( ( ) )
13511386 } )
0 commit comments