mirror of
https://github.com/stoatchat/stoatchat.git
synced 2026-07-14 21:47:02 +00:00
Push working draft.
This commit is contained in:
@@ -6,7 +6,7 @@ pub struct Channel {
|
||||
pub id: String,
|
||||
#[serde(rename = "type")]
|
||||
pub channel_type: u8,
|
||||
|
||||
|
||||
pub last_message: Option<String>,
|
||||
|
||||
// for Direct Messages
|
||||
@@ -15,6 +15,7 @@ pub struct Channel {
|
||||
|
||||
// for Guilds
|
||||
pub name: Option<String>,
|
||||
pub guild: Option<String>,
|
||||
|
||||
// for Guilds and Group DMs
|
||||
pub description: Option<String>,
|
||||
|
||||
@@ -1,4 +1,56 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use bson::{bson, doc};
|
||||
|
||||
use super::get_collection;
|
||||
use mongodb::options::FindOneOptions;
|
||||
|
||||
bitfield! {
|
||||
pub struct MemberPermissions(MSB0 [u8]);
|
||||
u8;
|
||||
pub get_access, set_access: 7;
|
||||
pub get_create_invite, set_create_invite: 6;
|
||||
pub get_kick_members, set_kick_members: 5;
|
||||
pub get_ban_members, set_ban_members: 4;
|
||||
pub get_read_messages, set_read_messages: 3;
|
||||
pub get_send_messages, set_send_messages: 2;
|
||||
}
|
||||
|
||||
pub fn find_member_permissions<C: Into<Option<String>>>(id: String, guild: String, channel: C) -> u8 {
|
||||
let col = get_collection("guilds");
|
||||
|
||||
match col.find_one(
|
||||
doc! {
|
||||
"_id": &guild,
|
||||
"members": {
|
||||
"$elemMatch": {
|
||||
"id": &id,
|
||||
}
|
||||
}
|
||||
},
|
||||
FindOneOptions::builder()
|
||||
.projection(
|
||||
doc! {
|
||||
"members.$": 1,
|
||||
"owner": 1,
|
||||
"default_permissions": 1,
|
||||
}
|
||||
)
|
||||
.build()
|
||||
) {
|
||||
Ok(result) => {
|
||||
if let Some(doc) = result {
|
||||
if doc.get_str("owner").unwrap() == id {
|
||||
return u8::MAX;
|
||||
}
|
||||
|
||||
doc.get_i32("default_permissions").unwrap() as u8
|
||||
} else {
|
||||
0
|
||||
}
|
||||
},
|
||||
Err(_) => 0
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct Member {
|
||||
@@ -24,4 +76,6 @@ pub struct Guild {
|
||||
pub channels: Vec<String>,
|
||||
pub members: Vec<Member>,
|
||||
pub invites: Vec<Invite>,
|
||||
|
||||
pub default_permissions: u32,
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
extern crate rocket;
|
||||
#[macro_use]
|
||||
extern crate rocket_contrib;
|
||||
#[macro_use]
|
||||
extern crate bitfield;
|
||||
|
||||
pub mod database;
|
||||
pub mod email;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::database::{self, channel::Channel, guild::Guild, user::User};
|
||||
use crate::database::{self, channel::Channel, guild::{ Guild, find_member_permissions }, user::User};
|
||||
|
||||
use bson::{bson, doc, from_bson, Bson};
|
||||
use rocket_contrib::json::{Json, JsonValue};
|
||||
@@ -40,7 +40,11 @@ pub fn my_guilds(user: User) -> JsonValue {
|
||||
|
||||
/// fetch a guild
|
||||
#[get("/<target>")]
|
||||
pub fn guild(user: User, target: Guild) -> JsonValue {
|
||||
pub fn guild(user: User, target: Guild) -> Option<JsonValue> {
|
||||
if find_member_permissions(user.id.clone(), target.id.clone(), None) == 0 {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut targets = vec![];
|
||||
for channel in target.channels {
|
||||
targets.push(Bson::String(channel));
|
||||
@@ -69,18 +73,18 @@ pub fn guild(user: User, target: Guild) -> JsonValue {
|
||||
}));
|
||||
}
|
||||
|
||||
json!({
|
||||
Some(json!({
|
||||
"id": target.id,
|
||||
"name": target.name,
|
||||
"description": target.description,
|
||||
"owner": target.owner,
|
||||
"channels": channels,
|
||||
})
|
||||
}))
|
||||
}
|
||||
Err(_) => json!({
|
||||
Err(_) => Some(json!({
|
||||
"success": false,
|
||||
"error": "Failed to fetch channels."
|
||||
}),
|
||||
})),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,7 +95,7 @@ pub struct CreateGuild {
|
||||
nonce: String,
|
||||
}
|
||||
|
||||
/// send a message to a channel
|
||||
/// create a new guild
|
||||
#[post("/create", data = "<info>")]
|
||||
pub fn create_guild(user: User, info: Json<CreateGuild>) -> JsonValue {
|
||||
if !user.email_verification.verified {
|
||||
@@ -120,12 +124,14 @@ pub fn create_guild(user: User, info: Json<CreateGuild>) -> JsonValue {
|
||||
});
|
||||
}
|
||||
|
||||
let id = Ulid::new().to_string();
|
||||
let channel_id = Ulid::new().to_string();
|
||||
if let Err(_) = channels.insert_one(
|
||||
doc! {
|
||||
"_id": channel_id.clone(),
|
||||
"type": ChannelType::GUILDCHANNEL as u32,
|
||||
"name": "general",
|
||||
"guild": id.clone(),
|
||||
},
|
||||
None,
|
||||
) {
|
||||
@@ -135,7 +141,6 @@ pub fn create_guild(user: User, info: Json<CreateGuild>) -> JsonValue {
|
||||
});
|
||||
}
|
||||
|
||||
let id = Ulid::new().to_string();
|
||||
if col
|
||||
.insert_one(
|
||||
doc! {
|
||||
@@ -153,6 +158,7 @@ pub fn create_guild(user: User, info: Json<CreateGuild>) -> JsonValue {
|
||||
}
|
||||
],
|
||||
"invites": [],
|
||||
"default_permissions": 51,
|
||||
},
|
||||
None,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user