diff --git a/crates/server/src/db/community.rs b/crates/server/src/db/community.rs index 1fceac3..849eb97 100644 --- a/crates/server/src/db/community.rs +++ b/crates/server/src/db/community.rs @@ -39,7 +39,10 @@ fn raw(r: &mut Reader) -> Result { } pub async fn read(id: Id, viewer: Id, db: &Connection) -> Result { - let raw = one(db, READ, (id, viewer), raw).await?; + community(one(db, READ, (id, viewer), raw).await?, db).await +} + +async fn community(raw: Raw, db: &Connection) -> Result { let kind = match raw.kind { Kind::Group => CommunityKind::Group, Kind::Direct => CommunityKind::Direct { @@ -85,21 +88,20 @@ pub async fn communities_of(user_id: Id, db: &Connection) -> Result Result, ServerError> { - all( + let raws = all( db, - "SELECT id, name, (SELECT avatar.id FROM avatar WHERE avatar.community_id = community.id), voice_enabled FROM community WHERE kind = ?1 ORDER BY name", + "SELECT id, name, kind, (SELECT avatar.id FROM avatar WHERE avatar.community_id = community.id), voice_enabled, NULL FROM community WHERE kind = ?1 ORDER BY name", (Kind::Group.as_str(),), - |r| { - Ok(Community { - id: r.get()?, - name: r.get()?, - kind: CommunityKind::Group, - avatar_id: r.get()?, - voice_enabled: r.get()?, - }) - }, + raw, ) - .await + .await?; + + let mut groups = Vec::new(); + for raw in raws { + groups.push(community(raw, db).await?); + } + + Ok(groups) } pub async fn update(