135 行
3.6 KiB
Rust
135 行
3.6 KiB
Rust
|
|
use rusqlite::{Connection, OptionalExtension, params};
|
||
|
|
use thiserror::Error;
|
||
|
|
|
||
|
|
pub struct ProfileMessageStore {
|
||
|
|
connection: Connection,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl ProfileMessageStore {
|
||
|
|
pub fn in_memory() -> Result<Self, DatabaseError> {
|
||
|
|
let connection = Connection::open_in_memory()?;
|
||
|
|
let store = Self { connection };
|
||
|
|
store.migrate()?;
|
||
|
|
Ok(store)
|
||
|
|
}
|
||
|
|
|
||
|
|
fn migrate(&self) -> Result<(), DatabaseError> {
|
||
|
|
self.connection.execute_batch(
|
||
|
|
"
|
||
|
|
PRAGMA foreign_keys = ON;
|
||
|
|
CREATE TABLE IF NOT EXISTS messages (
|
||
|
|
profile_id TEXT NOT NULL,
|
||
|
|
server_instance_id TEXT NOT NULL,
|
||
|
|
conversation_id TEXT NOT NULL,
|
||
|
|
message_id TEXT NOT NULL,
|
||
|
|
content TEXT NOT NULL,
|
||
|
|
PRIMARY KEY (profile_id, server_instance_id, conversation_id, message_id)
|
||
|
|
);
|
||
|
|
",
|
||
|
|
)?;
|
||
|
|
Ok(())
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn insert_message(
|
||
|
|
&self,
|
||
|
|
profile_id: &str,
|
||
|
|
server_instance_id: &str,
|
||
|
|
conversation_id: &str,
|
||
|
|
message_id: &str,
|
||
|
|
content: &str,
|
||
|
|
) -> Result<(), DatabaseError> {
|
||
|
|
self.connection.execute(
|
||
|
|
"
|
||
|
|
INSERT INTO messages (
|
||
|
|
profile_id,
|
||
|
|
server_instance_id,
|
||
|
|
conversation_id,
|
||
|
|
message_id,
|
||
|
|
content
|
||
|
|
) VALUES (?1, ?2, ?3, ?4, ?5)
|
||
|
|
",
|
||
|
|
params![
|
||
|
|
profile_id,
|
||
|
|
server_instance_id,
|
||
|
|
conversation_id,
|
||
|
|
message_id,
|
||
|
|
content
|
||
|
|
],
|
||
|
|
)?;
|
||
|
|
Ok(())
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn message_content(
|
||
|
|
&self,
|
||
|
|
profile_id: &str,
|
||
|
|
server_instance_id: &str,
|
||
|
|
conversation_id: &str,
|
||
|
|
message_id: &str,
|
||
|
|
) -> Result<Option<String>, DatabaseError> {
|
||
|
|
Ok(self
|
||
|
|
.connection
|
||
|
|
.query_row(
|
||
|
|
"
|
||
|
|
SELECT content
|
||
|
|
FROM messages
|
||
|
|
WHERE profile_id = ?1
|
||
|
|
AND server_instance_id = ?2
|
||
|
|
AND conversation_id = ?3
|
||
|
|
AND message_id = ?4
|
||
|
|
",
|
||
|
|
params![profile_id, server_instance_id, conversation_id, message_id],
|
||
|
|
|row| row.get(0),
|
||
|
|
)
|
||
|
|
.optional()?)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Debug, Error)]
|
||
|
|
pub enum DatabaseError {
|
||
|
|
#[error("SQLite operation failed")]
|
||
|
|
Sqlite(#[from] rusqlite::Error),
|
||
|
|
}
|
||
|
|
|
||
|
|
#[cfg(test)]
|
||
|
|
mod tests {
|
||
|
|
use super::*;
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn identical_remote_ids_remain_isolated_by_profile_and_server() {
|
||
|
|
let store = ProfileMessageStore::in_memory().expect("store");
|
||
|
|
store
|
||
|
|
.insert_message(
|
||
|
|
"profile-a",
|
||
|
|
"server-a",
|
||
|
|
"same-conversation",
|
||
|
|
"same-message",
|
||
|
|
"A",
|
||
|
|
)
|
||
|
|
.expect("insert A");
|
||
|
|
store
|
||
|
|
.insert_message(
|
||
|
|
"profile-b",
|
||
|
|
"server-b",
|
||
|
|
"same-conversation",
|
||
|
|
"same-message",
|
||
|
|
"B",
|
||
|
|
)
|
||
|
|
.expect("insert B");
|
||
|
|
|
||
|
|
assert_eq!(
|
||
|
|
store
|
||
|
|
.message_content("profile-a", "server-a", "same-conversation", "same-message")
|
||
|
|
.expect("query A")
|
||
|
|
.as_deref(),
|
||
|
|
Some("A")
|
||
|
|
);
|
||
|
|
assert_eq!(
|
||
|
|
store
|
||
|
|
.message_content("profile-b", "server-b", "same-conversation", "same-message")
|
||
|
|
.expect("query B")
|
||
|
|
.as_deref(),
|
||
|
|
Some("B")
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|