Call grid view, poll show-voters option with anonymous counts, poll card refresh
Browse files at this commitc3c7ee247eb87b88bbaf5ab9953729622be52bbaParent: 6d19ee2- feat
- call grid view, poll show-voters option with anonymous counts, poll card refresh
12 changed files +542 -120
- crates/client/src/gui/camera_view.rs +264 -52
- crates/client/src/gui/chat.rs +21 -12
- crates/client/src/gui/composer.rs +36 -9
- crates/client/src/gui/poll.rs +142 -31
- crates/client/src/gui/share_view.rs +10 -7
- crates/common/src/call.rs +7 -0
- crates/common/src/model.rs +3 -0
- crates/server/src/call/message.rs +13 -1
- crates/server/src/call/mod.rs +1 -0
- crates/server/src/db/message.rs +2 -1
- crates/server/src/db/poll.rs +41 -6
- crates/server/src/db/schema.sql +2 -1
crates/client/src/gui/camera_view.rsmodified+264 -52
| @@ -3,15 +3,15 @@ use std::sync::Arc; | ||
| 3 | 3 | |
| 4 | 4 | use common::share::{Fps, Kind, Quality, Resolution}; |
| 5 | 5 | use gpui_kit::{ |
| 6 | - Context, Entity, InteractiveElement, IntoElement, ObjectFit, ParentElement, RenderImage, | |
| 7 | - StatefulInteractiveElement, Styled, StyledImage, Task, Window, | |
| 6 | + AnyElement, Context, Entity, InteractiveElement, IntoElement, ObjectFit, ParentElement, | |
| 7 | + RenderImage, StatefulInteractiveElement, Styled, StyledImage, Task, Window, | |
| 8 | 8 | assets::IconName, |
| 9 | 9 | component::Icon, |
| 10 | 10 | div, img, px, |
| 11 | 11 | }; |
| 12 | 12 | |
| 13 | 13 | use super::{ |
| 14 | - Palette, Tooltip as _, Yumush, avatar, clickable, | |
| 14 | + Palette, Tooltip as _, Yumush, avatar, clickable, entity_action, | |
| 15 | 15 | media_view::to_render_image, |
| 16 | 16 | notify_error, |
| 17 | 17 | share_view::{self, FRAME_TICK}, |
| @@ -20,86 +20,100 @@ use super::{ | ||
| 20 | 20 | }; |
| 21 | 21 | use crate::share::session::WatchQuality; |
| 22 | 22 | |
| 23 | -pub(super) const CAMERA_TILE: WatchQuality = WatchQuality::Fixed(Quality { | |
| 23 | +pub(super) const TILE_QUALITY: WatchQuality = WatchQuality::Fixed(Quality { | |
| 24 | 24 | resolution: Resolution::P360, |
| 25 | 25 | fps: Fps::F30, |
| 26 | 26 | }); |
| 27 | 27 | |
| 28 | -pub(super) struct CameraTile { | |
| 28 | +#[derive(Clone, Copy, PartialEq, Eq)] | |
| 29 | +pub(super) enum Stage { | |
| 30 | + Strip, | |
| 31 | + Grid, | |
| 32 | + Hidden, | |
| 33 | +} | |
| 34 | + | |
| 35 | +pub(super) fn tile_quality(stage: Stage) -> WatchQuality { | |
| 36 | + match stage { | |
| 37 | + Stage::Grid => WatchQuality::Auto, | |
| 38 | + Stage::Strip | Stage::Hidden => TILE_QUALITY, | |
| 39 | + } | |
| 40 | +} | |
| 41 | + | |
| 42 | +pub(super) struct Tile { | |
| 29 | 43 | pub(super) generation: Option<u64>, |
| 30 | 44 | pub(super) frame: Option<Arc<RenderImage>>, |
| 31 | 45 | } |
| 32 | 46 | |
| 33 | 47 | impl Yumush { |
| 34 | - fn camera_users(&self) -> Vec<Id> { | |
| 35 | - let Some(community_id) = self.chat.voice_community() else { | |
| 36 | - return Vec::new(); | |
| 37 | - }; | |
| 38 | - self.chat | |
| 39 | - .shares | |
| 40 | - .get(&community_id) | |
| 48 | + fn tile_sources(&self) -> Vec<(Id, Kind)> { | |
| 49 | + let grid = self.chat.stage == Stage::Grid; | |
| 50 | + self.stage_sources() | |
| 41 | 51 | .into_iter() |
| 42 | - .flatten() | |
| 43 | - .filter(|share| share.kind == Kind::Camera) | |
| 44 | - .map(|share| share.user_id) | |
| 52 | + .filter(|(_, kind)| grid || *kind == Kind::Camera) | |
| 45 | 53 | .collect() |
| 46 | 54 | } |
| 47 | 55 | |
| 48 | - pub(super) fn tick_cameras(&mut self, cx: &mut Context<Self>) { | |
| 49 | - let wanted = self.camera_users(); | |
| 50 | - let viewing = self.viewing_camera(); | |
| 56 | + pub(super) fn tick_tiles(&mut self, cx: &mut Context<Self>) { | |
| 57 | + let wanted = self.tile_sources(); | |
| 58 | + let viewing = self.viewing(); | |
| 59 | + let quality = tile_quality(self.chat.stage); | |
| 51 | 60 | let mut changed = false; |
| 52 | 61 | |
| 53 | - for &user_id in &wanted { | |
| 54 | - if self.chat.cameras.contains_key(&user_id) { | |
| 62 | + for &source in &wanted { | |
| 63 | + if self.chat.tiles.contains_key(&source) { | |
| 55 | 64 | continue; |
| 56 | 65 | } |
| 57 | - self.chat.cameras.insert( | |
| 58 | - user_id, | |
| 59 | - CameraTile { | |
| 66 | + self.chat.tiles.insert( | |
| 67 | + source, | |
| 68 | + Tile { | |
| 60 | 69 | generation: None, |
| 61 | 70 | frame: None, |
| 62 | 71 | }, |
| 63 | 72 | ); |
| 73 | + changed = true; | |
| 74 | + if viewing == Some(source) { | |
| 75 | + continue; | |
| 76 | + } | |
| 77 | + let (user_id, kind) = source; | |
| 64 | 78 | let network = self.network.clone(); |
| 65 | 79 | let volume = self.settings.share.volume; |
| 66 | 80 | let choice = self.settings.voice.choice.clone(); |
| 67 | - spawn_warn(cx, "camera watch", async move { | |
| 81 | + spawn_warn(cx, "tile watch", async move { | |
| 68 | 82 | network |
| 69 | - .watch_share(user_id, Kind::Camera, CAMERA_TILE, volume, choice) | |
| 83 | + .watch_share(user_id, kind, quality, volume, choice) | |
| 70 | 84 | .await |
| 71 | 85 | }); |
| 72 | - changed = true; | |
| 73 | 86 | } |
| 74 | 87 | |
| 75 | - let gone: Vec<Id> = self | |
| 88 | + let gone: Vec<(Id, Kind)> = self | |
| 76 | 89 | .chat |
| 77 | - .cameras | |
| 90 | + .tiles | |
| 78 | 91 | .keys() |
| 79 | - .filter(|user_id| !wanted.contains(user_id)) | |
| 92 | + .filter(|source| !wanted.contains(source)) | |
| 80 | 93 | .copied() |
| 81 | 94 | .collect(); |
| 82 | - for user_id in gone { | |
| 95 | + for source in gone { | |
| 83 | 96 | if let Some(frame) = self |
| 84 | 97 | .chat |
| 85 | - .cameras | |
| 86 | - .remove(&user_id) | |
| 98 | + .tiles | |
| 99 | + .remove(&source) | |
| 87 | 100 | .and_then(|tile| tile.frame) |
| 88 | 101 | { |
| 89 | 102 | cx.drop_image(frame, None); |
| 90 | 103 | } |
| 91 | - if viewing != Some(user_id) { | |
| 104 | + if viewing != Some(source) { | |
| 92 | 105 | let network = self.network.clone(); |
| 106 | + let (user_id, kind) = source; | |
| 93 | 107 | cx.spawn(async move |_, _| { |
| 94 | - let _ = network.unwatch_share(user_id, Kind::Camera).await; | |
| 108 | + let _ = network.unwatch_share(user_id, kind).await; | |
| 95 | 109 | }) |
| 96 | 110 | .detach(); |
| 97 | 111 | } |
| 98 | 112 | changed = true; |
| 99 | 113 | } |
| 100 | 114 | |
| 101 | - for (user_id, tile) in self.chat.cameras.iter_mut() { | |
| 102 | - let Some(frame) = self.network.share_frame(*user_id, Kind::Camera) else { | |
| 115 | + for (&(user_id, kind), tile) in self.chat.tiles.iter_mut() { | |
| 116 | + let Some(frame) = self.network.share_frame(user_id, kind) else { | |
| 103 | 117 | continue; |
| 104 | 118 | }; |
| 105 | 119 | if tile.generation == Some(frame.generation) { |
| @@ -118,6 +132,18 @@ impl Yumush { | ||
| 118 | 132 | } |
| 119 | 133 | } |
| 120 | 134 | |
| 135 | + pub(super) fn set_stage(&mut self, stage: Stage, cx: &mut Context<Self>) { | |
| 136 | + self.chat.stage = stage; | |
| 137 | + let viewing = self.viewing(); | |
| 138 | + let quality = tile_quality(stage); | |
| 139 | + for &(user_id, kind) in self.chat.tiles.keys() { | |
| 140 | + if viewing != Some((user_id, kind)) { | |
| 141 | + self.network.set_watch_quality(user_id, kind, quality); | |
| 142 | + } | |
| 143 | + } | |
| 144 | + cx.notify(); | |
| 145 | + } | |
| 146 | + | |
| 121 | 147 | pub(super) fn toggle_camera(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 122 | 148 | let network = self.network.clone(); |
| 123 | 149 | let on = network.camera_on(); |
| @@ -134,6 +160,11 @@ impl Yumush { | ||
| 134 | 160 | .detach(); |
| 135 | 161 | } |
| 136 | 162 | |
| 163 | + pub(super) fn in_stage_chat(&self) -> bool { | |
| 164 | + self.chat.voice_community().is_some() | |
| 165 | + && self.chat.selected_community_id == self.chat.voice_community() | |
| 166 | + } | |
| 167 | + | |
| 137 | 168 | pub(super) fn stage_sources(&self) -> Vec<(Id, Kind)> { |
| 138 | 169 | let Some(community_id) = self.chat.voice_community() else { |
| 139 | 170 | return Vec::new(); |
| @@ -155,14 +186,16 @@ impl Yumush { | ||
| 155 | 186 | entity: &Entity<Self>, |
| 156 | 187 | palette: Palette, |
| 157 | 188 | ) -> Option<impl IntoElement> { |
| 158 | - let sources = self.stage_sources(); | |
| 159 | - if self.chat.stage_hidden | |
| 160 | - || sources.is_empty() | |
| 161 | - || self.chat.selected_community_id != self.chat.voice_community() | |
| 162 | - { | |
| 189 | + let shown = match self.chat.stage { | |
| 190 | + Stage::Strip => true, | |
| 191 | + Stage::Grid => self.chat.viewer.is_some(), | |
| 192 | + Stage::Hidden => false, | |
| 193 | + }; | |
| 194 | + if !shown || !self.in_stage_chat() { | |
| 163 | 195 | return None; |
| 164 | 196 | } |
| 165 | - let tiles: Vec<_> = sources | |
| 197 | + let tiles: Vec<_> = self | |
| 198 | + .stage_sources() | |
| 166 | 199 | .into_iter() |
| 167 | 200 | .map(|source| stage_tile(entity, self, palette, source)) |
| 168 | 201 | .collect(); |
| @@ -178,7 +211,92 @@ impl Yumush { | ||
| 178 | 211 | .overflow_x_scroll() |
| 179 | 212 | .border_b_1() |
| 180 | 213 | .border_color(palette.border) |
| 181 | - .children(tiles), | |
| 214 | + .children(tiles) | |
| 215 | + .child(icon_button( | |
| 216 | + "stage_grid", | |
| 217 | + palette, | |
| 218 | + IconName::LayoutDashboard, | |
| 219 | + "Grid", | |
| 220 | + entity_action(entity, |yumush, _, cx| { | |
| 221 | + yumush.set_stage(Stage::Grid, cx) | |
| 222 | + }), | |
| 223 | + )), | |
| 224 | + ) | |
| 225 | + } | |
| 226 | + | |
| 227 | + pub(super) fn stage_grid( | |
| 228 | + &self, | |
| 229 | + entity: &Entity<Self>, | |
| 230 | + palette: Palette, | |
| 231 | + ) -> Option<impl IntoElement> { | |
| 232 | + if self.chat.stage != Stage::Grid | |
| 233 | + || !self.in_stage_chat() | |
| 234 | + || self | |
| 235 | + .chat | |
| 236 | + .viewer | |
| 237 | + .as_ref() | |
| 238 | + .is_some_and(|viewer| !viewer.fullscreen) | |
| 239 | + { | |
| 240 | + return None; | |
| 241 | + } | |
| 242 | + let community_id = self.chat.voice_community()?; | |
| 243 | + let sources = self.stage_sources(); | |
| 244 | + let cells: Vec<AnyElement> = self | |
| 245 | + .chat | |
| 246 | + .voice_ids(community_id) | |
| 247 | + .iter() | |
| 248 | + .map(|&user_id| { | |
| 249 | + let shared = sources.contains(&(user_id, Kind::Camera)); | |
| 250 | + grid_cell(entity, self, palette, (user_id, Kind::Camera), shared) | |
| 251 | + }) | |
| 252 | + .chain( | |
| 253 | + sources | |
| 254 | + .iter() | |
| 255 | + .filter(|(_, kind)| *kind == Kind::Screen) | |
| 256 | + .map(|&source| grid_cell(entity, self, palette, source, true)), | |
| 257 | + ) | |
| 258 | + .collect(); | |
| 259 | + let columns = ((cells.len() as f32).sqrt().ceil() as usize).max(1); | |
| 260 | + let rows = cells.len().div_ceil(columns); | |
| 261 | + let mut cells = cells.into_iter(); | |
| 262 | + | |
| 263 | + Some( | |
| 264 | + div() | |
| 265 | + .relative() | |
| 266 | + .flex_1() | |
| 267 | + .min_h_0() | |
| 268 | + .p(px(8.)) | |
| 269 | + .flex() | |
| 270 | + .flex_col() | |
| 271 | + .gap(px(8.)) | |
| 272 | + .children((0..rows).map(|_| { | |
| 273 | + div() | |
| 274 | + .flex_1() | |
| 275 | + .min_h_0() | |
| 276 | + .flex() | |
| 277 | + .gap(px(8.)) | |
| 278 | + .children((0..columns).map(|_| { | |
| 279 | + cells | |
| 280 | + .next() | |
| 281 | + .unwrap_or_else(|| div().flex_1().into_any_element()) | |
| 282 | + })) | |
| 283 | + })) | |
| 284 | + .child( | |
| 285 | + icon_button( | |
| 286 | + "stage_minimize", | |
| 287 | + palette, | |
| 288 | + IconName::Minimize, | |
| 289 | + "Strip", | |
| 290 | + entity_action(entity, |yumush, _, cx| { | |
| 291 | + yumush.set_stage(Stage::Strip, cx) | |
| 292 | + }), | |
| 293 | + ) | |
| 294 | + .absolute() | |
| 295 | + .top(px(12.)) | |
| 296 | + .right(px(12.)) | |
| 297 | + .bg(palette.panel) | |
| 298 | + .occlude(), | |
| 299 | + ), | |
| 182 | 300 | ) |
| 183 | 301 | } |
| 184 | 302 | } |
| @@ -188,7 +306,7 @@ pub(super) fn ticker(cx: &mut Context<Yumush>) -> Task<()> { | ||
| 188 | 306 | loop { |
| 189 | 307 | cx.background_executor().timer(FRAME_TICK).await; |
| 190 | 308 | if this |
| 191 | - .update(cx, |yumush, cx| yumush.tick_cameras(cx)) | |
| 309 | + .update(cx, |yumush, cx| yumush.tick_tiles(cx)) | |
| 192 | 310 | .is_err() |
| 193 | 311 | { |
| 194 | 312 | break; |
| @@ -233,14 +351,12 @@ fn stage_tile( | ||
| 233 | 351 | let entity = entity.clone(); |
| 234 | 352 | let focused = yumush.is_watching(user_id, kind); |
| 235 | 353 | let speaking = yumush.chat.waves.speaking.contains(&user_id); |
| 236 | - let frame = match kind { | |
| 237 | - Kind::Camera => yumush | |
| 238 | - .chat | |
| 239 | - .cameras | |
| 240 | - .get(&user_id) | |
| 241 | - .and_then(|tile| tile.frame.clone()), | |
| 242 | - Kind::Screen => yumush.viewer_frame(user_id, kind), | |
| 243 | - }; | |
| 354 | + let frame = yumush | |
| 355 | + .chat | |
| 356 | + .tiles | |
| 357 | + .get(&(user_id, kind)) | |
| 358 | + .and_then(|tile| tile.frame.clone()) | |
| 359 | + .or_else(|| yumush.viewer_frame(user_id, kind)); | |
| 244 | 360 | let user = yumush.chat.author(user_id); |
| 245 | 361 | let name = user.map_or("Someone", |user| user.username.as_str()); |
| 246 | 362 | let content = match (frame, user) { |
| @@ -307,3 +423,99 @@ fn stage_tile( | ||
| 307 | 423 | .child(name.to_string()), |
| 308 | 424 | ) |
| 309 | 425 | } |
| 426 | + | |
| 427 | +fn grid_cell( | |
| 428 | + entity: &Entity<Yumush>, | |
| 429 | + yumush: &Yumush, | |
| 430 | + palette: Palette, | |
| 431 | + (user_id, kind): (Id, Kind), | |
| 432 | + shared: bool, | |
| 433 | +) -> AnyElement { | |
| 434 | + let frame = yumush | |
| 435 | + .chat | |
| 436 | + .tiles | |
| 437 | + .get(&(user_id, kind)) | |
| 438 | + .and_then(|tile| tile.frame.clone()); | |
| 439 | + let speaking = yumush.chat.waves.speaking.contains(&user_id); | |
| 440 | + let user = yumush.chat.author(user_id); | |
| 441 | + let name = user.map_or("Someone", |user| user.username.as_str()); | |
| 442 | + let content = match (frame, user) { | |
| 443 | + (Some(frame), _) => img(frame) | |
| 444 | + .size_full() | |
| 445 | + .object_fit(ObjectFit::Contain) | |
| 446 | + .into_any_element(), | |
| 447 | + (None, Some(user)) => div() | |
| 448 | + .size_full() | |
| 449 | + .flex() | |
| 450 | + .items_center() | |
| 451 | + .justify_center() | |
| 452 | + .child(avatar( | |
| 453 | + palette, | |
| 454 | + user, | |
| 455 | + px(64.), | |
| 456 | + text(24.), | |
| 457 | + &yumush.chat.avatars, | |
| 458 | + )) | |
| 459 | + .into_any_element(), | |
| 460 | + (None, None) => div().into_any_element(), | |
| 461 | + }; | |
| 462 | + | |
| 463 | + let cell = div() | |
| 464 | + .id(format!("grid_{user_id}_{kind:?}")) | |
| 465 | + .relative() | |
| 466 | + .flex_1() | |
| 467 | + .min_w_0() | |
| 468 | + .min_h_0() | |
| 469 | + .rounded(px(10.)) | |
| 470 | + .overflow_hidden() | |
| 471 | + .bg(gpui_kit::black()) | |
| 472 | + .border_2() | |
| 473 | + .border_color(if speaking { | |
| 474 | + palette.ok | |
| 475 | + } else { | |
| 476 | + palette.ok.opacity(0.) | |
| 477 | + }) | |
| 478 | + .child(content) | |
| 479 | + .child( | |
| 480 | + div() | |
| 481 | + .absolute() | |
| 482 | + .left(px(6.)) | |
| 483 | + .bottom(px(6.)) | |
| 484 | + .px(px(6.)) | |
| 485 | + .py(px(2.)) | |
| 486 | + .rounded(px(4.)) | |
| 487 | + .bg(palette.panel) | |
| 488 | + .text_size(text(11.)) | |
| 489 | + .flex() | |
| 490 | + .items_center() | |
| 491 | + .gap(px(4.)) | |
| 492 | + .children((kind == Kind::Screen).then(|| { | |
| 493 | + Icon::new(share_view::kind_icon(kind)) | |
| 494 | + .w(px(12.)) | |
| 495 | + .h(px(12.)) | |
| 496 | + })) | |
| 497 | + .child(name.to_string()), | |
| 498 | + ); | |
| 499 | + | |
| 500 | + if shared { | |
| 501 | + let entity = entity.clone(); | |
| 502 | + cell.cursor_pointer() | |
| 503 | + .on_click(move |_, window, cx| { | |
| 504 | + entity.update(cx, |yumush, cx| { | |
| 505 | + yumush.toggle_viewer(user_id, kind, window, cx) | |
| 506 | + }); | |
| 507 | + }) | |
| 508 | + .into_any_element() | |
| 509 | + } else { | |
| 510 | + cell.into_any_element() | |
| 511 | + } | |
| 512 | +} | |
| 513 | + | |
| 514 | +pub(super) fn stage_toggle(yumush: &mut Yumush, cx: &mut Context<Yumush>) { | |
| 515 | + let next = if yumush.chat.stage == Stage::Hidden { | |
| 516 | + Stage::Strip | |
| 517 | + } else { | |
| 518 | + Stage::Hidden | |
| 519 | + }; | |
| 520 | + yumush.set_stage(next, cx); | |
| 521 | +} |
crates/client/src/gui/chat.rsmodified+21 -12
| @@ -6,7 +6,7 @@ use std::{ | ||
| 6 | 6 | time::{Duration, Instant}, |
| 7 | 7 | }; |
| 8 | 8 | |
| 9 | -use crate::gui::camera_view::CameraTile; | |
| 9 | +use crate::gui::camera_view::{self, Stage, Tile}; | |
| 10 | 10 | use crate::gui::social::InviteLookup; |
| 11 | 11 | use crate::gui::voice_stats::{SAMPLE_HISTORY, StatsTab}; |
| 12 | 12 | use crate::gui::wave::Waves; |
| @@ -148,6 +148,8 @@ pub struct Chat { | ||
| 148 | 148 | pub(super) poll_question: Entity<InputState>, |
| 149 | 149 | pub(super) poll_options: Vec<Entity<InputState>>, |
| 150 | 150 | pub(super) poll_multiple: bool, |
| 151 | + pub(super) poll_public: bool, | |
| 152 | + pub(super) my_votes: HashMap<Id, Vec<u32>>, | |
| 151 | 153 | pub(super) poll_expanded: bool, |
| 152 | 154 | scroll: ScrollHandle, |
| 153 | 155 | pub(super) jump: Option<Id>, |
| @@ -255,9 +257,9 @@ pub struct Chat { | ||
| 255 | 257 | pub(super) media: Media, |
| 256 | 258 | pub(super) shares: HashMap<Id, Vec<Share>>, |
| 257 | 259 | pub(super) viewer: Option<Viewer>, |
| 258 | - pub(super) cameras: BTreeMap<Id, CameraTile>, | |
| 260 | + pub(super) tiles: BTreeMap<(Id, Kind), Tile>, | |
| 259 | 261 | pub(super) camera_task: Option<Task<()>>, |
| 260 | - pub(super) stage_hidden: bool, | |
| 262 | + pub(super) stage: Stage, | |
| 261 | 263 | pub(super) typing: HashMap<Id, HashMap<Id, Instant>>, |
| 262 | 264 | pub(super) typing_sent: HashMap<Id, Instant>, |
| 263 | 265 | } |
| @@ -351,6 +353,8 @@ impl Chat { | ||
| 351 | 353 | new_input("Option", window, cx), |
| 352 | 354 | ], |
| 353 | 355 | poll_multiple: false, |
| 356 | + poll_public: false, | |
| 357 | + my_votes: HashMap::new(), | |
| 354 | 358 | poll_expanded: false, |
| 355 | 359 | history: History::End, |
| 356 | 360 | scroll: ScrollHandle::new(), |
| @@ -486,9 +490,9 @@ impl Chat { | ||
| 486 | 490 | media: Media::default(), |
| 487 | 491 | shares: HashMap::new(), |
| 488 | 492 | viewer: None, |
| 489 | - cameras: BTreeMap::new(), | |
| 493 | + tiles: BTreeMap::new(), | |
| 490 | 494 | camera_task: None, |
| 491 | - stage_hidden: false, | |
| 495 | + stage: Stage::Strip, | |
| 492 | 496 | composer: Composer::Compose { reply: None }, |
| 493 | 497 | } |
| 494 | 498 | } |
| @@ -714,6 +718,7 @@ impl Chat { | ||
| 714 | 718 | self.jump = None; |
| 715 | 719 | self.pins.clear(); |
| 716 | 720 | self.poll_expanded = false; |
| 721 | + self.my_votes.clear(); | |
| 717 | 722 | self.community_hits.clear(); |
| 718 | 723 | self.history = History::End; |
| 719 | 724 | self.media.leave(); |
| @@ -1280,9 +1285,12 @@ impl Yumush { | ||
| 1280 | 1285 | .children(self.poll_banner(entity, themed)) |
| 1281 | 1286 | .map(|panel| match self.inline_viewer(entity, themed) { |
| 1282 | 1287 | Some(viewer) => panel.child(viewer), |
| 1283 | - None => panel | |
| 1284 | - .child(self.message_area(entity, themed)) | |
| 1285 | - .child(self.composer(entity, themed)), | |
| 1288 | + None => match self.stage_grid(entity, themed) { | |
| 1289 | + Some(grid) => panel.child(grid), | |
| 1290 | + None => panel | |
| 1291 | + .child(self.message_area(entity, themed)) | |
| 1292 | + .child(self.composer(entity, themed)), | |
| 1293 | + }, | |
| 1286 | 1294 | }), |
| 1287 | 1295 | None => panel.child(empty_state(palette)), |
| 1288 | 1296 | }, |
| @@ -1713,6 +1721,7 @@ impl Yumush { | ||
| 1713 | 1721 | let _ = this.update(cx, |yumush, cx| { |
| 1714 | 1722 | if yumush.chat.is_selected(community_id) { |
| 1715 | 1723 | yumush.chat.pins = pins; |
| 1724 | + yumush.request_poll_votes(cx); | |
| 1716 | 1725 | cx.notify(); |
| 1717 | 1726 | } |
| 1718 | 1727 | }); |
| @@ -1886,6 +1895,7 @@ impl Yumush { | ||
| 1886 | 1895 | yumush.mark_read(community_id, message_id, cx); |
| 1887 | 1896 | } |
| 1888 | 1897 | yumush.request_media(cx); |
| 1898 | + yumush.request_poll_votes(cx); | |
| 1889 | 1899 | yumush.request_authors(cx); |
| 1890 | 1900 | cx.notify(); |
| 1891 | 1901 | } |
| @@ -2456,7 +2466,7 @@ impl Yumush { | ||
| 2456 | 2466 | return None; |
| 2457 | 2467 | } |
| 2458 | 2468 | let stage = self.stage_sources().len(); |
| 2459 | - let hidden = self.chat.stage_hidden; | |
| 2469 | + let hidden = self.chat.stage == Stage::Hidden; | |
| 2460 | 2470 | |
| 2461 | 2471 | Some( |
| 2462 | 2472 | div() |
| @@ -2483,14 +2493,13 @@ impl Yumush { | ||
| 2483 | 2493 | ) |
| 2484 | 2494 | })) |
| 2485 | 2495 | .children( |
| 2486 | - (stage > 0 && self.chat.selected_community_id == self.chat.voice_community()) | |
| 2496 | + self.in_stage_chat() | |
| 2487 | 2497 | .then(|| { |
| 2488 | 2498 | clickable( |
| 2489 | 2499 | "stage_toggle", |
| 2490 | 2500 | palette, |
| 2491 | 2501 | entity_action(entity, |yumush: &mut Yumush, _, cx| { |
| 2492 | - yumush.chat.stage_hidden = !yumush.chat.stage_hidden; | |
| 2493 | - cx.notify(); | |
| 2502 | + camera_view::stage_toggle(yumush, cx) | |
| 2494 | 2503 | }), |
| 2495 | 2504 | ) |
| 2496 | 2505 | .tip(if hidden { "Show stage" } else { "Hide stage" }) |
crates/client/src/gui/composer.rsmodified+36 -9
| @@ -565,6 +565,7 @@ pub fn poll_popover(chat: &Chat, entity: &Entity<Yumush>, palette: Palette) -> i | ||
| 565 | 565 | let question = chat.poll_question.clone(); |
| 566 | 566 | let options = chat.poll_options.clone(); |
| 567 | 567 | let multiple = chat.poll_multiple; |
| 568 | + let public = chat.poll_public; | |
| 568 | 569 | let entity = entity.clone(); |
| 569 | 570 | |
| 570 | 571 | Popover::new("poll_popover") |
| @@ -579,6 +580,7 @@ pub fn poll_popover(chat: &Chat, entity: &Entity<Yumush>, palette: Palette) -> i | ||
| 579 | 580 | .content(move |_, _, cx| { |
| 580 | 581 | let popover = cx.entity(); |
| 581 | 582 | let toggle = entity.clone(); |
| 583 | + let toggle_public = entity.clone(); | |
| 582 | 584 | let add = entity.clone(); |
| 583 | 585 | let submit = entity.clone(); |
| 584 | 586 | pop_in( |
| @@ -623,15 +625,32 @@ pub fn poll_popover(chat: &Chat, entity: &Entity<Yumush>, palette: Palette) -> i | ||
| 623 | 625 | .items_center() |
| 624 | 626 | .justify_between() |
| 625 | 627 | .child( |
| 626 | - Checkbox::new("poll_multiple") | |
| 627 | - .label("Multiple answers") | |
| 628 | - .checked(multiple) | |
| 629 | - .on_click(move |_, _, cx| { | |
| 630 | - toggle.update(cx, |yumush, cx| { | |
| 631 | - yumush.chat.poll_multiple = !yumush.chat.poll_multiple; | |
| 632 | - cx.notify(); | |
| 633 | - }) | |
| 634 | - }), | |
| 628 | + div() | |
| 629 | + .flex() | |
| 630 | + .items_center() | |
| 631 | + .gap(px(12.)) | |
| 632 | + .child( | |
| 633 | + Checkbox::new("poll_multiple") | |
| 634 | + .label("Multiple answers") | |
| 635 | + .checked(multiple) | |
| 636 | + .on_click(move |_, _, cx| { | |
| 637 | + toggle.update(cx, |yumush, cx| { | |
| 638 | + yumush.chat.poll_multiple = | |
| 639 | + !yumush.chat.poll_multiple; | |
| 640 | + cx.notify(); | |
| 641 | + }) | |
| 642 | + }), | |
| 643 | + ) | |
| 644 | + .child( | |
| 645 | + Checkbox::new("poll_public") | |
| 646 | + .label("Show voters") | |
| 647 | + .checked(public) | |
| 648 | + .on_click(move |_, _, cx| { | |
| 649 | + toggle_public.update(cx, |yumush, cx| { | |
| 650 | + yumush.toggle_poll_public(cx) | |
| 651 | + }) | |
| 652 | + }), | |
| 653 | + ), | |
| 635 | 654 | ) |
| 636 | 655 | .children((options.len() < MAXIMUM_POLL_OPTIONS).then(|| { |
| 637 | 656 | icon_button( |
| @@ -699,6 +718,7 @@ impl Yumush { | ||
| 699 | 718 | return false; |
| 700 | 719 | } |
| 701 | 720 | let multiple = self.chat.poll_multiple; |
| 721 | + let public = self.chat.poll_public; | |
| 702 | 722 | let network = self.network.clone(); |
| 703 | 723 | |
| 704 | 724 | spawn_done(window, cx, async move { |
| @@ -708,6 +728,7 @@ impl Yumush { | ||
| 708 | 728 | question, |
| 709 | 729 | options, |
| 710 | 730 | multiple, |
| 731 | + public, | |
| 711 | 732 | }) |
| 712 | 733 | .await |
| 713 | 734 | }); |
| @@ -720,7 +741,13 @@ impl Yumush { | ||
| 720 | 741 | input.update(cx, |state, cx| state.set_value("", window, cx)); |
| 721 | 742 | } |
| 722 | 743 | self.chat.poll_multiple = false; |
| 744 | + self.chat.poll_public = false; | |
| 723 | 745 | cx.notify(); |
| 724 | 746 | true |
| 725 | 747 | } |
| 748 | + | |
| 749 | + pub(super) fn toggle_poll_public(&mut self, cx: &mut Context<Self>) { | |
| 750 | + self.chat.poll_public = !self.chat.poll_public; | |
| 751 | + cx.notify(); | |
| 752 | + } | |
| 726 | 753 | } |
crates/client/src/gui/poll.rsmodified+142 -31
| @@ -1,7 +1,5 @@ | ||
| 1 | 1 | use common::model::Id; |
| 2 | -use std::collections::HashSet; | |
| 3 | - | |
| 4 | -use common::call::{ClosePoll, Vote}; | |
| 2 | +use common::call::{ClosePoll, MyVotes, Vote}; | |
| 5 | 3 | use common::model::{Message, Poll, Rank, User}; |
| 6 | 4 | use gpui_kit::{ |
| 7 | 5 | Context, Div, Entity, FontWeight, InteractiveElement, IntoElement, ParentElement, Styled, |
| @@ -9,18 +7,14 @@ use gpui_kit::{ | ||
| 9 | 7 | }; |
| 10 | 8 | |
| 11 | 9 | use super::{ |
| 12 | - Palette, Yumush, | |
| 10 | + Palette, Tooltip as _, Yumush, avatar, | |
| 13 | 11 | chat::Chat, |
| 14 | 12 | clickable, text, |
| 15 | 13 | widget::{icon_button, spawn_done}, |
| 16 | 14 | }; |
| 17 | 15 | |
| 18 | 16 | pub fn voter_count(poll: &Poll) -> usize { |
| 19 | - poll.options | |
| 20 | - .iter() | |
| 21 | - .flat_map(|option| &option.voter_ids) | |
| 22 | - .collect::<HashSet<_>>() | |
| 23 | - .len() | |
| 17 | + poll.voters as usize | |
| 24 | 18 | } |
| 25 | 19 | |
| 26 | 20 | fn my_positions(poll: &Poll, user_id: Id) -> Vec<u32> { |
| @@ -54,6 +48,15 @@ pub enum PollMode<'a> { | ||
| 54 | 48 | Snapshot(Id), |
| 55 | 49 | } |
| 56 | 50 | |
| 51 | +fn status_icon(id: String, icon: IconName, tip: &'static str, palette: Palette) -> impl IntoElement { | |
| 52 | + div() | |
| 53 | + .id(id) | |
| 54 | + .tip(tip) | |
| 55 | + .flex_shrink_0() | |
| 56 | + .text_color(palette.muted) | |
| 57 | + .child(Icon::new(icon).w(px(12.)).h(px(12.))) | |
| 58 | +} | |
| 59 | + | |
| 57 | 60 | pub fn poll_card( |
| 58 | 61 | poll: &Poll, |
| 59 | 62 | mode: PollMode, |
| @@ -64,11 +67,17 @@ pub fn poll_card( | ||
| 64 | 67 | ) -> impl IntoElement { |
| 65 | 68 | let voters = voter_count(poll); |
| 66 | 69 | let me_id = me.map(|me| me.id); |
| 67 | - let mine = me_id.map(|id| my_positions(poll, id)).unwrap_or_default(); | |
| 68 | 70 | let (key, live) = match mode { |
| 69 | 71 | PollMode::Live(message) => (message.id.to_string(), Some(message)), |
| 70 | 72 | PollMode::Snapshot(id) => (format!("snapshot_{id}"), None), |
| 71 | 73 | }; |
| 74 | + let mine = match me_id { | |
| 75 | + Some(id) if poll.public => my_positions(poll, id), | |
| 76 | + _ => live | |
| 77 | + .and_then(|message| chat.my_votes.get(&message.id)) | |
| 78 | + .cloned() | |
| 79 | + .unwrap_or_default(), | |
| 80 | + }; | |
| 72 | 81 | let vote_id = live |
| 73 | 82 | .filter(|_| !poll.closed && me_id.is_some()) |
| 74 | 83 | .map(|message| message.id); |
| @@ -82,12 +91,16 @@ pub fn poll_card( | ||
| 82 | 91 | .map(|message| message.id); |
| 83 | 92 | |
| 84 | 93 | let rows = poll.options.iter().enumerate().map(|(position, option)| { |
| 85 | - let count = option.voter_ids.len(); | |
| 86 | - let share = percent(count, voters); | |
| 94 | + let share = percent(option.votes as usize, voters); | |
| 87 | 95 | let picked = mine.contains(&(position as u32)); |
| 88 | 96 | let next = me_id |
| 89 | 97 | .map(|_| next_votes(&mine, position as u32, poll.multiple)) |
| 90 | 98 | .unwrap_or_default(); |
| 99 | + let voters_here: &[Id] = if poll.public { | |
| 100 | + &option.voter_ids | |
| 101 | + } else { | |
| 102 | + &[] | |
| 103 | + }; | |
| 91 | 104 | let entity = entity.clone(); |
| 92 | 105 | let id = format!("poll_option_{key}_{position}"); |
| 93 | 106 | match vote_id { |
| @@ -98,12 +111,18 @@ pub fn poll_card( | ||
| 98 | 111 | None => div().id(id), |
| 99 | 112 | } |
| 100 | 113 | .relative() |
| 101 | - .h(px(32.)) | |
| 114 | + .h(px(36.)) | |
| 102 | 115 | .px(px(10.)) |
| 103 | 116 | .flex() |
| 104 | 117 | .items_center() |
| 105 | 118 | .gap(px(8.)) |
| 106 | - .rounded(px(6.)) | |
| 119 | + .rounded(px(8.)) | |
| 120 | + .border_1() | |
| 121 | + .border_color(if picked { | |
| 122 | + palette.accent | |
| 123 | + } else { | |
| 124 | + palette.accent.opacity(0.) | |
| 125 | + }) | |
| 107 | 126 | .overflow_hidden() |
| 108 | 127 | .bg(palette.field) |
| 109 | 128 | .child( |
| @@ -143,6 +162,28 @@ pub fn poll_card( | ||
| 143 | 162 | .truncate() |
| 144 | 163 | .child(option.text.clone()), |
| 145 | 164 | ) |
| 165 | + .children((!voters_here.is_empty()).then(|| { | |
| 166 | + div() | |
| 167 | + .flex() | |
| 168 | + .items_center() | |
| 169 | + .children(voters_here.iter().take(3).enumerate().filter_map( | |
| 170 | + |(index, id)| { | |
| 171 | + chat.author(*id).map(|user| { | |
| 172 | + avatar(palette, user, px(18.), text(8.), &chat.avatars) | |
| 173 | + .border_2() | |
| 174 | + .border_color(palette.panel) | |
| 175 | + .when(index > 0, |el| el.ml(px(-6.))) | |
| 176 | + }) | |
| 177 | + }, | |
| 178 | + )) | |
| 179 | + .children((voters_here.len() > 3).then(|| { | |
| 180 | + div() | |
| 181 | + .ml(px(4.)) | |
| 182 | + .text_size(text(11.)) | |
| 183 | + .text_color(palette.muted) | |
| 184 | + .child(format!("+{}", voters_here.len() - 3)) | |
| 185 | + })) | |
| 186 | + })) | |
| 146 | 187 | .child( |
| 147 | 188 | div() |
| 148 | 189 | .flex_shrink_0() |
| @@ -159,11 +200,14 @@ pub fn poll_card( | ||
| 159 | 200 | .justify_between() |
| 160 | 201 | .text_size(text(12.)) |
| 161 | 202 | .text_color(palette.muted) |
| 162 | - .child(match voters { | |
| 163 | - 1 => "1 vote".to_string(), | |
| 164 | - _ => format!("{voters} votes"), | |
| 165 | - }) | |
| 166 | - .children(poll.closed.then_some("Closed")) | |
| 203 | + .child( | |
| 204 | + div() | |
| 205 | + .flex() | |
| 206 | + .items_center() | |
| 207 | + .gap(px(4.)) | |
| 208 | + .child(Icon::new(IconName::Users).w(px(12.)).h(px(12.))) | |
| 209 | + .child(format!("{voters}")), | |
| 210 | + ) | |
| 167 | 211 | .children(close_id.map(|close_id| { |
| 168 | 212 | let close_entity = entity.clone(); |
| 169 | 213 | icon_button( |
| @@ -177,32 +221,64 @@ pub fn poll_card( | ||
| 177 | 221 | ) |
| 178 | 222 | })); |
| 179 | 223 | |
| 224 | + let (visibility, visibility_tip) = if poll.public { | |
| 225 | + (IconName::Eye, "Voters visible") | |
| 226 | + } else { | |
| 227 | + (IconName::EyeOff, "Anonymous") | |
| 228 | + }; | |
| 229 | + | |
| 180 | 230 | div() |
| 181 | 231 | .flex() |
| 182 | 232 | .flex_col() |
| 183 | - .gap(px(6.)) | |
| 184 | - .p(px(12.)) | |
| 185 | - .w(px(320.)) | |
| 233 | + .gap(px(8.)) | |
| 234 | + .p(px(14.)) | |
| 235 | + .w(px(360.)) | |
| 186 | 236 | .max_w_full() |
| 187 | - .rounded(px(10.)) | |
| 237 | + .rounded(px(12.)) | |
| 238 | + .border_1() | |
| 239 | + .border_color(palette.border) | |
| 188 | 240 | .bg(palette.panel) |
| 189 | 241 | .child( |
| 190 | 242 | div() |
| 191 | 243 | .flex() |
| 192 | - .items_center() | |
| 244 | + .items_start() | |
| 245 | + .justify_between() | |
| 193 | 246 | .gap(px(8.)) |
| 194 | - .mb(px(2.)) | |
| 195 | - .child( | |
| 196 | - div() | |
| 197 | - .flex_shrink_0() | |
| 198 | - .text_color(palette.accent) | |
| 199 | - .child(Icon::new(IconName::ChartBar).w(px(14.)).h(px(14.))), | |
| 200 | - ) | |
| 201 | 247 | .child( |
| 202 | 248 | div() |
| 203 | 249 | .min_w_0() |
| 204 | 250 | .font_weight(FontWeight::SEMIBOLD) |
| 251 | + .text_size(text(15.)) | |
| 205 | 252 | .child(poll.question.clone()), |
| 253 | + ) | |
| 254 | + .child( | |
| 255 | + div() | |
| 256 | + .flex_shrink_0() | |
| 257 | + .flex() | |
| 258 | + .items_center() | |
| 259 | + .gap(px(6.)) | |
| 260 | + .child(status_icon( | |
| 261 | + format!("poll_public_{key}"), | |
| 262 | + visibility, | |
| 263 | + visibility_tip, | |
| 264 | + palette, | |
| 265 | + )) | |
| 266 | + .children(poll.multiple.then(|| { | |
| 267 | + status_icon( | |
| 268 | + format!("poll_multiple_{key}"), | |
| 269 | + IconName::ListChecks, | |
| 270 | + "Multiple choice", | |
| 271 | + palette, | |
| 272 | + ) | |
| 273 | + })) | |
| 274 | + .children(poll.closed.then(|| { | |
| 275 | + status_icon( | |
| 276 | + format!("poll_closed_{key}"), | |
| 277 | + IconName::Lock, | |
| 278 | + "Closed", | |
| 279 | + palette, | |
| 280 | + ) | |
| 281 | + })), | |
| 206 | 282 | ), |
| 207 | 283 | ) |
| 208 | 284 | .children(rows) |
| @@ -307,6 +383,40 @@ impl Yumush { | ||
| 307 | 383 | ) |
| 308 | 384 | } |
| 309 | 385 | |
| 386 | + pub(super) fn request_poll_votes(&mut self, cx: &mut Context<Self>) { | |
| 387 | + let anonymous: Vec<Id> = self | |
| 388 | + .chat | |
| 389 | + .messages | |
| 390 | + .iter() | |
| 391 | + .chain(&self.chat.pins) | |
| 392 | + .filter(|message| message.poll.as_ref().is_some_and(|poll| !poll.public)) | |
| 393 | + .map(|message| message.id) | |
| 394 | + .collect(); | |
| 395 | + for message_id in anonymous { | |
| 396 | + self.request_my_votes(message_id, cx); | |
| 397 | + } | |
| 398 | + } | |
| 399 | + | |
| 400 | + pub(super) fn request_my_votes(&mut self, message_id: Id, cx: &mut Context<Self>) { | |
| 401 | + if self.chat.my_votes.contains_key(&message_id) { | |
| 402 | + return; | |
| 403 | + } | |
| 404 | + self.chat.my_votes.insert(message_id, Vec::new()); | |
| 405 | + | |
| 406 | + let network = self.network.clone(); | |
| 407 | + | |
| 408 | + cx.spawn(async move |this, cx| { | |
| 409 | + let Ok(positions) = network.call(MyVotes { message_id }).await else { | |
| 410 | + return; | |
| 411 | + }; | |
| 412 | + let _ = this.update(cx, |yumush, cx| { | |
| 413 | + yumush.chat.my_votes.insert(message_id, positions); | |
| 414 | + cx.notify(); | |
| 415 | + }); | |
| 416 | + }) | |
| 417 | + .detach(); | |
| 418 | + } | |
| 419 | + | |
| 310 | 420 | pub(super) fn vote( |
| 311 | 421 | &mut self, |
| 312 | 422 | message_id: Id, |
| @@ -314,6 +424,7 @@ impl Yumush { | ||
| 314 | 424 | window: &mut Window, |
| 315 | 425 | cx: &mut Context<Self>, |
| 316 | 426 | ) { |
| 427 | + self.chat.my_votes.insert(message_id, positions.clone()); | |
| 317 | 428 | let network = self.network.clone(); |
| 318 | 429 | |
| 319 | 430 | spawn_done(window, cx, async move { |
crates/client/src/gui/share_view.rsmodified+10 -7
| @@ -82,7 +82,7 @@ pub(super) struct Viewer { | ||
| 82 | 82 | kind: Kind, |
| 83 | 83 | generation: Option<u64>, |
| 84 | 84 | frame: Option<Arc<RenderImage>>, |
| 85 | - fullscreen: bool, | |
| 85 | + pub(super) fullscreen: bool, | |
| 86 | 86 | volume: Entity<SliderState>, |
| 87 | 87 | _subscriptions: Vec<Subscription>, |
| 88 | 88 | _tick: Task<()>, |
| @@ -207,12 +207,11 @@ impl Yumush { | ||
| 207 | 207 | .and_then(|viewer| viewer.frame.clone()) |
| 208 | 208 | } |
| 209 | 209 | |
| 210 | - pub(super) fn viewing_camera(&self) -> Option<Id> { | |
| 210 | + pub(super) fn viewing(&self) -> Option<(Id, Kind)> { | |
| 211 | 211 | self.chat |
| 212 | 212 | .viewer |
| 213 | 213 | .as_ref() |
| 214 | - .filter(|viewer| viewer.kind == Kind::Camera) | |
| 215 | - .map(|viewer| viewer.user_id) | |
| 214 | + .map(|viewer| (viewer.user_id, viewer.kind)) | |
| 216 | 215 | } |
| 217 | 216 | |
| 218 | 217 | pub(super) fn toggle_viewer( |
| @@ -268,7 +267,7 @@ impl Yumush { | ||
| 268 | 267 | let watch = WatchQuality::Auto; |
| 269 | 268 | let volume = self.settings.share.volume; |
| 270 | 269 | let choice = self.settings.voice.choice.clone(); |
| 271 | - if kind == Kind::Camera && self.chat.cameras.contains_key(&user_id) { | |
| 270 | + if self.chat.tiles.contains_key(&(user_id, kind)) { | |
| 272 | 271 | network.set_watch_quality(user_id, kind, watch); |
| 273 | 272 | } else { |
| 274 | 273 | cx.spawn_in(window, async move |this, cx| { |
| @@ -297,8 +296,12 @@ impl Yumush { | ||
| 297 | 296 | cx.defer(move |cx| cx.drop_image(frame, None)); |
| 298 | 297 | } |
| 299 | 298 | let network = self.network.clone(); |
| 300 | - if viewer.kind == Kind::Camera && self.chat.cameras.contains_key(&viewer.user_id) { | |
| 301 | - network.set_watch_quality(viewer.user_id, viewer.kind, camera_view::CAMERA_TILE); | |
| 299 | + if self.chat.tiles.contains_key(&(viewer.user_id, viewer.kind)) { | |
| 300 | + network.set_watch_quality( | |
| 301 | + viewer.user_id, | |
| 302 | + viewer.kind, | |
| 303 | + camera_view::tile_quality(self.chat.stage), | |
| 304 | + ); | |
| 302 | 305 | } else { |
| 303 | 306 | let user_id = viewer.user_id; |
| 304 | 307 | let kind = viewer.kind; |
crates/common/src/call.rsmodified+7 -0
| @@ -221,6 +221,7 @@ pub struct CreatePoll { | ||
| 221 | 221 | pub question: String, |
| 222 | 222 | pub options: Vec<String>, |
| 223 | 223 | pub multiple: bool, |
| 224 | + pub public: bool, | |
| 224 | 225 | } |
| 225 | 226 | |
| 226 | 227 | #[derive(Debug, Clone, Encode, Decode)] |
| @@ -234,6 +235,11 @@ pub struct ClosePoll { | ||
| 234 | 235 | pub message_id: Id, |
| 235 | 236 | } |
| 236 | 237 | |
| 238 | +#[derive(Debug, Clone, Encode, Decode)] | |
| 239 | +pub struct MyVotes { | |
| 240 | + pub message_id: Id, | |
| 241 | +} | |
| 242 | + | |
| 237 | 243 | #[derive(Debug, Clone, Encode, Decode)] |
| 238 | 244 | pub struct React { |
| 239 | 245 | pub message_id: Id, |
| @@ -648,6 +654,7 @@ calls! { | ||
| 648 | 654 | CreatePoll: User -> Message, |c| validate_poll(&c.question, &c.options); |
| 649 | 655 | Vote: User -> (); |
| 650 | 656 | ClosePoll: User -> (); |
| 657 | + MyVotes: User -> Vec<u32>; | |
| 651 | 658 | React: User -> (), |c| validate_reaction(&c.emoji); |
| 652 | 659 | Pin: User -> (); |
| 653 | 660 | ListPins: User -> Vec<Message>; |
crates/common/src/model.rsmodified+3 -0
| @@ -566,6 +566,7 @@ pub struct Reaction { | ||
| 566 | 566 | #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] |
| 567 | 567 | pub struct PollOption { |
| 568 | 568 | pub text: String, |
| 569 | + pub votes: u32, | |
| 569 | 570 | pub voter_ids: Vec<Id>, |
| 570 | 571 | } |
| 571 | 572 | |
| @@ -575,6 +576,8 @@ pub struct Poll { | ||
| 575 | 576 | pub options: Vec<PollOption>, |
| 576 | 577 | pub multiple: bool, |
| 577 | 578 | pub closed: bool, |
| 579 | + pub public: bool, | |
| 580 | + pub voters: u32, | |
| 578 | 581 | } |
| 579 | 582 | |
| 580 | 583 | #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] |
crates/server/src/call/message.rsmodified+13 -1
| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | use common::{ |
| 2 | 2 | call::{ |
| 3 | 3 | ClosePoll, CreateMessage, CreatePoll, DeleteMessage, ForwardMessages, ListMessages, |
| 4 | - ListPins, ListUnreads, MarkRead, Pin, React, Typing, UpdateMessage, Vote, | |
| 4 | + ListPins, ListUnreads, MarkRead, MyVotes, Pin, React, Typing, UpdateMessage, Vote, | |
| 5 | 5 | }, |
| 6 | 6 | error::Error, |
| 7 | 7 | event::Event, |
| @@ -166,6 +166,7 @@ impl Handle for CreatePoll { | ||
| 166 | 166 | &self.question, |
| 167 | 167 | &self.options, |
| 168 | 168 | self.multiple, |
| 169 | + self.public, | |
| 169 | 170 | cx.db, |
| 170 | 171 | ) |
| 171 | 172 | .await?; |
| @@ -192,6 +193,17 @@ impl Handle for Vote { | ||
| 192 | 193 | } |
| 193 | 194 | } |
| 194 | 195 | |
| 196 | +impl Handle for MyVotes { | |
| 197 | + async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<Vec<u32>, ServerError> { | |
| 198 | + let me = me.user()?; | |
| 199 | + let existing = db::message::read(self.message_id, cx.db).await?; | |
| 200 | + ensure(permission::can_read( | |
| 201 | + actor_rank(&me, existing.community_id, cx.db).await?, | |
| 202 | + ))?; | |
| 203 | + db::poll::my_votes(self.message_id, me.id, cx.db).await | |
| 204 | + } | |
| 205 | +} | |
| 206 | + | |
| 195 | 207 | impl Handle for ClosePoll { |
| 196 | 208 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<(), ServerError> { |
| 197 | 209 | let me = me.user()?; |
crates/server/src/call/mod.rsmodified+1 -0
| @@ -138,6 +138,7 @@ pub(crate) async fn dispatch(request: Request, cx: &mut Cx<'_>) -> Vec<u8> { | ||
| 138 | 138 | Request::CreatePoll(call) => serve(call, cx).await, |
| 139 | 139 | Request::Vote(call) => serve(call, cx).await, |
| 140 | 140 | Request::ClosePoll(call) => serve(call, cx).await, |
| 141 | + Request::MyVotes(call) => serve(call, cx).await, | |
| 141 | 142 | Request::React(call) => serve(call, cx).await, |
| 142 | 143 | Request::Pin(call) => serve(call, cx).await, |
| 143 | 144 | Request::ListPins(call) => serve(call, cx).await, |
crates/server/src/db/message.rsmodified+2 -1
| @@ -192,10 +192,11 @@ pub async fn create_poll( | ||
| 192 | 192 | question: &str, |
| 193 | 193 | options: &[String], |
| 194 | 194 | multiple: bool, |
| 195 | + public: bool, | |
| 195 | 196 | db: &Connection, |
| 196 | 197 | ) -> Result<Message, ServerError> { |
| 197 | 198 | let id = insert(user_id, community_id, "", None, db).await?; |
| 198 | - poll::create(id, question, options, multiple, db).await?; | |
| 199 | + poll::create(id, question, options, multiple, public, db).await?; | |
| 199 | 200 | |
| 200 | 201 | read(id, db).await |
| 201 | 202 | } |
crates/server/src/db/poll.rsmodified+41 -6
| @@ -1,3 +1,5 @@ | ||
| 1 | +use std::collections::HashSet; | |
| 2 | + | |
| 1 | 3 | use common::{ |
| 2 | 4 | error::Error, |
| 3 | 5 | model::{Id, Poll, PollOption}, |
| @@ -11,12 +13,13 @@ pub async fn create( | ||
| 11 | 13 | question: &str, |
| 12 | 14 | options: &[String], |
| 13 | 15 | multiple: bool, |
| 16 | + public: bool, | |
| 14 | 17 | db: &Connection, |
| 15 | 18 | ) -> Result<(), ServerError> { |
| 16 | 19 | run( |
| 17 | 20 | db, |
| 18 | - "INSERT INTO poll (message_id, question, multiple) VALUES (?1, ?2, ?3)", | |
| 19 | - (message_id, question, multiple), | |
| 21 | + "INSERT INTO poll (message_id, question, multiple, public) VALUES (?1, ?2, ?3, ?4)", | |
| 22 | + (message_id, question, multiple, public), | |
| 20 | 23 | ) |
| 21 | 24 | .await?; |
| 22 | 25 | for (position, text) in (0u32..).zip(options) { |
| @@ -32,11 +35,18 @@ pub async fn create( | ||
| 32 | 35 | } |
| 33 | 36 | |
| 34 | 37 | pub async fn of(message_id: Id, db: &Connection) -> Result<Option<Poll>, ServerError> { |
| 35 | - let Some((question, multiple, closed)) = maybe( | |
| 38 | + let Some((question, multiple, closed, public)) = maybe( | |
| 36 | 39 | db, |
| 37 | - "SELECT question, multiple, closed FROM poll WHERE message_id = ?1", | |
| 40 | + "SELECT question, multiple, closed, public FROM poll WHERE message_id = ?1", | |
| 38 | 41 | (message_id,), |
| 39 | - |r| Ok((r.get::<String>()?, r.get::<bool>()?, r.get::<bool>()?)), | |
| 42 | + |r| { | |
| 43 | + Ok(( | |
| 44 | + r.get::<String>()?, | |
| 45 | + r.get::<bool>()?, | |
| 46 | + r.get::<bool>()?, | |
| 47 | + r.get::<bool>()?, | |
| 48 | + )) | |
| 49 | + }, | |
| 40 | 50 | ) |
| 41 | 51 | .await? |
| 42 | 52 | else { |
| @@ -50,6 +60,7 @@ pub async fn of(message_id: Id, db: &Connection) -> Result<Option<Poll>, ServerE | ||
| 50 | 60 | |r| { |
| 51 | 61 | Ok(PollOption { |
| 52 | 62 | text: r.get()?, |
| 63 | + votes: 0, | |
| 53 | 64 | voter_ids: Vec::new(), |
| 54 | 65 | }) |
| 55 | 66 | }, |
| @@ -62,9 +73,17 @@ pub async fn of(message_id: Id, db: &Connection) -> Result<Option<Poll>, ServerE | ||
| 62 | 73 | |r| Ok((r.get()?, r.get()?)), |
| 63 | 74 | ) |
| 64 | 75 | .await?; |
| 76 | + let voters = votes | |
| 77 | + .iter() | |
| 78 | + .map(|(_, user_id)| user_id) | |
| 79 | + .collect::<HashSet<_>>() | |
| 80 | + .len() as u32; | |
| 65 | 81 | for (position, user_id) in votes { |
| 66 | 82 | if let Some(option) = options.get_mut(position as usize) { |
| 67 | - option.voter_ids.push(user_id); | |
| 83 | + option.votes += 1; | |
| 84 | + if public { | |
| 85 | + option.voter_ids.push(user_id); | |
| 86 | + } | |
| 68 | 87 | } |
| 69 | 88 | } |
| 70 | 89 | |
| @@ -73,6 +92,8 @@ pub async fn of(message_id: Id, db: &Connection) -> Result<Option<Poll>, ServerE | ||
| 73 | 92 | options, |
| 74 | 93 | multiple, |
| 75 | 94 | closed, |
| 95 | + public, | |
| 96 | + voters, | |
| 76 | 97 | })) |
| 77 | 98 | } |
| 78 | 99 | |
| @@ -100,6 +121,20 @@ pub async fn set_votes( | ||
| 100 | 121 | Ok(()) |
| 101 | 122 | } |
| 102 | 123 | |
| 124 | +pub async fn my_votes( | |
| 125 | + message_id: Id, | |
| 126 | + user_id: Id, | |
| 127 | + db: &Connection, | |
| 128 | +) -> Result<Vec<u32>, ServerError> { | |
| 129 | + all( | |
| 130 | + db, | |
| 131 | + "SELECT position FROM poll_vote WHERE message_id = ?1 AND user_id = ?2 ORDER BY position", | |
| 132 | + (message_id, user_id), | |
| 133 | + |r| r.get(), | |
| 134 | + ) | |
| 135 | + .await | |
| 136 | +} | |
| 137 | + | |
| 103 | 138 | pub async fn close(message_id: Id, db: &Connection) -> Result<(), ServerError> { |
| 104 | 139 | ensure_with( |
| 105 | 140 | run( |
crates/server/src/db/schema.sqlmodified+2 -1
| @@ -122,7 +122,8 @@ CREATE TABLE poll ( | ||
| 122 | 122 | message_id INTEGER PRIMARY KEY REFERENCES message (id) ON DELETE CASCADE, |
| 123 | 123 | question TEXT NOT NULL, |
| 124 | 124 | multiple INTEGER NOT NULL, |
| 125 | - closed INTEGER NOT NULL DEFAULT 0 | |
| 125 | + closed INTEGER NOT NULL DEFAULT 0, | |
| 126 | + public INTEGER NOT NULL DEFAULT 0 | |
| 126 | 127 | ) STRICT; |
| 127 | 128 | |
| 128 | 129 | CREATE TABLE poll_option ( |