changed stuff according to merge request !2

This commit is contained in:
Anish Nayak
2025-08-12 23:13:03 +01:00
parent 540db21c45
commit bacdc8ea0f
12 changed files with 262 additions and 134 deletions
+83 -41
View File
@@ -1,5 +1,5 @@
use scylla::{DeserializeValue, SerializeValue};
use scylla::{
DeserializeValue, SerializeValue,
client::session::Session,
deserialize::row::DeserializeRow,
errors::{ExecutionError, SingleRowError},
@@ -192,8 +192,8 @@ impl From<TwilightUser> for User {
}
#[derive(Debug, Clone, SerializeValue, DeserializeValue)]
pub struct MessageMetadata {
pub call_id: i64,
pub struct Message {
pub id: i64,
pub author_id: i64,
pub author_name: Option<String>,
pub author_avatar_url: Option<String>,
@@ -204,13 +204,12 @@ pub struct MessageMetadata {
pub propagation_message_id: Option<i64>,
pub propagation_channel_id: Option<i64>,
pub replying_to_id: Option<i64>,
pub message_replied_to_metadata: Option<Box<MessageMetadata>>,
pub is_deleted: Option<bool>,
pub is_edited: Option<bool>,
pub is_censored: Option<bool>,
}
impl<'a> Object<'a> for MessageMetadata {
impl<'a> Object<'a> for Message {
type Row = (
i64,
i64,
@@ -223,14 +222,13 @@ impl<'a> Object<'a> for MessageMetadata {
Option<i64>,
Option<i64>,
Option<i64>,
Option<Box<MessageMetadata>>,
Option<bool>,
Option<bool>,
Option<bool>,
);
fn from_row(row: Self::Row) -> Self {
Self {
call_id: row.0,
id: row.0,
author_id: row.1,
author_name: row.2,
author_avatar_url: row.3,
@@ -241,15 +239,14 @@ impl<'a> Object<'a> for MessageMetadata {
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,
is_deleted: row.11,
is_edited: row.12,
is_censored: row.13,
}
}
fn to_row(value: Self) -> Self::Row {
(
value.call_id,
value.id,
value.author_id,
value.author_name,
value.author_avatar_url,
@@ -260,7 +257,6 @@ impl<'a> Object<'a> for MessageMetadata {
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,
@@ -268,17 +264,14 @@ impl<'a> Object<'a> for MessageMetadata {
}
}
// --- BEGIN: Payphone Message and Report Models ---
#[derive(Debug, Clone)]
pub struct Message {
pub struct MessageRow {
pub call_id: u64,
pub message_id: u64,
pub message_metadata: MessageMetadata,
pub data: Message,
}
impl Message {
pub async fn get_by_id(
impl MessageRow {
pub async fn get_by_call_and_message_id(
call_id: u64,
message_id: u64,
session: &Session,
@@ -288,7 +281,7 @@ impl Message {
let result = session
.query_unpaged(
r#"
SELECT call_id, message_id, message_metadata
SELECT call_id, message_id, data
FROM payphone.messages
WHERE call_id = ? AND message_id = ?
LIMIT 1;
@@ -299,12 +292,8 @@ impl Message {
.map_err(DatabaseError::QueryError)?
.into_rows_result()
.map_err(|_| DatabaseError::ResultNotRows)?;
match result.single_row::<(i64, i64, MessageMetadata)>() {
Ok(row) => Ok(Some(Self {
call_id: i64_to_u64(row.0),
message_id: i64_to_u64(row.1),
message_metadata: row.2,
})),
match result.single_row::<<MessageRow as Object>::Row>() {
Ok(row) => Ok(Some(<MessageRow as Object>::from_row(row))),
Err(error) => match error {
SingleRowError::DeserializationFailed(_) | SingleRowError::TypeCheckFailed(_) => {
Err(DatabaseError::DeserializationFailed)
@@ -315,10 +304,54 @@ impl Message {
}
}
impl<'a> Object<'a> for Report {
type Row = (
i64, // report_id
Message,
Option<Message>,
OffsetDateTime,
Option<OffsetDateTime>,
Vec<i64>,
Option<i64>,
Option<String>,
);
fn from_row(row: Self::Row) -> Self {
Self {
report_id: i64_to_u64(row.0),
message: row.1,
replying_to: row.2,
created_at: row.3,
reviewed_at: row.4,
created_by_ids: row.5.iter().map(|&id| i64_to_u64(id)).collect(),
reviewed_by_id: row.6.map(i64_to_u64),
resolution: row.7,
}
}
fn to_row(value: Self) -> Self::Row {
(
u64_to_i64(value.report_id),
value.message,
value.replying_to,
value.created_at,
value.reviewed_at,
value
.created_by_ids
.iter()
.map(|&id| u64_to_i64(id))
.collect(),
value.reviewed_by_id.map(u64_to_i64),
value.resolution,
)
}
}
// --- BEGIN: Payphone Message and Report Models ---
#[derive(Debug, Clone)]
pub struct Report {
pub report_id: u64,
pub message: MessageMetadata,
pub message: Message,
pub replying_to: Option<Message>,
pub created_at: OffsetDateTime,
pub reviewed_at: Option<OffsetDateTime>,
pub created_by_ids: Vec<u64>,
@@ -326,33 +359,42 @@ pub struct Report {
pub resolution: Option<String>,
}
impl<'a> Object<'a> for MessageRow {
type Row = (i64, i64, <Message as Object<'a>>::Row);
fn from_row(row: Self::Row) -> Self {
Self {
call_id: i64_to_u64(row.0),
message_id: i64_to_u64(row.1),
data: <Message as Object>::from_row(row.2),
}
}
fn to_row(value: Self) -> Self::Row {
(
u64_to_i64(value.call_id),
u64_to_i64(value.message_id),
<Message as Object>::to_row(value.data),
)
}
}
impl Report {
pub async fn insert(&self, session: &Session) -> Result<(), DatabaseError> {
let row = Self::to_row(self.clone());
session
.query_unpaged(
r#"
INSERT INTO payphone.reports (
report_id,
message,
replying_to,
created_at,
reviewed_at,
created_by_ids,
reviewed_by_id,
resolution
) VALUES (?, ?, ?, ?, ?, ?, ?);
) VALUES (?, ?, ?, ?, ?, ?, ?, ?);
"#,
(
u64_to_i64(self.report_id),
&self.message,
self.created_at,
self.reviewed_at,
self.created_by_ids
.iter()
.map(|&id| u64_to_i64(id))
.collect::<Vec<i64>>(),
self.reviewed_by_id.map(u64_to_i64),
self.resolution.as_ref(),
),
row,
)
.await
.map_err(DatabaseError::QueryError)?;