Skip to content

Commit

Permalink
feat: Support Message Attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulrahman1s committed Jul 14, 2022
1 parent b55da7e commit ee88e4b
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 5 deletions.
5 changes: 5 additions & 0 deletions assets/migrations/00004_messages.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ALTER TABLE attachments
ADD COLUMN deleted BOOLEAN NOT NULL DEFAULT FALSE;

ALTER TABLE messages
ADD COLUMN attachments JSONB NOT NULL DEFAULT '[]'::JSONB;
11 changes: 8 additions & 3 deletions src/routes/messages/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use validator::Validate;
#[derive(Deserialize, Validate, OpgModel)]
pub struct CreateMessageOptions {
#[validate(length(min = 1, max = 2000))]
content: String,
content: Option<String>,
#[validate(length(max = 5))]
attachments: Option<Vec<Attachment>>,
}

pub async fn create(
Expand All @@ -23,8 +25,11 @@ pub async fn create(

let mut msg = Message::new(channel_id, user.id);

// TODO: Add more fields
msg.content = data.content.into();
msg.content = data.content;

if let Some(attachments) = data.attachments {
msg.attachments = ormlite::types::Json(attachments);
}

if msg.is_empty() {
return Err(Error::EmptyMessage);
Expand Down
19 changes: 18 additions & 1 deletion src/routes/messages/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,24 @@ pub async fn delete(
permissions.has(Permissions::MANAGE_MESSAGES)?;
}

msg.remove().await?;
let attachment_ids: Vec<i64> = msg
.attachments
.0
.clone()
.into_iter()
.map(|a| a.id)
.collect();

let mut tx = pool().begin().await?;

sqlx::query("UPDATE attachments SET deleted = TRUE WHERE id = ANY($1)")
.bind(attachment_ids)
.execute(&mut tx)
.await?;

msg.delete(&mut tx).await?;

tx.commit().await?;

publish(channel_id, Payload::MessageDelete(id.into())).await;

Expand Down
23 changes: 23 additions & 0 deletions src/structures/attachment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use super::Base;
use ormlite::model::*;
use serde::{Deserialize, Serialize};

#[serde_as]
#[derive(Model, FromRow, Serialize, Deserialize, Debug, OpgModel, Clone)]
#[ormlite(table = "attachments")]
pub struct Attachment {
#[serde_as(as = "serde_with::DisplayFromStr")]
#[opg(string)]
pub id: i64,
pub filename: String,
#[serde(skip_serializing_if = "Option::is_none", default)]
pub width: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none", default)]
pub height: Option<i32>,
pub content_type: String,
pub size: i32,
#[serde(skip_serializing, default)]
pub deleted: bool,
}

impl Base for Attachment {}
8 changes: 7 additions & 1 deletion src/structures/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ use super::*;
use crate::utils::snowflake;
use chrono::NaiveDateTime;
use ormlite::model::*;
use ormlite::types::Json;
use serde::{Deserialize, Serialize};

#[derive(Serialize, OpgModel)]
struct MessageAttachments(Vec<Attachment>);

#[serde_as]
#[derive(Debug, Serialize, Deserialize, Model, FromRow, Clone, Default, OpgModel)]
#[ormlite(table = "messages")]
Expand All @@ -12,6 +16,8 @@ pub struct Message {
#[opg(string)]
pub id: i64,
pub content: Option<String>,
#[opg(custom = "MessageAttachments")]
pub attachments: Json<Vec<Attachment>>,
#[serde_as(as = "serde_with::DisplayFromStr")]
#[opg(string)]
pub channel_id: i64,
Expand All @@ -33,7 +39,7 @@ impl Message {
}

pub fn is_empty(&self) -> bool {
self.content.is_none() /* && self.attachments.len() == 0 */
self.content.is_none() && self.attachments.0.is_empty()
}

#[cfg(test)]
Expand Down
2 changes: 2 additions & 0 deletions src/structures/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod attachment;
pub mod base;
pub mod bot;
pub mod channel;
Expand All @@ -10,6 +11,7 @@ pub mod session;
pub mod user;

pub use crate::database::pool;
pub use attachment::*;
pub use base::*;
pub use bot::*;
pub use channel::*;
Expand Down

0 comments on commit ee88e4b

Please sign in to comment.