forked from jmug/stoatchat
feat(delta): example implementation of new core models
This commit is contained in:
41
Cargo.lock
generated
41
Cargo.lock
generated
@@ -2186,15 +2186,6 @@ dependencies = [
|
|||||||
"vcpkg",
|
"vcpkg",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "optional_struct"
|
|
||||||
version = "0.2.0"
|
|
||||||
source = "git+https://github.com/insertish/OptionalStruct?rev=ee56427cee1f007839825d93d07fffd5a5e038c7#ee56427cee1f007839825d93d07fffd5a5e038c7"
|
|
||||||
dependencies = [
|
|
||||||
"quote 0.3.15",
|
|
||||||
"syn 0.11.11",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "os_info"
|
name = "os_info"
|
||||||
version = "3.4.0"
|
version = "3.4.0"
|
||||||
@@ -2835,8 +2826,8 @@ dependencies = [
|
|||||||
"log",
|
"log",
|
||||||
"mongodb",
|
"mongodb",
|
||||||
"nanoid",
|
"nanoid",
|
||||||
"optional_struct",
|
|
||||||
"revolt-result",
|
"revolt-result",
|
||||||
|
"revolt_optional_struct",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
]
|
]
|
||||||
@@ -2864,6 +2855,7 @@ dependencies = [
|
|||||||
"regex",
|
"regex",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"revolt-database",
|
"revolt-database",
|
||||||
|
"revolt-models",
|
||||||
"revolt-quark",
|
"revolt-quark",
|
||||||
"revolt_rocket_okapi",
|
"revolt_rocket_okapi",
|
||||||
"rocket",
|
"rocket",
|
||||||
@@ -2881,6 +2873,11 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "revolt-models"
|
name = "revolt-models"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"revolt-database",
|
||||||
|
"schemars",
|
||||||
|
"serde",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "revolt-quark"
|
name = "revolt-quark"
|
||||||
@@ -2909,13 +2906,14 @@ dependencies = [
|
|||||||
"nanoid",
|
"nanoid",
|
||||||
"num_enum",
|
"num_enum",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
"optional_struct",
|
|
||||||
"pretty_env_logger",
|
"pretty_env_logger",
|
||||||
"rand 0.8.5",
|
"rand 0.8.5",
|
||||||
"redis-kiss",
|
"redis-kiss",
|
||||||
"regex",
|
"regex",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
|
"revolt-result",
|
||||||
"revolt_okapi",
|
"revolt_okapi",
|
||||||
|
"revolt_optional_struct",
|
||||||
"revolt_rocket_okapi",
|
"revolt_rocket_okapi",
|
||||||
"rocket",
|
"rocket",
|
||||||
"rocket_cors",
|
"rocket_cors",
|
||||||
@@ -2934,6 +2932,7 @@ dependencies = [
|
|||||||
name = "revolt-result"
|
name = "revolt-result"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"schemars",
|
||||||
"serde",
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -2949,6 +2948,16 @@ dependencies = [
|
|||||||
"serde_json",
|
"serde_json",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "revolt_optional_struct"
|
||||||
|
version = "0.2.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0d342739658623fe9d72b42f7a36ba19094d1d9bf9b423c3e337ab203c15d53e"
|
||||||
|
dependencies = [
|
||||||
|
"quote 0.3.15",
|
||||||
|
"syn 0.11.11",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "revolt_rocket_okapi"
|
name = "revolt_rocket_okapi"
|
||||||
version = "0.9.1"
|
version = "0.9.1"
|
||||||
@@ -3408,9 +3417,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde"
|
name = "serde"
|
||||||
version = "1.0.152"
|
version = "1.0.160"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "bb7d1f0d3021d347a83e556fc4683dea2ea09d87bccdf88ff5c12545d89d5efb"
|
checksum = "bb2f3770c8bce3bcda7e149193a069a0f4365bda1fa5cd88e03bca26afc1216c"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"serde_derive",
|
"serde_derive",
|
||||||
]
|
]
|
||||||
@@ -3426,13 +3435,13 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde_derive"
|
name = "serde_derive"
|
||||||
version = "1.0.152"
|
version = "1.0.160"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "af487d118eecd09402d70a5d72551860e788df87b464af30e5ea6a38c75c541e"
|
checksum = "291a097c63d8497e00160b166a967a4a79c64f3facdd01cbd7502231688d77df"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote 1.0.26",
|
"quote 1.0.26",
|
||||||
"syn 1.0.107",
|
"syn 2.0.15",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
@@ -57,6 +57,7 @@ revolt-quark = { path = "../quark" }
|
|||||||
|
|
||||||
# core
|
# core
|
||||||
revolt-database = { path = "../core/database" }
|
revolt-database = { path = "../core/database" }
|
||||||
|
revolt-models = { path = "../core/models", features = [ "schemas" ] }
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
vergen = "7.5.0"
|
vergen = "7.5.0"
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
use revolt_quark::{
|
use revolt_database::Database;
|
||||||
models::{Bot, User},
|
use revolt_models::Bot;
|
||||||
Db, Error, Ref, Result,
|
use revolt_quark::{models::User, Db, Error, Ref, Result};
|
||||||
};
|
use rocket::{serde::json::Json, State};
|
||||||
use rocket::serde::json::Json;
|
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
|
||||||
/// # Bot Response
|
/// # Bot Response
|
||||||
|
/// TODO: move to revolt-models
|
||||||
#[derive(Serialize, JsonSchema)]
|
#[derive(Serialize, JsonSchema)]
|
||||||
pub struct BotResponse {
|
pub struct BotResponse {
|
||||||
/// Bot object
|
/// Bot object
|
||||||
@@ -19,18 +19,23 @@ pub struct BotResponse {
|
|||||||
/// Fetch details of a bot you own by its id.
|
/// Fetch details of a bot you own by its id.
|
||||||
#[openapi(tag = "Bots")]
|
#[openapi(tag = "Bots")]
|
||||||
#[get("/<target>")]
|
#[get("/<target>")]
|
||||||
pub async fn fetch_bot(db: &Db, user: User, target: Ref) -> Result<Json<BotResponse>> {
|
pub async fn fetch_bot(
|
||||||
|
legacy_db: &Db,
|
||||||
|
db: &State<Database>,
|
||||||
|
user: User,
|
||||||
|
target: Ref,
|
||||||
|
) -> Result<Json<BotResponse>> {
|
||||||
if user.bot.is_some() {
|
if user.bot.is_some() {
|
||||||
return Err(Error::IsBot);
|
return Err(Error::IsBot);
|
||||||
}
|
}
|
||||||
|
|
||||||
let bot = target.as_bot(db).await?;
|
let bot = db.fetch_bot(&target.id).await.map_err(Error::from_core)?;
|
||||||
if bot.owner != user.id {
|
if bot.owner != user.id {
|
||||||
return Err(Error::NotFound);
|
return Err(Error::NotFound);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Json(BotResponse {
|
Ok(Json(BotResponse {
|
||||||
user: db.fetch_user(&bot.id).await?.foreign(),
|
user: legacy_db.fetch_user(&bot.id).await?.foreign(),
|
||||||
bot,
|
bot: bot.into(),
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,44 +1,32 @@
|
|||||||
use revolt_quark::{
|
use revolt_database::Database;
|
||||||
models::{File, User},
|
use revolt_models::PublicBot;
|
||||||
Db, Error, Ref, Result,
|
use revolt_quark::{models::User, Db, Error, Ref, Result};
|
||||||
};
|
|
||||||
|
|
||||||
use rocket::serde::json::Json;
|
use rocket::serde::json::Json;
|
||||||
use serde::{Deserialize, Serialize};
|
use rocket::State;
|
||||||
|
|
||||||
/// # Public Bot
|
|
||||||
#[derive(Serialize, Deserialize, JsonSchema)]
|
|
||||||
pub struct PublicBot {
|
|
||||||
/// Bot Id
|
|
||||||
#[serde(rename = "_id")]
|
|
||||||
id: String,
|
|
||||||
/// Bot Username
|
|
||||||
username: String,
|
|
||||||
/// Profile Avatar
|
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
|
||||||
avatar: Option<File>,
|
|
||||||
/// Profile Description
|
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
|
||||||
description: Option<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// # Fetch Public Bot
|
/// # Fetch Public Bot
|
||||||
///
|
///
|
||||||
/// Fetch details of a public (or owned) bot by its id.
|
/// Fetch details of a public (or owned) bot by its id.
|
||||||
#[openapi(tag = "Bots")]
|
#[openapi(tag = "Bots")]
|
||||||
#[get("/<target>/invite")]
|
#[get("/<target>/invite")]
|
||||||
pub async fn fetch_public_bot(db: &Db, user: Option<User>, target: Ref) -> Result<Json<PublicBot>> {
|
pub async fn fetch_public_bot(
|
||||||
let bot = target.as_bot(db).await?;
|
legacy_db: &Db,
|
||||||
|
db: &State<Database>,
|
||||||
|
user: Option<User>,
|
||||||
|
target: Ref,
|
||||||
|
) -> Result<Json<PublicBot>> {
|
||||||
|
let bot = db.fetch_bot(&target.id).await.map_err(Error::from_core)?;
|
||||||
if !bot.public && user.map_or(true, |x| x.id != bot.owner) {
|
if !bot.public && user.map_or(true, |x| x.id != bot.owner) {
|
||||||
return Err(Error::NotFound);
|
return Err(Error::NotFound);
|
||||||
}
|
}
|
||||||
|
|
||||||
let user = db.fetch_user(&bot.id).await?;
|
let user = legacy_db.fetch_user(&bot.id).await?;
|
||||||
|
|
||||||
Ok(Json(PublicBot {
|
Ok(Json(PublicBot::from(
|
||||||
id: bot.id,
|
bot,
|
||||||
username: user.username,
|
user.username,
|
||||||
avatar: user.avatar,
|
user.avatar.map(|f| f.id),
|
||||||
description: user.profile.and_then(|p| p.content),
|
user.profile.and_then(|p| p.content),
|
||||||
}))
|
)))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user