Include message timestamp

This commit is contained in:
Niels Andriesse 2021-03-26 10:21:08 +11:00
parent 02671b4a0a
commit 0872281b78
3 changed files with 13 additions and 7 deletions

View File

@ -365,10 +365,13 @@ pub async fn insert_message(
let tx = conn.transaction().map_err(|_| Error::DatabaseFailedInternally)?;
// Insert the message
let stmt = format!(
"INSERT INTO {} (public_key, data, signature) VALUES (?1, ?2, ?3)",
"INSERT INTO {} (public_key, timestamp, data, signature) VALUES (?1, ?2, ?3, ?4)",
storage::MESSAGES_TABLE
);
match tx.execute(&stmt, params![&requesting_public_key, message.data, message.signature]) {
match tx.execute(
&stmt,
params![&requesting_public_key, message.timestamp, message.data, message.signature],
) {
Ok(_) => (),
Err(e) => {
println!("Couldn't insert message due to error: {}.", e);
@ -418,10 +421,10 @@ pub async fn get_messages(
// Query the database
let raw_query: String;
if query_params.get("from_server_id").is_some() {
raw_query = format!("SELECT id, public_key, data, signature FROM {} WHERE rowid > (?1) ORDER BY rowid ASC LIMIT (?2)", storage::MESSAGES_TABLE);
raw_query = format!("SELECT id, public_key, timestamp, data, signature FROM {} WHERE rowid > (?1) ORDER BY rowid ASC LIMIT (?2)", storage::MESSAGES_TABLE);
} else {
raw_query = format!(
"SELECT id, public_key, data, signature FROM {} ORDER BY rowid DESC LIMIT (?2)",
"SELECT id, public_key, timestamp, data, signature FROM {} ORDER BY rowid DESC LIMIT (?2)",
storage::MESSAGES_TABLE
);
}
@ -430,8 +433,9 @@ pub async fn get_messages(
Ok(models::Message {
server_id: row.get(0)?,
public_key: row.get(1)?,
data: row.get(2)?,
signature: row.get(3)?,
timestamp: row.get(2)?,
data: row.get(3)?,
signature: row.get(4)?,
})
}) {
Ok(rows) => rows,

View File

@ -4,13 +4,14 @@ use serde::{Deserialize, Serialize};
pub struct Message {
pub server_id: Option<i64>,
pub public_key: Option<String>,
pub timestamp: i64,
pub data: String,
pub signature: String,
}
impl Message {
pub fn is_valid(&self) -> bool {
return !self.data.is_empty() && !self.signature.is_empty();
return self.timestamp > 0 && !self.data.is_empty() && !self.signature.is_empty();
}
}

View File

@ -89,6 +89,7 @@ fn create_room_tables_if_needed(conn: &DatabaseConnection) {
"CREATE TABLE IF NOT EXISTS {} (
id INTEGER PRIMARY KEY,
public_key TEXT,
timestamp INTEGER,
data TEXT,
signature TEXT
)",