mirror of
https://github.com/stoatchat/stoatchat.git
synced 2026-07-14 05:26:59 +00:00
Add a way to choose message fetch sort.
This commit is contained in:
@@ -1,3 +1,3 @@
|
||||
#!/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
|
||||
|
||||
@@ -26,10 +26,6 @@ pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
|
||||
data.validate()
|
||||
.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 perm = permissions::PermissionCalculator::new(&user)
|
||||
.with_channel(&target)
|
||||
|
||||
@@ -11,6 +11,12 @@ use rocket_contrib::json::JsonValue;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use validator::Validate;
|
||||
|
||||
#[derive(Serialize, Deserialize, FromFormValue)]
|
||||
pub enum Sort {
|
||||
Latest,
|
||||
Oldest
|
||||
}
|
||||
|
||||
#[derive(Validate, Serialize, Deserialize, FromForm)]
|
||||
pub struct Options {
|
||||
#[validate(range(min = 1, max = 100))]
|
||||
@@ -19,6 +25,7 @@ pub struct Options {
|
||||
before: Option<String>,
|
||||
#[validate(length(min = 26, max = 26))]
|
||||
after: Option<String>,
|
||||
sort: Option<Sort>
|
||||
}
|
||||
|
||||
#[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 });
|
||||
}
|
||||
|
||||
let sort = if let Sort::Latest = options.sort.as_ref().unwrap_or_else(|| &Sort::Latest) { -1 } else { 1 };
|
||||
let mut cursor = get_collection("messages")
|
||||
.find(
|
||||
query,
|
||||
FindOptions::builder()
|
||||
.limit(options.limit.unwrap_or(50))
|
||||
.sort(doc! {
|
||||
"_id": -1
|
||||
"_id": sort
|
||||
})
|
||||
.build(),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user