diff --git a/src/graphql.rs b/src/graphql.rs index 797dbd1..7c3bebb 100644 --- a/src/graphql.rs +++ b/src/graphql.rs @@ -382,6 +382,14 @@ pub struct Chat { users: Vec, } +#[derive(Clone, Debug, PartialEq, Eq, GraphQLObject, Type)] +pub struct GroupChat { + id: ID, + title: String, + description: Option, + users: Vec, +} + #[derive(Clone, Copy, Debug)] pub struct Query; @@ -485,6 +493,26 @@ impl Query { Ok(chats) } + + async fn group_chats(context: &Context, user: ID, password_hash: String) -> FieldResult> { + let user = id_to_uuid(&user)?.to_simple(); + user_authentication(&context.db, &user, &password_hash).await?; + + let mut chats = Vec::new(); + for chat in sqlx::query(format!( + r#"SELECT id, title, description, users FROM chat_index WHERE is_group_chat = 1 AND users LIKE "%{}%""#, + user, + ).as_str()).fetch_all(&context.db).await? { + chats.push(GroupChat { + id: ID::from(chat.try_get::("id")?), + title: chat.try_get("title")?, + description: chat.try_get("description")?, + users: from_sql_formatted_array(chat.try_get("users")?, "users")?.map(|u: String| u.into()).collect(), + }); + } + + Ok(chats) + } } #[derive(Clone, Copy, Debug)]