Add a way to send messages with attachments.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use crate::database::*;
|
||||
use crate::util::result::{Error, Result};
|
||||
|
||||
use mongodb::bson::doc;
|
||||
use mongodb::{bson::{doc, from_document}, options::FindOneOptions};
|
||||
use rocket_contrib::json::{Json, JsonValue};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use ulid::Ulid;
|
||||
@@ -14,6 +14,8 @@ pub struct Data {
|
||||
// Maximum length of 36 allows both ULIDs and UUIDs.
|
||||
#[validate(length(min = 1, max = 36))]
|
||||
nonce: String,
|
||||
#[validate(length(min = 1, max = 128))]
|
||||
attachment: Option<String>,
|
||||
}
|
||||
|
||||
#[post("/<target>/messages", data = "<message>")]
|
||||
@@ -37,7 +39,9 @@ pub async fn req(user: User, target: Ref, message: Json<Data>) -> Result<JsonVal
|
||||
doc! {
|
||||
"nonce": &message.nonce
|
||||
},
|
||||
None,
|
||||
FindOneOptions::builder()
|
||||
.projection(doc! { "_id": 1 })
|
||||
.build()
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError {
|
||||
@@ -49,12 +53,56 @@ pub async fn req(user: User, target: Ref, message: Json<Data>) -> Result<JsonVal
|
||||
Err(Error::DuplicateNonce)?
|
||||
}
|
||||
|
||||
let id = Ulid::new().to_string();
|
||||
let attachments = get_collection("attachments");
|
||||
let attachment = if let Some(attachment_id) = &message.attachment {
|
||||
if let Some(doc) = attachments
|
||||
.find_one(
|
||||
doc! {
|
||||
"_id": attachment_id,
|
||||
"message_id": {
|
||||
"$exists": false
|
||||
}
|
||||
},
|
||||
None
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError {
|
||||
operation: "find_one",
|
||||
with: "attachment",
|
||||
})? {
|
||||
let attachment = from_document::<File>(doc)
|
||||
.map_err(|_| Error::DatabaseError { operation: "from_document", with: "attachment" })?;
|
||||
|
||||
attachments.update_one(
|
||||
doc! {
|
||||
"_id": &attachment.id
|
||||
},
|
||||
doc! {
|
||||
"$set": {
|
||||
"message_id": &id
|
||||
}
|
||||
},
|
||||
None
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError { operation: "update_one", with: "attachment" })?;
|
||||
|
||||
Some(attachment)
|
||||
} else {
|
||||
return Err(Error::UnknownAttachment)
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let msg = Message {
|
||||
id: Ulid::new().to_string(),
|
||||
id,
|
||||
channel: target.id().to_string(),
|
||||
author: user.id,
|
||||
|
||||
content: message.content.clone(),
|
||||
attachment,
|
||||
nonce: Some(message.nonce.clone()),
|
||||
edited: None,
|
||||
};
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::util::variables::{
|
||||
DISABLE_REGISTRATION, EXTERNAL_WS_URL, HCAPTCHA_SITEKEY, INVITE_ONLY, USE_EMAIL, USE_HCAPTCHA,
|
||||
DISABLE_REGISTRATION, EXTERNAL_WS_URL, HCAPTCHA_SITEKEY, INVITE_ONLY, USE_EMAIL, USE_HCAPTCHA, USE_AUTUMN, AUTUMN_URL
|
||||
};
|
||||
|
||||
use mongodb::bson::doc;
|
||||
@@ -16,7 +16,11 @@ pub async fn root() -> JsonValue {
|
||||
"key": HCAPTCHA_SITEKEY.to_string()
|
||||
},
|
||||
"email": *USE_EMAIL,
|
||||
"invite_only": *INVITE_ONLY
|
||||
"invite_only": *INVITE_ONLY,
|
||||
"autumn": {
|
||||
"enabled": *USE_AUTUMN,
|
||||
"url": *AUTUMN_URL
|
||||
}
|
||||
},
|
||||
"ws": *EXTERNAL_WS_URL,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user