Re-write email backend, use SMTP directly.

This commit is contained in:
Paul Makles
2020-08-30 17:16:53 +01:00
parent ff0e539c7b
commit cbac802978
21 changed files with 354 additions and 303 deletions

View File

@@ -1,12 +1,12 @@
use super::get_collection;
use lru::LruCache;
use std::sync::{Arc, Mutex};
use mongodb::bson::{doc, from_bson, Bson};
use rocket::http::RawStr;
use rocket::request::FromParam;
use rocket_contrib::json::JsonValue;
use serde::{Deserialize, Serialize};
use std::sync::{Arc, Mutex};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct LastMessage {
@@ -50,26 +50,22 @@ impl Channel {
"last_message": self.last_message,
"recipients": self.recipients,
}),
1 => {
json!({
"id": self.id,
"type": self.channel_type,
"last_message": self.last_message,
"recipients": self.recipients,
"name": self.name,
"owner": self.owner,
"description": self.description,
})
}
2 => {
json!({
"id": self.id,
"type": self.channel_type,
"guild": self.guild,
"name": self.name,
"description": self.description,
})
}
1 => json!({
"id": self.id,
"type": self.channel_type,
"last_message": self.last_message,
"recipients": self.recipients,
"name": self.name,
"owner": self.owner,
"description": self.description,
}),
2 => json!({
"id": self.id,
"type": self.channel_type,
"guild": self.guild,
"name": self.name,
"description": self.description,
}),
_ => unreachable!(),
}
}

View File

@@ -1,5 +1,5 @@
use super::get_collection;
use super::channel::fetch_channels;
use super::get_collection;
use lru::LruCache;
use mongodb::bson::{doc, from_bson, Bson};
@@ -66,13 +66,17 @@ impl Guild {
}
pub fn seralise_with_channels(self) -> Result<JsonValue, String> {
let channels = self.fetch_channels()?
let channels = self
.fetch_channels()?
.into_iter()
.map(|x| x.serialise())
.collect();
let mut value = self.serialise();
value.as_object_mut().unwrap().insert("channels".to_string(), channels);
value
.as_object_mut()
.unwrap()
.insert("channels".to_string(), channels);
Ok(value)
}
}
@@ -167,10 +171,7 @@ pub fn fetch_guilds(ids: &Vec<String>) -> Result<Vec<Guild>, String> {
pub fn serialise_guilds_with_channels(ids: &Vec<String>) -> Result<Vec<JsonValue>, String> {
let guilds = fetch_guilds(&ids)?;
let cids: Vec<String> = guilds
.iter()
.flat_map(|x| x.channels.clone())
.collect();
let cids: Vec<String> = guilds.iter().flat_map(|x| x.channels.clone()).collect();
let channels = fetch_channels(&cids)?;
Ok(guilds
@@ -180,10 +181,11 @@ pub fn serialise_guilds_with_channels(ids: &Vec<String>) -> Result<Vec<JsonValue
let mut obj = x.serialise();
obj.as_object_mut().unwrap().insert(
"channels".to_string(),
channels.iter()
channels
.iter()
.filter(|x| x.guild.is_some() && x.guild.as_ref().unwrap() == &id)
.map(|x| x.clone().serialise())
.collect()
.collect(),
);
obj
})
@@ -315,19 +317,19 @@ pub fn process_event(event: &Notification) {
Notification::guild_user_join(ev) => {
let mut cache = MEMBER_CACHE.lock().unwrap();
cache.put(
MemberKey ( ev.id.clone(), ev.user.clone() ),
MemberKey(ev.id.clone(), ev.user.clone()),
Member {
id: MemberRef {
guild: ev.id.clone(),
user: ev.user.clone()
user: ev.user.clone(),
},
nickname: None
}
nickname: None,
},
);
}
Notification::guild_user_leave(ev) => {
let mut cache = MEMBER_CACHE.lock().unwrap();
cache.pop(&MemberKey ( ev.id.clone(), ev.user.clone() ));
cache.pop(&MemberKey(ev.id.clone(), ev.user.clone()));
}
_ => {}
}

View File

