diff --git a/crates/client/src/gui/camera_view.rs b/crates/client/src/gui/camera_view.rs index 1dee2fe..a4f1c61 100644 --- a/crates/client/src/gui/camera_view.rs +++ b/crates/client/src/gui/camera_view.rs @@ -3,15 +3,15 @@ use std::sync::Arc; use common::share::{Fps, Kind, Quality, Resolution}; use gpui_kit::{ - Context, Entity, InteractiveElement, IntoElement, ObjectFit, ParentElement, RenderImage, - StatefulInteractiveElement, Styled, StyledImage, Task, Window, + AnyElement, Context, Entity, InteractiveElement, IntoElement, ObjectFit, ParentElement, + RenderImage, StatefulInteractiveElement, Styled, StyledImage, Task, Window, assets::IconName, component::Icon, div, img, px, }; use super::{ - Palette, Tooltip as _, Yumush, avatar, clickable, + Palette, Tooltip as _, Yumush, avatar, clickable, entity_action, media_view::to_render_image, notify_error, share_view::{self, FRAME_TICK}, @@ -20,86 +20,100 @@ use super::{ }; use crate::share::session::WatchQuality; -pub(super) const CAMERA_TILE: WatchQuality = WatchQuality::Fixed(Quality { +pub(super) const TILE_QUALITY: WatchQuality = WatchQuality::Fixed(Quality { resolution: Resolution::P360, fps: Fps::F30, }); -pub(super) struct CameraTile { +#[derive(Clone, Copy, PartialEq, Eq)] +pub(super) enum Stage { + Strip, + Grid, + Hidden, +} + +pub(super) fn tile_quality(stage: Stage) -> WatchQuality { + match stage { + Stage::Grid => WatchQuality::Auto, + Stage::Strip | Stage::Hidden => TILE_QUALITY, + } +} + +pub(super) struct Tile { pub(super) generation: Option, pub(super) frame: Option>, } impl Yumush { - fn camera_users(&self) -> Vec { - let Some(community_id) = self.chat.voice_community() else { - return Vec::new(); - }; - self.chat - .shares - .get(&community_id) + fn tile_sources(&self) -> Vec<(Id, Kind)> { + let grid = self.chat.stage == Stage::Grid; + self.stage_sources() .into_iter() - .flatten() - .filter(|share| share.kind == Kind::Camera) - .map(|share| share.user_id) + .filter(|(_, kind)| grid || *kind == Kind::Camera) .collect() } - pub(super) fn tick_cameras(&mut self, cx: &mut Context) { - let wanted = self.camera_users(); - let viewing = self.viewing_camera(); + pub(super) fn tick_tiles(&mut self, cx: &mut Context) { + let wanted = self.tile_sources(); + let viewing = self.viewing(); + let quality = tile_quality(self.chat.stage); let mut changed = false; - for &user_id in &wanted { - if self.chat.cameras.contains_key(&user_id) { + for &source in &wanted { + if self.chat.tiles.contains_key(&source) { continue; } - self.chat.cameras.insert( - user_id, - CameraTile { + self.chat.tiles.insert( + source, + Tile { generation: None, frame: None, }, ); + changed = true; + if viewing == Some(source) { + continue; + } + let (user_id, kind) = source; let network = self.network.clone(); let volume = self.settings.share.volume; let choice = self.settings.voice.choice.clone(); - spawn_warn(cx, "camera watch", async move { + spawn_warn(cx, "tile watch", async move { network - .watch_share(user_id, Kind::Camera, CAMERA_TILE, volume, choice) + .watch_share(user_id, kind, quality, volume, choice) .await }); - changed = true; } - let gone: Vec = self + let gone: Vec<(Id, Kind)> = self .chat - .cameras + .tiles .keys() - .filter(|user_id| !wanted.contains(user_id)) + .filter(|source| !wanted.contains(source)) .copied() .collect(); - for user_id in gone { + for source in gone { if let Some(frame) = self .chat - .cameras - .remove(&user_id) + .tiles + .remove(&source) .and_then(|tile| tile.frame) { cx.drop_image(frame, None); } - if viewing != Some(user_id) { + if viewing != Some(source) { let network = self.network.clone(); + let (user_id, kind) = source; cx.spawn(async move |_, _| { - let _ = network.unwatch_share(user_id, Kind::Camera).await; + let _ = network.unwatch_share(user_id, kind).await; }) .detach(); } changed = true; } - for (user_id, tile) in self.chat.cameras.iter_mut() { - let Some(frame) = self.network.share_frame(*user_id, Kind::Camera) else { + for (&(user_id, kind), tile) in self.chat.tiles.iter_mut() { + let Some(frame) = self.network.share_frame(user_id, kind) else { continue; }; if tile.generation == Some(frame.generation) { @@ -118,6 +132,18 @@ impl Yumush { } } + pub(super) fn set_stage(&mut self, stage: Stage, cx: &mut Context) { + self.chat.stage = stage; + let viewing = self.viewing(); + let quality = tile_quality(stage); + for &(user_id, kind) in self.chat.tiles.keys() { + if viewing != Some((user_id, kind)) { + self.network.set_watch_quality(user_id, kind, quality); + } + } + cx.notify(); + } + pub(super) fn toggle_camera(&mut self, window: &mut Window, cx: &mut Context) { let network = self.network.clone(); let on = network.camera_on(); @@ -134,6 +160,11 @@ impl Yumush { .detach(); } + pub(super) fn in_stage_chat(&self) -> bool { + self.chat.voice_community().is_some() + && self.chat.selected_community_id == self.chat.voice_community() + } + pub(super) fn stage_sources(&self) -> Vec<(Id, Kind)> { let Some(community_id) = self.chat.voice_community() else { return Vec::new(); @@ -155,14 +186,16 @@ impl Yumush { entity: &Entity, palette: Palette, ) -> Option { - let sources = self.stage_sources(); - if self.chat.stage_hidden - || sources.is_empty() - || self.chat.selected_community_id != self.chat.voice_community() - { + let shown = match self.chat.stage { + Stage::Strip => true, + Stage::Grid => self.chat.viewer.is_some(), + Stage::Hidden => false, + }; + if !shown || !self.in_stage_chat() { return None; } - let tiles: Vec<_> = sources + let tiles: Vec<_> = self + .stage_sources() .into_iter() .map(|source| stage_tile(entity, self, palette, source)) .collect(); @@ -178,7 +211,92 @@ impl Yumush { .overflow_x_scroll() .border_b_1() .border_color(palette.border) - .children(tiles), + .children(tiles) + .child(icon_button( + "stage_grid", + palette, + IconName::LayoutDashboard, + "Grid", + entity_action(entity, |yumush, _, cx| { + yumush.set_stage(Stage::Grid, cx) + }), + )), + ) + } + + pub(super) fn stage_grid( + &self, + entity: &Entity, + palette: Palette, + ) -> Option { + if self.chat.stage != Stage::Grid + || !self.in_stage_chat() + || self + .chat + .viewer + .as_ref() + .is_some_and(|viewer| !viewer.fullscreen) + { + return None; + } + let community_id = self.chat.voice_community()?; + let sources = self.stage_sources(); + let cells: Vec = self + .chat + .voice_ids(community_id) + .iter() + .map(|&user_id| { + let shared = sources.contains(&(user_id, Kind::Camera)); + grid_cell(entity, self, palette, (user_id, Kind::Camera), shared) + }) + .chain( + sources + .iter() + .filter(|(_, kind)| *kind == Kind::Screen) + .map(|&source| grid_cell(entity, self, palette, source, true)), + ) + .collect(); + let columns = ((cells.len() as f32).sqrt().ceil() as usize).max(1); + let rows = cells.len().div_ceil(columns); + let mut cells = cells.into_iter(); + + Some( + div() + .relative() + .flex_1() + .min_h_0() + .p(px(8.)) + .flex() + .flex_col() + .gap(px(8.)) + .children((0..rows).map(|_| { + div() + .flex_1() + .min_h_0() + .flex() + .gap(px(8.)) + .children((0..columns).map(|_| { + cells + .next() + .unwrap_or_else(|| div().flex_1().into_any_element()) + })) + })) + .child( + icon_button( + "stage_minimize", + palette, + IconName::Minimize, + "Strip", + entity_action(entity, |yumush, _, cx| { + yumush.set_stage(Stage::Strip, cx) + }), + ) + .absolute() + .top(px(12.)) + .right(px(12.)) + .bg(palette.panel) + .occlude(), + ), ) } } @@ -188,7 +306,7 @@ pub(super) fn ticker(cx: &mut Context) -> Task<()> { loop { cx.background_executor().timer(FRAME_TICK).await; if this - .update(cx, |yumush, cx| yumush.tick_cameras(cx)) + .update(cx, |yumush, cx| yumush.tick_tiles(cx)) .is_err() { break; @@ -233,14 +351,12 @@ fn stage_tile( let entity = entity.clone(); let focused = yumush.is_watching(user_id, kind); let speaking = yumush.chat.waves.speaking.contains(&user_id); - let frame = match kind { - Kind::Camera => yumush - .chat - .cameras - .get(&user_id) - .and_then(|tile| tile.frame.clone()), - Kind::Screen => yumush.viewer_frame(user_id, kind), - }; + let frame = yumush + .chat + .tiles + .get(&(user_id, kind)) + .and_then(|tile| tile.frame.clone()) + .or_else(|| yumush.viewer_frame(user_id, kind)); let user = yumush.chat.author(user_id); let name = user.map_or("Someone", |user| user.username.as_str()); let content = match (frame, user) { @@ -307,3 +423,99 @@ fn stage_tile( .child(name.to_string()), ) } + +fn grid_cell( + entity: &Entity, + yumush: &Yumush, + palette: Palette, + (user_id, kind): (Id, Kind), + shared: bool, +) -> AnyElement { + let frame = yumush + .chat + .tiles + .get(&(user_id, kind)) + .and_then(|tile| tile.frame.clone()); + let speaking = yumush.chat.waves.speaking.contains(&user_id); + let user = yumush.chat.author(user_id); + let name = user.map_or("Someone", |user| user.username.as_str()); + let content = match (frame, user) { + (Some(frame), _) => img(frame) + .size_full() + .object_fit(ObjectFit::Contain) + .into_any_element(), + (None, Some(user)) => div() + .size_full() + .flex() + .items_center() + .justify_center() + .child(avatar( + palette, + user, + px(64.), + text(24.), + &yumush.chat.avatars, + )) + .into_any_element(), + (None, None) => div().into_any_element(), + }; + + let cell = div() + .id(format!("grid_{user_id}_{kind:?}")) + .relative() + .flex_1() + .min_w_0() + .min_h_0() + .rounded(px(10.)) + .overflow_hidden() + .bg(gpui_kit::black()) + .border_2() + .border_color(if speaking { + palette.ok + } else { + palette.ok.opacity(0.) + }) + .child(content) + .child( + div() + .absolute() + .left(px(6.)) + .bottom(px(6.)) + .px(px(6.)) + .py(px(2.)) + .rounded(px(4.)) + .bg(palette.panel) + .text_size(text(11.)) + .flex() + .items_center() + .gap(px(4.)) + .children((kind == Kind::Screen).then(|| { + Icon::new(share_view::kind_icon(kind)) + .w(px(12.)) + .h(px(12.)) + })) + .child(name.to_string()), + ); + + if shared { + let entity = entity.clone(); + cell.cursor_pointer() + .on_click(move |_, window, cx| { + entity.update(cx, |yumush, cx| { + yumush.toggle_viewer(user_id, kind, window, cx) + }); + }) + .into_any_element() + } else { + cell.into_any_element() + } +} + +pub(super) fn stage_toggle(yumush: &mut Yumush, cx: &mut Context) { + let next = if yumush.chat.stage == Stage::Hidden { + Stage::Strip + } else { + Stage::Hidden + }; + yumush.set_stage(next, cx); +} diff --git a/crates/client/src/gui/chat.rs b/crates/client/src/gui/chat.rs index db79161..42e5b9c 100644 --- a/crates/client/src/gui/chat.rs +++ b/crates/client/src/gui/chat.rs @@ -6,7 +6,7 @@ use std::{ time::{Duration, Instant}, }; -use crate::gui::camera_view::CameraTile; +use crate::gui::camera_view::{self, Stage, Tile}; use crate::gui::social::InviteLookup; use crate::gui::voice_stats::{SAMPLE_HISTORY, StatsTab}; use crate::gui::wave::Waves; @@ -148,6 +148,8 @@ pub struct Chat { pub(super) poll_question: Entity, pub(super) poll_options: Vec>, pub(super) poll_multiple: bool, + pub(super) poll_public: bool, + pub(super) my_votes: HashMap>, pub(super) poll_expanded: bool, scroll: ScrollHandle, pub(super) jump: Option, @@ -255,9 +257,9 @@ pub struct Chat { pub(super) media: Media, pub(super) shares: HashMap>, pub(super) viewer: Option, - pub(super) cameras: BTreeMap, + pub(super) tiles: BTreeMap<(Id, Kind), Tile>, pub(super) camera_task: Option>, - pub(super) stage_hidden: bool, + pub(super) stage: Stage, pub(super) typing: HashMap>, pub(super) typing_sent: HashMap, } @@ -351,6 +353,8 @@ impl Chat { new_input("Option", window, cx), ], poll_multiple: false, + poll_public: false, + my_votes: HashMap::new(), poll_expanded: false, history: History::End, scroll: ScrollHandle::new(), @@ -486,9 +490,9 @@ impl Chat { media: Media::default(), shares: HashMap::new(), viewer: None, - cameras: BTreeMap::new(), + tiles: BTreeMap::new(), camera_task: None, - stage_hidden: false, + stage: Stage::Strip, composer: Composer::Compose { reply: None }, } } @@ -714,6 +718,7 @@ impl Chat { self.jump = None; self.pins.clear(); self.poll_expanded = false; + self.my_votes.clear(); self.community_hits.clear(); self.history = History::End; self.media.leave(); @@ -1280,9 +1285,12 @@ impl Yumush { .children(self.poll_banner(entity, themed)) .map(|panel| match self.inline_viewer(entity, themed) { Some(viewer) => panel.child(viewer), - None => panel - .child(self.message_area(entity, themed)) - .child(self.composer(entity, themed)), + None => match self.stage_grid(entity, themed) { + Some(grid) => panel.child(grid), + None => panel + .child(self.message_area(entity, themed)) + .child(self.composer(entity, themed)), + }, }), None => panel.child(empty_state(palette)), }, @@ -1713,6 +1721,7 @@ impl Yumush { let _ = this.update(cx, |yumush, cx| { if yumush.chat.is_selected(community_id) { yumush.chat.pins = pins; + yumush.request_poll_votes(cx); cx.notify(); } }); @@ -1886,6 +1895,7 @@ impl Yumush { yumush.mark_read(community_id, message_id, cx); } yumush.request_media(cx); + yumush.request_poll_votes(cx); yumush.request_authors(cx); cx.notify(); } @@ -2456,7 +2466,7 @@ impl Yumush { return None; } let stage = self.stage_sources().len(); - let hidden = self.chat.stage_hidden; + let hidden = self.chat.stage == Stage::Hidden; Some( div() @@ -2483,14 +2493,13 @@ impl Yumush { ) })) .children( - (stage > 0 && self.chat.selected_community_id == self.chat.voice_community()) + self.in_stage_chat() .then(|| { clickable( "stage_toggle", palette, entity_action(entity, |yumush: &mut Yumush, _, cx| { - yumush.chat.stage_hidden = !yumush.chat.stage_hidden; - cx.notify(); + camera_view::stage_toggle(yumush, cx) }), ) .tip(if hidden { "Show stage" } else { "Hide stage" }) diff --git a/crates/client/src/gui/composer.rs b/crates/client/src/gui/composer.rs index 146fd6e..cf3c7ca 100644 --- a/crates/client/src/gui/composer.rs +++ b/crates/client/src/gui/composer.rs @@ -565,6 +565,7 @@ pub fn poll_popover(chat: &Chat, entity: &Entity, palette: Palette) -> i let question = chat.poll_question.clone(); let options = chat.poll_options.clone(); let multiple = chat.poll_multiple; + let public = chat.poll_public; let entity = entity.clone(); Popover::new("poll_popover") @@ -579,6 +580,7 @@ pub fn poll_popover(chat: &Chat, entity: &Entity, palette: Palette) -> i .content(move |_, _, cx| { let popover = cx.entity(); let toggle = entity.clone(); + let toggle_public = entity.clone(); let add = entity.clone(); let submit = entity.clone(); pop_in( @@ -623,15 +625,32 @@ pub fn poll_popover(chat: &Chat, entity: &Entity, palette: Palette) -> i .items_center() .justify_between() .child( - Checkbox::new("poll_multiple") - .label("Multiple answers") - .checked(multiple) - .on_click(move |_, _, cx| { - toggle.update(cx, |yumush, cx| { - yumush.chat.poll_multiple = !yumush.chat.poll_multiple; - cx.notify(); - }) - }), + div() + .flex() + .items_center() + .gap(px(12.)) + .child( + Checkbox::new("poll_multiple") + .label("Multiple answers") + .checked(multiple) + .on_click(move |_, _, cx| { + toggle.update(cx, |yumush, cx| { + yumush.chat.poll_multiple = + !yumush.chat.poll_multiple; + cx.notify(); + }) + }), + ) + .child( + Checkbox::new("poll_public") + .label("Show voters") + .checked(public) + .on_click(move |_, _, cx| { + toggle_public.update(cx, |yumush, cx| { + yumush.toggle_poll_public(cx) + }) + }), + ), ) .children((options.len() < MAXIMUM_POLL_OPTIONS).then(|| { icon_button( @@ -699,6 +718,7 @@ impl Yumush { return false; } let multiple = self.chat.poll_multiple; + let public = self.chat.poll_public; let network = self.network.clone(); spawn_done(window, cx, async move { @@ -708,6 +728,7 @@ impl Yumush { question, options, multiple, + public, }) .await }); @@ -720,7 +741,13 @@ impl Yumush { input.update(cx, |state, cx| state.set_value("", window, cx)); } self.chat.poll_multiple = false; + self.chat.poll_public = false; cx.notify(); true } + + pub(super) fn toggle_poll_public(&mut self, cx: &mut Context) { + self.chat.poll_public = !self.chat.poll_public; + cx.notify(); + } } diff --git a/crates/client/src/gui/poll.rs b/crates/client/src/gui/poll.rs index ae891cd..5f56cf4 100644 --- a/crates/client/src/gui/poll.rs +++ b/crates/client/src/gui/poll.rs @@ -1,7 +1,5 @@ use common::model::Id; -use std::collections::HashSet; - -use common::call::{ClosePoll, Vote}; +use common::call::{ClosePoll, MyVotes, Vote}; use common::model::{Message, Poll, Rank, User}; use gpui_kit::{ Context, Div, Entity, FontWeight, InteractiveElement, IntoElement, ParentElement, Styled, @@ -9,18 +7,14 @@ use gpui_kit::{ }; use super::{ - Palette, Yumush, + Palette, Tooltip as _, Yumush, avatar, chat::Chat, clickable, text, widget::{icon_button, spawn_done}, }; pub fn voter_count(poll: &Poll) -> usize { - poll.options - .iter() - .flat_map(|option| &option.voter_ids) - .collect::>() - .len() + poll.voters as usize } fn my_positions(poll: &Poll, user_id: Id) -> Vec { @@ -54,6 +48,15 @@ pub enum PollMode<'a> { Snapshot(Id), } +fn status_icon(id: String, icon: IconName, tip: &'static str, palette: Palette) -> impl IntoElement { + div() + .id(id) + .tip(tip) + .flex_shrink_0() + .text_color(palette.muted) + .child(Icon::new(icon).w(px(12.)).h(px(12.))) +} + pub fn poll_card( poll: &Poll, mode: PollMode, @@ -64,11 +67,17 @@ pub fn poll_card( ) -> impl IntoElement { let voters = voter_count(poll); let me_id = me.map(|me| me.id); - let mine = me_id.map(|id| my_positions(poll, id)).unwrap_or_default(); let (key, live) = match mode { PollMode::Live(message) => (message.id.to_string(), Some(message)), PollMode::Snapshot(id) => (format!("snapshot_{id}"), None), }; + let mine = match me_id { + Some(id) if poll.public => my_positions(poll, id), + _ => live + .and_then(|message| chat.my_votes.get(&message.id)) + .cloned() + .unwrap_or_default(), + }; let vote_id = live .filter(|_| !poll.closed && me_id.is_some()) .map(|message| message.id); @@ -82,12 +91,16 @@ pub fn poll_card( .map(|message| message.id); let rows = poll.options.iter().enumerate().map(|(position, option)| { - let count = option.voter_ids.len(); - let share = percent(count, voters); + let share = percent(option.votes as usize, voters); let picked = mine.contains(&(position as u32)); let next = me_id .map(|_| next_votes(&mine, position as u32, poll.multiple)) .unwrap_or_default(); + let voters_here: &[Id] = if poll.public { + &option.voter_ids + } else { + &[] + }; let entity = entity.clone(); let id = format!("poll_option_{key}_{position}"); match vote_id { @@ -98,12 +111,18 @@ pub fn poll_card( None => div().id(id), } .relative() - .h(px(32.)) + .h(px(36.)) .px(px(10.)) .flex() .items_center() .gap(px(8.)) - .rounded(px(6.)) + .rounded(px(8.)) + .border_1() + .border_color(if picked { + palette.accent + } else { + palette.accent.opacity(0.) + }) .overflow_hidden() .bg(palette.field) .child( @@ -143,6 +162,28 @@ pub fn poll_card( .truncate() .child(option.text.clone()), ) + .children((!voters_here.is_empty()).then(|| { + div() + .flex() + .items_center() + .children(voters_here.iter().take(3).enumerate().filter_map( + |(index, id)| { + chat.author(*id).map(|user| { + avatar(palette, user, px(18.), text(8.), &chat.avatars) + .border_2() + .border_color(palette.panel) + .when(index > 0, |el| el.ml(px(-6.))) + }) + }, + )) + .children((voters_here.len() > 3).then(|| { + div() + .ml(px(4.)) + .text_size(text(11.)) + .text_color(palette.muted) + .child(format!("+{}", voters_here.len() - 3)) + })) + })) .child( div() .flex_shrink_0() @@ -159,11 +200,14 @@ pub fn poll_card( .justify_between() .text_size(text(12.)) .text_color(palette.muted) - .child(match voters { - 1 => "1 vote".to_string(), - _ => format!("{voters} votes"), - }) - .children(poll.closed.then_some("Closed")) + .child( + div() + .flex() + .items_center() + .gap(px(4.)) + .child(Icon::new(IconName::Users).w(px(12.)).h(px(12.))) + .child(format!("{voters}")), + ) .children(close_id.map(|close_id| { let close_entity = entity.clone(); icon_button( @@ -177,32 +221,64 @@ pub fn poll_card( ) })); + let (visibility, visibility_tip) = if poll.public { + (IconName::Eye, "Voters visible") + } else { + (IconName::EyeOff, "Anonymous") + }; + div() .flex() .flex_col() - .gap(px(6.)) - .p(px(12.)) - .w(px(320.)) + .gap(px(8.)) + .p(px(14.)) + .w(px(360.)) .max_w_full() - .rounded(px(10.)) + .rounded(px(12.)) + .border_1() + .border_color(palette.border) .bg(palette.panel) .child( div() .flex() - .items_center() + .items_start() + .justify_between() .gap(px(8.)) - .mb(px(2.)) - .child( - div() - .flex_shrink_0() - .text_color(palette.accent) - .child(Icon::new(IconName::ChartBar).w(px(14.)).h(px(14.))), - ) .child( div() .min_w_0() .font_weight(FontWeight::SEMIBOLD) + .text_size(text(15.)) .child(poll.question.clone()), + ) + .child( + div() + .flex_shrink_0() + .flex() + .items_center() + .gap(px(6.)) + .child(status_icon( + format!("poll_public_{key}"), + visibility, + visibility_tip, + palette, + )) + .children(poll.multiple.then(|| { + status_icon( + format!("poll_multiple_{key}"), + IconName::ListChecks, + "Multiple choice", + palette, + ) + })) + .children(poll.closed.then(|| { + status_icon( + format!("poll_closed_{key}"), + IconName::Lock, + "Closed", + palette, + ) + })), ), ) .children(rows) @@ -307,6 +383,40 @@ impl Yumush { ) } + pub(super) fn request_poll_votes(&mut self, cx: &mut Context) { + let anonymous: Vec = self + .chat + .messages + .iter() + .chain(&self.chat.pins) + .filter(|message| message.poll.as_ref().is_some_and(|poll| !poll.public)) + .map(|message| message.id) + .collect(); + for message_id in anonymous { + self.request_my_votes(message_id, cx); + } + } + + pub(super) fn request_my_votes(&mut self, message_id: Id, cx: &mut Context) { + if self.chat.my_votes.contains_key(&message_id) { + return; + } + self.chat.my_votes.insert(message_id, Vec::new()); + + let network = self.network.clone(); + + cx.spawn(async move |this, cx| { + let Ok(positions) = network.call(MyVotes { message_id }).await else { + return; + }; + let _ = this.update(cx, |yumush, cx| { + yumush.chat.my_votes.insert(message_id, positions); + cx.notify(); + }); + }) + .detach(); + } + pub(super) fn vote( &mut self, message_id: Id, @@ -314,6 +424,7 @@ impl Yumush { window: &mut Window, cx: &mut Context, ) { + self.chat.my_votes.insert(message_id, positions.clone()); let network = self.network.clone(); spawn_done(window, cx, async move { diff --git a/crates/client/src/gui/share_view.rs b/crates/client/src/gui/share_view.rs index 5590829..640a168 100644 --- a/crates/client/src/gui/share_view.rs +++ b/crates/client/src/gui/share_view.rs @@ -82,7 +82,7 @@ pub(super) struct Viewer { kind: Kind, generation: Option, frame: Option>, - fullscreen: bool, + pub(super) fullscreen: bool, volume: Entity, _subscriptions: Vec, _tick: Task<()>, @@ -207,12 +207,11 @@ impl Yumush { .and_then(|viewer| viewer.frame.clone()) } - pub(super) fn viewing_camera(&self) -> Option { + pub(super) fn viewing(&self) -> Option<(Id, Kind)> { self.chat .viewer .as_ref() - .filter(|viewer| viewer.kind == Kind::Camera) - .map(|viewer| viewer.user_id) + .map(|viewer| (viewer.user_id, viewer.kind)) } pub(super) fn toggle_viewer( @@ -268,7 +267,7 @@ impl Yumush { let watch = WatchQuality::Auto; let volume = self.settings.share.volume; let choice = self.settings.voice.choice.clone(); - if kind == Kind::Camera && self.chat.cameras.contains_key(&user_id) { + if self.chat.tiles.contains_key(&(user_id, kind)) { network.set_watch_quality(user_id, kind, watch); } else { cx.spawn_in(window, async move |this, cx| { @@ -297,8 +296,12 @@ impl Yumush { cx.defer(move |cx| cx.drop_image(frame, None)); } let network = self.network.clone(); - if viewer.kind == Kind::Camera && self.chat.cameras.contains_key(&viewer.user_id) { - network.set_watch_quality(viewer.user_id, viewer.kind, camera_view::CAMERA_TILE); + if self.chat.tiles.contains_key(&(viewer.user_id, viewer.kind)) { + network.set_watch_quality( + viewer.user_id, + viewer.kind, + camera_view::tile_quality(self.chat.stage), + ); } else { let user_id = viewer.user_id; let kind = viewer.kind; diff --git a/crates/common/src/call.rs b/crates/common/src/call.rs index 5c68a87..c6e2518 100644 --- a/crates/common/src/call.rs +++ b/crates/common/src/call.rs @@ -221,6 +221,7 @@ pub struct CreatePoll { pub question: String, pub options: Vec, pub multiple: bool, + pub public: bool, } #[derive(Debug, Clone, Encode, Decode)] @@ -234,6 +235,11 @@ pub struct ClosePoll { pub message_id: Id, } +#[derive(Debug, Clone, Encode, Decode)] +pub struct MyVotes { + pub message_id: Id, +} + #[derive(Debug, Clone, Encode, Decode)] pub struct React { pub message_id: Id, @@ -648,6 +654,7 @@ calls! { CreatePoll: User -> Message, |c| validate_poll(&c.question, &c.options); Vote: User -> (); ClosePoll: User -> (); + MyVotes: User -> Vec; React: User -> (), |c| validate_reaction(&c.emoji); Pin: User -> (); ListPins: User -> Vec; diff --git a/crates/common/src/model.rs b/crates/common/src/model.rs index a684b99..88cf593 100644 --- a/crates/common/src/model.rs +++ b/crates/common/src/model.rs @@ -566,6 +566,7 @@ pub struct Reaction { #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct PollOption { pub text: String, + pub votes: u32, pub voter_ids: Vec, } @@ -575,6 +576,8 @@ pub struct Poll { pub options: Vec, pub multiple: bool, pub closed: bool, + pub public: bool, + pub voters: u32, } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] diff --git a/crates/server/src/call/message.rs b/crates/server/src/call/message.rs index fca2525..aaecc6c 100644 --- a/crates/server/src/call/message.rs +++ b/crates/server/src/call/message.rs @@ -1,7 +1,7 @@ use common::{ call::{ ClosePoll, CreateMessage, CreatePoll, DeleteMessage, ForwardMessages, ListMessages, - ListPins, ListUnreads, MarkRead, Pin, React, Typing, UpdateMessage, Vote, + ListPins, ListUnreads, MarkRead, MyVotes, Pin, React, Typing, UpdateMessage, Vote, }, error::Error, event::Event, @@ -166,6 +166,7 @@ impl Handle for CreatePoll { &self.question, &self.options, self.multiple, + self.public, cx.db, ) .await?; @@ -192,6 +193,17 @@ impl Handle for Vote { } } +impl Handle for MyVotes { + async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result, ServerError> { + let me = me.user()?; + let existing = db::message::read(self.message_id, cx.db).await?; + ensure(permission::can_read( + actor_rank(&me, existing.community_id, cx.db).await?, + ))?; + db::poll::my_votes(self.message_id, me.id, cx.db).await + } +} + impl Handle for ClosePoll { async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<(), ServerError> { let me = me.user()?; diff --git a/crates/server/src/call/mod.rs b/crates/server/src/call/mod.rs index 9d4fbb2..fb1e8ad 100644 --- a/crates/server/src/call/mod.rs +++ b/crates/server/src/call/mod.rs @@ -138,6 +138,7 @@ pub(crate) async fn dispatch(request: Request, cx: &mut Cx<'_>) -> Vec { Request::CreatePoll(call) => serve(call, cx).await, Request::Vote(call) => serve(call, cx).await, Request::ClosePoll(call) => serve(call, cx).await, + Request::MyVotes(call) => serve(call, cx).await, Request::React(call) => serve(call, cx).await, Request::Pin(call) => serve(call, cx).await, Request::ListPins(call) => serve(call, cx).await, diff --git a/crates/server/src/db/message.rs b/crates/server/src/db/message.rs index 39ee38d..13e27b7 100644 --- a/crates/server/src/db/message.rs +++ b/crates/server/src/db/message.rs @@ -192,10 +192,11 @@ pub async fn create_poll( question: &str, options: &[String], multiple: bool, + public: bool, db: &Connection, ) -> Result { let id = insert(user_id, community_id, "", None, db).await?; - poll::create(id, question, options, multiple, db).await?; + poll::create(id, question, options, multiple, public, db).await?; read(id, db).await } diff --git a/crates/server/src/db/poll.rs b/crates/server/src/db/poll.rs index daf40cb..31687f7 100644 --- a/crates/server/src/db/poll.rs +++ b/crates/server/src/db/poll.rs @@ -1,3 +1,5 @@ +use std::collections::HashSet; + use common::{ error::Error, model::{Id, Poll, PollOption}, @@ -11,12 +13,13 @@ pub async fn create( question: &str, options: &[String], multiple: bool, + public: bool, db: &Connection, ) -> Result<(), ServerError> { run( db, - "INSERT INTO poll (message_id, question, multiple) VALUES (?1, ?2, ?3)", - (message_id, question, multiple), + "INSERT INTO poll (message_id, question, multiple, public) VALUES (?1, ?2, ?3, ?4)", + (message_id, question, multiple, public), ) .await?; for (position, text) in (0u32..).zip(options) { @@ -32,11 +35,18 @@ pub async fn create( } pub async fn of(message_id: Id, db: &Connection) -> Result, ServerError> { - let Some((question, multiple, closed)) = maybe( + let Some((question, multiple, closed, public)) = maybe( db, - "SELECT question, multiple, closed FROM poll WHERE message_id = ?1", + "SELECT question, multiple, closed, public FROM poll WHERE message_id = ?1", (message_id,), - |r| Ok((r.get::()?, r.get::()?, r.get::()?)), + |r| { + Ok(( + r.get::()?, + r.get::()?, + r.get::()?, + r.get::()?, + )) + }, ) .await? else { @@ -50,6 +60,7 @@ pub async fn of(message_id: Id, db: &Connection) -> Result, ServerE |r| { Ok(PollOption { text: r.get()?, + votes: 0, voter_ids: Vec::new(), }) }, @@ -62,9 +73,17 @@ pub async fn of(message_id: Id, db: &Connection) -> Result, ServerE |r| Ok((r.get()?, r.get()?)), ) .await?; + let voters = votes + .iter() + .map(|(_, user_id)| user_id) + .collect::>() + .len() as u32; for (position, user_id) in votes { if let Some(option) = options.get_mut(position as usize) { - option.voter_ids.push(user_id); + option.votes += 1; + if public { + option.voter_ids.push(user_id); + } } } @@ -73,6 +92,8 @@ pub async fn of(message_id: Id, db: &Connection) -> Result, ServerE options, multiple, closed, + public, + voters, })) } @@ -100,6 +121,20 @@ pub async fn set_votes( Ok(()) } +pub async fn my_votes( + message_id: Id, + user_id: Id, + db: &Connection, +) -> Result, ServerError> { + all( + db, + "SELECT position FROM poll_vote WHERE message_id = ?1 AND user_id = ?2 ORDER BY position", + (message_id, user_id), + |r| r.get(), + ) + .await +} + pub async fn close(message_id: Id, db: &Connection) -> Result<(), ServerError> { ensure_with( run( diff --git a/crates/server/src/db/schema.sql b/crates/server/src/db/schema.sql index 981a14f..ba5646c 100644 --- a/crates/server/src/db/schema.sql +++ b/crates/server/src/db/schema.sql @@ -122,7 +122,8 @@ CREATE TABLE poll ( message_id INTEGER PRIMARY KEY REFERENCES message (id) ON DELETE CASCADE, question TEXT NOT NULL, multiple INTEGER NOT NULL, - closed INTEGER NOT NULL DEFAULT 0 + closed INTEGER NOT NULL DEFAULT 0, + public INTEGER NOT NULL DEFAULT 0 ) STRICT; CREATE TABLE poll_option (