refactor(core): use core crates for sending messages

This commit is contained in:
Paul Makles
2023-09-03 16:31:39 +01:00
parent 279d9ef1b5
commit 9929f7499a
7 changed files with 100 additions and 71 deletions

View File

@@ -7,6 +7,7 @@ use revolt_models::v0::{
self, DataMessageSend, Embed, MessageAuthor, MessageSort, MessageWebhook, PushNotification,
ReplyIntent, SendableEmbed, RE_MENTION,
};
use revolt_permissions::{ChannelPermission, PermissionValue};
use revolt_result::Result;
use ulid::Ulid;
@@ -14,7 +15,7 @@ use crate::{
events::client::EventV1,
tasks::{self, ack::AckEvent},
util::idempotency::IdempotencyKey,
Channel, Database, File,
Channel, Database, Emoji, File,
};
auto_derived_partial!(
@@ -353,7 +354,9 @@ impl Message {
// Pass-through nonce value for clients
message.nonce = Some(idempotency.into_key());
// Send the message
message.send(db, author, &channel, generate_embeds).await?;
Ok(message)
}
@@ -528,6 +531,27 @@ impl SystemMessage {
}
impl Interactions {
/// Validate interactions info is correct
pub async fn validate(&self, db: &Database, permissions: &PermissionValue) -> Result<()> {
let config = config().await;
if let Some(reactions) = &self.reactions {
permissions.throw_if_lacking_channel_permission(ChannelPermission::React)?;
if reactions.len() > config.features.limits.default.message_reactions {
return Err(create_error!(InvalidOperation));
}
for reaction in reactions {
if !Emoji::can_use(db, reaction).await? {
return Err(create_error!(InvalidOperation));
}
}
}
Ok(())
}
/// Check if we can use a given emoji to react
pub fn can_use(&self, emoji: &str) -> bool {
if self.restrict_reactions {

View File

@@ -626,18 +626,26 @@ impl From<crate::FieldsRole> for FieldsRole {
}
impl crate::User {
pub async fn into<P>(self, perspective: P) -> User
pub async fn into<'a, P>(self, perspective: P) -> User
where
P: Into<Option<crate::User>>,
P: Into<Option<&'a crate::User>>,
{
let relationship = if let Some(perspective) = perspective.into() {
perspective
.relations
.unwrap_or_default()
.into_iter()
.find(|relationship| relationship.id == self.id)
.map(|relationship| relationship.status.into())
.unwrap_or_default()
if perspective.id == self.id {
RelationshipStatus::User
} else {
perspective
.relations
.as_ref()
.map(|relations| {
relations
.iter()
.find(|relationship| relationship.id == self.id)
.map(|relationship| relationship.status.clone().into())
.unwrap_or_default()
})
.unwrap_or_default()
}
} else {
RelationshipStatus::None
};