Add listing private chats

This commit is contained in:
Elias Schriefer 2021-12-10 22:56:13 +01:00
parent 9bc103018e
commit 33ade65420

View File

@ -376,6 +376,12 @@ impl Default for RestrictionPolicy {
}
}
#[derive(Clone, Debug, PartialEq, Eq, GraphQLObject, Type)]
pub struct Chat {
id: ID,
users: Vec<ID>,
}
#[derive(Clone, Copy, Debug)]
pub struct Query;
@ -461,6 +467,24 @@ impl Query {
},
})
}
async fn chats(context: &Context, user: ID, password_hash: String) -> FieldResult<Vec<Chat>> {
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::<String, _>("id")?),
users: from_sql_formatted_array(chat.try_get("users")?, "users")?.map(|u: String| u.into()).collect(),
});
}
Ok(chats)
}
}
#[derive(Clone, Copy, Debug)]