diff --git a/src/database/permissions/channel.rs b/src/database/permissions/channel.rs index df696795e195a1c248fd81bbb9b08f02ca8c49f2..b4096a340055858f8610e3af40ea90681860953a 100644 --- a/src/database/permissions/channel.rs +++ b/src/database/permissions/channel.rs @@ -25,46 +25,6 @@ bitfield! { impl_op_ex!(+ |a: &ChannelPermission, b: &ChannelPermission| -> u32 { *a as u32 | *b as u32 }); impl_op_ex_commutative!(+ |a: &u32, b: &ChannelPermission| -> u32 { *a | *b as u32 }); -/*pub async fn calculate(user: &User, target: &Channel) -> Result<u32> { - match target { - Channel::SavedMessages { user: owner, .. } => { - if &user.id == owner { - Ok(ChannelPermission::View - + ChannelPermission::SendMessage - + ChannelPermission::ManageMessages) - } else { - Ok(0) - } - } - Channel::DirectMessage { recipients, .. } => { - if recipients.iter().find(|x| *x == &user.id).is_some() { - if let Some(recipient) = recipients.iter().find(|x| *x != &user.id) { - let perms = super::user::get(&user, recipient).await?; - - if perms.get_send_message() { - return Ok(ChannelPermission::View + ChannelPermission::SendMessage); - } - - return Ok(ChannelPermission::View as u32); - } - } - - Ok(0) - } - Channel::Group { recipients, .. } => { - if recipients.iter().find(|x| *x == &user.id).is_some() { - Ok(ChannelPermission::View + ChannelPermission::SendMessage) - } else { - Ok(0) - } - } - } -} - -pub async fn get(user: &User, target: &Channel) -> Result<ChannelPermissions<[u32; 1]>> { - Ok(ChannelPermissions([calculate(&user, &target).await?])) -}*/ - impl<'a> PermissionCalculator<'a> { pub async fn calculate_channel(self) -> Result<u32> { let channel = if let Some(channel) = self.channel { diff --git a/src/database/permissions/user.rs b/src/database/permissions/user.rs index 07aa09379c997e815c97e52486b0fb9ad8a0aeb0..4515e5c3452b25c05dac773992d7bf04814a5022 100644 --- a/src/database/permissions/user.rs +++ b/src/database/permissions/user.rs @@ -72,10 +72,10 @@ impl<'a> PermissionCalculator<'a> { { "type": "Group" }, { "type": "DirectMessage" }, ], - "$and": { - "recipients": &self.perspective.id, - "recipients": target - } + "$and": [ + { "recipients": &self.perspective.id }, + { "recipients": target } + ] }, None, ) diff --git a/src/main.rs b/src/main.rs index efe439bb7657605ad07dc676b3111fc76c4879ef..a8ab180cdb96080beab61c75332357d7662170a0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,14 +18,16 @@ pub mod notifications; pub mod routes; pub mod util; -use log::info; -use futures::join; use chrono::Duration; +use futures::join; +use log::info; use rauth::auth::Auth; +use rauth::options::{EmailVerification, Options, SMTP}; use rocket_cors::AllowedOrigins; use rocket_prometheus::PrometheusMetrics; -use rauth::options::{EmailVerification, Options, SMTP}; -use util::variables::{PUBLIC_URL, SMTP_FROM, SMTP_HOST, SMTP_PASSWORD, SMTP_USERNAME, USE_EMAIL, USE_PROMETHEUS}; +use util::variables::{ + PUBLIC_URL, SMTP_FROM, SMTP_HOST, SMTP_PASSWORD, SMTP_USERNAME, USE_EMAIL, USE_PROMETHEUS, +}; #[async_std::main] async fn main() { diff --git a/src/notifications/payload.rs b/src/notifications/payload.rs index b9570dc3429e3ea5f5b03a27704bb65392be43ea..5a8d612da2db1a2138280e311376ad99f6c290c2 100644 --- a/src/notifications/payload.rs +++ b/src/notifications/payload.rs @@ -59,6 +59,8 @@ pub async fn generate_ready(mut user: User) -> Result<ClientboundNotification> { if let Channel::Group { recipients, .. } = &channel { user_ids.extend(recipients.iter().cloned()); + } else if let Channel::DirectMessage { recipients, .. } = &channel { + user_ids.extend(recipients.iter().cloned()); } channels.push(channel); @@ -97,11 +99,7 @@ pub async fn generate_ready(mut user: User) -> Result<ClientboundNotification> { .for_user_given() .await?; - users.push( - other - .from(&user) - .with(permissions) - ); + users.push(other.from(&user).with(permissions)); } } } diff --git a/src/routes/users/fetch_user.rs b/src/routes/users/fetch_user.rs index 0ca10a4c5d3265d1b108075a911d11fe29ea4484..9a660719c67c7b80092eb5f0fee9ae97f2f71eb3 100644 --- a/src/routes/users/fetch_user.rs +++ b/src/routes/users/fetch_user.rs @@ -16,9 +16,5 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> { Err(Error::LabelMe)? } - Ok(json!( - target - .from(&user) - .with(perm) - )) + Ok(json!(target.from(&user).with(perm))) } diff --git a/src/routes/users/get_avatar.rs b/src/routes/users/get_avatar.rs index e85e806933600b2b1f9831f3ba66abf097aef83c..d928a4474749d3eaeed89a3c5f6fa837975a0ce7 100644 --- a/src/routes/users/get_avatar.rs +++ b/src/routes/users/get_avatar.rs @@ -1,12 +1,12 @@ +use md5; +use mongodb::bson::doc; use mongodb::options::FindOneOptions; use rocket::response::Redirect; -use mongodb::bson::doc; use urlencoding; -use md5; +use crate::database::*; use crate::util::result::{Error, Result}; use crate::util::variables::PUBLIC_URL; -use crate::database::*; #[get("/<target>/avatar")] pub async fn req(target: Ref) -> Result<Redirect> { @@ -17,32 +17,33 @@ pub async fn req(target: Ref) -> Result<Redirect> { }, FindOneOptions::builder() .projection(doc! { "email": 1 }) - .build() + .build(), ) .await - .map_err(|_| Error::DatabaseError { operation: "find_one", with: "user" })? + .map_err(|_| Error::DatabaseError { + operation: "find_one", + with: "user", + })? .ok_or_else(|| Error::UnknownUser)?; - + let email = doc .get_str("email") - .map_err(|_| Error::DatabaseError { operation: "get_str(email)", with: "user" })? + .map_err(|_| Error::DatabaseError { + operation: "get_str(email)", + with: "user", + })? .to_lowercase(); - let url = format!( - "https://www.gravatar.com/avatar/{:x}?s=128&d={}", - md5::compute(email), - urlencoding::encode( - &format!( - "{}/users/{}/default_avatar", - *PUBLIC_URL, - &target.id - ) - ) - ); + let url = format!( + "https://www.gravatar.com/avatar/{:x}?s=128&d={}", + md5::compute(email), + urlencoding::encode(&format!( + "{}/users/{}/default_avatar", + *PUBLIC_URL, &target.id + )) + ); - dbg!(&url); + dbg!(&url); - Ok( - Redirect::to(url) - ) + Ok(Redirect::to(url)) }