Rename fetch to query, add fetch by id.
This commit is contained in:
@@ -46,6 +46,10 @@ impl Ref {
|
|||||||
pub async fn fetch_channel(&self) -> Result<Channel> {
|
pub async fn fetch_channel(&self) -> Result<Channel> {
|
||||||
self.fetch("channels").await
|
self.fetch("channels").await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn fetch_message(&self) -> Result<Message> {
|
||||||
|
self.fetch("messages").await
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl User {
|
impl User {
|
||||||
|
|||||||
@@ -1,76 +1,17 @@
|
|||||||
use crate::database::*;
|
use crate::database::*;
|
||||||
use crate::util::result::{Error, Result};
|
use crate::util::result::{Error, Result};
|
||||||
|
|
||||||
use futures::StreamExt;
|
|
||||||
use mongodb::{
|
|
||||||
bson::{doc, from_document},
|
|
||||||
options::FindOptions,
|
|
||||||
};
|
|
||||||
use rocket::request::Form;
|
|
||||||
use rocket_contrib::json::JsonValue;
|
use rocket_contrib::json::JsonValue;
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use validator::Validate;
|
|
||||||
|
|
||||||
#[derive(Validate, Serialize, Deserialize, FromForm)]
|
#[get("/<target>/messages/<msg>")]
|
||||||
pub struct Options {
|
pub async fn req(user: User, target: Ref, msg: Ref) -> Result<JsonValue> {
|
||||||
#[validate(range(min = 1, max = 100))]
|
let channel = target.fetch_channel().await?;
|
||||||
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..>")]
|
let perm = permissions::channel::calculate(&user, &channel).await;
|
||||||
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() {
|
if !perm.get_view() {
|
||||||
Err(Error::LabelMe)?
|
Err(Error::LabelMe)?
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut query = doc! { "channel": target.id() };
|
let message = msg.fetch_message().await?;
|
||||||
|
Ok(json!(message))
|
||||||
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))
|
|
||||||
}
|
}
|
||||||
|
|||||||
76
src/routes/channels/message_query.rs
Normal file
76
src/routes/channels/message_query.rs
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
use crate::database::*;
|
||||||
|
use crate::util::result::{Error, Result};
|
||||||
|
|
||||||
|
use futures::StreamExt;
|
||||||
|
use mongodb::{
|
||||||
|
bson::{doc, from_document},
|
||||||
|
options::FindOptions,
|
||||||
|
};
|
||||||
|
use rocket::request::Form;
|
||||||
|
use rocket_contrib::json::JsonValue;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use validator::Validate;
|
||||||
|
|
||||||
|
#[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,6 +3,7 @@ use rocket::Route;
|
|||||||
mod delete_channel;
|
mod delete_channel;
|
||||||
mod fetch_channel;
|
mod fetch_channel;
|
||||||
mod message_fetch;
|
mod message_fetch;
|
||||||
|
mod message_query;
|
||||||
mod message_send;
|
mod message_send;
|
||||||
|
|
||||||
pub fn routes() -> Vec<Route> {
|
pub fn routes() -> Vec<Route> {
|
||||||
@@ -10,6 +11,7 @@ pub fn routes() -> Vec<Route> {
|
|||||||
fetch_channel::req,
|
fetch_channel::req,
|
||||||
delete_channel::req,
|
delete_channel::req,
|
||||||
message_send::req,
|
message_send::req,
|
||||||
|
message_query::req,
|
||||||
message_fetch::req
|
message_fetch::req
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user