Add before, after and sort support to search.
This commit is contained in:
@@ -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
|
||||||
|
|||||||
@@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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 filter = doc! {
|
||||||
|
"channel": target.id(),
|
||||||
|
"$text": {
|
||||||
|
"$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")
|
let mut cursor = get_collection("messages")
|
||||||
.find(
|
.find(
|
||||||
doc! {
|
filter,
|
||||||
"channel": target.id(),
|
|
||||||
"$text": {
|
|
||||||
"$search": &options.query
|
|
||||||
}
|
|
||||||
},
|
|
||||||
FindOptions::builder()
|
FindOptions::builder()
|
||||||
.projection(doc! {
|
.projection(
|
||||||
"score": {
|
if let Sort::Relevance = &options.sort {
|
||||||
"$meta": "textScore"
|
doc! {
|
||||||
|
"score": {
|
||||||
|
"$meta": "textScore"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
doc! {}
|
||||||
}
|
}
|
||||||
})
|
)
|
||||||
.limit(limit)
|
.limit(limit)
|
||||||
.sort(doc! {
|
.sort(
|
||||||
"score": {
|
match &options.sort {
|
||||||
"$meta": "textScore"
|
Sort::Relevance => doc! {
|
||||||
|
"score": {
|
||||||
|
"$meta": "textScore"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Sort::Latest => doc! {
|
||||||
|
"_id": -1
|
||||||
|
},
|
||||||
|
Sort::Oldest => doc! {
|
||||||
|
"_id": 1
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
)
|
||||||
.build(),
|
.build(),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
pub const VERSION: &str = "0.5.1-alpha.8-patch.0";
|
pub const VERSION: &str = "0.5.1-alpha.9";
|
||||||
|
|||||||
Reference in New Issue
Block a user