From cc85acf6166509bcd8df657f6eea54315de938e8 Mon Sep 17 00:00:00 2001 From: ReBlast Date: Fri, 22 Nov 2024 22:07:27 +0500 Subject: [PATCH] feat(events): add client event implementations --- src/messages/events/client/clear_activity.rs | 4 +++- src/messages/events/client/disconnect.rs | 9 ++++++-- src/messages/events/client/reset_timestamp.rs | 6 ++++- src/messages/events/client/update_activity.rs | 15 ++++++++++++- .../events/client/update_workspace.rs | 22 +++++++++++++++++-- 5 files changed, 49 insertions(+), 7 deletions(-) diff --git a/src/messages/events/client/clear_activity.rs b/src/messages/events/client/clear_activity.rs index 5291f0b2..949f89fc 100644 --- a/src/messages/events/client/clear_activity.rs +++ b/src/messages/events/client/clear_activity.rs @@ -4,7 +4,9 @@ pub struct ClearActivityEvent; use crate::messages::events::event::{EventContext, OnEvent}; impl OnEvent for ClearActivityEvent { - fn on_event(self, _ctx: &mut EventContext) -> crate::Result<()> { + fn on_event(self, ctx: &mut EventContext) -> crate::Result<()> { + ctx.cord.rich_client.clear()?; + Ok(()) } } diff --git a/src/messages/events/client/disconnect.rs b/src/messages/events/client/disconnect.rs index 964905f5..b1507da7 100644 --- a/src/messages/events/client/disconnect.rs +++ b/src/messages/events/client/disconnect.rs @@ -1,10 +1,15 @@ #[derive(Debug, Default)] pub struct DisconnectEvent; -use crate::messages::events::event::{EventContext, OnEvent}; +use crate::{ + ipc::pipe::PipeServerImpl, + messages::events::event::{EventContext, OnEvent}, +}; impl OnEvent for DisconnectEvent { - fn on_event(self, _ctx: &mut EventContext) -> crate::Result<()> { + fn on_event(self, ctx: &mut EventContext) -> crate::Result<()> { + ctx.cord.pipe.disconnect(ctx.client_id)?; + Ok(()) } } diff --git a/src/messages/events/client/reset_timestamp.rs b/src/messages/events/client/reset_timestamp.rs index 57cd6227..78f68197 100644 --- a/src/messages/events/client/reset_timestamp.rs +++ b/src/messages/events/client/reset_timestamp.rs @@ -4,7 +4,11 @@ use crate::messages::events::event::{EventContext, OnEvent}; pub struct ResetTimestampEvent; impl OnEvent for ResetTimestampEvent { - fn on_event(self, _ctx: &mut EventContext) -> crate::Result<()> { + fn on_event(self, ctx: &mut EventContext) -> crate::Result<()> { + if let Some(config) = &mut ctx.cord.config { + config.timestamp = None; + } + Ok(()) } } diff --git a/src/messages/events/client/update_activity.rs b/src/messages/events/client/update_activity.rs index cac1d4c4..15462459 100644 --- a/src/messages/events/client/update_activity.rs +++ b/src/messages/events/client/update_activity.rs @@ -1,5 +1,8 @@ +use std::sync::atomic::Ordering; + use crate::messages::events::event::{EventContext, OnEvent}; use crate::presence::activity::ActivityContext; +use crate::presence::types::Packet; #[derive(Debug)] pub struct UpdateActivityEvent { @@ -13,7 +16,17 @@ impl UpdateActivityEvent { } impl OnEvent for UpdateActivityEvent { - fn on_event(self, _ctx: &mut EventContext) -> crate::Result<()> { + fn on_event(self, ctx: &mut EventContext) -> crate::Result<()> { + if let Some(config) = &mut ctx.cord.config { + if !ctx.cord.rich_client.is_ready.load(Ordering::SeqCst) { + return Ok(()); + } + ctx.cord.rich_client.update(&Packet::new( + ctx.cord.rich_client.pid, + Some(self.context.build(config)), + ))?; + } + Ok(()) } } diff --git a/src/messages/events/client/update_workspace.rs b/src/messages/events/client/update_workspace.rs index 1c9867f9..1b88e70f 100644 --- a/src/messages/events/client/update_workspace.rs +++ b/src/messages/events/client/update_workspace.rs @@ -1,4 +1,10 @@ -use crate::messages::events::event::{EventContext, OnEvent}; +use std::borrow::Borrow; + +use crate::{ + messages::events::event::{EventContext, OnEvent}, + types, + util::utils, +}; #[derive(Debug)] pub struct UpdateWorkspaceEvent { @@ -12,7 +18,19 @@ impl UpdateWorkspaceEvent { } impl OnEvent for UpdateWorkspaceEvent { - fn on_event(self, _ctx: &mut EventContext) -> crate::Result<()> { + fn on_event(self, ctx: &mut EventContext) -> crate::Result<()> { + if let Some(config) = &mut ctx.cord.config { + let workspace = utils::find_workspace(&self.workspace); + if let Some(filename) = workspace.file_name() { + let filename = filename.to_string_lossy(); + config.workspace = filename.to_string(); + + types::validate_buttons(&mut config.buttons, filename.borrow()); + + // todo: check for workspace blacklist + } + } + Ok(()) } }