Private
Public Access
1
0

daemon: add support for getting messages from db

This commit is contained in:
2025-04-28 16:00:04 -07:00
parent 9c245a5b52
commit c189e5f9e3
11 changed files with 267 additions and 15 deletions

View File

@@ -207,6 +207,21 @@ impl<'a> Repository<'a> {
Ok(result)
}
pub fn get_last_message_for_conversation(&mut self, conversation_guid: &str) -> Result<Option<Message>> {
use crate::schema::messages::dsl::*;
use crate::schema::conversation_messages::dsl::*;
let message_record = conversation_messages
.filter(conversation_id.eq(conversation_guid))
.inner_join(messages)
.select(MessageRecord::as_select())
.order_by(schema::messages::date.desc())
.first::<MessageRecord>(self.connection)
.optional()?;
Ok(message_record.map(|r| r.into()))
}
// Helper function to get the last inserted row ID
// This is a workaround since the Sqlite backend doesn't support `RETURNING`
// Huge caveat with this is that it depends on whatever the last insert was, prevents concurrent inserts.

View File

@@ -310,6 +310,10 @@ async fn test_insert_messages_batch() {
),
}
}
// Make sure the last message is the last one we inserted
let last_message = repository.get_last_message_for_conversation(&conversation_id).unwrap().unwrap();
assert_eq!(last_message.id, message4.id);
})
.await;
}