mirror of
https://github.com/stoatchat/stoatchat.git
synced 2026-07-14 05:26:59 +00:00
Add more guild routes.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use bson::UtcDateTime;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct UserEmailVerification {
|
||||
pub verified: bool,
|
||||
pub target: Option<String>,
|
||||
@@ -10,13 +10,13 @@ pub struct UserEmailVerification {
|
||||
pub code: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct UserRelationship {
|
||||
pub id: String,
|
||||
pub status: u8,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct User {
|
||||
#[serde(rename = "_id")]
|
||||
pub id: String,
|
||||
|
||||
@@ -1,38 +1,24 @@
|
||||
use rocket::http::{ RawStr };
|
||||
use rocket::request::{ FromParam };
|
||||
use bson::{ bson, doc, from_bson };
|
||||
use bson::{bson, doc, from_bson};
|
||||
use rocket::http::RawStr;
|
||||
use rocket::request::FromParam;
|
||||
|
||||
use crate::database;
|
||||
|
||||
use database::channel::Channel;
|
||||
use database::message::Message;
|
||||
use database::guild::Guild;
|
||||
|
||||
impl<'r> FromParam<'r> for Channel {
|
||||
impl<'r> FromParam<'r> for Guild {
|
||||
type Error = &'r RawStr;
|
||||
|
||||
fn from_param(param: &'r RawStr) -> Result<Self, Self::Error> {
|
||||
let col = database::get_db().collection("channels");
|
||||
let result = col.find_one(doc! { "_id": param.to_string() }, None).unwrap();
|
||||
let col = database::get_db().collection("guilds");
|
||||
let result = col
|
||||
.find_one(doc! { "_id": param.to_string() }, None)
|
||||
.unwrap();
|
||||
|
||||
if let Some(channel) = result {
|
||||
Ok(from_bson(bson::Bson::Document(channel)).expect("Failed to unwrap channel."))
|
||||
} else {
|
||||
Err(param)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'r> FromParam<'r> for Message {
|
||||
type Error = &'r RawStr;
|
||||
|
||||
fn from_param(param: &'r RawStr) -> Result<Self, Self::Error> {
|
||||
let col = database::get_db().collection("messages");
|
||||
let result = col.find_one(doc! { "_id": param.to_string() }, None).unwrap();
|
||||
|
||||
if let Some(message) = result {
|
||||
Ok(from_bson(bson::Bson::Document(message)).expect("Failed to unwrap message."))
|
||||
} else {
|
||||
Err(param)
|
||||
}
|
||||
if let Some(guild) = result {
|
||||
Ok(from_bson(bson::Bson::Document(guild)).expect("Failed to unwrap guild."))
|
||||
} else {
|
||||
Err(param)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
pub mod auth;
|
||||
pub mod channel;
|
||||
pub mod guild;
|
||||
|
||||
@@ -1,12 +1,89 @@
|
||||
use crate::database::{self, user::User};
|
||||
use crate::database::{self, channel::Channel, guild::Guild, user::User};
|
||||
|
||||
use bson::{bson, doc};
|
||||
use bson::{bson, doc, from_bson, Bson};
|
||||
use rocket_contrib::json::{Json, JsonValue};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use ulid::Ulid;
|
||||
|
||||
use super::channel::ChannelType;
|
||||
|
||||
/// fetch your guilds
|
||||
#[get("/@me")]
|
||||
pub fn my_guilds(user: User) -> JsonValue {
|
||||
let col = database::get_collection("guilds");
|
||||
let guilds = col
|
||||
.find(
|
||||
doc! {
|
||||
"members": {
|
||||
"$elemMatch": {
|
||||
"id": user.id,
|
||||
}
|
||||
}
|
||||
},
|
||||
None,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let mut parsed = vec![];
|
||||
for item in guilds {
|
||||
let doc = item.unwrap();
|
||||
parsed.push(json!({
|
||||
"id": doc.get_str("_id").unwrap(),
|
||||
"name": doc.get_str("name").unwrap(),
|
||||
"description": doc.get_str("description").unwrap(),
|
||||
"owner": doc.get_str("owner").unwrap(),
|
||||
}));
|
||||
}
|
||||
|
||||
json!(parsed)
|
||||
}
|
||||
|
||||
/// fetch a guild
|
||||
#[get("/<target>")]
|
||||
pub fn guild(user: User, target: Guild) -> JsonValue {
|
||||
let mut targets = vec![];
|
||||
for channel in target.channels {
|
||||
targets.push(Bson::String(channel));
|
||||
}
|
||||
|
||||
let col = database::get_collection("channels");
|
||||
match col.find(
|
||||
doc! {
|
||||
"_id": {
|
||||
"$in": targets,
|
||||
}
|
||||
},
|
||||
None,
|
||||
) {
|
||||
Ok(results) => {
|
||||
let mut channels = vec![];
|
||||
for item in results {
|
||||
let channel: Channel = from_bson(bson::Bson::Document(item.unwrap()))
|
||||
.expect("Failed to unwrap channel.");
|
||||
|
||||
channels.push(json!({
|
||||
"_id": channel.id,
|
||||
"last_message": channel.last_message,
|
||||
"name": channel.name,
|
||||
"description": channel.description,
|
||||
}));
|
||||
}
|
||||
|
||||
json!({
|
||||
"id": target.id,
|
||||
"name": target.name,
|
||||
"description": target.description,
|
||||
"owner": target.owner,
|
||||
"channels": channels,
|
||||
})
|
||||
}
|
||||
Err(_) => json!({
|
||||
"success": false,
|
||||
"error": "Failed to fetch channels."
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct CreateGuild {
|
||||
name: String,
|
||||
@@ -47,7 +124,7 @@ pub fn create_guild(user: User, info: Json<CreateGuild>) -> JsonValue {
|
||||
if let Err(_) = channels.insert_one(
|
||||
doc! {
|
||||
"_id": channel_id.clone(),
|
||||
"channel_type": ChannelType::GUILDCHANNEL as u32,
|
||||
"type": ChannelType::GUILDCHANNEL as u32,
|
||||
"name": "general",
|
||||
},
|
||||
None,
|
||||
@@ -71,7 +148,9 @@ pub fn create_guild(user: User, info: Json<CreateGuild>) -> JsonValue {
|
||||
channel_id.clone()
|
||||
],
|
||||
"members": [
|
||||
user.id
|
||||
{
|
||||
"id": user.id,
|
||||
}
|
||||
],
|
||||
"invites": [],
|
||||
},
|
||||
|
||||
@@ -45,5 +45,8 @@ pub fn mount(rocket: Rocket) -> Rocket {
|
||||
channel::delete_message
|
||||
],
|
||||
)
|
||||
.mount("/api/guild", routes![guild::create_guild])
|
||||
.mount(
|
||||
"/api/guild",
|
||||
routes![guild::my_guilds, guild::guild, guild::create_guild],
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user