Add method to fetch messages

Co-authored-by: Elias Schriefer
This commit is contained in:
Erik Foris 2021-12-16 18:06:02 +01:00
parent d6e92943ba
commit 7109a06b0c

View File

@ -601,8 +601,36 @@ impl Query {
Ok(chats)
}
async fn getMessages(context: &Context, user: ID, password_hash: String, chat: ID, last_msg: Option<ID>) -> FieldResult<Vec<Message>> {
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::<String, _>("id")?),
timestamp: message.try_get::<i64, _>("timestamp")?.try_into()?,
sender: message.try_get::<String, _>("sender_id")?.into(),
msg_type: message.try_get::<u8,_>("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;