diff --git a/src/graphql.rs b/src/graphql.rs index 3b1913e..1e6468e 100644 --- a/src/graphql.rs +++ b/src/graphql.rs @@ -581,6 +581,30 @@ impl Mutation { Ok(chat_uuid.to_simple().to_string().into()) } + + async fn newGroupChat(context: &Context, user: ID, password_hash: String, title: String, description: Option, with: Vec) -> FieldResult { + let user = id_to_uuid(&user)?.to_simple(); + user_authentication(&context.db, &user, &password_hash).await?; + let mut chat_partners = BTreeSet::new(); + for u in with { + let u = id_to_uuid(&u)?.to_simple(); + user_exists(&context.db, &u).await?; + chat_partners.insert(u); + } + chat_partners.insert(user); + + let group_chat_uuid = Uuid::new_v4(); + sqlx::query(format!( + r#"INSERT INTO chat_index VALUES ("{}", 1, "{}", {}, "{}")"#, + group_chat_uuid.to_simple(), + title, + description.map(|d| format!(r#""{}""#, d)).unwrap_or("NULL".to_string()), + format_array_for_sql(chat_partners) + ).as_str()).execute(&context.db).await?; + crate::sqlite::create_chat(&context.db, &group_chat_uuid).await?; + + Ok(group_chat_uuid.to_simple().to_string().into()) + } } pub type Schema<'root_node> = RootNode<'root_node, Query, Mutation, EmptySubscription>;