diff --git a/src/database/migrations/0001_init.cql b/src/database/migrations/0001_init.cql index aaf64e0..7227970 100644 --- a/src/database/migrations/0001_init.cql +++ b/src/database/migrations/0001_init.cql @@ -18,6 +18,7 @@ CREATE TYPE IF NOT EXISTS payphone.message_metadata ( propagation_message_id BIGINT, propagation_channel_id BIGINT, replying_to_id BIGINT, + message_replied_to_metadata FROZEN, is_deleted BOOLEAN, is_edited BOOLEAN, is_censored BOOLEAN diff --git a/src/database/models.rs b/src/database/models.rs index d41e9b3..5d07eac 100644 --- a/src/database/models.rs +++ b/src/database/models.rs @@ -12,10 +12,55 @@ pub struct MessageMetadata { pub propagation_message_id: Option, pub propagation_channel_id: Option, pub replying_to_id: Option, + pub message_replied_to_metadata: Option>, pub is_deleted: Option, pub is_edited: Option, pub is_censored: Option, } + +impl<'a> Object<'a> for MessageMetadata { + type Row = ( + i64, i64, Option, Option, Option, Option, Option>, Option, Option, Option, Option, Option>, Option, Option, Option + ); + fn from_row(row: Self::Row) -> Self { + Self { + call_id: row.0, + author_id: row.1, + author_name: row.2, + author_avatar_url: row.3, + author_mask: row.4, + content: row.5, + attachments: row.6, + sticker: row.7, + propagation_message_id: row.8, + propagation_channel_id: row.9, + replying_to_id: row.10, + message_replied_to_metadata: row.11, + is_deleted: row.12, + is_edited: row.13, + is_censored: row.14, + } + } + fn to_row(value: Self) -> Self::Row { + ( + value.call_id, + value.author_id, + value.author_name, + value.author_avatar_url, + value.author_mask, + value.content, + value.attachments, + value.sticker, + value.propagation_message_id, + value.propagation_channel_id, + value.replying_to_id, + value.message_replied_to_metadata, + value.is_deleted, + value.is_edited, + value.is_censored, + ) + } +} use scylla::{ client::session::Session, deserialize::row::DeserializeRow, diff --git a/src/groups/calls/commands.rs b/src/groups/calls/commands.rs index 0654258..5997a40 100644 --- a/src/groups/calls/commands.rs +++ b/src/groups/calls/commands.rs @@ -38,16 +38,25 @@ impl MessageCommand for ReportMessageCommand { .map_err(|_| InteractionError::InteractionNotFound)? .ok_or(InteractionError::InteractionNotFound)?; + // If the message is a reply, fetch the replied-to message and denormalize its metadata + let mut message_metadata = message.message_metadata.clone(); + if let Some(replying_to_id) = message_metadata.replying_to_id { + if let Ok(Some(reply_msg)) = Message::get_by_id(call_id, replying_to_id as u64, db).await { + message_metadata.message_replied_to_metadata = Some(Box::new(reply_msg.message_metadata)); + } + } + // Build the report using the new UDT-based message metadata let report = Report { report_id: get_random_u64(), - message: message.message_metadata.clone(), + message: message_metadata, created_at: time::OffsetDateTime::now_utc(), reviewed_at: None, created_by_ids: vec![ctx.event.author_id().map(|id| id.get()).unwrap_or(0)], reviewed_by_id: None, resolution: None, }; + // Insert the report report.insert(db).await.map_err(|_| InteractionError::DeferringFailed)?;