feat(messages): implement fetch, query, search

This commit is contained in:
Paul Makles
2022-02-10 12:53:26 +00:00
parent a6cec559b1
commit a4cbbfea51
4 changed files with 85 additions and 56 deletions

View File

@@ -1,22 +1,15 @@
use std::collections::HashSet;
use revolt_quark::{Error, Result};
use futures::{try_join, StreamExt};
use mongodb::{
bson::{doc, from_document},
options::FindOptions,
use revolt_quark::{
models::{
message::{BulkMessageResponse, MessageSort},
User,
},
perms,
Db, Error, Ref, Result,
};
use rocket::serde::json::Value;
use rocket::serde::json::Json;
use serde::{Deserialize, Serialize};
use validator::Validate;
#[derive(Serialize, Deserialize, FromFormField)]
pub enum Sort {
Latest,
Oldest,
}
#[derive(Validate, Serialize, Deserialize, FromForm)]
pub struct Options {
#[validate(range(min = 1, max = 100))]
@@ -25,7 +18,7 @@ pub struct Options {
before: Option<String>,
#[validate(length(min = 26, max = 26))]
after: Option<String>,
sort: Option<Sort>,
sort: Option<MessageSort>,
// Specifying 'nearby' ignores 'before', 'after' and 'sort'.
// It will also take half of limit rounded as the limits to each side.
// It also fetches the message ID specified.
@@ -36,8 +29,42 @@ pub struct Options {
#[get("/<target>/messages?<options..>")]
pub async fn req(
/*user: UserRef, target: Ref,*/ target: String,
db: &Db,
user: User,
target: Ref,
options: Options,
) -> Result<Value> {
todo!()
) -> Result<Json<BulkMessageResponse>> {
options
.validate()
.map_err(|error| Error::FailedValidation { error })?;
if let Some(MessageSort::Relevance) = options.sort {
return Err(Error::InvalidOperation);
}
let channel = target.as_channel(db).await?;
if !perms(&user)
.channel(&channel)
.calc_channel(db)
.await
.get_view()
{
return Err(Error::NotFound);
}
let Options {
limit,
before,
after,
sort,
nearby,
include_users,
..
} = options;
let messages = db
.fetch_messages(channel.id(), limit, before, after, sort, nearby)
.await?;
BulkMessageResponse::transform(db, &channel, messages, include_users).await.map(Json)
}