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

@@ -52,6 +52,7 @@ message_length = 2000
message_embeds = 5
message_replies = 5
message_attachments = 5
message_reactions = 20
servers = 100
server_emoji = 100
server_roles = 200

View File

@@ -101,6 +101,7 @@ pub struct FeaturesLimits {
pub message_replies: usize,
pub message_attachments: usize,
pub message_embeds: usize,
pub message_reactions: usize,
pub servers: usize,
pub server_emoji: usize,
pub server_roles: usize,

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
};

View File

@@ -39,20 +39,17 @@ impl PermissionValue {
}
/// Check whether certain a permission has been granted
pub fn has(&mut self, v: u64) -> bool {
pub fn has(&self, v: u64) -> bool {
(self.0 & v) == v
}
/// Check whether certain a channel permission has been granted
pub fn has_channel_permission(&mut self, permission: ChannelPermission) -> bool {
pub fn has_channel_permission(&self, permission: ChannelPermission) -> bool {
self.has(permission as u64)
}
/// Throw if missing channel permission
pub fn throw_if_lacking_channel_permission(
&mut self,
permission: ChannelPermission,
) -> Result<()> {
pub fn throw_if_lacking_channel_permission(&self, permission: ChannelPermission) -> Result<()> {
if self.has_channel_permission(permission) {
Ok(())
} else {