Expire calls where one of both parties doesn't talk for 10 minutes.
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
use async_trait::async_trait;
|
||||
use deadpool_redis::redis::AsyncTypedCommands;
|
||||
use scc::HashSet;
|
||||
use time::UtcDateTime;
|
||||
use twilight_model::id::Id;
|
||||
|
||||
use crate::{
|
||||
@@ -115,12 +117,32 @@ impl Command<State, Extras> for CallCommand {
|
||||
))
|
||||
.await
|
||||
.bye();
|
||||
|
||||
ctx.rt
|
||||
.http
|
||||
.create_message(Id::new(call.other_party_channel_id(channel.id)))
|
||||
.content(&format!("☎️ {}", get_string_call_answered(ctx.prefix)))
|
||||
.await
|
||||
.bye();
|
||||
|
||||
let mut redis = ctx.rt.state.redis.get().await?;
|
||||
|
||||
let timestamp = UtcDateTime::now().unix_timestamp();
|
||||
|
||||
redis
|
||||
.zadd(
|
||||
"active-calls",
|
||||
format!("{}:{}", call.id, channel.id),
|
||||
timestamp,
|
||||
)
|
||||
.await?;
|
||||
redis
|
||||
.zadd(
|
||||
"active-calls",
|
||||
format!("{}:{}", call.id, call.other_party_channel_id(channel.id)),
|
||||
timestamp,
|
||||
)
|
||||
.await?;
|
||||
} else {
|
||||
ctx.send()
|
||||
.content(format!(
|
||||
@@ -215,6 +237,19 @@ impl Command<State, Extras> for SkipCommand {
|
||||
log::error!("Failed to send message to other party: {e:?}");
|
||||
})?;
|
||||
|
||||
let mut redis = ctx.rt.state.redis.get().await?;
|
||||
|
||||
let channel_keys = [
|
||||
format!("{}:{}", call.id, call.channel_ids[0]),
|
||||
format!("{}:{}", call.id, call.channel_ids[1]),
|
||||
];
|
||||
|
||||
redis
|
||||
.zrem("active-calls", &channel_keys)
|
||||
.await
|
||||
.inspect_err(|e| log::error!("Failed to remove inactive call from redis: {e}"))
|
||||
.ok();
|
||||
|
||||
let call = Call::start(&channel, &ctx.rt.state.db)
|
||||
.await
|
||||
.inspect_err(|e| {
|
||||
@@ -236,6 +271,25 @@ impl Command<State, Extras> for SkipCommand {
|
||||
.content(&format!("☎️ {}", get_string_call_answered(ctx.prefix)))
|
||||
.await
|
||||
.bye();
|
||||
|
||||
let mut redis = ctx.rt.state.redis.get().await?;
|
||||
|
||||
let timestamp = UtcDateTime::now().unix_timestamp();
|
||||
|
||||
redis
|
||||
.zadd(
|
||||
"active-calls",
|
||||
format!("{}:{}", call.id, channel.id),
|
||||
timestamp,
|
||||
)
|
||||
.await?;
|
||||
redis
|
||||
.zadd(
|
||||
"active-calls",
|
||||
format!("{}:{}", call.id, call.other_party_channel_id(channel.id)),
|
||||
timestamp,
|
||||
)
|
||||
.await?;
|
||||
} else {
|
||||
ctx.send()
|
||||
.content(format!(
|
||||
@@ -304,6 +358,8 @@ impl Command<State, Extras> for HangupCommand {
|
||||
}
|
||||
|
||||
if let Some(call_id) = channel.call_id {
|
||||
let mut redis = ctx.rt.state.redis.get().await?;
|
||||
|
||||
let Some(call) = Call::get_by_id(call_id, &ctx.rt.state.db)
|
||||
.await
|
||||
.inspect_err(|e| log::error!("Failed to get call: {e:?}"))?
|
||||
@@ -311,11 +367,27 @@ impl Command<State, Extras> for HangupCommand {
|
||||
ctx.send().content("❌ The call could not be found.\n-# This is not supposed to happen. If the issue persists, contact a moderator in the official server.").await?;
|
||||
|
||||
channel.call_id = None;
|
||||
channel.upsert(&ctx.rt.state.db).await?;
|
||||
channel.upsert(&ctx.rt.state.db).await.bye();
|
||||
|
||||
redis
|
||||
.zrem("active-calls", format!("{}:{}", call_id, channel.id))
|
||||
.await
|
||||
.bye();
|
||||
|
||||
return Err(CommandError::DatabaseInconsistentState);
|
||||
};
|
||||
|
||||
let channel_keys = [
|
||||
format!("{call_id}:{}", call.channel_ids[0]),
|
||||
format!("{call_id}:{}", call.channel_ids[1]),
|
||||
];
|
||||
|
||||
redis
|
||||
.zrem("active-calls", &channel_keys)
|
||||
.await
|
||||
.inspect_err(|e| log::error!("Failed to remove inactive call from redis: {e}"))
|
||||
.ok();
|
||||
|
||||
call.hangup(&ctx.rt.state.db)
|
||||
.await
|
||||
.inspect_err(|e| log::error!("Failed to hang up call: {e:?}"))?;
|
||||
|
||||
Reference in New Issue
Block a user