@@ -1,27 +1,33 @@
use super::super::get_db;
use super::scripts::LATEST_REVISION;
use mongodb::options::CreateCollectionOptions;
use mongodb::bson::doc;
use log::info;
use mongodb::bson::doc;
use mongodb::options::CreateCollectionOptions;
pub fn create_database() {
info!("Creating database.");
let db = get_db();
db.create_collection("users", None).expect("Failed to create users collection.");
db.create_collection("channels", None).expect("Failed to create channels collection.");
db.create_collection("guilds", None).expect("Failed to create guilds collection.");
db.create_collection("members", None).expect("Failed to create members collection.");
db.create_collection("messages", None).expect("Failed to create messages collection.");
db.create_collection("migrations", None).expect("Failed to create migrations collection.");
db.create_collection("users", None)
.expect("Failed to create users collection.");
db.create_collection("channels", None)
.expect("Failed to create channels collection.");
db.create_collection("guilds", None)
.expect("Failed to create guilds collection.");
db.create_collection("members", None)
.expect("Failed to create members collection.");
db.create_collection("messages", None)
.expect("Failed to create messages collection.");
db.create_collection("migrations", None)
.expect("Failed to create migrations collection.");
db.create_collection(
"pubsub",
CreateCollectionOptions::builder()
.capped(true)
.size(1_000_000)
.build()
.build(),
)
.expect("Failed to create pubsub collection.");
@@ -31,9 +37,9 @@ pub fn create_database() {
"_id": 0,
"revision": LATEST_REVISION
},
None
None,
)
.expect("Failed to save migration info.");
info!("Created database.");
}

View File

