mirror of
https://github.com/stoatchat/stoatchat.git
synced 2026-07-14 13:36:59 +00:00
Refractor to use UserRef.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use bson::{bson, doc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::{get_collection, MemberPermissions};
|
||||
use super::get_collection;
|
||||
use mongodb::options::FindOneOptions;
|
||||
|
||||
pub fn find_member_permissions<C: Into<Option<String>>>(
|
||||
|
||||
@@ -24,10 +24,10 @@ pub fn get_collection(collection: &str) -> Collection {
|
||||
get_db().collection(collection)
|
||||
}
|
||||
|
||||
pub mod permissions;
|
||||
pub mod channel;
|
||||
pub mod guild;
|
||||
pub mod message;
|
||||
pub mod permissions;
|
||||
pub mod user;
|
||||
|
||||
pub use permissions::*;
|
||||
|
||||
@@ -9,6 +9,107 @@ bitfield! {
|
||||
pub get_send_messages, set_send_messages: 2;
|
||||
}
|
||||
|
||||
//struct PermissionCalculator {
|
||||
//channel: Option<>,
|
||||
//}
|
||||
use super::get_collection;
|
||||
use crate::guards::channel::ChannelRef;
|
||||
use crate::guards::guild::GuildRef;
|
||||
|
||||
use bson::{bson, doc};
|
||||
use mongodb::options::FindOneOptions;
|
||||
|
||||
pub struct PermissionCalculator {
|
||||
pub user_id: String,
|
||||
pub channel: Option<ChannelRef>,
|
||||
pub guild: Option<GuildRef>,
|
||||
}
|
||||
|
||||
impl PermissionCalculator {
|
||||
pub fn new(user_id: String) -> PermissionCalculator {
|
||||
PermissionCalculator {
|
||||
user_id,
|
||||
channel: None,
|
||||
guild: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn channel(self, channel: ChannelRef) -> PermissionCalculator {
|
||||
PermissionCalculator {
|
||||
channel: Some(channel),
|
||||
..self
|
||||
}
|
||||
}
|
||||
|
||||
pub fn guild(self, guild: GuildRef) -> PermissionCalculator {
|
||||
PermissionCalculator {
|
||||
guild: Some(guild),
|
||||
..self
|
||||
}
|
||||
}
|
||||
|
||||
pub fn calculate(self) -> u8 {
|
||||
let guild = if let Some(value) = self.guild {
|
||||
Some(value)
|
||||
} else if let Some(channel) = &self.channel {
|
||||
match channel.channel_type {
|
||||
0..=1 => None,
|
||||
2 => {
|
||||
if let Some(id) = &channel.guild {
|
||||
GuildRef::from(id.clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let mut permissions = 0;
|
||||
if let Some(guild) = guild {
|
||||
if let Some(_data) = guild.fetch_data_given(
|
||||
doc! {
|
||||
"members": {
|
||||
"$elemMatch": {
|
||||
"id": &self.user_id,
|
||||
}
|
||||
}
|
||||
},
|
||||
doc! { }
|
||||
) {
|
||||
if guild.owner == self.user_id {
|
||||
return u8::MAX;
|
||||
}
|
||||
|
||||
permissions = guild.default_permissions;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(channel) = &self.channel {
|
||||
match channel.channel_type {
|
||||
0 => {
|
||||
if let Some(arr) = &channel.recipients {
|
||||
for item in arr {
|
||||
if item == &self.user_id {
|
||||
permissions = 49;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
1 => {
|
||||
unreachable!()
|
||||
},
|
||||
2 => {
|
||||
// nothing implemented yet
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
permissions as u8
|
||||
}
|
||||
|
||||
pub fn as_permission(self) -> MemberPermissions<[u8; 1]> {
|
||||
MemberPermissions([ self.calculate() ])
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user