Include group members in payload.

This commit is contained in:
Paul Makles
2021-01-26 22:05:32 +00:00
parent f42480886b
commit 3b85dcce14
15 changed files with 179 additions and 109 deletions

View File

@@ -54,16 +54,18 @@ impl Channel {
pub async fn get(id: &str) -> Result<Channel> {
let doc = get_collection("channels")
.find_one(
doc! { "_id": id },
None
)
.find_one(doc! { "_id": id }, None)
.await
.map_err(|_| Error::DatabaseError { operation: "find_one", with: "channel" })?
.map_err(|_| Error::DatabaseError {
operation: "find_one",
with: "channel",
})?
.ok_or_else(|| Error::UnknownChannel)?;
from_document::<Channel>(doc)
.map_err(|_| Error::DatabaseError { operation: "from_document", with: "channel" })
from_document::<Channel>(doc).map_err(|_| Error::DatabaseError {
operation: "from_document",
with: "channel",
})
}
pub async fn publish(self) -> Result<()> {

View File

@@ -1,5 +1,7 @@
use serde::{Deserialize, Serialize};
use crate::{database::permissions::user::UserPermissions, notifications::websocket::is_online};
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub enum RelationshipStatus {
None,
@@ -32,3 +34,17 @@ pub struct User {
#[serde(skip_serializing_if = "Option::is_none")]
pub online: Option<bool>,
}
impl User {
pub fn with(mut self, permissions: UserPermissions<[u32; 1]>) -> User {
if !permissions.get_view_all() {
self.relations = None;
}
if permissions.get_view_profile() {
self.online = Some(is_online(&self.id));
}
self
}
}

View File

@@ -84,22 +84,31 @@ impl<'a> PermissionCalculator<'a> {
}
}
Channel::DirectMessage { recipients, .. } => {
if recipients.iter().find(|x| *x == &self.perspective.id).is_some() {
if let Some(recipient) = recipients.iter().find(|x| *x != &self.perspective.id) {
if recipients
.iter()
.find(|x| *x == &self.perspective.id)
.is_some()
{
if let Some(recipient) = recipients.iter().find(|x| *x != &self.perspective.id)
{
let perms = self.for_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 == &self.perspective.id).is_some() {
if recipients
.iter()
.find(|x| *x == &self.perspective.id)
.is_some()
{
Ok(ChannelPermission::View + ChannelPermission::SendMessage)
} else {
Ok(0)
@@ -109,6 +118,6 @@ impl<'a> PermissionCalculator<'a> {
}
pub async fn for_channel(self) -> Result<ChannelPermissions<[u32; 1]>> {
Ok(ChannelPermissions([ self.calculate_channel().await? ]))
Ok(ChannelPermissions([self.calculate_channel().await?]))
}
}

View File

@@ -10,6 +10,8 @@ pub struct PermissionCalculator<'a> {
user: Option<&'a User>,
channel: Option<&'a Channel>,
has_mutual_conncetion: bool,
}
impl<'a> PermissionCalculator<'a> {
@@ -18,7 +20,9 @@ impl<'a> PermissionCalculator<'a> {
perspective,
user: None,
channel: None
channel: None,
has_mutual_conncetion: false,
}
}
@@ -35,4 +39,11 @@ impl<'a> PermissionCalculator<'a> {
..self
}
}
pub fn with_mutual_connection(self) -> PermissionCalculator<'a> {
PermissionCalculator {
has_mutual_conncetion: true,
..self
}
}
}

View File

@@ -3,9 +3,9 @@ use crate::util::result::{Error, Result};
use super::PermissionCalculator;
use std::ops;
use mongodb::bson::doc;
use num_enum::TryFromPrimitive;
use std::ops;
#[derive(Debug, PartialEq, Eq, TryFromPrimitive, Copy, Clone)]
#[repr(u32)]
@@ -13,7 +13,9 @@ pub enum UserPermission {
Access = 1,
ViewProfile = 2,
SendMessage = 4,
Invite = 8
Invite = 8,
ViewAll = 2147483648,
}
bitfield! {
@@ -23,9 +25,13 @@ bitfield! {
pub get_view_profile, _: 30;
pub get_send_message, _: 29;
pub get_invite, _: 28;
pub get_view_all, _: 0;
}
impl_op_ex!(+ |a: &UserPermission, b: &UserPermission| -> u32 { *a as u32 | *b as u32 });
impl_op_ex!(-|a: &UserPermission, b: &UserPermission| -> u32 { *a as u32 & !(*b as u32) });
impl_op_ex!(-|a: &u32, b: &UserPermission| -> u32 { *a & !(*b as u32) });
impl_op_ex_commutative!(+ |a: &u32, b: &UserPermission| -> u32 { *a | *b as u32 });
pub fn get_relationship(a: &User, b: &str) -> RelationshipStatus {
@@ -44,11 +50,13 @@ pub fn get_relationship(a: &User, b: &str) -> RelationshipStatus {
impl<'a> PermissionCalculator<'a> {
pub async fn calculate_user(self, target: &str) -> Result<u32> {
if &self.perspective.id == target {
return Ok(u32::MAX);
}
let mut permissions: u32 = 0;
match get_relationship(&self.perspective, &target) {
RelationshipStatus::Friend => {
return Ok(u32::MAX)
}
RelationshipStatus::Friend => return Ok(u32::MAX - UserPermission::ViewAll),
RelationshipStatus::Blocked | RelationshipStatus::BlockedOther => {
return Ok(UserPermission::Access as u32)
}
@@ -62,20 +70,25 @@ impl<'a> PermissionCalculator<'a> {
_ => {}
}
if get_collection("channels")
.find_one(
doc! {
"type": "Group",
"$and": {
"recipients": &self.perspective.id,
"recipients": target
}
},
None
)
.await
.map_err(|_| Error::DatabaseError { operation: "find", with: "channels" })?
.is_some() {
if self.has_mutual_conncetion
|| get_collection("channels")
.find_one(
doc! {
"type": "Group",
"$and": {
"recipients": &self.perspective.id,
"recipients": target
}
},
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "find",
with: "channels",
})?
.is_some()
{
return Ok(UserPermission::Access as u32);
}
@@ -83,11 +96,11 @@ impl<'a> PermissionCalculator<'a> {
}
pub async fn for_user(self, target: &str) -> Result<UserPermissions<[u32; 1]>> {
Ok(UserPermissions([ self.calculate_user(&target).await? ]))
Ok(UserPermissions([self.calculate_user(&target).await?]))
}
pub async fn for_user_given(self) -> Result<UserPermissions<[u32; 1]>> {
let id = &self.user.unwrap().id;
Ok(UserPermissions([ self.calculate_user(&id).await? ]))
Ok(UserPermissions([self.calculate_user(&id).await?]))
}
}