Run cargo fmt.

This commit is contained in:
Paul
2021-05-01 17:29:31 +01:00
parent 59b18fd376
commit 8cfa5d7091
12 changed files with 166 additions and 119 deletions

View File

@@ -1,8 +1,8 @@
use mongodb::bson::{doc, from_document};
use serde::{Deserialize, Serialize};
use crate::util::result::{Error, Result};
use crate::database::*;
use crate::util::result::{Error, Result};
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(tag = "type")]
@@ -23,7 +23,7 @@ pub struct File {
metadata: Metadata,
content_type: String,
size: isize,
#[serde(skip_serializing_if = "Option::is_none")]
deleted: Option<bool>,
@@ -38,7 +38,12 @@ pub struct File {
}
impl File {
pub async fn find_and_use(attachment_id: &str, tag: &str, parent_type: &str, parent_id: &str) -> Result<File> {
pub async fn find_and_use(
attachment_id: &str,
tag: &str,
parent_type: &str,
parent_id: &str,
) -> Result<File> {
let attachments = get_collection("attachments");
let key = format!("{}_id", parent_type);
if let Some(doc) = attachments

View File

@@ -1,10 +1,13 @@
use crate::database::*;
use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result};
use mongodb::{bson::{Document, doc, from_document, to_document}, options::FindOptions};
use futures::StreamExt;
use mongodb::{
bson::{doc, from_document, to_document, Document},
options::FindOptions,
};
use rocket_contrib::json::JsonValue;
use serde::{Deserialize, Serialize};
use futures::StreamExt;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct LastMessage {
@@ -132,7 +135,7 @@ impl Channel {
.into_iter()
.filter_map(|x| x.get_str("_id").ok().map(|x| x.to_string()))
.collect::<Vec<String>>();
// If we found any, mark them as deleted.
if message_ids.len() > 0 {
get_collection("attachments")
@@ -147,7 +150,7 @@ impl Channel {
"deleted": true
}
},
None
None,
)
.await
.map_err(|_| Error::DatabaseError {

View File

@@ -271,7 +271,7 @@ impl Message {
"deleted": true
}
},
None
None,
)
.await
.map_err(|_| Error::DatabaseError {

View File

@@ -116,10 +116,15 @@ impl User {
/// Mutate the user object to appear as seen by user.
/// Also overrides the relationship status.
pub async fn from_override(mut self, user: &User, relationship: RelationshipStatus) -> Result<User> {
pub async fn from_override(
mut self,
user: &User,
relationship: RelationshipStatus,
) -> Result<User> {
let permissions = PermissionCalculator::new(&user)
.with_relationship(&relationship)
.for_user(&self.id).await?;
.for_user(&self.id)
.await?;
self.relations = None;
self.relationship = Some(relationship);

View File

@@ -49,7 +49,12 @@ impl<'a> PermissionCalculator<'a> {
}
let mut permissions: u32 = 0;
match self.relationship.clone().map(|v| v.to_owned()).unwrap_or_else(|| get_relationship(&self.perspective, &target)) {
match self
.relationship
.clone()
.map(|v| v.to_owned())
.unwrap_or_else(|| get_relationship(&self.perspective, &target))
{
RelationshipStatus::Friend => return Ok(u32::MAX),
RelationshipStatus::Blocked | RelationshipStatus::BlockedOther => {
return Ok(UserPermission::Access as u32)

View File

@@ -81,7 +81,7 @@ pub enum ClientboundNotification {
UserRelationship {
id: String,
user: User,
status: RelationshipStatus
status: RelationshipStatus,
},
UserPresence {
id: String,

View File

@@ -1,10 +1,7 @@
use crate::database::*;
use crate::util::result::{Error, Result};
use mongodb::{
bson::{doc},
options::FindOneOptions,
};
use mongodb::{bson::doc, options::FindOneOptions};
use rocket_contrib::json::{Json, JsonValue};
use serde::{Deserialize, Serialize};
use ulid::Ulid;

View File

@@ -67,8 +67,12 @@ pub async fn req(user: User, username: String) -> Result<JsonValue> {
)
) {
Ok(_) => {
let target_user = target_user.from_override(&user, RelationshipStatus::Friend).await?;
let user = user.from_override(&target_user, RelationshipStatus::Friend).await?;
let target_user = target_user
.from_override(&user, RelationshipStatus::Friend)
.await?;
let user = user
.from_override(&target_user, RelationshipStatus::Friend)
.await?;
try_join!(
ClientboundNotification::UserRelationship {
@@ -126,8 +130,12 @@ pub async fn req(user: User, username: String) -> Result<JsonValue> {
)
) {
Ok(_) => {
let target_user = target_user.from_override(&user, RelationshipStatus::Outgoing).await?;
let user = user.from_override(&target_user, RelationshipStatus::Incoming).await?;
let target_user = target_user
.from_override(&user, RelationshipStatus::Outgoing)
.await?;
let user = user
.from_override(&target_user, RelationshipStatus::Incoming)
.await?;
try_join!(
ClientboundNotification::UserRelationship {
id: user.id.clone(),

View File

@@ -76,8 +76,12 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
)
) {
Ok(_) => {
let target = target.from_override(&user, RelationshipStatus::Friend).await?;
let user = user.from_override(&target, RelationshipStatus::Friend).await?;
let target = target
.from_override(&user, RelationshipStatus::Friend)
.await?;
let user = user
.from_override(&target, RelationshipStatus::Friend)
.await?;
let target_id = target.id.clone();
try_join!(
@@ -134,8 +138,12 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
)
) {
Ok(_) => {
let target = target.from_override(&user, RelationshipStatus::Blocked).await?;
let user = user.from_override(&target, RelationshipStatus::BlockedOther).await?;
let target = target
.from_override(&user, RelationshipStatus::Blocked)
.await?;
let user = user
.from_override(&target, RelationshipStatus::BlockedOther)
.await?;
let target_id = target.id.clone();
try_join!(

View File

@@ -23,31 +23,39 @@ pub struct Data {
#[patch("/<_ignore_id>", data = "<data>")]
pub async fn req(user: User, mut data: Json<Data>, _ignore_id: String) -> Result<()> {
if data.0.status.is_none() && data.0.profile.is_none() && data.0.avatar.is_none() {
return Ok(())
return Ok(());
}
data.validate()
.map_err(|error| Error::FailedValidation { error })?;
let mut set = to_document(&data.0).map_err(|_| Error::DatabaseError { operation: "to_document", with: "data" })?;
let mut set = to_document(&data.0).map_err(|_| Error::DatabaseError {
operation: "to_document",
with: "data",
})?;
let avatar = std::mem::replace(&mut data.0.avatar, None);
let attachment = if let Some(attachment_id) = avatar {
let attachment = File::find_and_use(&attachment_id, "avatars", "user", &user.id).await?;
set.insert("avatar", to_document(&attachment).map_err(|_| Error::DatabaseError { operation: "to_document", with: "attachment" })?);
set.insert(
"avatar",
to_document(&attachment).map_err(|_| Error::DatabaseError {
operation: "to_document",
with: "attachment",
})?,
);
Some(attachment)
} else {
None
};
get_collection("users")
.update_one(
doc! { "_id": &user.id },
doc! { "$set": set },
None
)
.await
.map_err(|_| Error::DatabaseError { operation: "update_one", with: "user" })?;
.update_one(doc! { "_id": &user.id }, doc! { "$set": set }, None)
.await
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "user",
})?;
if let Some(status) = data.0.status {
ClientboundNotification::UserUpdate {

View File

@@ -45,8 +45,12 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
)
) {
Ok(_) => {
let target = target.from_override(&user, RelationshipStatus::None).await?;
let user = user.from_override(&target, RelationshipStatus::None).await?;
let target = target
.from_override(&user, RelationshipStatus::None)
.await?;
let user = user
.from_override(&target, RelationshipStatus::None)
.await?;
let target_id = target.id.clone();
try_join!(

View File

@@ -12,100 +12,104 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
let target = target.fetch_user().await?;
match get_relationship(&user, &target.id) {
RelationshipStatus::Blocked => {
match get_relationship(&target, &user.id) {
RelationshipStatus::Blocked => {
RelationshipStatus::Blocked => match get_relationship(&target, &user.id) {
RelationshipStatus::Blocked => {
col.update_one(
doc! {
"_id": &user.id,
"relations._id": &target.id
},
doc! {
"$set": {
"relations.$.status": "BlockedOther"
}
},
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "user",
})?;
let target = target
.from_override(&user, RelationshipStatus::BlockedOther)
.await?;
ClientboundNotification::UserRelationship {
id: user.id.clone(),
user: target,
status: RelationshipStatus::BlockedOther,
}
.publish(user.id.clone())
.await
.ok();
Ok(json!({ "status": "BlockedOther" }))
}
RelationshipStatus::BlockedOther => {
match try_join!(
col.update_one(
doc! {
"_id": &user.id,
"relations._id": &target.id
"_id": &user.id
},
doc! {
"$set": {
"relations.$.status": "BlockedOther"
"$pull": {
"relations": {
"_id": &target.id
}
}
},
None,
None
),
col.update_one(
doc! {
"_id": &target.id
},
doc! {
"$pull": {
"relations": {
"_id": &user.id
}
}
},
None
)
.await
.map_err(|_| Error::DatabaseError {
) {
Ok(_) => {
let target = target
.from_override(&user, RelationshipStatus::None)
.await?;
let user = user
.from_override(&target, RelationshipStatus::None)
.await?;
let target_id = target.id.clone();
try_join!(
ClientboundNotification::UserRelationship {
id: user.id.clone(),
user: target,
status: RelationshipStatus::None
}
.publish(user.id.clone()),
ClientboundNotification::UserRelationship {
id: target_id.clone(),
user: user,
status: RelationshipStatus::None
}
.publish(target_id)
)
.ok();
Ok(json!({ "status": "None" }))
}
Err(_) => Err(Error::DatabaseError {
operation: "update_one",
with: "user",
})?;
let target = target.from_override(&user, RelationshipStatus::BlockedOther).await?;
ClientboundNotification::UserRelationship {
id: user.id.clone(),
user: target,
status: RelationshipStatus::BlockedOther,
}
.publish(user.id.clone())
.await
.ok();
Ok(json!({ "status": "BlockedOther" }))
}),
}
RelationshipStatus::BlockedOther => {
match try_join!(
col.update_one(
doc! {
"_id": &user.id
},
doc! {
"$pull": {
"relations": {
"_id": &target.id
}
}
},
None
),
col.update_one(
doc! {
"_id": &target.id
},
doc! {
"$pull": {
"relations": {
"_id": &user.id
}
}
},
None
)
) {
Ok(_) => {
let target = target.from_override(&user, RelationshipStatus::None).await?;
let user = user.from_override(&target, RelationshipStatus::None).await?;
let target_id = target.id.clone();
try_join!(
ClientboundNotification::UserRelationship {
id: user.id.clone(),
user: target,
status: RelationshipStatus::None
}
.publish(user.id.clone()),
ClientboundNotification::UserRelationship {
id: target_id.clone(),
user: user,
status: RelationshipStatus::None
}
.publish(target_id)
)
.ok();
Ok(json!({ "status": "None" }))
}
Err(_) => Err(Error::DatabaseError {
operation: "update_one",
with: "user",
}),
}
}
_ => Err(Error::InternalError),
}
}
_ => Err(Error::InternalError),
},
_ => Err(Error::NoEffect),
}
}