daemon: add support for getting messages from db
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user