Add role hoisting / ranking.

Add bot creation for #8.
This commit is contained in:
Paul
2021-08-11 20:04:22 +01:00
parent e70f848f21
commit 084d71f050
18 changed files with 282 additions and 29 deletions

View File

@@ -0,0 +1,12 @@
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Bot {
#[serde(rename = "_id")]
pub id: String,
pub owner: String,
pub token: String,
pub public: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub interactions_url: Option<String>,
}

View File

@@ -5,6 +5,7 @@ mod microservice;
mod server;
mod sync;
mod user;
mod bots;
use microservice::*;
@@ -16,3 +17,4 @@ pub use message::*;
pub use server::*;
pub use sync::*;
pub use user::*;
pub use bots::*;

View File

@@ -37,13 +37,20 @@ pub type PermissionTuple = (
i32 // channel permission
);
fn if_false(t: &bool) -> bool {
*t == false
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Role {
pub name: String,
pub permissions: PermissionTuple,
#[serde(skip_serializing_if = "Option::is_none")]
pub colour: Option<String>
// Bri'ish API conventions
pub colour: Option<String>,
#[serde(skip_serializing_if = "if_false", default)]
pub hoist: bool,
#[serde(default)]
pub rank: i64,
}
#[derive(Serialize, Deserialize, Debug, Clone)]

View File

@@ -73,7 +73,12 @@ pub enum Badges {
impl_op_ex_commutative!(+ |a: &i32, b: &Badges| -> i32 { *a | *b as i32 });
// When changing this struct, update notifications/payload.rs#80
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct BotInformation {
owner: String
}
// When changing this struct, update notifications/payload.rs#113
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct User {
#[serde(rename = "_id")]
@@ -91,6 +96,11 @@ pub struct User {
#[serde(skip_serializing_if = "Option::is_none")]
pub profile: Option<UserProfile>,
#[serde(skip_serializing_if = "Option::is_none")]
pub flags: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub bot: Option<BotInformation>,
// ? This should never be pushed to the collection.
#[serde(skip_serializing_if = "Option::is_none")]
pub relationship: Option<RelationshipStatus>,

View File

@@ -10,6 +10,61 @@ impl<'r> FromRequest<'r> for User {
type Error = rauth::util::Error;
async fn from_request(request: &'r Request<'_>) -> request::Outcome<Self, Self::Error> {
let header_bot_token = request
.headers()
.get("x-bot-token")
.next()
.map(|x| x.to_string());
if let Some(bot_token) = header_bot_token {
return if let Ok(result) = get_collection("bots")
.find_one(
doc! {
"token": bot_token
},
None,
)
.await
{
if let Some(doc) = result {
let id = doc.get_str("_id").unwrap();
if let Ok(result) = get_collection("users")
.find_one(
doc! {
"_id": &id
},
None,
)
.await
{
if let Some(doc) = result {
Outcome::Success(from_document(doc).unwrap())
} else {
Outcome::Failure((Status::Forbidden, rauth::util::Error::InvalidSession))
}
} else {
Outcome::Failure((
Status::InternalServerError,
rauth::util::Error::DatabaseError {
operation: "find_one",
with: "user",
},
))
}
} else {
Outcome::Failure((Status::Forbidden, rauth::util::Error::InvalidSession))
}
} else {
Outcome::Failure((
Status::InternalServerError,
rauth::util::Error::DatabaseError {
operation: "find_one",
with: "bot",
},
))
}
}
let session: Session = request.guard::<Session>().await.unwrap();
if let Ok(result) = get_collection("users")

View File

@@ -57,6 +57,10 @@ pub async fn create_database() {
.await
.expect("Failed to create user_settings collection.");
db.create_collection("bots", None)
.await
.expect("Failed to create bots collection.");
db.create_collection(
"pubsub",
CreateCollectionOptions::builder()

View File

@@ -11,7 +11,7 @@ struct MigrationInfo {
revision: i32,
}
pub const LATEST_REVISION: i32 = 7;
pub const LATEST_REVISION: i32 = 8;
pub async fn migrate_database() {
let migrations = get_collection("migrations");
@@ -203,6 +203,15 @@ pub async fn run_migrations(revision: i32) -> i32 {
.expect("Failed to create message index.");
}
if revision <= 7 {
info!("Running migration [revision 7 / 2021-08-11]: Add message text index.");
get_db()
.create_collection("bots", None)
.await
.expect("Failed to create bots collection.");
}
// Reminder to update LATEST_REVISION when adding new migrations.
LATEST_REVISION
}