| 1 | use common::{ |
| 2 | error::Error, |
| 3 | model::{Community, CommunityKind, Id}, |
| 4 | string_enum, |
| 5 | }; |
| 6 | |
| 7 | use super::{Connection, Reader, all, maybe, one, run, user}; |
| 8 | use crate::error::{ServerError, ensure_with}; |
| 9 | |
| 10 | string_enum! { |
| 11 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 12 | Kind { |
| 13 | Group => "group", |
| 14 | Direct => "direct", |
| 15 | Notes => "notes", |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | const READ: &str = "SELECT c.id, CASE WHEN d.user_a != d.user_b AND ?2 NOT IN (d.user_a, d.user_b) THEN a.username || ' & ' || b.username ELSE c.name END, c.kind, (SELECT avatar.id FROM avatar WHERE avatar.community_id = c.id), c.voice_enabled, CASE WHEN d.user_a = ?2 THEN d.user_b ELSE d.user_a END FROM community c LEFT JOIN direct d ON d.community_id = c.id LEFT JOIN user a ON a.id = d.user_a LEFT JOIN user b ON b.id = d.user_b WHERE c.id = ?1"; |
| 20 | |
| 21 | struct Raw { |
| 22 | id: Id, |
| 23 | name: String, |
| 24 | kind: Kind, |
| 25 | avatar_id: Option<Id>, |
| 26 | voice_enabled: bool, |
| 27 | peer: Option<Id>, |
| 28 | } |
| 29 | |
| 30 | fn raw(r: &mut Reader) -> Result<Raw, ServerError> { |
| 31 | Ok(Raw { |
| 32 | id: r.get()?, |
| 33 | name: r.get()?, |
| 34 | kind: r.named()?, |
| 35 | avatar_id: r.get()?, |
| 36 | voice_enabled: r.get()?, |
| 37 | peer: r.get()?, |
| 38 | }) |
| 39 | } |
| 40 | |
| 41 | pub async fn read(id: Id, viewer: Id, db: &Connection) -> Result<Community, ServerError> { |
| 42 | let raw = one(db, READ, (id, viewer), raw).await?; |
| 43 | let kind = match raw.kind { |
| 44 | Kind::Group => CommunityKind::Group, |
| 45 | Kind::Direct => CommunityKind::Direct { |
| 46 | peer: user::read(raw.peer.ok_or(Error::NotFound)?, db).await?, |
| 47 | }, |
| 48 | Kind::Notes => CommunityKind::Notes, |
| 49 | }; |
| 50 | |
| 51 | Ok(Community { |
| 52 | id: raw.id, |
| 53 | name: raw.name, |
| 54 | kind, |
| 55 | avatar_id: raw.avatar_id, |
| 56 | voice_enabled: raw.voice_enabled, |
| 57 | }) |
| 58 | } |
| 59 | |
| 60 | pub async fn create(kind: Kind, name: &str, db: &Connection) -> Result<Id, ServerError> { |
| 61 | one( |
| 62 | db, |
| 63 | "INSERT INTO community (kind, name, voice_enabled) VALUES (?1, ?2, ?3) RETURNING id", |
| 64 | (kind.as_str(), name, true), |
| 65 | |r| r.get(), |
| 66 | ) |
| 67 | .await |
| 68 | } |
| 69 | |
| 70 | pub async fn communities_of(user_id: Id, db: &Connection) -> Result<Vec<Community>, ServerError> { |
| 71 | let ids: Vec<Id> = all( |
| 72 | db, |
| 73 | "SELECT m.community_id FROM member m JOIN community c ON c.id = m.community_id WHERE m.user_id = ?1 AND (c.kind != ?2 OR EXISTS (SELECT 1 FROM message WHERE message.community_id = c.id AND message.deleted_at IS NULL)) ORDER BY m.community_id", |
| 74 | (user_id, Kind::Direct.as_str()), |
| 75 | |r| r.get(), |
| 76 | ) |
| 77 | .await?; |
| 78 | |
| 79 | let mut communities = Vec::new(); |
| 80 | for id in ids { |
| 81 | communities.push(read(id, user_id, db).await?); |
| 82 | } |
| 83 | |
| 84 | Ok(communities) |
| 85 | } |
| 86 | |
| 87 | pub async fn groups(db: &Connection) -> Result<Vec<Community>, ServerError> { |
| 88 | let ids: Vec<Id> = all( |
| 89 | db, |
| 90 | "SELECT id FROM community WHERE kind = ?1 ORDER BY name", |
| 91 | (Kind::Group.as_str(),), |
| 92 | |r| r.get(), |
| 93 | ) |
| 94 | .await?; |
| 95 | |
| 96 | let mut groups = Vec::new(); |
| 97 | for id in ids { |
| 98 | groups.push(read(id, 0, db).await?); |
| 99 | } |
| 100 | |
| 101 | Ok(groups) |
| 102 | } |
| 103 | |
| 104 | pub async fn update( |
| 105 | community_id: Id, |
| 106 | name: &str, |
| 107 | viewer_id: Id, |
| 108 | db: &Connection, |
| 109 | ) -> Result<Community, ServerError> { |
| 110 | let changed = run( |
| 111 | db, |
| 112 | "UPDATE community SET name = ?2 WHERE id = ?1", |
| 113 | (community_id, name), |
| 114 | ) |
| 115 | .await?; |
| 116 | ensure_with(changed == 1, Error::NotFound)?; |
| 117 | |
| 118 | read(community_id, viewer_id, db).await |
| 119 | } |
| 120 | |
| 121 | pub async fn delete(id: Id, db: &Connection) -> Result<(), ServerError> { |
| 122 | ensure_with( |
| 123 | run(db, "DELETE FROM community WHERE id = ?1", (id,)).await? == 1, |
| 124 | Error::NotFound, |
| 125 | ) |
| 126 | } |
| 127 | |
| 128 | pub async fn kind_of(id: Id, db: &Connection) -> Result<Option<Kind>, ServerError> { |
| 129 | maybe(db, "SELECT kind FROM community WHERE id = ?1", (id,), |r| { |
| 130 | r.named() |
| 131 | }) |
| 132 | .await |
| 133 | } |
| 134 | |
| 135 | pub async fn set_voice_enabled( |
| 136 | community_id: Id, |
| 137 | enabled: bool, |
| 138 | db: &Connection, |
| 139 | ) -> Result<(), ServerError> { |
| 140 | run( |
| 141 | db, |
| 142 | "UPDATE community SET voice_enabled = ?2 WHERE id = ?1", |
| 143 | (community_id, enabled), |
| 144 | ) |
| 145 | .await?; |
| 146 | |
| 147 | Ok(()) |
| 148 | } |
| 149 | |
| 150 | pub async fn voice_enabled(community_id: Id, db: &Connection) -> Result<bool, ServerError> { |
| 151 | one( |
| 152 | db, |
| 153 | "SELECT voice_enabled FROM community WHERE id = ?1", |
| 154 | (community_id,), |
| 155 | |r| r.get(), |
| 156 | ) |
| 157 | .await |
| 158 | } |
| 159 | |
| 160 | pub async fn label(id: Id, db: &Connection) -> Result<String, ServerError> { |
| 161 | let prefix = match kind_of(id, db).await?.ok_or(Error::NotFound)? { |
| 162 | Kind::Group => { |
| 163 | return one(db, "SELECT name FROM community WHERE id = ?1", (id,), |r| { |
| 164 | r.get() |
| 165 | }) |
| 166 | .await; |
| 167 | } |
| 168 | Kind::Direct => "DM", |
| 169 | Kind::Notes => "Notes", |
| 170 | }; |
| 171 | |
| 172 | let usernames: Vec<String> = all( |
| 173 | db, |
| 174 | "SELECT user.username FROM user JOIN member ON member.user_id = user.id WHERE member.community_id = ?1 ORDER BY user.username", |
| 175 | (id,), |
| 176 | |r| r.get(), |
| 177 | ) |
| 178 | .await?; |
| 179 | |
| 180 | Ok(format!("{prefix} · {}", usernames.join(" & "))) |
| 181 | } |