Skip to content
Snippets Groups Projects
Verified Commit 8e908ce1 authored by insert's avatar insert
Browse files

Add message edit records + work on guilds.

parent 3609b7cf
Branches
Tags
No related merge requests found
......@@ -11,5 +11,11 @@ pub struct Channel {
// for Direct Messages
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>,
}
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>,
}
use serde::{ Deserialize, Serialize };
use bson::{ UtcDateTime };
#[derive(Serialize, Deserialize, Debug)]
pub struct PreviousEntry {
pub content: String,
pub time: UtcDateTime,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Message {
#[serde(rename = "_id")]
pub id: String,
// pub nonce: String, used internally
pub channel: String,
pub author: 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 channel;
pub mod message;
pub mod guild;
......@@ -24,25 +24,19 @@ fn public_uri() -> String {
pub fn send_verification_email(email: String, code: String) -> bool {
let url = format!("{}/api/account/verify/{}", public_uri(), code);
match send_email(
send_email(
email,
"Verify your email!".to_string(),
format!("Verify your email here: {}", url).to_string(),
format!("<a href=\"{}\">Click to verify your email!</a>", url).to_string()
) {
Ok(_) => true,
Err(_) => false,
}
format!("Verify your email here: {}", url),
format!("<a href=\"{}\">Click to verify your email!</a>", url)
).is_ok()
}
pub fn send_welcome_email(email: String, username: String) -> bool {
match send_email(
send_email(
email,
"Welcome to REVOLT!".to_string(),
format!("Welcome, {}! You can now use REVOLT.", username.clone()).to_string(),
format!("<b>Welcome, {}!</b><br/>You can now use REVOLT.<br/><a href=\"{}\">Go to REVOLT</a>", username.clone(), public_uri()).to_string()
) {
Ok(_) => true,
Err(_) => false,
}
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())
).is_ok()
}
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)]
pub struct SendMessage {
content: String,
nonce: String,
}
/// send a message to a channel
......@@ -143,51 +144,63 @@ pub fn send_message(user: User, target: Channel, message: Json<SendMessage>) ->
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");
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();
Some(match col.insert_one(
Some(if col.insert_one(
doc! {
"_id": id.clone(),
"nonce": nonce.clone(),
"channel": target.id.clone(),
"author": user.id.clone(),
"content": message.content.clone(),
"content": content.clone(),
},
None
) {
Ok(_) => {
if target.channel_type == ChannelType::DM as u8 {
let col = database::get_collection("channels");
col.update_one(
doc! { "_id": target.id.clone() },
doc! { "$set": { "active": true } },
None
).unwrap();
}
websocket::queue_message(
get_recipients(&target),
json!({
"type": "message",
"data": {
"id": id.clone(),
"channel": target.id,
"author": user.id,
"content": message.content.clone(),
},
}).to_string()
);
json!({
"success": true,
"id": id
})
},
Err(_) =>
json!({
"success": false,
"error": "Failed database query."
})
})
).is_ok() {
if target.channel_type == ChannelType::DM as u8 {
let col = database::get_collection("channels");
col.update_one(
doc! { "_id": target.id.clone() },
doc! { "$set": { "active": true } },
None
).unwrap();
}
websocket::queue_message(
get_recipients(&target),
json!({
"type": "message",
"data": {
"id": id.clone(),
"nonce": nonce,
"channel": target.id,
"author": user.id,
"content": content,
},
}).to_string()
);
json!({
"success": true,
"id": id
})
} else {
json!({
"success": false,
"error": "Failed database query."
})
})
}
/// get a message
......@@ -195,14 +208,31 @@ pub fn send_message(user: User, target: Channel, message: Json<SendMessage>) ->
pub fn get_message(user: User, target: Channel, message: Message) -> Option<JsonValue> {
if !has_permission(&user, &target) {
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(
json!({
"id": message.id,
"author": message.author,
"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
#[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) {
return None
}
......@@ -228,6 +258,13 @@ pub fn edit_message(user: User, target: Channel, message: Message, edit: Json<Se
} else {
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();
match col.update_one(
doc! { "_id": message.id.clone() },
......@@ -235,7 +272,13 @@ pub fn edit_message(user: User, target: Channel, message: Message, edit: Json<Se
"$set": {
"content": edit.content.clone(),
"edited": UtcDatetime(edited.clone())
}
},
"$push": {
"previous_content": {
"content": message.content,
"time": time,
}
},
},
None
) {
......
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 user;
pub mod channel;
pub mod guild;
pub fn mount(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/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/guild", routes![ guild::create_guild ])
}
use rocket_contrib::json::{ JsonValue };
use bson::{ bson, doc };
use bson::{ doc };
/// root
#[get("/")]
......
......@@ -124,6 +124,7 @@ pub fn dm(user: User, target: User) -> JsonValue {
).expect("Failed insert query.");
json!({
"success": true,
"id": id.to_string()
})
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment