diff --git a/src/graphql.rs b/src/graphql.rs index b75c99e..9241cbe 100644 --- a/src/graphql.rs +++ b/src/graphql.rs @@ -376,6 +376,12 @@ impl Default for RestrictionPolicy { } } +#[derive(Clone, Debug, PartialEq, Eq, GraphQLObject, Type)] +pub struct Chat { + id: ID, + users: Vec, +} + #[derive(Clone, Copy, Debug)] pub struct Query; @@ -461,6 +467,24 @@ impl Query { }, }) } + + async fn 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, users FROM chat_index WHERE is_group_chat = 0 AND users LIKE "%{}%""#, + user, + ).as_str()).fetch_all(&context.db).await? { + chats.push(Chat { + id: ID::from(chat.try_get::("id")?), + users: from_sql_formatted_array(chat.try_get("users")?, "users")?.map(|u: String| u.into()).collect(), + }); + } + + Ok(chats) + } } #[derive(Clone, Copy, Debug)]