Compare commits

...

3 Commits
0.2.6 ... 0.2.7

Author SHA1 Message Date
Paul Makles
a8eb403280 Logging + database migrations system. 2020-08-11 21:08:01 +02:00
Paul Makles
750f8c6738 Provide version object at /, add ready event. 2020-08-11 16:20:24 +02:00
Paul Makles
83ee9253fe Fix fetch guild member. 2020-08-11 12:41:23 +02:00
11 changed files with 179 additions and 16 deletions

43
Cargo.lock generated
View File

@@ -509,6 +509,19 @@ dependencies = [
"syn 1.0.37",
]
[[package]]
name = "env_logger"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36"
dependencies = [
"atty",
"humantime",
"log 0.4.11",
"regex",
"termcolor",
]
[[package]]
name = "err-derive"
version = "0.2.4"
@@ -888,6 +901,15 @@ version = "1.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9"
[[package]]
name = "humantime"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f"
dependencies = [
"quick-error",
]
[[package]]
name = "hyper"
version = "0.10.16"
@@ -1885,14 +1907,16 @@ dependencies = [
[[package]]
name = "revolt"
version = "0.2.6"
version = "0.2.7"
dependencies = [
"bcrypt",
"bitfield",
"chrono",
"dotenv",
"env_logger",
"hashbrown 0.7.2",
"lazy_static",
"log 0.4.11",
"lru",
"mongodb",
"num_enum",
@@ -2423,6 +2447,15 @@ dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "termcolor"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f"
dependencies = [
"winapi-util",
]
[[package]]
name = "thiserror"
version = "1.0.20"
@@ -2586,9 +2619,9 @@ checksum = "e987b6bf443f4b5b3b6f38704195592cca41c5bb7aedd3c3693c7081f8289860"
[[package]]
name = "tracing"
version = "0.1.18"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0aae59226cf195d8e74d4b34beae1859257efb4e5fed3f147d2dc2c7d372178"
checksum = "6d79ca061b032d6ce30c660fded31189ca0b9922bf483cd70759f13a2d86786c"
dependencies = [
"cfg-if",
"log 0.4.11",
@@ -2597,9 +2630,9 @@ dependencies = [
[[package]]
name = "tracing-core"
version = "0.1.12"
version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2734b5a028fa697686f16c6d18c2c6a3c7e41513f9a213abb6754c4acb3c8d7"
checksum = "db63662723c316b43ca36d833707cc93dff82a02ba3d7e354f342682cc8b3545"
dependencies = [
"lazy_static",
]

View File

@@ -1,6 +1,6 @@
[package]
name = "revolt"
version = "0.2.6"
version = "0.2.7"
authors = ["Paul Makles <paulmakles@gmail.com>"]
edition = "2018"
@@ -28,3 +28,5 @@ rocket_cors = "0.5.2"
bitfield = "0.13.2"
lru = "0.5.3"
lazy_static = "1.4.0"
log = "0.4.11"
env_logger = "0.7.1"

View File

@@ -42,6 +42,7 @@ pub struct Guild {
pub description: String,
pub owner: String,
// ? FIXME: ADD: pub channels: Vec<Channel>,
pub invites: Vec<Invite>,
pub bans: Vec<Ban>,

View File

@@ -0,0 +1,39 @@
use super::super::get_db;
use super::scripts::LATEST_REVISION;
use mongodb::options::CreateCollectionOptions;
use mongodb::bson::doc;
use log::info;
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(
"pubsub",
CreateCollectionOptions::builder()
.capped(true)
.size(1_000_000)
.build()
)
.expect("Failed to create pubsub collection.");
db.collection("migrations")
.insert_one(
doc! {
"_id": 0,
"revision": LATEST_REVISION
},
None
)
.expect("Failed to save migration info.");
info!("Created database.");
}

View File

@@ -0,0 +1,19 @@
use super::get_connection;
pub mod init;
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.");
if list.iter().position(|x| x == "revolt").is_none() {
init::create_database();
} else {
scripts::migrate_database();
}
}

View File

@@ -0,0 +1,53 @@
use super::super::get_collection;
use serde::{Serialize, Deserialize};
use mongodb::bson::{Bson, from_bson, doc};
use log::info;
#[derive(Serialize, Deserialize)]
struct MigrationInfo {
_id: i32,
revision: i32
}
pub const LATEST_REVISION: i32 = 1;
pub fn migrate_database() {
let migrations = get_collection("migrations");
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 revision = run_migrations(info.revision);
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 {
panic!("Database was configured incorrectly, possibly because initalization failed.")
}
}
pub fn run_migrations(revision: i32) -> i32 {
info!("Starting database migration.");
if revision <= 0 {
info!("Running migration [revision 0]: Test migration system.");
}
// Reminder to update LATEST_REVISION when adding new migrations.
LATEST_REVISION
}

View File

@@ -10,13 +10,8 @@ pub fn connect() {
Client::with_uri_str(&env::var("DB_URI").expect("DB_URI not in environment variables!"))
.expect("Failed to init db connection.");
client
.database("revolt")
.collection("migrations")
.find(doc! {}, None)
.expect("Failed to get migration data from database.");
DBCONN.set(client).unwrap();
migrations::run_migrations();
}
pub fn get_connection() -> &'static Client {
@@ -31,6 +26,8 @@ pub fn get_collection(collection: &str) -> Collection {
get_db().collection(collection)
}
pub mod migrations;
pub mod channel;
pub mod guild;
pub mod message;

View File

@@ -21,6 +21,7 @@ use std::thread;
fn main() {
dotenv::dotenv().ok();
env_logger::init();
database::connect();
notifications::start_worker();

View File

@@ -36,13 +36,26 @@ impl Handler for Server {
match state.try_authenticate(self.id.clone(), token.to_string()) {
StateResult::Success(user_id) => {
self.user_id = Some(user_id);
let user = crate::database::user::fetch_user(&user_id).unwrap().unwrap();
self.sender.send(
json!({
"type": "authenticate",
"success": true,
})
.to_string(),
)?;
self.user_id = Some(user_id);
self.sender.send(
json!({
"type": "ready",
"data": {
// ! FIXME: rewrite
"user": user,
}
})
.to_string(),
)
}
StateResult::DatabaseError => self.sender.send(

View File

@@ -640,7 +640,7 @@ pub fn fetch_members(user: User, target: Guild) -> Option<Response> {
pub fn fetch_member(user: User, target: Guild, other: String) -> Option<Response> {
with_permissions!(user, target);
if let Ok(result) = get_member(MemberKey(target.id, user.id)) {
if let Ok(result) = get_member(MemberKey(target.id, other)) {
if let Some(member) = result {
Some(Response::Success(json!({
"id": member.id.user,
@@ -653,7 +653,7 @@ pub fn fetch_member(user: User, target: Guild, other: String) -> Option<Response
}
} else {
Some(Response::InternalServerError(
json!({ "error": "Failed to fetch member or user does not exist." }),
json!({ "error": "Failed to fetch member." }),
))
}
}

View File

@@ -6,7 +6,12 @@ use mongodb::bson::doc;
#[get("/")]
pub fn root() -> Response {
Response::Success(json!({
"revolt": "0.2.6"
"revolt": "0.2.7",
"version": {
"major": 0,
"minor": 2,
"patch": 7
}
}))
}