feat(core): permissions query, finish bots impl

This commit is contained in:
Paul Makles
2023-08-05 16:14:47 +01:00
parent 9f3c1036d0
commit a681df04bd
20 changed files with 481 additions and 173 deletions

View File

@@ -1,8 +1,9 @@
use indexmap::{IndexMap, IndexSet};
use iso8601_timestamp::Timestamp;
use revolt_models::v0::{Embed, MessageSort, MessageWebhook};
use revolt_result::{create_error, Result};
use crate::File;
use crate::{Database, File};
auto_derived_partial!(
/// Message
@@ -166,7 +167,17 @@ auto_derived!(
);
#[allow(clippy::disallowed_methods)]
impl Message {}
impl Message {
/// Send a message without any notifications
pub async fn send_without_notifications(&mut self, db: &Database) -> Result<()> {
todo!()
}
/// Send a message
pub async fn send(&mut self) -> Result<()> {
todo!()
}
}
impl Interactions {
/// Validate interactions info is correct

View File

@@ -1,5 +1,10 @@
use std::fmt;
use revolt_result::Result;
use ulid::Ulid;
use crate::Database;
auto_derived!(
/// Ratelimit Event
pub struct RatelimitEvent {
@@ -23,3 +28,20 @@ impl fmt::Display for RatelimitEventType {
fmt::Debug::fmt(self, f)
}
}
#[allow(clippy::disallowed_methods)]
impl RatelimitEvent {
/// Create ratelimit event
pub async fn create(
db: &Database,
target_id: String,
event_type: RatelimitEventType,
) -> Result<()> {
db.insert_ratelimit_event(&RatelimitEvent {
id: Ulid::new().to_string(),
target_id,
event_type,
})
.await
}
}

View File

@@ -1,7 +1,11 @@
use iso8601_timestamp::Timestamp;
use revolt_result::Result;
use revolt_permissions::{calculate_channel_permissions, ChannelPermission};
use revolt_result::{create_error, Result};
use crate::{Database, File, Server};
use crate::{
events::client::EventV1, util::permissions::DatabasePermissionQuery, Database, File, Server,
User,
};
auto_derived_partial!(
/// Server Member
@@ -57,7 +61,85 @@ auto_derived!(
}
);
#[allow(clippy::disallowed_methods)]
impl Member {
/// Create a new member in a server
pub async fn create(
db: &Database,
server: &Server,
user: &User,
// channels: Option<Vec<Channel>>,
//) -> Result<Vec<Channel>> {
) -> Result<()> {
if db.fetch_ban(&server.id, &user.id).await.is_ok() {
return Err(create_error!(Banned));
}
let member = Member {
id: MemberCompositeKey {
server: server.id.to_string(),
user: user.id.to_string(),
},
..Default::default()
};
db.insert_member(&member).await?;
let mut channels = vec![];
if true {
let query = DatabasePermissionQuery::new(db, user).server(server);
let existing_channels = db.fetch_channels(&server.channels).await?;
for channel in existing_channels {
let mut channel_query = query.clone().channel(&channel);
if calculate_channel_permissions(&mut channel_query)
.await
.has_channel_permission(ChannelPermission::ViewChannel)
{
channels.push(channel);
}
}
}
EventV1::ServerMemberJoin {
id: server.id.clone(),
user: user.id.clone(),
}
.p(server.id.clone())
.await;
EventV1::ServerCreate {
id: server.id.clone(),
server: server.clone().into(),
channels: channels
.clone()
.into_iter()
.map(|channel| channel.into())
.collect(),
}
.private(user.id.clone())
.await;
if let Some(_id) = server
.system_messages
.as_ref()
.and_then(|x| x.user_joined.as_ref())
{
/* TODO: SystemMessage::UserJoined {
id: user.id.clone(),
}
.into_message(id.to_string())
.create_no_web_push(db, id, false)
.await
.ok(); */
}
// Ok(channels)
Ok(())
}
/// Update member data
pub async fn update<'a>(
&mut self,

View File

@@ -1,6 +1,6 @@
use std::{collections::HashSet, time::Duration};
use crate::{Database, File, RatelimitEvent};
use crate::{events::client::EventV1, Database, File, RatelimitEvent};
use once_cell::sync::Lazy;
use rand::seq::SliceRandom;
@@ -51,6 +51,15 @@ auto_derived_partial!(
);
auto_derived!(
/// Optional fields on user object
pub enum FieldsUser {
Avatar,
StatusText,
StatusPresence,
ProfileContent,
ProfileBackground,
}
/// User's relationship with another user (or themselves)
pub enum RelationshipStatus {
None,
@@ -108,15 +117,6 @@ auto_derived!(
/// Id of the owner of this bot
pub owner: String,
}
/// Optional fields on user object
pub enum FieldsUser {
Avatar,
StatusText,
StatusPresence,
ProfileContent,
ProfileBackground,
}
);
pub static DISCRIMINATOR_SEARCH_SPACE: Lazy<HashSet<String>> = Lazy::new(|| {
@@ -229,11 +229,11 @@ impl User {
return Err(create_error!(DiscriminatorChangeRatelimited));
}
db.insert_ratelimit_event(&RatelimitEvent {
id: Ulid::new().to_string(),
RatelimitEvent::create(
db,
target_id,
event_type: crate::RatelimitEventType::DiscriminatorChange,
})
crate::RatelimitEventType::DiscriminatorChange,
)
.await?;
}
}
@@ -245,6 +245,40 @@ impl User {
.to_string())
}
/// Update a user's username
pub async fn update_username(&mut self, db: &Database, username: String) -> Result<()> {
let username = User::validate_username(username)?;
if self.username.to_lowercase() == username.to_lowercase() {
self.update(
db,
PartialUser {
username: Some(username),
..Default::default()
},
vec![],
)
.await
} else {
self.update(
db,
PartialUser {
discriminator: Some(
User::find_discriminator(
db,
&username,
Some((self.discriminator.to_string(), self.id.clone())),
)
.await?,
),
username: Some(username),
..Default::default()
},
vec![],
)
.await
}
}
/// Check whether a username is already in use by another user
#[allow(dead_code)]
async fn is_username_taken(db: &Database, username: &str) -> Result<bool> {
@@ -272,13 +306,14 @@ impl User {
self.apply_options(partial.clone());
db.update_user(&self.id, &partial, remove.clone()).await?;
/* // TODO: EventV1::UserUpdate {
EventV1::UserUpdate {
id: self.id.clone(),
data: partial,
clear: remove,
data: partial.into(),
clear: remove.into_iter().map(|v| v.into()).collect(),
event_id: Some(Ulid::new().to_string()),
}
.p_user(self.id.clone(), db)
.await; */
.await;
Ok(())
}