Add before, after and sort support to search.

This commit is contained in:
Paul
2021-07-21 18:26:35 +01:00
parent 27a9fc3e21
commit 1a8006b3e4
4 changed files with 59 additions and 18 deletions

View File

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

View File

@@ -159,7 +159,7 @@ impl User {
/// Utility function for checking claimed usernames. /// Utility function for checking claimed usernames.
pub async fn is_username_taken(username: &str) -> Result<bool> { pub async fn is_username_taken(username: &str) -> Result<bool> {
if username.to_lowercase() == "revolt" || username.to_lowercase() == "admin" { if username.to_lowercase() == "revolt" || username.to_lowercase() == "admin" || username.to_lowercase() == "system" {
return Ok(true); return Ok(true);
} }

View File

@@ -19,6 +19,12 @@ pub enum Sort {
Oldest, Oldest,
} }
impl Default for Sort {
fn default() -> Sort {
Sort::Relevance
}
}
#[derive(Validate, Serialize, Deserialize, FromForm)] #[derive(Validate, Serialize, Deserialize, FromForm)]
pub struct Options { pub struct Options {
#[validate(length(min = 1, max = 64))] #[validate(length(min = 1, max = 64))]
@@ -30,7 +36,8 @@ 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>, #[serde(default = "Sort::default")]
sort: Sort,
include_users: Option<bool>, include_users: Option<bool>,
} }
@@ -54,26 +61,60 @@ pub async fn req(user: User, target: Ref, options: Json<Options>) -> Result<Json
let mut messages = vec![]; let mut messages = vec![];
let limit = options.limit.unwrap_or(50); let limit = options.limit.unwrap_or(50);
let mut cursor = get_collection("messages") let mut filter = doc! {
.find(
doc! {
"channel": target.id(), "channel": target.id(),
"$text": { "$text": {
"$search": &options.query "$search": &options.query
} }
}, };
if let Some(doc) = match (&options.before, &options.after) {
(Some(before), Some(after)) => Some(doc! {
"lt": before,
"gt": after
}),
(Some(before), _) => Some(doc! {
"lt": before
}),
(_, Some(after)) => Some(doc! {
"gt": after
}),
_ => None
} {
filter.insert("_id", doc);
}
let mut cursor = get_collection("messages")
.find(
filter,
FindOptions::builder() FindOptions::builder()
.projection(doc! { .projection(
if let Sort::Relevance = &options.sort {
doc! {
"score": { "score": {
"$meta": "textScore" "$meta": "textScore"
} }
}) }
} else {
doc! {}
}
)
.limit(limit) .limit(limit)
.sort(doc! { .sort(
match &options.sort {
Sort::Relevance => doc! {
"score": { "score": {
"$meta": "textScore" "$meta": "textScore"
} }
}) },
Sort::Latest => doc! {
"_id": -1
},
Sort::Oldest => doc! {
"_id": 1
}
}
)
.build(), .build(),
) )
.await .await

View File

@@ -1 +1 @@
pub const VERSION: &str = "0.5.1-alpha.8-patch.0"; pub const VERSION: &str = "0.5.1-alpha.9";