forked from jmug/stoatchat
feat: implement channel invites, perms, group create, members fetch
chore: when you accidentally run fmt
This commit is contained in:
@@ -1,8 +1,15 @@
|
||||
use revolt_quark::Result;
|
||||
use std::{collections::HashSet, iter::FromIterator};
|
||||
|
||||
use revolt_quark::{
|
||||
get_relationship,
|
||||
models::{user::RelationshipStatus, Channel, User},
|
||||
Db, Error, Result,
|
||||
};
|
||||
|
||||
use mongodb::bson::doc;
|
||||
use rocket::serde::json::{Json, Value};
|
||||
use rocket::serde::json::Json;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use ulid::Ulid;
|
||||
use validator::Validate;
|
||||
|
||||
#[derive(Validate, Serialize, Deserialize)]
|
||||
@@ -13,10 +20,48 @@ pub struct Data {
|
||||
description: Option<String>,
|
||||
users: Vec<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
nsfw: Option<bool>
|
||||
nsfw: Option<bool>,
|
||||
}
|
||||
|
||||
#[post("/create", data = "<info>")]
|
||||
pub async fn req(/*_idempotency: IdempotencyKey, user: User,*/ info: Json<Data>) -> Result<Value> {
|
||||
todo!();
|
||||
pub async fn req(db: &Db, user: User, info: Json<Data>) -> Result<Json<Channel>> {
|
||||
let info = info.into_inner();
|
||||
info.validate()
|
||||
.map_err(|error| Error::FailedValidation { error })?;
|
||||
|
||||
let mut set: HashSet<String> = HashSet::from_iter(info.users.into_iter());
|
||||
set.insert(user.id.clone());
|
||||
|
||||
// ! FIXME: un hard code this
|
||||
if set.len() > 50 {
|
||||
return Err(Error::GroupTooLarge { max: 50 });
|
||||
}
|
||||
|
||||
for target in &set {
|
||||
match get_relationship(&user, target) {
|
||||
RelationshipStatus::Friend | RelationshipStatus::User => {}
|
||||
_ => {
|
||||
return Err(Error::NotFriends);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let group = Channel::Group {
|
||||
id: Ulid::new().to_string(),
|
||||
|
||||
name: info.name,
|
||||
owner: user.id,
|
||||
description: info.description,
|
||||
recipients: set.into_iter().collect::<Vec<String>>(),
|
||||
|
||||
icon: None,
|
||||
last_message_id: None,
|
||||
|
||||
permissions: None,
|
||||
|
||||
nsfw: info.nsfw.unwrap_or(false),
|
||||
};
|
||||
|
||||
db.insert_channel(&group).await?;
|
||||
Ok(Json(group))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user