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
Generated
+1 -1
View File
@@ -2013,7 +2013,7 @@ checksum = "df94ce210e5bc13cb6651479fa48d14f601d9858cfe0467f43ae157023b938d3"
[[package]]
name = "payphone"
version = "4.4.24"
version = "4.4.31"
dependencies = [
"anyhow",
"async-trait",
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "payphone"
version = "4.4.24"
version = "4.4.31"
edition = "2024"
default-run = "payphone"
+2 -66
View File
@@ -1331,11 +1331,6 @@ impl Message {
propagation_message_id: u64,
session: &Session,
) -> Result<Option<Self>, DatabaseError> {
let message_by_propagation_id =
MessageByPropagationId::get_by_propagation_message_id(propagation_message_id, session)
.await?;
if let Some(message_by_propagation_id) = message_by_propagation_id {
let result = session
.query_unpaged(
r#"
@@ -1348,12 +1343,11 @@ impl Message {
FROM
payphone.messages
WHERE
call_id = ? AND message_id = ?
propagation_message_id = ?
LIMIT 1;
"#,
(
u64_to_i64(message_by_propagation_id.call_id),
u64_to_i64(message_by_propagation_id.message_id),
u64_to_i64(propagation_message_id),
),
)
.await?
@@ -1362,9 +1356,6 @@ impl Message {
return get_single_row_from_result(&result).await;
}
Ok(None)
}
pub async fn get_last_by_author_id(
call_id: u64,
author_id: u64,
@@ -1438,61 +1429,6 @@ impl Message {
}
}
pub struct MessageByPropagationId {
pub call_id: u64,
pub message_id: u64,
pub propagation_message_id: u64,
}
impl<'a> Object<'a> for MessageByPropagationId {
type Row = (i64, i64, i64);
fn from_row(row: Self::Row) -> Self {
Self {
call_id: i64_to_u64(row.0),
message_id: i64_to_u64(row.1),
propagation_message_id: i64_to_u64(row.2),
}
}
fn to_row(value: Self) -> Self::Row {
(
u64_to_i64(value.call_id),
u64_to_i64(value.message_id),
u64_to_i64(value.propagation_message_id),
)
}
}
impl MessageByPropagationId {
pub async fn get_by_propagation_message_id(
propagation_message_id: u64,
session: &Session,
) -> Result<Option<Self>, DatabaseError> {
let propagation_message_id = u64_to_i64(propagation_message_id);
let result = session
.query_unpaged(
r#"
SELECT
call_id,
message_id,
propagation_message_id
FROM
payphone.messages_by_propagation_id
WHERE
propagation_message_id = ?
LIMIT 1;
"#,
(propagation_message_id,),
)
.await?
.into_rows_result()?;
get_single_row_from_result(&result).await
}
}
pub struct MessageByAuthorId {
pub call_id: u64,
pub message_id: u64,
+12 -15
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,14 +323,13 @@ 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 {
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)
@@ -359,8 +361,7 @@ impl OnMessageHandler<State, Extras> for CallMessageHandler {
)
};
let replying_to_user =
User::get_by_id(replying_to.author_id, &rt.state.db).await?;
let replying_to_user = User::get_by_id(replying_to.author_id, &rt.state.db).await?;
embeds.push(
EmbedBuilder::new()
@@ -380,7 +381,6 @@ impl OnMessageHandler<State, Extras> for CallMessageHandler {
.build(),
);
}
}
if participant.is_masked {
let last_message =
@@ -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."
]
+11 -10
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,21 +47,20 @@ 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) =
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
get_user_by_id(ctx.event.author.id.get(), &ctx.rt.state.db).await