forked from jmug/stoatchat
feat(admin): add global message query route
This commit is contained in:
31
crates/delta/src/routes/admin/message_query.rs
Normal file
31
crates/delta/src/routes/admin/message_query.rs
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
use revolt_quark::{
|
||||||
|
models::{
|
||||||
|
message::{BulkMessageResponse, MessageQuery},
|
||||||
|
User,
|
||||||
|
},
|
||||||
|
Db, Error, Result,
|
||||||
|
};
|
||||||
|
use rocket::serde::json::Json;
|
||||||
|
|
||||||
|
/// # Globally Fetch Messages
|
||||||
|
///
|
||||||
|
/// This is a privileged route to globally fetch messages.
|
||||||
|
#[openapi(tag = "Admin")]
|
||||||
|
#[post("/messages", data = "<data>")]
|
||||||
|
pub async fn message_query(
|
||||||
|
db: &Db,
|
||||||
|
user: User,
|
||||||
|
data: Json<MessageQuery>,
|
||||||
|
) -> Result<Json<BulkMessageResponse>> {
|
||||||
|
// Must be privileged for this route
|
||||||
|
if !user.privileged {
|
||||||
|
return Err(Error::NotPrivileged);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch data using query
|
||||||
|
let data = data.into_inner();
|
||||||
|
let messages = db.fetch_messages(data).await?;
|
||||||
|
BulkMessageResponse::transform(db, None, messages, Some(true))
|
||||||
|
.await
|
||||||
|
.map(Json)
|
||||||
|
}
|
||||||
9
crates/delta/src/routes/admin/mod.rs
Normal file
9
crates/delta/src/routes/admin/mod.rs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
use revolt_rocket_okapi::revolt_okapi::openapi3::OpenApi;
|
||||||
|
use rocket::Route;
|
||||||
|
|
||||||
|
mod message_query;
|
||||||
|
mod stats;
|
||||||
|
|
||||||
|
pub fn routes() -> (Vec<Route>, OpenApi) {
|
||||||
|
openapi_get_routes_spec![stats::stats, message_query::message_query]
|
||||||
|
}
|
||||||
@@ -6,7 +6,7 @@ use rocket::serde::json::Json;
|
|||||||
/// # Query Stats
|
/// # Query Stats
|
||||||
///
|
///
|
||||||
/// Fetch various technical statistics.
|
/// Fetch various technical statistics.
|
||||||
#[openapi(tag = "Core")]
|
#[openapi(tag = "Admin")]
|
||||||
#[get("/stats")]
|
#[get("/stats")]
|
||||||
pub async fn stats(db: &Db) -> Result<Json<Stats>> {
|
pub async fn stats(db: &Db) -> Result<Json<Stats>> {
|
||||||
Ok(Json(db.generate_stats().await?))
|
Ok(Json(db.generate_stats().await?))
|
||||||
@@ -91,7 +91,7 @@ pub async fn req(
|
|||||||
})
|
})
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
BulkMessageResponse::transform(db, &channel, messages, include_users)
|
BulkMessageResponse::transform(db, Some(&channel), messages, include_users)
|
||||||
.await
|
.await
|
||||||
.map(Json)
|
.map(Json)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -90,7 +90,7 @@ pub async fn req(
|
|||||||
})
|
})
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
BulkMessageResponse::transform(db, &channel, messages, include_users)
|
BulkMessageResponse::transform(db, Some(&channel), messages, include_users)
|
||||||
.await
|
.await
|
||||||
.map(Json)
|
.map(Json)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ pub use rocket::http::Status;
|
|||||||
pub use rocket::response::Redirect;
|
pub use rocket::response::Redirect;
|
||||||
use rocket::{Build, Rocket};
|
use rocket::{Build, Rocket};
|
||||||
|
|
||||||
|
mod admin;
|
||||||
mod bots;
|
mod bots;
|
||||||
mod channels;
|
mod channels;
|
||||||
mod customisation;
|
mod customisation;
|
||||||
@@ -12,7 +13,6 @@ mod push;
|
|||||||
mod root;
|
mod root;
|
||||||
mod safety;
|
mod safety;
|
||||||
mod servers;
|
mod servers;
|
||||||
mod stats;
|
|
||||||
mod sync;
|
mod sync;
|
||||||
mod users;
|
mod users;
|
||||||
|
|
||||||
@@ -22,7 +22,8 @@ pub fn mount(mut rocket: Rocket<Build>) -> Rocket<Build> {
|
|||||||
mount_endpoints_and_merged_docs! {
|
mount_endpoints_and_merged_docs! {
|
||||||
rocket, "/".to_owned(), settings,
|
rocket, "/".to_owned(), settings,
|
||||||
"/" => (vec![], custom_openapi_spec()),
|
"/" => (vec![], custom_openapi_spec()),
|
||||||
"" => openapi_get_routes_spec![root::root, stats::stats, root::ping],
|
"" => openapi_get_routes_spec![root::root, root::ping],
|
||||||
|
"/admin" => admin::routes(),
|
||||||
"/users" => users::routes(),
|
"/users" => users::routes(),
|
||||||
"/bots" => bots::routes(),
|
"/bots" => bots::routes(),
|
||||||
"/channels" => channels::routes(),
|
"/channels" => channels::routes(),
|
||||||
@@ -109,8 +110,9 @@ fn custom_openapi_spec() -> OpenApi {
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "Platform Moderation",
|
"name": "Platform Administration",
|
||||||
"tags": [
|
"tags": [
|
||||||
|
"Admin",
|
||||||
"User Safety"
|
"User Safety"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -377,7 +377,7 @@ impl SendableEmbed {
|
|||||||
impl BulkMessageResponse {
|
impl BulkMessageResponse {
|
||||||
pub async fn transform(
|
pub async fn transform(
|
||||||
db: &Database,
|
db: &Database,
|
||||||
channel: &Channel,
|
channel: Option<&Channel>,
|
||||||
messages: Vec<Message>,
|
messages: Vec<Message>,
|
||||||
include_users: Option<bool>,
|
include_users: Option<bool>,
|
||||||
) -> Result<BulkMessageResponse> {
|
) -> Result<BulkMessageResponse> {
|
||||||
@@ -386,7 +386,8 @@ impl BulkMessageResponse {
|
|||||||
let users = User::fetch_foreign_users(db, &user_ids).await?;
|
let users = User::fetch_foreign_users(db, &user_ids).await?;
|
||||||
|
|
||||||
Ok(match channel {
|
Ok(match channel {
|
||||||
Channel::TextChannel { server, .. } | Channel::VoiceChannel { server, .. } => {
|
Some(Channel::TextChannel { server, .. })
|
||||||
|
| Some(Channel::VoiceChannel { server, .. }) => {
|
||||||
BulkMessageResponse::MessagesAndUsers {
|
BulkMessageResponse::MessagesAndUsers {
|
||||||
messages,
|
messages,
|
||||||
users,
|
users,
|
||||||
|
|||||||
Reference in New Issue
Block a user