feat(protocol): classify poke notification strength

This commit is contained in:
Edison Jwa
2026-06-08 22:51:41 +09:00
parent a644770488
commit 31cf45ce35
4 changed files with 216 additions and 9 deletions
+35 -8
View File
@@ -41,6 +41,7 @@ use crate::dto::{
ChannelId, ChannelInfo, ChatMessage, ClientId, ClientInfo, ClientProfile, MessageTarget,
ProtocolDelta, ServerActivity, ServerSnapshot,
};
use crate::poke_limiter::PokeLimiter;
use crate::ProtocolError;
const SPEAKING_ACTIVITY_WINDOW: Duration = Duration::from_millis(750);
@@ -713,6 +714,7 @@ async fn connection_task(
// reply channel — at most 3 s of pending state per move.
let mut pending_moves: PendingMoves = HashMap::new();
let mut voice_activity: HashMap<u64, Instant> = HashMap::new();
let mut poke_limiter = PokeLimiter::new();
// Main loop: pump events, service requests, forward voice.
loop {
@@ -742,6 +744,7 @@ async fn connection_task(
&channels.activity,
&channels.delta,
&mut pending_moves,
&mut poke_limiter,
),
},
Ok(Some(Err(e))) => {
@@ -841,6 +844,7 @@ async fn connection_task(
&channels,
&mut pending_moves,
&mut voice_activity,
&mut poke_limiter,
)
.await;
let _ = reply.send(r);
@@ -903,6 +907,7 @@ fn handle_non_audio_stream_item(
activity_tx: &mpsc::Sender<ServerActivity>,
delta_tx: &mpsc::Sender<ProtocolDelta>,
pending_moves: &mut PendingMoves,
poke_limiter: &mut PokeLimiter,
) {
match item {
StreamItem::BookEvents(events) => {
@@ -964,19 +969,25 @@ fn handle_non_audio_stream_item(
message,
} = ev
{
let mapped = match target {
tsclientlib::MessageTarget::Server => MessageTarget::Server,
tsclientlib::MessageTarget::Channel => MessageTarget::Channel,
let (mapped, poke_strength) = match target {
tsclientlib::MessageTarget::Server => (MessageTarget::Server, None),
tsclientlib::MessageTarget::Channel => (MessageTarget::Channel, None),
tsclientlib::MessageTarget::Client(id) => {
MessageTarget::Client(id.0 as u64)
(MessageTarget::Client(id.0 as u64), None)
}
tsclientlib::MessageTarget::Poke(id) => {
let own_client_id =
con.get_state().ok().map(|state| state.own_client.0 as u64);
let strength = poke_limiter.record(invoker.id.0 as u64, own_client_id);
(MessageTarget::Poke(id.0 as u64), Some(strength))
}
tsclientlib::MessageTarget::Poke(id) => MessageTarget::Poke(id.0 as u64),
};
let _ = chat_tx.try_send(ChatMessage {
sender_id: ClientId(invoker.id.0 as u64),
sender_name: sanitize(&invoker.name),
message: sanitize(&message),
target: mapped,
poke_strength,
});
}
}
@@ -1164,6 +1175,7 @@ async fn fetch_client_profile(
channels: &EventChannels,
pending_moves: &mut PendingMoves,
voice_activity: &mut HashMap<u64, Instant>,
poke_limiter: &mut PokeLimiter,
) -> Result<ClientProfile, ProtocolError> {
let target_id = TsClientId(client_id as u16);
@@ -1209,6 +1221,7 @@ async fn fetch_client_profile(
channels,
pending_moves,
voice_activity,
poke_limiter,
)
.await;
}
@@ -1219,6 +1232,7 @@ async fn fetch_client_profile(
channels,
pending_moves,
voice_activity,
poke_limiter,
)
.await;
}
@@ -1233,6 +1247,7 @@ async fn fetch_client_profile(
channels,
pending_moves,
voice_activity,
poke_limiter,
)
.await
{
@@ -1251,6 +1266,7 @@ async fn fetch_client_profile(
channels,
pending_moves,
voice_activity,
poke_limiter,
)
.await
{
@@ -1264,9 +1280,16 @@ async fn fetch_client_profile(
}
let db_info = if refresh_plan.needs_client_db_info {
request_client_db_info(con, database_id, channels, pending_moves, voice_activity)
.await
.ok()
request_client_db_info(
con,
database_id,
channels,
pending_moves,
voice_activity,
poke_limiter,
)
.await
.ok()
} else {
None
};
@@ -1426,6 +1449,7 @@ async fn request_messages(
channels: &EventChannels,
pending_moves: &mut PendingMoves,
voice_activity: &mut HashMap<u64, Instant>,
poke_limiter: &mut PokeLimiter,
) -> Result<Vec<InMessage>, ProtocolError> {
let handle = command
.send_with_result(con)
@@ -1468,6 +1492,7 @@ async fn request_messages(
&channels.activity,
&channels.delta,
pending_moves,
poke_limiter,
),
}
}
@@ -1479,6 +1504,7 @@ async fn request_client_db_info(
channels: &EventChannels,
pending_moves: &mut PendingMoves,
voice_activity: &mut HashMap<u64, Instant>,
poke_limiter: &mut PokeLimiter,
) -> Result<InClientDbInfoPart, ProtocolError> {
let messages = request_messages(
con,
@@ -1486,6 +1512,7 @@ async fn request_client_db_info(
channels,
pending_moves,
voice_activity,
poke_limiter,
)
.await?;
for message in messages {