Add message edit records + work on guilds.
This commit is contained in:
@@ -12,4 +12,10 @@ pub struct Channel {
|
|||||||
// for Direct Messages
|
// for Direct Messages
|
||||||
pub recipients: Option<Vec<String>>,
|
pub recipients: Option<Vec<String>>,
|
||||||
pub active: Option<bool>,
|
pub active: Option<bool>,
|
||||||
|
|
||||||
|
// for Guilds
|
||||||
|
pub name: Option<String>,
|
||||||
|
|
||||||
|
// for Guilds and Group DMs
|
||||||
|
pub description: Option<String>,
|
||||||
}
|
}
|
||||||
|
|||||||
27
src/database/guild.rs
Normal file
27
src/database/guild.rs
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
use serde::{ Deserialize, Serialize };
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
pub struct Member {
|
||||||
|
pub id: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
pub struct Invite {
|
||||||
|
pub id: String,
|
||||||
|
pub custom: bool,
|
||||||
|
pub channel: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
pub struct Guild {
|
||||||
|
#[serde(rename = "_id")]
|
||||||
|
pub id: String,
|
||||||
|
// pub nonce: String, used internally
|
||||||
|
pub name: String,
|
||||||
|
pub description: String,
|
||||||
|
pub owner: String,
|
||||||
|
|
||||||
|
pub channels: Vec<String>,
|
||||||
|
pub members: Vec<Member>,
|
||||||
|
pub invites: Vec<Invite>,
|
||||||
|
}
|
||||||
@@ -1,13 +1,22 @@
|
|||||||
use serde::{ Deserialize, Serialize };
|
use serde::{ Deserialize, Serialize };
|
||||||
use bson::{ UtcDateTime };
|
use bson::{ UtcDateTime };
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
pub struct PreviousEntry {
|
||||||
|
pub content: String,
|
||||||
|
pub time: UtcDateTime,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
pub struct Message {
|
pub struct Message {
|
||||||
#[serde(rename = "_id")]
|
#[serde(rename = "_id")]
|
||||||
pub id: String,
|
pub id: String,
|
||||||
|
// pub nonce: String, used internally
|
||||||
pub channel: String,
|
pub channel: String,
|
||||||
pub author: String,
|
pub author: String,
|
||||||
|
|
||||||
pub content: String,
|
pub content: String,
|
||||||
pub edited: Option<UtcDateTime>,
|
pub edited: Option<UtcDateTime>,
|
||||||
|
|
||||||
|
pub previous_content: Option<Vec<PreviousEntry>>
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,3 +27,4 @@ pub fn get_collection(collection: &str) -> Collection {
|
|||||||
pub mod user;
|
pub mod user;
|
||||||
pub mod channel;
|
pub mod channel;
|
||||||
pub mod message;
|
pub mod message;
|
||||||
|
pub mod guild;
|
||||||
|
|||||||
22
src/email.rs
22
src/email.rs
@@ -24,25 +24,19 @@ fn public_uri() -> String {
|
|||||||
|
|
||||||
pub fn send_verification_email(email: String, code: String) -> bool {
|
pub fn send_verification_email(email: String, code: String) -> bool {
|
||||||
let url = format!("{}/api/account/verify/{}", public_uri(), code);
|
let url = format!("{}/api/account/verify/{}", public_uri(), code);
|
||||||
match send_email(
|
send_email(
|
||||||
email,
|
email,
|
||||||
"Verify your email!".to_string(),
|
"Verify your email!".to_string(),
|
||||||
format!("Verify your email here: {}", url).to_string(),
|
format!("Verify your email here: {}", url),
|
||||||
format!("<a href=\"{}\">Click to verify your email!</a>", url).to_string()
|
format!("<a href=\"{}\">Click to verify your email!</a>", url)
|
||||||
) {
|
).is_ok()
|
||||||
Ok(_) => true,
|
|
||||||
Err(_) => false,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn send_welcome_email(email: String, username: String) -> bool {
|
pub fn send_welcome_email(email: String, username: String) -> bool {
|
||||||
match send_email(
|
send_email(
|
||||||
email,
|
email,
|
||||||
"Welcome to REVOLT!".to_string(),
|
"Welcome to REVOLT!".to_string(),
|
||||||
format!("Welcome, {}! You can now use REVOLT.", username.clone()).to_string(),
|
format!("Welcome, {}! You can now use REVOLT.", username.clone()),
|
||||||
format!("<b>Welcome, {}!</b><br/>You can now use REVOLT.<br/><a href=\"{}\">Go to REVOLT</a>", username.clone(), public_uri()).to_string()
|
format!("<b>Welcome, {}!</b><br/>You can now use REVOLT.<br/><a href=\"{}\">Go to REVOLT</a>", username.clone(), public_uri())
|
||||||
) {
|
).is_ok()
|
||||||
Ok(_) => true,
|
|
||||||
Err(_) => false,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
38
src/guards/guild.rs
Normal file
38
src/guards/guild.rs
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
use rocket::http::{ RawStr };
|
||||||
|
use rocket::request::{ FromParam };
|
||||||
|
use bson::{ bson, doc, from_bson };
|
||||||
|
|
||||||
|
use crate::database;
|
||||||
|
|
||||||
|
use database::channel::Channel;
|
||||||
|
use database::message::Message;
|
||||||
|
|
||||||
|
impl<'r> FromParam<'r> for Channel {
|
||||||
|
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();
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -134,6 +134,7 @@ pub fn messages(user: User, target: Channel) -> Option<JsonValue> {
|
|||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct SendMessage {
|
pub struct SendMessage {
|
||||||
content: String,
|
content: String,
|
||||||
|
nonce: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// send a message to a channel
|
/// send a message to a channel
|
||||||
@@ -143,18 +144,30 @@ pub fn send_message(user: User, target: Channel, message: Json<SendMessage>) ->
|
|||||||
return None
|
return None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let content: String = message.content.chars().take(2000).collect();
|
||||||
|
let nonce: String = message.nonce.chars().take(32).collect();
|
||||||
|
|
||||||
let col = database::get_collection("messages");
|
let col = database::get_collection("messages");
|
||||||
|
if let Some(_) = col.find_one(doc! { "nonce": nonce.clone() }, None).unwrap() {
|
||||||
|
return Some(
|
||||||
|
json!({
|
||||||
|
"success": false,
|
||||||
|
"error": "Message already sent!"
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
let id = Ulid::new().to_string();
|
let id = Ulid::new().to_string();
|
||||||
Some(match col.insert_one(
|
Some(if col.insert_one(
|
||||||
doc! {
|
doc! {
|
||||||
"_id": id.clone(),
|
"_id": id.clone(),
|
||||||
|
"nonce": nonce.clone(),
|
||||||
"channel": target.id.clone(),
|
"channel": target.id.clone(),
|
||||||
"author": user.id.clone(),
|
"author": user.id.clone(),
|
||||||
"content": message.content.clone(),
|
"content": content.clone(),
|
||||||
},
|
},
|
||||||
None
|
None
|
||||||
) {
|
).is_ok() {
|
||||||
Ok(_) => {
|
|
||||||
if target.channel_type == ChannelType::DM as u8 {
|
if target.channel_type == ChannelType::DM as u8 {
|
||||||
let col = database::get_collection("channels");
|
let col = database::get_collection("channels");
|
||||||
col.update_one(
|
col.update_one(
|
||||||
@@ -170,9 +183,10 @@ pub fn send_message(user: User, target: Channel, message: Json<SendMessage>) ->
|
|||||||
"type": "message",
|
"type": "message",
|
||||||
"data": {
|
"data": {
|
||||||
"id": id.clone(),
|
"id": id.clone(),
|
||||||
|
"nonce": nonce,
|
||||||
"channel": target.id,
|
"channel": target.id,
|
||||||
"author": user.id,
|
"author": user.id,
|
||||||
"content": message.content.clone(),
|
"content": content,
|
||||||
},
|
},
|
||||||
}).to_string()
|
}).to_string()
|
||||||
);
|
);
|
||||||
@@ -181,8 +195,7 @@ pub fn send_message(user: User, target: Channel, message: Json<SendMessage>) ->
|
|||||||
"success": true,
|
"success": true,
|
||||||
"id": id
|
"id": id
|
||||||
})
|
})
|
||||||
},
|
} else {
|
||||||
Err(_) =>
|
|
||||||
json!({
|
json!({
|
||||||
"success": false,
|
"success": false,
|
||||||
"error": "Failed database query."
|
"error": "Failed database query."
|
||||||
@@ -197,12 +210,29 @@ pub fn get_message(user: User, target: Channel, message: Message) -> Option<Json
|
|||||||
return None
|
return None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let prev =
|
||||||
|
// ! CHECK IF USER HAS PERMISSION TO VIEW EDITS OF MESSAGES
|
||||||
|
if let Some(previous) = message.previous_content {
|
||||||
|
let mut entries = vec![];
|
||||||
|
for entry in previous {
|
||||||
|
entries.push(json!({
|
||||||
|
"content": entry.content,
|
||||||
|
"time": entry.time.timestamp(),
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
Some(entries)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
Some(
|
Some(
|
||||||
json!({
|
json!({
|
||||||
"id": message.id,
|
"id": message.id,
|
||||||
"author": message.author,
|
"author": message.author,
|
||||||
"content": message.content,
|
"content": message.content,
|
||||||
"edited": if let Some(t) = message.edited { Some(t.timestamp()) } else { None }
|
"edited": if let Some(t) = message.edited { Some(t.timestamp()) } else { None },
|
||||||
|
"previous_content": prev,
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -214,7 +244,7 @@ pub struct EditMessage {
|
|||||||
|
|
||||||
/// edit a message
|
/// edit a message
|
||||||
#[patch("/<target>/messages/<message>", data = "<edit>")]
|
#[patch("/<target>/messages/<message>", data = "<edit>")]
|
||||||
pub fn edit_message(user: User, target: Channel, message: Message, edit: Json<SendMessage>) -> Option<JsonValue> {
|
pub fn edit_message(user: User, target: Channel, message: Message, edit: Json<EditMessage>) -> Option<JsonValue> {
|
||||||
if !has_permission(&user, &target) {
|
if !has_permission(&user, &target) {
|
||||||
return None
|
return None
|
||||||
}
|
}
|
||||||
@@ -228,6 +258,13 @@ pub fn edit_message(user: User, target: Channel, message: Message, edit: Json<Se
|
|||||||
} else {
|
} else {
|
||||||
let col = database::get_collection("messages");
|
let col = database::get_collection("messages");
|
||||||
|
|
||||||
|
let time =
|
||||||
|
if let Some(edited) = message.edited {
|
||||||
|
edited.0
|
||||||
|
} else {
|
||||||
|
Ulid::from_string(&message.id).unwrap().datetime()
|
||||||
|
};
|
||||||
|
|
||||||
let edited = Utc::now();
|
let edited = Utc::now();
|
||||||
match col.update_one(
|
match col.update_one(
|
||||||
doc! { "_id": message.id.clone() },
|
doc! { "_id": message.id.clone() },
|
||||||
@@ -235,7 +272,13 @@ pub fn edit_message(user: User, target: Channel, message: Message, edit: Json<Se
|
|||||||
"$set": {
|
"$set": {
|
||||||
"content": edit.content.clone(),
|
"content": edit.content.clone(),
|
||||||
"edited": UtcDatetime(edited.clone())
|
"edited": UtcDatetime(edited.clone())
|
||||||
|
},
|
||||||
|
"$push": {
|
||||||
|
"previous_content": {
|
||||||
|
"content": message.content,
|
||||||
|
"time": time,
|
||||||
}
|
}
|
||||||
|
},
|
||||||
},
|
},
|
||||||
None
|
None
|
||||||
) {
|
) {
|
||||||
|
|||||||
84
src/routes/guild.rs
Normal file
84
src/routes/guild.rs
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
use crate::database::{ self, user::User };
|
||||||
|
|
||||||
|
use bson::{ bson, doc };
|
||||||
|
use rocket_contrib::json::{ JsonValue, Json };
|
||||||
|
use serde::{ Serialize, Deserialize };
|
||||||
|
use ulid::Ulid;
|
||||||
|
|
||||||
|
use super::channel::ChannelType;
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub struct CreateGuild {
|
||||||
|
name: String,
|
||||||
|
description: Option<String>,
|
||||||
|
nonce: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// send a message to a channel
|
||||||
|
#[post("/create", data = "<info>")]
|
||||||
|
pub fn create_guild(user: User, info: Json<CreateGuild>) -> JsonValue {
|
||||||
|
if !user.email_verification.verified {
|
||||||
|
return json!({
|
||||||
|
"success": false,
|
||||||
|
"error": "Email not verified!",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let name: String = info.name.chars().take(32).collect();
|
||||||
|
let description: String = info.description.clone().unwrap_or("No description.".to_string()).chars().take(255).collect();
|
||||||
|
let nonce: String = info.nonce.chars().take(32).collect();
|
||||||
|
|
||||||
|
let channels = database::get_collection("channels");
|
||||||
|
let col = database::get_collection("guilds");
|
||||||
|
if let Some(_) = col.find_one(doc! { "nonce": nonce.clone() }, None).unwrap() {
|
||||||
|
return json!({
|
||||||
|
"success": false,
|
||||||
|
"error": "Guild already created!"
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
let channel_id = Ulid::new().to_string();
|
||||||
|
if let Err(_) = channels.insert_one(
|
||||||
|
doc! {
|
||||||
|
"_id": channel_id.clone(),
|
||||||
|
"channel_type": ChannelType::GUILDCHANNEL as u32,
|
||||||
|
"name": "general",
|
||||||
|
},
|
||||||
|
None) {
|
||||||
|
return json!({
|
||||||
|
"success": false,
|
||||||
|
"error": "Failed to create guild channel."
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
let id = Ulid::new().to_string();
|
||||||
|
if col.insert_one(
|
||||||
|
doc! {
|
||||||
|
"_id": id.clone(),
|
||||||
|
"nonce": nonce,
|
||||||
|
"name": name,
|
||||||
|
"description": description,
|
||||||
|
"owner": user.id.clone(),
|
||||||
|
"channels": [
|
||||||
|
channel_id.clone()
|
||||||
|
],
|
||||||
|
"members": [
|
||||||
|
user.id
|
||||||
|
],
|
||||||
|
"invites": [],
|
||||||
|
},
|
||||||
|
None
|
||||||
|
).is_ok() {
|
||||||
|
json!({
|
||||||
|
"success": true,
|
||||||
|
"id": id,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
channels.delete_one(doc! { "_id": channel_id }, None).expect("Failed to delete the channel we just made.");
|
||||||
|
|
||||||
|
json!({
|
||||||
|
"success": false,
|
||||||
|
"error": "Failed to create guild."
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ pub mod root;
|
|||||||
pub mod account;
|
pub mod account;
|
||||||
pub mod user;
|
pub mod user;
|
||||||
pub mod channel;
|
pub mod channel;
|
||||||
|
pub mod guild;
|
||||||
|
|
||||||
pub fn mount(rocket: Rocket) -> Rocket {
|
pub fn mount(rocket: Rocket) -> Rocket {
|
||||||
rocket
|
rocket
|
||||||
@@ -11,4 +12,5 @@ pub fn mount(rocket: Rocket) -> Rocket {
|
|||||||
.mount("/api/account", routes![ account::create, account::verify_email, account::resend_email, account::login, account::token ])
|
.mount("/api/account", routes![ account::create, account::verify_email, account::resend_email, account::login, account::token ])
|
||||||
.mount("/api/users", routes![ user::me, user::user, user::lookup, user::dms, user::dm, user::get_friends, user::get_friend, user::add_friend, user::remove_friend ])
|
.mount("/api/users", routes![ user::me, user::user, user::lookup, user::dms, user::dm, user::get_friends, user::get_friend, user::add_friend, user::remove_friend ])
|
||||||
.mount("/api/channels", routes![ channel::channel, channel::delete, channel::messages, channel::get_message, channel::send_message, channel::edit_message, channel::delete_message ])
|
.mount("/api/channels", routes![ channel::channel, channel::delete, channel::messages, channel::get_message, channel::send_message, channel::edit_message, channel::delete_message ])
|
||||||
|
.mount("/api/guild", routes![ guild::create_guild ])
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use rocket_contrib::json::{ JsonValue };
|
use rocket_contrib::json::{ JsonValue };
|
||||||
use bson::{ bson, doc };
|
use bson::{ doc };
|
||||||
|
|
||||||
/// root
|
/// root
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
|
|||||||
@@ -124,6 +124,7 @@ pub fn dm(user: User, target: User) -> JsonValue {
|
|||||||
).expect("Failed insert query.");
|
).expect("Failed insert query.");
|
||||||
|
|
||||||
json!({
|
json!({
|
||||||
|
"success": true,
|
||||||
"id": id.to_string()
|
"id": id.to_string()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user