@@ -6,10 +6,9 @@ pub mod scripts;
pub fn run_migrations() {
let client = get_connection();
let list = client.list_database_names(
None,
None
).expect("Failed to fetch database names.");
let list = client
.list_database_names(None, None)
.expect("Failed to fetch database names.");
if list.iter().position(|x| x == "revolt").is_none() {
init::create_database();

View File

@@ -1,40 +1,43 @@
use super::super::get_collection;
use serde::{Serialize, Deserialize};
use mongodb::bson::{Bson, from_bson, doc};
use mongodb::options::FindOptions;
use log::info;
use mongodb::bson::{doc, from_bson, Bson};
use mongodb::options::FindOptions;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct MigrationInfo {
_id: i32,
revision: i32
revision: i32,
}
pub const LATEST_REVISION: i32 = 2;
pub fn migrate_database() {
let migrations = get_collection("migrations");
let data = migrations.find_one(None, None)
let data = migrations
.find_one(None, None)
.expect("Failed to fetch migration data.");
if let Some(doc) = data {
let info: MigrationInfo = from_bson(Bson::Document(doc))
.expect("Failed to read migration information.");
let info: MigrationInfo =
from_bson(Bson::Document(doc)).expect("Failed to read migration information.");
let revision = run_migrations(info.revision);
migrations.update_one(
doc! {
"_id": info._id
},
doc! {
"$set": {
"revision": revision
}
},
None
).expect("Failed to commit migration information.");
migrations
.update_one(
doc! {
"_id": info._id
},
doc! {
"$set": {
"revision": revision
}
},
None,
)
.expect("Failed to commit migration information.");
info!("Migration complete. Currently at revision {}.", revision);
} else {
@@ -53,32 +56,39 @@ pub fn run_migrations(revision: i32) -> i32 {
info!("Running migration [revision 1]: Add channels to guild object.");
let col = get_collection("guilds");
let guilds = col.find(
None,
FindOptions::builder()
.projection(doc! { "_id": 1 })
.build()
)
let guilds = col
.find(
None,
FindOptions::builder().projection(doc! { "_id": 1 }).build(),
)
.expect("Failed to fetch guilds.");
let result = get_collection("channels").find(
doc! {
"type": 2
},
FindOptions::builder()
.projection(doc! { "_id": 1, "guild": 1 })
.build()
).expect("Failed to fetch channels.");
let result = get_collection("channels")
.find(
doc! {
"type": 2
},
FindOptions::builder()
.projection(doc! { "_id": 1, "guild": 1 })
.build(),
)
.expect("Failed to fetch channels.");
let mut channels = vec![];
for doc in result {
let channel = doc.expect("Failed to fetch channel.");
let id = channel.get_str("_id").expect("Failed to get channel id.").to_string();
let gid = channel.get_str("guild").expect("Failed to get guild id.").to_string();
let id = channel
.get_str("_id")
.expect("Failed to get channel id.")
.to_string();
let gid = channel
.get_str("guild")
.expect("Failed to get guild id.")
.to_string();
channels.push(( id, gid ));
channels.push((id, gid));
}
for doc in guilds {
let guild = doc.expect("Failed to fetch guild.");
let id = guild.get_str("_id").expect("Failed to get guild id.");
@@ -88,7 +98,7 @@ pub fn run_migrations(revision: i32) -> i32 {
.filter(|x| x.1 == id)
.map(|x| x.0.clone())
.collect();
col.update_one(
doc! {
"_id": id
@@ -98,8 +108,9 @@ pub fn run_migrations(revision: i32) -> i32 {
"channels": list
}
},
None
).expect("Failed to update guild.");
None,
)
.expect("Failed to update guild.");
}
}

View File

@@ -118,12 +118,10 @@ pub fn has_mutual_connection(user_id: &str, target_id: &str, with_permission: bo
}
false
} else if result.count() > 0 {
true
} else {
if result.count() > 0 {
true
} else {
false
}
false
}
} else {
false

View File

@@ -173,9 +173,12 @@ impl PermissionCalculator {
}
if let Some(other) = other_user {
let relationship =
get_relationship_internal(&self.user.id, &other, &self.user.relations);
let relationship = get_relationship_internal(
&self.user.id,
&other,
&self.user.relations,
);
if relationship == Relationship::Friend {
permissions = 1024 + 128 + 32 + 16 + 1;
} else if relationship == Relationship::Blocked

View File

@@ -1,16 +1,16 @@
use super::channel::fetch_channels;
use super::get_collection;
use super::guild::{serialise_guilds_with_channels};
use super::channel::{fetch_channels};
use super::guild::serialise_guilds_with_channels;
use lru::LruCache;
use mongodb::bson::{doc, from_bson, Bson, DateTime};
use mongodb::options::FindOptions;
use rocket::http::{RawStr, Status};
use rocket::request::{self, FromParam, FromRequest, Request};
use rocket_contrib::json::JsonValue;
use rocket::Outcome;
use std::sync::{Arc, Mutex};
use rocket_contrib::json::JsonValue;
use serde::{Deserialize, Serialize};
use std::sync::{Arc, Mutex};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UserEmailVerification {
@@ -66,23 +66,21 @@ impl User {
doc! {
"_id.user": &self.id
},
None
).map_err(|_| "Failed to fetch members.")?;
Ok(members.into_iter()
None,
)
.map_err(|_| "Failed to fetch members.")?;
Ok(members
.into_iter()
.filter_map(|x| match x {
Ok(doc) => {
match doc.get_document("_id") {
Ok(id) => {
match id.get_str("guild") {
Ok(value) => Some(value.to_string()),
Err(_) => None
}
}
Err(_) => None
}
}
Err(_) => None
Ok(doc) => match doc.get_document("_id") {
Ok(id) => match id.get_str("guild") {
Ok(value) => Some(value.to_string()),
Err(_) => None,
},
Err(_) => None,
},
Err(_) => None,
})
.collect())
}
@@ -93,18 +91,16 @@ impl User {
doc! {
"recipients": &self.id
},
FindOptions::builder()
.projection(doc! { "_id": 1 })
.build()
).map_err(|_| "Failed to fetch channel ids.")?;
Ok(channels.into_iter()
FindOptions::builder().projection(doc! { "_id": 1 }).build(),
)
.map_err(|_| "Failed to fetch channel ids.")?;
Ok(channels
.into_iter()
.filter_map(|x| x.ok())
.filter_map(|x| {
match x.get_str("_id") {
Ok(value) => Some(value.to_string()),
Err(_) => None
}
.filter_map(|x| match x.get_str("_id") {
Ok(value) => Some(value.to_string()),
Err(_) => None,
})
.collect())
}
@@ -112,22 +108,12 @@ impl User {
pub fn create_payload(self) -> Result<JsonValue, String> {
let v = vec![];
let relations = self.relations.as_ref().unwrap_or(&v);
let users: Vec<JsonValue> = fetch_users(
&relations
.iter()
.map(|x| x.id.clone())
.collect()
)?
let users: Vec<JsonValue> = fetch_users(&relations.iter().map(|x| x.id.clone()).collect())?
.into_iter()
.map(|x| {
let id = x.id.clone();
x.serialise(
relations.iter()
.find(|y| y.id == id)
.unwrap()
.status as i32
)
x.serialise(relations.iter().find(|y| y.id == id).unwrap().status as i32)
})
.collect();
@@ -135,7 +121,7 @@ impl User {
.into_iter()
.map(|x| x.serialise())
.collect();
Ok(json!({
"users": users,
"channels": channels,
@@ -239,7 +225,7 @@ impl<'a, 'r> FromRequest<'a, 'r> for User {
type Error = AuthError;
fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
let u = request.headers().get("x-user").next();
let u = request.headers().get("x-user").next();
let t = request.headers().get("x-auth-token").next();
if let Some(uid) = u {
@@ -298,17 +284,13 @@ pub fn process_event(event: &Notification) {
if let Some(pos) = relations.iter().position(|x| x.id == ev.user) {
relations.remove(pos);
}
} else if let Some(entry) = relations.iter_mut().find(|x| x.id == ev.user) {
entry.status = ev.status as u8;
} else {
if let Some(entry) = relations.iter_mut().find(|x| x.id == ev.user) {
entry.status = ev.status as u8;
} else {
relations.push(
UserRelationship {
id: ev.id.clone(),
status: ev.status as u8
}
);
}
relations.push(UserRelationship {
id: ev.id.clone(),
status: ev.status as u8,
});
}
}
}