Skip to content

Commit

Permalink
feat: add explicit rich presence clearing; can be completely hidden w…
Browse files Browse the repository at this point in the history
…ith `force = true`
  • Loading branch information
vyfor committed Dec 6, 2024
1 parent 8e88a20 commit 8e99c03
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 20 deletions.
2 changes: 2 additions & 0 deletions lua/cord/activity/manager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ function ActivityManager:resume()
end
end

function ActivityManager:clear_activity(force) self.tx:clear_activity(force) end

function ActivityManager:should_update_time()
return self.config.display.show_time
and (
Expand Down
2 changes: 1 addition & 1 deletion lua/cord/event/sender.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function Producer:update_activity(activity)
self:send_event('update_activity', activity)
end

function Producer:clear_activity() self:send_event 'clear_activity' end
function Producer:clear_activity(force) self:send_event('clear_activity', force) end

function Producer:disconnect() self:send_event 'disconnect' end

Expand Down
53 changes: 35 additions & 18 deletions src/messages/events/client/clear_activity.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,48 @@
use crate::messages::events::event::{EventContext, OnEvent};
use crate::presence::packet::Packet;
use crate::protocol::msgpack::Deserialize;

#[derive(Debug, Default)]
pub struct ClearActivityEvent;
pub struct ClearActivityEvent {
force: bool,
}

impl OnEvent for ClearActivityEvent {
fn on_event(self, ctx: &mut EventContext) -> crate::Result<()> {
let sessions = ctx.cord.session_manager.sessions.read().unwrap();
let latest = sessions
.iter()
.filter(|s| s.1.last_activity.is_some())
.max_by_key(|s| {
(
s.1.last_activity.as_ref().is_some_and(|a| !a.is_idle),
s.1.last_updated,
)
});

if let Some((_, session)) = latest {
ctx.cord.rich_client.update(&Packet::new(
ctx.cord.rich_client.pid,
session.last_activity.as_ref(),
))?;
} else {
if self.force {
ctx.cord.rich_client.clear()?;
} else {
let sessions = ctx.cord.session_manager.sessions.read().unwrap();
let latest = sessions
.iter()
.filter(|s| s.1.last_activity.is_some())
.max_by_key(|s| {
(
s.1.last_activity.as_ref().is_some_and(|a| !a.is_idle),
s.1.last_updated,
)
});

if let Some((_, session)) = latest {
ctx.cord.rich_client.update(&Packet::new(
ctx.cord.rich_client.pid,
session.last_activity.as_ref(),
))?;
} else {
ctx.cord.rich_client.clear()?;
}
}

Ok(())
}
}

impl Deserialize for ClearActivityEvent {
fn deserialize(
input: crate::protocol::msgpack::Value,
) -> crate::Result<Self> {
let force = input.as_bool().unwrap_or_default();

Ok(ClearActivityEvent { force })
}
}
4 changes: 3 additions & 1 deletion src/messages/events/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ impl ClientEvent {
"update_activity" => Self::UpdateActivity(
UpdateActivityEvent::new(Activity::deserialize(data!(map))?),
),
"clear_activity" => Self::ClearActivity(ClearActivityEvent),
"clear_activity" => Self::ClearActivity(
ClearActivityEvent::deserialize(data!(map))?,
),
"disconnect" => Self::Disconnect(DisconnectEvent),
_ => return Err(format!("Unknown message type: {}", ty).into()),
})
Expand Down

0 comments on commit 8e99c03

Please sign in to comment.