forked from jmug/stoatchat
Run cargo fmt.
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
use mongodb::bson::{doc, from_document};
|
use mongodb::bson::{doc, from_document};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::util::result::{Error, Result};
|
|
||||||
use crate::database::*;
|
use crate::database::*;
|
||||||
|
use crate::util::result::{Error, Result};
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
@@ -38,7 +38,12 @@ pub struct File {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl 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 attachments = get_collection("attachments");
|
||||||
let key = format!("{}_id", parent_type);
|
let key = format!("{}_id", parent_type);
|
||||||
if let Some(doc) = attachments
|
if let Some(doc) = attachments
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
use crate::database::*;
|
use crate::database::*;
|
||||||
use crate::notifications::events::ClientboundNotification;
|
use crate::notifications::events::ClientboundNotification;
|
||||||
use crate::util::result::{Error, Result};
|
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 rocket_contrib::json::JsonValue;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use futures::StreamExt;
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
pub struct LastMessage {
|
pub struct LastMessage {
|
||||||
@@ -147,7 +150,7 @@ impl Channel {
|
|||||||
"deleted": true
|
"deleted": true
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
None
|
None,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map_err(|_| Error::DatabaseError {
|
.map_err(|_| Error::DatabaseError {
|
||||||
|
|||||||
@@ -271,7 +271,7 @@ impl Message {
|
|||||||
"deleted": true
|
"deleted": true
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
None
|
None,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map_err(|_| Error::DatabaseError {
|
.map_err(|_| Error::DatabaseError {
|
||||||
|
|||||||
@@ -116,10 +116,15 @@ impl User {
|
|||||||
|
|
||||||
/// Mutate the user object to appear as seen by user.
|
/// Mutate the user object to appear as seen by user.
|
||||||
/// Also overrides the relationship status.
|
/// 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)
|
let permissions = PermissionCalculator::new(&user)
|
||||||
.with_relationship(&relationship)
|
.with_relationship(&relationship)
|
||||||
.for_user(&self.id).await?;
|
.for_user(&self.id)
|
||||||
|
.await?;
|
||||||
|
|
||||||
self.relations = None;
|
self.relations = None;
|
||||||
self.relationship = Some(relationship);
|
self.relationship = Some(relationship);
|
||||||
|
|||||||
@@ -49,7 +49,12 @@ impl<'a> PermissionCalculator<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let mut permissions: u32 = 0;
|
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::Friend => return Ok(u32::MAX),
|
||||||
RelationshipStatus::Blocked | RelationshipStatus::BlockedOther => {
|
RelationshipStatus::Blocked | RelationshipStatus::BlockedOther => {
|
||||||
return Ok(UserPermission::Access as u32)
|
return Ok(UserPermission::Access as u32)
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ pub enum ClientboundNotification {
|
|||||||
UserRelationship {
|
UserRelationship {
|
||||||
id: String,
|
id: String,
|
||||||
user: User,
|
user: User,
|
||||||
status: RelationshipStatus
|
status: RelationshipStatus,
|
||||||
},
|
},
|
||||||
UserPresence {
|
UserPresence {
|
||||||
id: String,
|
id: String,
|
||||||
|
|||||||
@@ -1,10 +1,7 @@
|
|||||||
use crate::database::*;
|
use crate::database::*;
|
||||||
use crate::util::result::{Error, Result};
|
use crate::util::result::{Error, Result};
|
||||||
|
|
||||||
use mongodb::{
|
use mongodb::{bson::doc, options::FindOneOptions};
|
||||||
bson::{doc},
|
|
||||||
options::FindOneOptions,
|
|
||||||
};
|
|
||||||
use rocket_contrib::json::{Json, JsonValue};
|
use rocket_contrib::json::{Json, JsonValue};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use ulid::Ulid;
|
use ulid::Ulid;
|
||||||
|
|||||||
@@ -67,8 +67,12 @@ pub async fn req(user: User, username: String) -> Result<JsonValue> {
|
|||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
let target_user = target_user.from_override(&user, RelationshipStatus::Friend).await?;
|
let target_user = target_user
|
||||||
let user = user.from_override(&target_user, RelationshipStatus::Friend).await?;
|
.from_override(&user, RelationshipStatus::Friend)
|
||||||
|
.await?;
|
||||||
|
let user = user
|
||||||
|
.from_override(&target_user, RelationshipStatus::Friend)
|
||||||
|
.await?;
|
||||||
|
|
||||||
try_join!(
|
try_join!(
|
||||||
ClientboundNotification::UserRelationship {
|
ClientboundNotification::UserRelationship {
|
||||||
@@ -126,8 +130,12 @@ pub async fn req(user: User, username: String) -> Result<JsonValue> {
|
|||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
let target_user = target_user.from_override(&user, RelationshipStatus::Outgoing).await?;
|
let target_user = target_user
|
||||||
let user = user.from_override(&target_user, RelationshipStatus::Incoming).await?;
|
.from_override(&user, RelationshipStatus::Outgoing)
|
||||||
|
.await?;
|
||||||
|
let user = user
|
||||||
|
.from_override(&target_user, RelationshipStatus::Incoming)
|
||||||
|
.await?;
|
||||||
try_join!(
|
try_join!(
|
||||||
ClientboundNotification::UserRelationship {
|
ClientboundNotification::UserRelationship {
|
||||||
id: user.id.clone(),
|
id: user.id.clone(),
|
||||||
|
|||||||
@@ -76,8 +76,12 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
|||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
let target = target.from_override(&user, RelationshipStatus::Friend).await?;
|
let target = target
|
||||||
let user = user.from_override(&target, RelationshipStatus::Friend).await?;
|
.from_override(&user, RelationshipStatus::Friend)
|
||||||
|
.await?;
|
||||||
|
let user = user
|
||||||
|
.from_override(&target, RelationshipStatus::Friend)
|
||||||
|
.await?;
|
||||||
let target_id = target.id.clone();
|
let target_id = target.id.clone();
|
||||||
|
|
||||||
try_join!(
|
try_join!(
|
||||||
@@ -134,8 +138,12 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
|||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
let target = target.from_override(&user, RelationshipStatus::Blocked).await?;
|
let target = target
|
||||||
let user = user.from_override(&target, RelationshipStatus::BlockedOther).await?;
|
.from_override(&user, RelationshipStatus::Blocked)
|
||||||
|
.await?;
|
||||||
|
let user = user
|
||||||
|
.from_override(&target, RelationshipStatus::BlockedOther)
|
||||||
|
.await?;
|
||||||
let target_id = target.id.clone();
|
let target_id = target.id.clone();
|
||||||
|
|
||||||
try_join!(
|
try_join!(
|
||||||
|
|||||||
@@ -23,31 +23,39 @@ pub struct Data {
|
|||||||
#[patch("/<_ignore_id>", data = "<data>")]
|
#[patch("/<_ignore_id>", data = "<data>")]
|
||||||
pub async fn req(user: User, mut data: Json<Data>, _ignore_id: String) -> Result<()> {
|
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() {
|
if data.0.status.is_none() && data.0.profile.is_none() && data.0.avatar.is_none() {
|
||||||
return Ok(())
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
data.validate()
|
data.validate()
|
||||||
.map_err(|error| Error::FailedValidation { error })?;
|
.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 avatar = std::mem::replace(&mut data.0.avatar, None);
|
||||||
let attachment = if let Some(attachment_id) = avatar {
|
let attachment = if let Some(attachment_id) = avatar {
|
||||||
let attachment = File::find_and_use(&attachment_id, "avatars", "user", &user.id).await?;
|
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)
|
Some(attachment)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
get_collection("users")
|
get_collection("users")
|
||||||
.update_one(
|
.update_one(doc! { "_id": &user.id }, doc! { "$set": set }, None)
|
||||||
doc! { "_id": &user.id },
|
|
||||||
doc! { "$set": set },
|
|
||||||
None
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
.map_err(|_| Error::DatabaseError { operation: "update_one", with: "user" })?;
|
.map_err(|_| Error::DatabaseError {
|
||||||
|
operation: "update_one",
|
||||||
|
with: "user",
|
||||||
|
})?;
|
||||||
|
|
||||||
if let Some(status) = data.0.status {
|
if let Some(status) = data.0.status {
|
||||||
ClientboundNotification::UserUpdate {
|
ClientboundNotification::UserUpdate {
|
||||||
|
|||||||
@@ -45,8 +45,12 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
|||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
let target = target.from_override(&user, RelationshipStatus::None).await?;
|
let target = target
|
||||||
let user = user.from_override(&target, RelationshipStatus::None).await?;
|
.from_override(&user, RelationshipStatus::None)
|
||||||
|
.await?;
|
||||||
|
let user = user
|
||||||
|
.from_override(&target, RelationshipStatus::None)
|
||||||
|
.await?;
|
||||||
let target_id = target.id.clone();
|
let target_id = target.id.clone();
|
||||||
|
|
||||||
try_join!(
|
try_join!(
|
||||||
|
|||||||
@@ -12,8 +12,7 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
|||||||
let target = target.fetch_user().await?;
|
let target = target.fetch_user().await?;
|
||||||
|
|
||||||
match get_relationship(&user, &target.id) {
|
match get_relationship(&user, &target.id) {
|
||||||
RelationshipStatus::Blocked => {
|
RelationshipStatus::Blocked => match get_relationship(&target, &user.id) {
|
||||||
match get_relationship(&target, &user.id) {
|
|
||||||
RelationshipStatus::Blocked => {
|
RelationshipStatus::Blocked => {
|
||||||
col.update_one(
|
col.update_one(
|
||||||
doc! {
|
doc! {
|
||||||
@@ -33,7 +32,9 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
|||||||
with: "user",
|
with: "user",
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let target = target.from_override(&user, RelationshipStatus::BlockedOther).await?;
|
let target = target
|
||||||
|
.from_override(&user, RelationshipStatus::BlockedOther)
|
||||||
|
.await?;
|
||||||
ClientboundNotification::UserRelationship {
|
ClientboundNotification::UserRelationship {
|
||||||
id: user.id.clone(),
|
id: user.id.clone(),
|
||||||
user: target,
|
user: target,
|
||||||
@@ -75,8 +76,12 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
|||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
let target = target.from_override(&user, RelationshipStatus::None).await?;
|
let target = target
|
||||||
let user = user.from_override(&target, RelationshipStatus::None).await?;
|
.from_override(&user, RelationshipStatus::None)
|
||||||
|
.await?;
|
||||||
|
let user = user
|
||||||
|
.from_override(&target, RelationshipStatus::None)
|
||||||
|
.await?;
|
||||||
let target_id = target.id.clone();
|
let target_id = target.id.clone();
|
||||||
|
|
||||||
try_join!(
|
try_join!(
|
||||||
@@ -104,8 +109,7 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => Err(Error::InternalError),
|
_ => Err(Error::InternalError),
|
||||||
}
|
},
|
||||||
}
|
|
||||||
_ => Err(Error::NoEffect),
|
_ => Err(Error::NoEffect),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user