Add a way to choose message fetch sort.

This commit is contained in:
Paul
2021-05-03 16:35:59 +01:00
parent 2173b1e9f8
commit a319e72655
3 changed files with 10 additions and 6 deletions

View File

@@ -1,3 +1,3 @@
#!/bin/bash #!/bin/bash
export version=0.4.1-alpha.6 export version=0.4.1-alpha.7
echo "pub const VERSION: &str = \"${version}\";" > src/version.rs echo "pub const VERSION: &str = \"${version}\";" > src/version.rs

View File

@@ -26,10 +26,6 @@ pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
data.validate() data.validate()
.map_err(|error| Error::FailedValidation { error })?; .map_err(|error| Error::FailedValidation { error })?;
if data.name.is_none() && data.description.is_none() && data.icon.is_none() && data.remove.is_none() {
return Ok(());
}
let target = target.fetch_channel().await?; let target = target.fetch_channel().await?;
let perm = permissions::PermissionCalculator::new(&user) let perm = permissions::PermissionCalculator::new(&user)
.with_channel(&target) .with_channel(&target)

View File

@@ -11,6 +11,12 @@ use rocket_contrib::json::JsonValue;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use validator::Validate; use validator::Validate;
#[derive(Serialize, Deserialize, FromFormValue)]
pub enum Sort {
Latest,
Oldest
}
#[derive(Validate, Serialize, Deserialize, FromForm)] #[derive(Validate, Serialize, Deserialize, FromForm)]
pub struct Options { pub struct Options {
#[validate(range(min = 1, max = 100))] #[validate(range(min = 1, max = 100))]
@@ -19,6 +25,7 @@ pub struct Options {
before: Option<String>, before: Option<String>,
#[validate(length(min = 26, max = 26))] #[validate(length(min = 26, max = 26))]
after: Option<String>, after: Option<String>,
sort: Option<Sort>
} }
#[get("/<target>/messages?<options..>")] #[get("/<target>/messages?<options..>")]
@@ -47,13 +54,14 @@ pub async fn req(user: User, target: Ref, options: Form<Options>) -> Result<Json
query.insert("_id", doc! { "$gt": after }); query.insert("_id", doc! { "$gt": after });
} }
let sort = if let Sort::Latest = options.sort.as_ref().unwrap_or_else(|| &Sort::Latest) { -1 } else { 1 };
let mut cursor = get_collection("messages") let mut cursor = get_collection("messages")
.find( .find(
query, query,
FindOptions::builder() FindOptions::builder()
.limit(options.limit.unwrap_or(50)) .limit(options.limit.unwrap_or(50))
.sort(doc! { .sort(doc! {
"_id": -1 "_id": sort
}) })
.build(), .build(),
) )