forked from jmug/stoatchat
Fetch messages route.
This commit is contained in:
@@ -4,7 +4,7 @@ use crate::util::result::{Error, Result};
|
|||||||
use validator::Validate;
|
use validator::Validate;
|
||||||
use rocket::http::RawStr;
|
use rocket::http::RawStr;
|
||||||
use rocket::request::FromParam;
|
use rocket::request::FromParam;
|
||||||
use mongodb::bson::{doc, from_bson, Bson};
|
use mongodb::bson::{Bson, doc, from_bson, from_document};
|
||||||
use serde::{Deserialize, Serialize, de::DeserializeOwned};
|
use serde::{Deserialize, Serialize, de::DeserializeOwned};
|
||||||
|
|
||||||
#[derive(Validate, Serialize, Deserialize)]
|
#[derive(Validate, Serialize, Deserialize)]
|
||||||
@@ -34,8 +34,8 @@ impl Ref {
|
|||||||
.ok_or_else(|| Error::UnknownUser)?;
|
.ok_or_else(|| Error::UnknownUser)?;
|
||||||
|
|
||||||
Ok(
|
Ok(
|
||||||
from_bson::<T>(Bson::Document(doc)).map_err(|_| Error::DatabaseError {
|
from_document::<T>(doc).map_err(|_| Error::DatabaseError {
|
||||||
operation: "from_bson",
|
operation: "from_document",
|
||||||
with: &collection,
|
with: &collection,
|
||||||
})?,
|
})?,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use crate::database::*;
|
use crate::database::*;
|
||||||
|
|
||||||
use mongodb::bson::{doc, from_bson, Bson};
|
use mongodb::bson::{Bson, doc, from_bson, from_document};
|
||||||
use rauth::auth::Session;
|
use rauth::auth::Session;
|
||||||
use rocket::http::Status;
|
use rocket::http::Status;
|
||||||
use rocket::request::{self, FromRequest, Outcome, Request};
|
use rocket::request::{self, FromRequest, Outcome, Request};
|
||||||
@@ -22,7 +22,7 @@ impl<'a, 'r> FromRequest<'a, 'r> for User {
|
|||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
if let Some(doc) = result {
|
if let Some(doc) = result {
|
||||||
Outcome::Success(from_bson(Bson::Document(doc)).unwrap())
|
Outcome::Success(from_document(doc).unwrap())
|
||||||
} else {
|
} else {
|
||||||
Outcome::Failure((Status::Forbidden, rauth::util::Error::InvalidSession))
|
Outcome::Failure((Status::Forbidden, rauth::util::Error::InvalidSession))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ use super::super::{get_collection, get_db};
|
|||||||
|
|
||||||
use crate::rocket::futures::StreamExt;
|
use crate::rocket::futures::StreamExt;
|
||||||
use log::info;
|
use log::info;
|
||||||
use mongodb::bson::{doc, from_bson, Bson};
|
use mongodb::bson::{Bson, doc, from_bson, from_document};
|
||||||
use mongodb::options::FindOptions;
|
use mongodb::options::FindOptions;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
@@ -23,7 +23,7 @@ pub async fn migrate_database() {
|
|||||||
|
|
||||||
if let Some(doc) = data {
|
if let Some(doc) = data {
|
||||||
let info: MigrationInfo =
|
let info: MigrationInfo =
|
||||||
from_bson(Bson::Document(doc)).expect("Failed to read migration information.");
|
from_document(doc).expect("Failed to read migration information.");
|
||||||
|
|
||||||
let revision = run_migrations(info.revision).await;
|
let revision = run_migrations(info.revision).await;
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ use crate::{
|
|||||||
util::result::{Error, Result},
|
util::result::{Error, Result},
|
||||||
};
|
};
|
||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
use mongodb::{bson::{Bson, doc, from_bson}, options::FindOptions};
|
use mongodb::{bson::{Bson, doc, from_bson, from_document}, options::FindOptions};
|
||||||
|
|
||||||
use super::websocket::is_online;
|
use super::websocket::is_online;
|
||||||
|
|
||||||
@@ -37,8 +37,8 @@ pub async fn generate_ready(mut user: User) -> Result<ClientboundNotification> {
|
|||||||
while let Some(result) = cursor.next().await {
|
while let Some(result) = cursor.next().await {
|
||||||
if let Ok(doc) = result {
|
if let Ok(doc) = result {
|
||||||
let mut user: User =
|
let mut user: User =
|
||||||
from_bson(Bson::Document(doc)).map_err(|_| Error::DatabaseError {
|
from_document(doc).map_err(|_| Error::DatabaseError {
|
||||||
operation: "from_bson",
|
operation: "from_document",
|
||||||
with: "user",
|
with: "user",
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
@@ -89,9 +89,9 @@ pub async fn generate_ready(mut user: User) -> Result<ClientboundNotification> {
|
|||||||
while let Some(result) = cursor.next().await {
|
while let Some(result) = cursor.next().await {
|
||||||
if let Ok(doc) = result {
|
if let Ok(doc) = result {
|
||||||
channels.push(
|
channels.push(
|
||||||
from_bson(Bson::Document(doc))
|
from_document(doc)
|
||||||
.map_err(|_| Error::DatabaseError {
|
.map_err(|_| Error::DatabaseError {
|
||||||
operation: "from_bson",
|
operation: "from_document",
|
||||||
with: "channel",
|
with: "channel",
|
||||||
})?
|
})?
|
||||||
);
|
);
|
||||||
|
|||||||
67
src/routes/channels/message_fetch.rs
Normal file
67
src/routes/channels/message_fetch.rs
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
use crate::database::*;
|
||||||
|
use crate::util::result::{Error, Result};
|
||||||
|
|
||||||
|
use futures::StreamExt;
|
||||||
|
use validator::Validate;
|
||||||
|
use rocket::request::Form;
|
||||||
|
use serde::{Serialize, Deserialize};
|
||||||
|
use rocket_contrib::json::JsonValue;
|
||||||
|
use mongodb::{bson::{doc, from_document}, options::FindOptions};
|
||||||
|
|
||||||
|
#[derive(Validate, Serialize, Deserialize, FromForm)]
|
||||||
|
pub struct Options {
|
||||||
|
#[validate(range(min = 1, max = 100))]
|
||||||
|
limit: Option<i64>,
|
||||||
|
#[validate(length(min = 26, max = 26))]
|
||||||
|
before: Option<String>,
|
||||||
|
#[validate(length(min = 26, max = 26))]
|
||||||
|
after: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[get("/<target>/messages?<options..>")]
|
||||||
|
pub async fn req(user: User, target: Ref, options: Form<Options>) -> Result<JsonValue> {
|
||||||
|
options.validate()
|
||||||
|
.map_err(|error| Error::FailedValidation { error })?;
|
||||||
|
|
||||||
|
let target = target.fetch_channel().await?;
|
||||||
|
|
||||||
|
let perm = permissions::channel::calculate(&user, &target).await;
|
||||||
|
if !perm.get_view() {
|
||||||
|
Err(Error::LabelMe)?
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut query = doc! { "channel": target.id() };
|
||||||
|
|
||||||
|
if let Some(before) = &options.before {
|
||||||
|
query.insert("_id", doc! { "$lt": before });
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(after) = &options.after {
|
||||||
|
query.insert("_id", doc! { "$gt": after });
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut cursor = get_collection("messages")
|
||||||
|
.find(
|
||||||
|
query,
|
||||||
|
FindOptions::builder()
|
||||||
|
.limit(options.limit.unwrap_or(50))
|
||||||
|
.sort(doc! {
|
||||||
|
"_id": -1
|
||||||
|
})
|
||||||
|
.build(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.map_err(|_| Error::DatabaseError { operation: "find", with: "messages" })?;
|
||||||
|
|
||||||
|
let mut messages = vec![];
|
||||||
|
while let Some(result) = cursor.next().await {
|
||||||
|
if let Ok(doc) = result {
|
||||||
|
messages.push(
|
||||||
|
from_document::<Message>(doc)
|
||||||
|
.map_err(|_| Error::DatabaseError { operation: "from_document", with: "message" })?
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(json!(messages))
|
||||||
|
}
|
||||||
@@ -3,11 +3,13 @@ use rocket::Route;
|
|||||||
mod fetch_channel;
|
mod fetch_channel;
|
||||||
mod delete_channel;
|
mod delete_channel;
|
||||||
mod message_send;
|
mod message_send;
|
||||||
|
mod message_fetch;
|
||||||
|
|
||||||
pub fn routes() -> Vec<Route> {
|
pub fn routes() -> Vec<Route> {
|
||||||
routes![
|
routes![
|
||||||
fetch_channel::req,
|
fetch_channel::req,
|
||||||
delete_channel::req,
|
delete_channel::req,
|
||||||
message_send::req
|
message_send::req,
|
||||||
|
message_fetch::req
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user