chore(refactor): validate further up the code

This commit is contained in:
Paul Makles
2022-06-08 14:14:16 +01:00
parent 0abc12e5fa
commit 2051c8ce45
3 changed files with 5 additions and 9 deletions

View File

@@ -71,10 +71,6 @@ pub async fn req(
new_embeds.clear(); new_embeds.clear();
for embed in embeds { for embed in embeds {
embed
.validate()
.map_err(|error| Error::FailedValidation { error })?;
new_embeds.push(embed.clone().into_embed(db, message.id.clone()).await?); new_embeds.push(embed.clone().into_embed(db, message.id.clone()).await?);
} }
} }

View File

@@ -138,10 +138,6 @@ pub async fn message_send(
let mut embeds = vec![]; let mut embeds = vec![];
if let Some(sendable_embeds) = data.embeds { if let Some(sendable_embeds) = data.embeds {
for sendable_embed in sendable_embeds { for sendable_embed in sendable_embeds {
sendable_embed
.validate()
.map_err(|error| Error::FailedValidation { error })?;
embeds.push(sendable_embed.into_embed(db, message_id.clone()).await?) embeds.push(sendable_embed.into_embed(db, message_id.clone()).await?)
} }
} }

View File

@@ -2,6 +2,7 @@ use std::collections::HashSet;
use serde_json::json; use serde_json::json;
use ulid::Ulid; use ulid::Ulid;
use validator::Validate;
use crate::{ use crate::{
events::client::EventV1, events::client::EventV1,
@@ -17,7 +18,7 @@ use crate::{
january::{Embed, Text}, january::{Embed, Text},
push::PushNotification, push::PushNotification,
}, },
Database, Result, Database, Error, Result,
}; };
impl Message { impl Message {
@@ -232,6 +233,9 @@ impl From<SystemMessage> for String {
impl SendableEmbed { impl SendableEmbed {
pub async fn into_embed(self, db: &Database, message_id: String) -> Result<Embed> { pub async fn into_embed(self, db: &Database, message_id: String) -> Result<Embed> {
self.validate()
.map_err(|error| Error::FailedValidation { error })?;
let media = if let Some(id) = self.media { let media = if let Some(id) = self.media {
Some( Some(
db.find_and_use_attachment(&id, "attachments", "message", &message_id) db.find_and_use_attachment(&id, "attachments", "message", &message_id)