Servers: Add invites. Create, view and join.
This commit is contained in:
@@ -15,7 +15,8 @@ pub async fn req(user: User, target: Ref, member: Ref) -> Result<()> {
|
||||
.with_channel(&channel)
|
||||
.for_channel()
|
||||
.await?;
|
||||
if !perm.get_view() {
|
||||
|
||||
if !perm.get_invite_others() {
|
||||
Err(Error::MissingPermission)?
|
||||
}
|
||||
|
||||
|
||||
46
src/routes/channels/invite_channel.rs
Normal file
46
src/routes/channels/invite_channel.rs
Normal file
@@ -0,0 +1,46 @@
|
||||
use crate::database::*;
|
||||
use crate::util::result::{Error, Result};
|
||||
|
||||
use mongodb::bson::doc;
|
||||
use nanoid::nanoid;
|
||||
use rocket_contrib::json::JsonValue;
|
||||
|
||||
lazy_static! {
|
||||
static ref ALPHABET: [char; 54] = [
|
||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
|
||||
'J', 'K', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
|
||||
'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'
|
||||
];
|
||||
}
|
||||
|
||||
#[post("/<target>/invite")]
|
||||
pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
||||
let target = target.fetch_channel().await?;
|
||||
let perm = permissions::PermissionCalculator::new(&user)
|
||||
.with_channel(&target)
|
||||
.for_channel()
|
||||
.await?;
|
||||
|
||||
if !perm.get_invite_others() {
|
||||
return Err(Error::MissingPermission);
|
||||
}
|
||||
|
||||
let code = nanoid!(8, &*ALPHABET);
|
||||
match &target {
|
||||
Channel::Group { .. } => {
|
||||
unimplemented!()
|
||||
}
|
||||
Channel::TextChannel { id, .. } => {
|
||||
Invite::Server {
|
||||
code: code.clone(),
|
||||
creator: user.id,
|
||||
channel: id.clone(),
|
||||
}
|
||||
.save()
|
||||
.await?;
|
||||
|
||||
Ok(json!({ "code": code }))
|
||||
}
|
||||
_ => Err(Error::InvalidOperation),
|
||||
}
|
||||
}
|
||||
@@ -18,8 +18,9 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
||||
|
||||
let target = target.fetch_channel().await?;
|
||||
match target {
|
||||
Channel::SavedMessages { .. }
|
||||
| Channel::TextChannel { .. } => return Err(Error::CannotJoinCall),
|
||||
Channel::SavedMessages { .. } | Channel::TextChannel { .. } => {
|
||||
return Err(Error::CannotJoinCall)
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ mod fetch_members;
|
||||
mod group_add_member;
|
||||
mod group_create;
|
||||
mod group_remove_member;
|
||||
mod invite_channel;
|
||||
mod join_call;
|
||||
mod message_delete;
|
||||
mod message_edit;
|
||||
@@ -21,6 +22,7 @@ pub fn routes() -> Vec<Route> {
|
||||
fetch_members::req,
|
||||
delete_channel::req,
|
||||
edit_channel::req,
|
||||
invite_channel::req,
|
||||
message_send::req,
|
||||
message_query::req,
|
||||
message_query_stale::req,
|
||||
|
||||
56
src/routes/invites/invite_fetch.rs
Normal file
56
src/routes/invites/invite_fetch.rs
Normal file
@@ -0,0 +1,56 @@
|
||||
use crate::database::*;
|
||||
use crate::util::result::Result;
|
||||
|
||||
use rocket_contrib::json::JsonValue;
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Serialize, Debug, Clone)]
|
||||
#[serde(tag = "type")]
|
||||
pub enum InviteResponse {
|
||||
Server {
|
||||
server_name: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
server_icon: Option<File>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
server_banner: Option<File>,
|
||||
channel_name: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
channel_description: Option<String>,
|
||||
user_name: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
user_avatar: Option<File>
|
||||
}
|
||||
}
|
||||
|
||||
#[get("/<target>")]
|
||||
pub async fn req(target: Ref) -> Result<JsonValue> {
|
||||
let target = target.fetch_invite().await?;
|
||||
|
||||
match target {
|
||||
Invite::Server {
|
||||
channel,
|
||||
creator,
|
||||
..
|
||||
} => {
|
||||
let channel = Ref::from_unchecked(channel).fetch_channel().await?;
|
||||
let creator = Ref::from_unchecked(creator).fetch_user().await?;
|
||||
|
||||
if let Channel::TextChannel { server, name, description, .. } = channel {
|
||||
let server = Ref::from_unchecked(server).fetch_server().await?;
|
||||
|
||||
Ok(json!(InviteResponse::Server {
|
||||
server_name: server.name,
|
||||
server_icon: server.icon,
|
||||
server_banner: server.banner,
|
||||
channel_name: name,
|
||||
channel_description: description,
|
||||
user_name: creator.username,
|
||||
user_avatar: creator.avatar
|
||||
}))
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
31
src/routes/invites/invite_join.rs
Normal file
31
src/routes/invites/invite_join.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
use crate::database::*;
|
||||
use crate::util::result::Result;
|
||||
|
||||
use rocket_contrib::json::JsonValue;
|
||||
|
||||
#[post("/<target>")]
|
||||
pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
||||
let target = target.fetch_invite().await?;
|
||||
|
||||
match target {
|
||||
Invite::Server {
|
||||
channel,
|
||||
..
|
||||
} => {
|
||||
let channel = Ref::from_unchecked(channel).fetch_channel().await?;
|
||||
let server = if let Channel::TextChannel { server, .. } = &channel {
|
||||
Ref::from_unchecked(server.clone()).fetch_server().await?
|
||||
} else {
|
||||
unreachable!()
|
||||
};
|
||||
|
||||
server.join_member(&user.id).await?;
|
||||
|
||||
Ok(json!({
|
||||
"channel": channel,
|
||||
"server": server
|
||||
}))
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
8
src/routes/invites/mod.rs
Normal file
8
src/routes/invites/mod.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
use rocket::Route;
|
||||
|
||||
mod invite_fetch;
|
||||
mod invite_join;
|
||||
|
||||
pub fn routes() -> Vec<Route> {
|
||||
routes![invite_fetch::req, invite_join::req]
|
||||
}
|
||||
@@ -3,6 +3,7 @@ pub use rocket::response::Redirect;
|
||||
use rocket::Rocket;
|
||||
|
||||
mod channels;
|
||||
mod invites;
|
||||
mod onboard;
|
||||
mod push;
|
||||
mod root;
|
||||
@@ -17,6 +18,7 @@ pub fn mount(rocket: Rocket) -> Rocket {
|
||||
.mount("/users", users::routes())
|
||||
.mount("/channels", channels::routes())
|
||||
.mount("/servers", servers::routes())
|
||||
.mount("/invites", invites::routes())
|
||||
.mount("/push", push::routes())
|
||||
.mount("/sync", sync::routes())
|
||||
}
|
||||
|
||||
@@ -73,7 +73,7 @@ pub async fn req(user: User, target: Ref, info: Json<Data>) -> Result<JsonValue>
|
||||
"channels": id
|
||||
}
|
||||
},
|
||||
None
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError {
|
||||
|
||||
@@ -18,6 +18,7 @@ pub struct Data {
|
||||
|
||||
#[post("/create", data = "<info>")]
|
||||
pub async fn req(user: User, info: Json<Data>) -> Result<JsonValue> {
|
||||
let info = info.into_inner();
|
||||
info.validate()
|
||||
.map_err(|error| Error::FailedValidation { error })?;
|
||||
|
||||
@@ -41,26 +42,24 @@ pub async fn req(user: User, info: Json<Data>) -> Result<JsonValue> {
|
||||
let id = Ulid::new().to_string();
|
||||
let cid = Ulid::new().to_string();
|
||||
|
||||
get_collection("server_members")
|
||||
.insert_one(
|
||||
doc! {
|
||||
"_id": {
|
||||
"server": &id,
|
||||
"user": &user.id
|
||||
}
|
||||
},
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError {
|
||||
operation: "insert_one",
|
||||
with: "server_members",
|
||||
})?;
|
||||
let server = Server {
|
||||
id: id.clone(),
|
||||
nonce: Some(info.nonce.clone()),
|
||||
owner: user.id.clone(),
|
||||
|
||||
name: info.name,
|
||||
channels: vec![cid.clone()],
|
||||
|
||||
icon: None,
|
||||
banner: None,
|
||||
};
|
||||
|
||||
server.join_member(&user.id).await?;
|
||||
|
||||
Channel::TextChannel {
|
||||
id: cid.clone(),
|
||||
server: id.clone(),
|
||||
nonce: Some(info.nonce.clone()),
|
||||
id: cid,
|
||||
server: id,
|
||||
nonce: Some(info.nonce),
|
||||
name: "general".to_string(),
|
||||
description: None,
|
||||
icon: None,
|
||||
@@ -68,18 +67,6 @@ pub async fn req(user: User, info: Json<Data>) -> Result<JsonValue> {
|
||||
.publish()
|
||||
.await?;
|
||||
|
||||
let server = Server {
|
||||
id: id.clone(),
|
||||
nonce: Some(info.nonce.clone()),
|
||||
owner: user.id.clone(),
|
||||
|
||||
name: info.name.clone(),
|
||||
channels: vec![cid],
|
||||
|
||||
icon: None,
|
||||
banner: None,
|
||||
};
|
||||
|
||||
server.clone().publish().await?;
|
||||
|
||||
Ok(json!(server))
|
||||
|
||||
@@ -18,5 +18,9 @@ pub async fn req(user: User, target: Ref) -> Result<()> {
|
||||
// ! FIXME: either delete server if owner
|
||||
// ! OR leave server if member
|
||||
|
||||
// also need to delete server invites
|
||||
// and members
|
||||
// and bans
|
||||
|
||||
target.delete().await
|
||||
}
|
||||
|
||||
@@ -57,7 +57,9 @@ pub async fn req(
|
||||
|
||||
ClientboundNotification::UserUpdate {
|
||||
id: user.id.clone(),
|
||||
data: json!(data.0),
|
||||
data: json!({
|
||||
"username": data.username
|
||||
}),
|
||||
clear: None,
|
||||
}
|
||||
.publish(user.id.clone());
|
||||
|
||||
Reference in New Issue
Block a user