Use propagation message ID to query instead of materialized view

This commit is contained in:
2026-02-25 19:35:54 -03:00
parent e7aa3247e5
commit 6aee4f39b0
8 changed files with 123 additions and 167 deletions
+58 -61
View File
@@ -31,7 +31,7 @@ use crate::{
components::{CodeOfConductButton, NeedHelpButton},
embeds,
masks::{get_mask_avatar_url, get_mask_display_name},
moderation::{escape_message, moderate_message, Action, Rule},
moderation::{Action, Rule, escape_message, moderate_message},
},
state::State,
utils::{ellipse, i64_to_u64},
@@ -226,7 +226,9 @@ impl OnMessageHandler<State, Extras> for CallMessageHandler {
opposite_channel.id,
channel.id,
call.id,
opposite_channel.call_id.map_or("None".into(), |i| i.to_string())
opposite_channel
.call_id
.map_or("None".into(), |i| i.to_string())
);
return Ok(());
}
@@ -290,6 +292,7 @@ impl OnMessageHandler<State, Extras> for CallMessageHandler {
if rules
.iter()
.filter(|rule| !rule.ignore_for_moderators() || !user.is_moderator)
.any(|rule| rule.action() == &Action::BlockAndPenalty)
{
rt.http
@@ -320,66 +323,63 @@ impl OnMessageHandler<State, Extras> for CallMessageHandler {
} else {
let mut embeds = vec![];
if let Some(replying_to) = replying_to {
if let Some((propagation_channel_id, propagation_message_id)) = replying_to
if let Some(replying_to) = replying_to
&& let Some((propagation_channel_id, propagation_message_id)) = replying_to
.data
.propagation_channel_id
.zip(replying_to.data.propagation_message_id)
{
let (display_name, avatar_url) =
if let Some(mask) = &replying_to.data.author_mask {
(
if i64_to_u64(propagation_channel_id) != event.channel_id.get() {
get_mask_display_name(mask)
} else {
format!(
"{} ({})",
get_mask_display_name(mask),
replying_to.data.author_name
)
},
get_mask_avatar_url(mask).expect("Failed to get mask avatar URL"),
)
{
let (display_name, avatar_url) = if let Some(mask) = &replying_to.data.author_mask {
(
if i64_to_u64(propagation_channel_id) != event.channel_id.get() {
get_mask_display_name(mask)
} else {
(
replying_to.data.author_name.clone(),
replying_to.data.author_avatar_url.clone(),
format!(
"{} ({})",
get_mask_display_name(mask),
replying_to.data.author_name
)
};
},
get_mask_avatar_url(mask).expect("Failed to get mask avatar URL"),
)
} else {
(
replying_to.data.author_name.clone(),
replying_to.data.author_avatar_url.clone(),
)
};
let message_link = if i64_to_u64(propagation_channel_id) == channel.id {
format!(
"https://discord.com/channels/{}/{}/{}",
opposite_channel.server_id, opposite_channel.id, replying_to.message_id
let message_link = if i64_to_u64(propagation_channel_id) == channel.id {
format!(
"https://discord.com/channels/{}/{}/{}",
opposite_channel.server_id, opposite_channel.id, replying_to.message_id
)
} else {
format!(
"https://discord.com/channels/{}/{}/{}",
channel.server_id, propagation_channel_id, propagation_message_id
)
};
let replying_to_user = User::get_by_id(replying_to.author_id, &rt.state.db).await?;
embeds.push(
EmbedBuilder::new()
.author(
EmbedAuthorBuilder::new(format!("Replying to {display_name}",))
.icon_url(ImageSource::url(avatar_url).unwrap())
.url(message_link),
)
} else {
format!(
"https://discord.com/channels/{}/{}/{}",
channel.server_id, propagation_channel_id, propagation_message_id
)
};
let replying_to_user =
User::get_by_id(replying_to.author_id, &rt.state.db).await?;
embeds.push(
EmbedBuilder::new()
.author(
EmbedAuthorBuilder::new(format!("Replying to {display_name}",))
.icon_url(ImageSource::url(avatar_url).unwrap())
.url(message_link),
)
.color(if replying_to.data.author_mask.is_some() {
COLOR_WHITE
} else {
replying_to_user
.and_then(|i| i.color.map(|c| c as u32))
.unwrap_or(COLOR_WHITE)
})
.description(ellipse(moderate_message(&replying_to.data.content).0, 100))
.build(),
);
}
.color(if replying_to.data.author_mask.is_some() {
COLOR_WHITE
} else {
replying_to_user
.and_then(|i| i.color.map(|c| c as u32))
.unwrap_or(COLOR_WHITE)
})
.description(ellipse(moderate_message(&replying_to.data.content).0, 100))
.build(),
);
}
if participant.is_masked {
@@ -440,14 +440,12 @@ impl OnMessageHandler<State, Extras> for CallMessageHandler {
error: ApiError::General(error),
..
} = e.kind()
{
if error.code == 10015 {
&& error.code == 10015 {
log::debug!("Webhook not found, updating...");
opposite_channel
.update_webhook(&rt.state.db, &rt.http)
.await?;
}
}
return Ok(());
}
}
@@ -624,8 +622,8 @@ impl OnMessageDeleteHandler<State, Extras> for CallMessageDeleteHandler {
return Ok(());
};
if let Some(propagation_message_id) = message.data.propagation_message_id {
if let Some((webhook_id, webhook_token)) = opposite_channel
if let Some(propagation_message_id) = message.data.propagation_message_id
&& let Some((webhook_id, webhook_token)) = opposite_channel
.webhook_id
.zip(opposite_channel.webhook_token)
{
@@ -641,7 +639,6 @@ impl OnMessageDeleteHandler<State, Extras> for CallMessageDeleteHandler {
message.insert(&rt.state.db).await?;
}
}
Ok(())
}
@@ -14,8 +14,12 @@ qiga
qigas
niqa
niqas
niqer
niqers
nqa
nqas
nqer
nlga
nlger
nlgers
ngiga
+18
View File
@@ -399,6 +399,16 @@ pub trait Rule: Send + Sync + Debug {
/// [`(Vec<String>, Vec<String>)`] - The list of detected rule violations in the original
/// (first vector) and normalized (second vector) message.
fn detect(&self, original: &str, normalized: &str) -> (Vec<String>, Vec<String>);
/// Whether this rule should be ignored for moderators. This is used to avoid censoring
/// messages from moderators, while still allowing them to be detected and logged from regular
/// users.
///
/// Returns:
/// [`bool`] - Whether this rule should be ignored for moderators.
fn ignore_for_moderators(&self) -> bool {
false
}
}
/// Parses a file containing phrases to be detected by rules.
@@ -624,6 +634,10 @@ impl Rule for AdvertisingRule {
(detections, vec![])
}
fn ignore_for_moderators(&self) -> bool {
true
}
}
#[derive(Debug)]
@@ -760,6 +774,10 @@ impl Rule for LinksRule {
(detections, vec![])
}
fn ignore_for_moderators(&self) -> bool {
true
}
}
/// The list of moderation rules.
+1 -1
View File
@@ -1,3 +1,3 @@
[
"Hey! I'm Payphone's dev, Nyeki. I know you've seen this message quite some times already, it's annoying to you and it sucks for me, yet I really want to keep Payphone going. We need to pay for hosting in two days, and we're nowhere near the amount we need. I'll have to shut down Payphone forever if we can't make it to $60 by the end of the month, $45 in two days. We're volunteering to develop and moderate Payphone daily for free, but we can't do it without your help. If you can spare a few dollars, please consider [donating to Payphone](<https://ko-fi.com/payphonedev>). Every little bit helps, and it would really mean the world to me. Thank you so much in advance for your support, I really appreciate every single cent I get of help to keep Payphone up and running."
"Thank you X and Anonymous Person for your support! We have covered next month's hosting costs."
]
+18 -17
View File
@@ -26,10 +26,12 @@ use crate::{
/// [`Result<User, ArgumentParser>`] - The user from the DB, or an [`ArgumentParsingError`] ready
/// to be returned by the parser.
async fn get_user_by_id(id: u64, db: &Session) -> Result<User, ArgumentParsingError> {
let Some(user) = User::get_by_id(id, db).await? else {
return Err(ArgumentParsingError::InvalidArgument);
};
Ok(user)
let user = User::get_by_id(id, db).await?;
match user {
Some(user) => Ok(user),
None => Err(ArgumentParsingError::InvalidArgument),
}
}
/// Fetches the user from the message's reply and falls back to the command executor.
@@ -45,20 +47,19 @@ async fn get_user_by_id(id: u64, db: &Session) -> Result<User, ArgumentParsingEr
async fn user_from_reply_or_command_executor(
ctx: &CommandContext<'_, State, Extras>,
) -> Result<User, ArgumentParsingError> {
if let Some(referenced) = &ctx.event.referenced_message {
if !referenced.author.system.unwrap_or(false) {
if referenced.author.bot {
if let Some(message) =
Message::get_by_propagation_message_id(referenced.id.get(), &ctx.rt.state.db)
.await?
{
return get_user_by_id(message.author_id, &ctx.rt.state.db).await;
}
}
// If the replied message's author is not a bot or system user, use them.
return get_user_by_id(referenced.author.id.get(), &ctx.rt.state.db).await;
if let Some(referenced) = &ctx.event.referenced_message
&& !referenced.author.system.unwrap_or(false)
{
if referenced.author.bot
&& let Some(message) =
Message::get_by_propagation_message_id(referenced.id.get(), &ctx.rt.state.db)
.await?
{
return get_user_by_id(message.author_id, &ctx.rt.state.db).await;
}
// If the replied message's author is not a bot or system user, use them.
return get_user_by_id(referenced.author.id.get(), &ctx.rt.state.db).await;
}
// fallback to command executor