diff --git a/server-api/client-tests/client.py b/server-api/client-tests/client.py index 63849af..b05c088 100644 --- a/server-api/client-tests/client.py +++ b/server-api/client-tests/client.py @@ -91,3 +91,4 @@ if __name__ == '__main__': worker.post('/channels/create', name='send-channel', kind=TEXT_CHANNEL) worker.post('/message/send', channel='send-channel', content="some random content") + worker.delete('/channels/delete', name='send-channel') diff --git a/server-api/db/src/messages.rs b/server-api/db/src/messages.rs index b995d59..c4b8e65 100644 --- a/server-api/db/src/messages.rs +++ b/server-api/db/src/messages.rs @@ -9,6 +9,11 @@ use crate::{UBigInt, BigInt, VarChar}; use crate::common::{FromDB}; +macro_rules! msg_channel { + $(value:expr) => { + format!("messages_{}", value) + } +} #[allow(dead_code)] pub struct Message { pub id: UBigInt, @@ -23,6 +28,8 @@ impl FromDB for Message { type Row = Option<(UBigInt, BigInt, VarChar, UBigInt, VarChar)>; async fn get(p: &Pool, id: UBigInt) -> Response { + //! Typically used as the backend to the .update(...) method to + //! pick out a message to later edit if let Ok(conn) = p.get_conn().await { let q = "SELECT id, time, content, author_id, channel_name WHERE id = :id"; let result: Result<(Conn, Self::Row), SqlError> = @@ -45,6 +52,7 @@ impl FromDB for Message { } async fn update(p: &Pool, row: Self) -> Response { + //! Primarily used by users to edit previous comments // NOTE: we only allow the changing of content in this since // no other column has good reason to be modified if let Ok(conn) = p.get_conn().await { @@ -62,6 +70,8 @@ impl FromDB for Message { } async fn delete(p: &Pool, id: UBigInt) -> Response { + //! Deletes a single message + //! Typically used by normal users/bots to remove unwanted comments if let Ok(conn) = p.get_conn().await { let q = "DELETE FROM messages WHERE id = :id"; let result: Result = @@ -74,3 +84,20 @@ impl FromDB for Message { return Response::Other(no_conn!("Message::FromDB::update")) } } +impl Message { + async fn add(p: &Pool, author_id: UBigInt, content: VarChar, channel_id: UBigInt) -> Response { + //! Adds a user message to the given channel via channel_id + let conn = p.get_conn().await; + if conn.is_err() { + return Response::Other(no_conn!("Message::add")); + } + // generate the channel name + let fq_channel_id = msg_channel!(channel_id); + conn.prep_exec( + "INSERT INTO :table (id, time, content, author_id)", + params!{"cid" => fq_channel_id} + ); + return Response::Success; + } +} +