Run cargo fmt
This commit is contained in:
@@ -1,7 +1,10 @@
|
|||||||
use crate::{database::*, notifications::{events::ClientboundNotification, hive}};
|
|
||||||
use crate::util::result::{Error, Result};
|
use crate::util::result::{Error, Result};
|
||||||
use serde::{Deserialize, Serialize};
|
use crate::{
|
||||||
|
database::*,
|
||||||
|
notifications::{events::ClientboundNotification, hive},
|
||||||
|
};
|
||||||
use mongodb::bson::to_document;
|
use mongodb::bson::to_document;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
@@ -52,15 +55,14 @@ impl Channel {
|
|||||||
operation: "insert_one",
|
operation: "insert_one",
|
||||||
with: "channel",
|
with: "channel",
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
// ! IMPORTANT FIXME: THESE SUBSCRIPTIONS SHOULD BE DONE FROM HIVE NOT HERE!!!
|
// ! IMPORTANT FIXME: THESE SUBSCRIPTIONS SHOULD BE DONE FROM HIVE NOT HERE!!!
|
||||||
let channel_id = self.id().to_string();
|
let channel_id = self.id().to_string();
|
||||||
match &self {
|
match &self {
|
||||||
Channel::SavedMessages { user, .. } => {
|
Channel::SavedMessages { user, .. } => {
|
||||||
hive::subscribe_if_exists(user.clone(), channel_id.clone()).ok();
|
hive::subscribe_if_exists(user.clone(), channel_id.clone()).ok();
|
||||||
}
|
}
|
||||||
Channel::DirectMessage { recipients, .. } |
|
Channel::DirectMessage { recipients, .. } | Channel::Group { recipients, .. } => {
|
||||||
Channel::Group { recipients, .. } => {
|
|
||||||
for recipient in recipients {
|
for recipient in recipients {
|
||||||
hive::subscribe_if_exists(recipient.clone(), channel_id.clone()).ok();
|
hive::subscribe_if_exists(recipient.clone(), channel_id.clone()).ok();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,20 +19,21 @@ pub struct Data {
|
|||||||
// Maximum length of 36 allows both ULIDs and UUIDs.
|
// Maximum length of 36 allows both ULIDs and UUIDs.
|
||||||
#[validate(length(min = 1, max = 36))]
|
#[validate(length(min = 1, max = 36))]
|
||||||
nonce: String,
|
nonce: String,
|
||||||
users: Vec<String>
|
users: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/create", data = "<info>")]
|
#[post("/create", data = "<info>")]
|
||||||
pub async fn req(user: User, info: Json<Data>) -> Result<JsonValue> {
|
pub async fn req(user: User, info: Json<Data>) -> Result<JsonValue> {
|
||||||
info
|
info.validate()
|
||||||
.validate()
|
|
||||||
.map_err(|error| Error::FailedValidation { error })?;
|
.map_err(|error| Error::FailedValidation { error })?;
|
||||||
|
|
||||||
let mut set: HashSet<String> = HashSet::from_iter(info.users.iter().cloned());
|
let mut set: HashSet<String> = HashSet::from_iter(info.users.iter().cloned());
|
||||||
set.insert(user.id.clone());
|
set.insert(user.id.clone());
|
||||||
|
|
||||||
if set.len() > *MAX_GROUP_SIZE {
|
if set.len() > *MAX_GROUP_SIZE {
|
||||||
Err(Error::GroupTooLarge { max: *MAX_GROUP_SIZE })?
|
Err(Error::GroupTooLarge {
|
||||||
|
max: *MAX_GROUP_SIZE,
|
||||||
|
})?
|
||||||
}
|
}
|
||||||
|
|
||||||
if get_collection("channels")
|
if get_collection("channels")
|
||||||
@@ -63,9 +64,12 @@ pub async fn req(user: User, info: Json<Data>) -> Result<JsonValue> {
|
|||||||
id,
|
id,
|
||||||
nonce: Some(info.nonce.clone()),
|
nonce: Some(info.nonce.clone()),
|
||||||
name: info.name.clone(),
|
name: info.name.clone(),
|
||||||
description: info.description.clone().unwrap_or_else(|| "A group.".to_string()),
|
description: info
|
||||||
|
.description
|
||||||
|
.clone()
|
||||||
|
.unwrap_or_else(|| "A group.".to_string()),
|
||||||
owner: user.id,
|
owner: user.id,
|
||||||
recipients: set.into_iter().collect::<Vec<String>>()
|
recipients: set.into_iter().collect::<Vec<String>>(),
|
||||||
};
|
};
|
||||||
|
|
||||||
channel.clone().publish().await?;
|
channel.clone().publish().await?;
|
||||||
|
|||||||
@@ -55,11 +55,8 @@ pub async fn req(user: User, target: Ref, message: Json<Data>) -> Result<JsonVal
|
|||||||
nonce: Some(message.nonce.clone()),
|
nonce: Some(message.nonce.clone()),
|
||||||
edited: None,
|
edited: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
msg
|
msg.clone().publish().await?;
|
||||||
.clone()
|
|
||||||
.publish()
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(json!(msg))
|
Ok(json!(msg))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,12 +2,12 @@ use rocket::Route;
|
|||||||
|
|
||||||
mod delete_channel;
|
mod delete_channel;
|
||||||
mod fetch_channel;
|
mod fetch_channel;
|
||||||
|
mod group_create;
|
||||||
mod message_delete;
|
mod message_delete;
|
||||||
mod message_edit;
|
mod message_edit;
|
||||||
mod message_fetch;
|
mod message_fetch;
|
||||||
mod message_query;
|
mod message_query;
|
||||||
mod message_send;
|
mod message_send;
|
||||||
mod group_create;
|
|
||||||
|
|
||||||
pub fn routes() -> Vec<Route> {
|
pub fn routes() -> Vec<Route> {
|
||||||
routes![
|
routes![
|
||||||
|
|||||||
@@ -37,10 +37,8 @@ pub enum Error {
|
|||||||
#[snafu(display("Cannot edit someone else's message."))]
|
#[snafu(display("Cannot edit someone else's message."))]
|
||||||
CannotEditMessage,
|
CannotEditMessage,
|
||||||
#[snafu(display("Group size is too large."))]
|
#[snafu(display("Group size is too large."))]
|
||||||
GroupTooLarge {
|
GroupTooLarge { max: usize },
|
||||||
max: usize
|
|
||||||
},
|
|
||||||
|
|
||||||
// ? General errors.
|
// ? General errors.
|
||||||
#[snafu(display("Failed to validate fields."))]
|
#[snafu(display("Failed to validate fields."))]
|
||||||
FailedValidation { error: ValidationErrors },
|
FailedValidation { error: ValidationErrors },
|
||||||
@@ -77,7 +75,7 @@ impl<'r> Responder<'r, 'static> for Error {
|
|||||||
|
|
||||||
Error::CannotEditMessage => Status::Forbidden,
|
Error::CannotEditMessage => Status::Forbidden,
|
||||||
Error::GroupTooLarge { .. } => Status::Forbidden,
|
Error::GroupTooLarge { .. } => Status::Forbidden,
|
||||||
|
|
||||||
Error::FailedValidation { .. } => Status::UnprocessableEntity,
|
Error::FailedValidation { .. } => Status::UnprocessableEntity,
|
||||||
Error::DatabaseError { .. } => Status::InternalServerError,
|
Error::DatabaseError { .. } => Status::InternalServerError,
|
||||||
Error::InternalError => Status::InternalServerError,
|
Error::InternalError => Status::InternalServerError,
|
||||||
|
|||||||
Reference in New Issue
Block a user