From 7109a06b0c69a6a2740e0a0ea0a05ab59cd4cec5 Mon Sep 17 00:00:00 2001 From: Erik Date: Thu, 16 Dec 2021 18:06:02 +0100 Subject: [PATCH] Add method to fetch messages Co-authored-by: Elias Schriefer --- src/graphql.rs | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/graphql.rs b/src/graphql.rs index bf3aa90..4230ae9 100644 --- a/src/graphql.rs +++ b/src/graphql.rs @@ -601,8 +601,36 @@ impl Query { Ok(chats) } -} + async fn getMessages(context: &Context, user: ID, password_hash: String, chat: ID, last_msg: Option) -> FieldResult> { + let user = id_to_uuid(&user)?.to_simple(); + user_authentication(&context.db, &user, &password_hash).await?; + + let mut messages = Vec::new(); + for message in sqlx::query(format!( + r#"SELECT * FROM msgdata_{} SORT BY timestamp DESCENDING"#, + chat, + ).as_str()).fetch_all(&context.db).await? { + messages.push(Message { + id: ID::from(message.try_get::("id")?), + timestamp: message.try_get::("timestamp")?.try_into()?, + sender: message.try_get::("sender_id")?.into(), + msg_type: message.try_get::("msg_type")?.try_into()?, + content: message.try_get("content")?, + hide_for: message.try_get("hide_for")? + .map(|array| from_sql_formatted_array(array, "hide_for")) + .transpose()? + .map(|u: String| u.into()) + .collect(), + seen_by: from_sql_formatted_array(message.try_get("seen_by")?, "seen_by")? + .map(|s| s.into()) + .collect(), + }); + } + + Ok(messages) + } +} #[derive(Clone, Copy, Debug)] pub struct Mutation;