Messaging: Upsert mentions into unreads.
Messaging: Allow multiple attachment upload.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use crate::database::*;
|
||||
use crate::notifications::events::ClientboundNotification;
|
||||
use crate::util::result::{Error, Result};
|
||||
use crate::database::*;
|
||||
|
||||
use mongodb::bson::doc;
|
||||
use mongodb::options::UpdateOptions;
|
||||
@@ -33,21 +33,20 @@ pub async fn req(user: User, target: Ref, message: Ref) -> Result<()> {
|
||||
"last_id": &message.id
|
||||
}
|
||||
},
|
||||
UpdateOptions::builder()
|
||||
.upsert(true)
|
||||
.build()
|
||||
UpdateOptions::builder().upsert(true).build(),
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError {
|
||||
operation: "update_one",
|
||||
with: "channel_unreads",
|
||||
})?;
|
||||
|
||||
|
||||
ClientboundNotification::ChannelAck {
|
||||
id: id.to_string(),
|
||||
user: user.id.clone(),
|
||||
message_id: message.id
|
||||
}.publish(user.id);
|
||||
|
||||
message_id: message.id,
|
||||
}
|
||||
.publish(user.id);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -99,8 +99,9 @@ pub async fn req(user: User, target: Ref) -> Result<()> {
|
||||
.publish(id.clone());
|
||||
|
||||
Content::SystemMessage(SystemMessage::UserLeft { id: user.id })
|
||||
.send_as_system(&target)
|
||||
.await.ok();
|
||||
.send_as_system(&target)
|
||||
.await
|
||||
.ok();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -117,7 +117,8 @@ pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
|
||||
by: user.id.clone(),
|
||||
})
|
||||
.send_as_system(&target)
|
||||
.await.ok();
|
||||
.await
|
||||
.ok();
|
||||
}
|
||||
|
||||
if let Some(_) = data.description {
|
||||
@@ -125,13 +126,15 @@ pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
|
||||
by: user.id.clone(),
|
||||
})
|
||||
.send_as_system(&target)
|
||||
.await.ok();
|
||||
.await
|
||||
.ok();
|
||||
}
|
||||
|
||||
if let Some(_) = data.icon {
|
||||
Content::SystemMessage(SystemMessage::ChannelIconChanged { by: user.id })
|
||||
.send_as_system(&target)
|
||||
.await.ok();
|
||||
.send_as_system(&target)
|
||||
.await
|
||||
.ok();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -60,7 +60,8 @@ pub async fn req(user: User, target: Ref, member: Ref) -> Result<()> {
|
||||
by: user.id,
|
||||
})
|
||||
.send_as_system(&channel)
|
||||
.await.ok();
|
||||
.await
|
||||
.ok();
|
||||
|
||||
Ok(())
|
||||
} else {
|
||||
|
||||
@@ -39,8 +39,7 @@ pub async fn req(user: User, info: Json<Data>) -> Result<JsonValue> {
|
||||
|
||||
for target in &set {
|
||||
match get_relationship(&user, target) {
|
||||
RelationshipStatus::Friend |
|
||||
RelationshipStatus::User => {},
|
||||
RelationshipStatus::Friend | RelationshipStatus::User => {}
|
||||
_ => {
|
||||
return Err(Error::NotFriends);
|
||||
}
|
||||
|
||||
@@ -56,7 +56,8 @@ pub async fn req(user: User, target: Ref, member: Ref) -> Result<()> {
|
||||
by: user.id,
|
||||
})
|
||||
.send_as_system(&channel)
|
||||
.await.ok();
|
||||
.await
|
||||
.ok();
|
||||
|
||||
Ok(())
|
||||
} else {
|
||||
|
||||
@@ -2,7 +2,7 @@ use crate::database::*;
|
||||
use crate::util::result::{Error, Result};
|
||||
|
||||
use chrono::Utc;
|
||||
use mongodb::bson::{Bson, DateTime, Document, doc};
|
||||
use mongodb::bson::{doc, Bson, DateTime, Document};
|
||||
use rocket_contrib::json::Json;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use validator::Validate;
|
||||
@@ -45,10 +45,7 @@ pub async fn req(user: User, target: Ref, msg: Ref, edit: Json<Data>) -> Result<
|
||||
|
||||
for embed in embeds {
|
||||
match embed {
|
||||
Embed::Website(_) |
|
||||
Embed::Image(_) |
|
||||
Embed::None => { }
|
||||
// Otherwise push to new_embeds.
|
||||
Embed::Website(_) | Embed::Image(_) | Embed::None => {} // Otherwise push to new_embeds.
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,7 +70,5 @@ pub async fn req(user: User, target: Ref, msg: Ref, edit: Json<Data>) -> Result<
|
||||
with: "message",
|
||||
})?;
|
||||
|
||||
message
|
||||
.publish_update(update)
|
||||
.await
|
||||
message.publish_update(update).await
|
||||
}
|
||||
|
||||
@@ -2,11 +2,11 @@ use crate::database::*;
|
||||
use crate::util::result::{Error, Result};
|
||||
|
||||
use mongodb::{bson::doc, options::FindOneOptions};
|
||||
use regex::Regex;
|
||||
use rocket_contrib::json::{Json, JsonValue};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use validator::Validate;
|
||||
use regex::Regex;
|
||||
use ulid::Ulid;
|
||||
use validator::Validate;
|
||||
|
||||
#[derive(Validate, Serialize, Deserialize)]
|
||||
pub struct Data {
|
||||
@@ -16,7 +16,7 @@ pub struct Data {
|
||||
#[validate(length(min = 1, max = 36))]
|
||||
nonce: String,
|
||||
#[validate(length(min = 1, max = 128))]
|
||||
attachment: Option<String>,
|
||||
attachments: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
@@ -29,7 +29,9 @@ pub async fn req(user: User, target: Ref, message: Json<Data>) -> Result<JsonVal
|
||||
.validate()
|
||||
.map_err(|error| Error::FailedValidation { error })?;
|
||||
|
||||
if message.content.len() == 0 && message.attachment.is_none() {
|
||||
if message.content.len() == 0
|
||||
&& (message.attachments.is_none() || message.attachments.as_ref().unwrap().len() == 0)
|
||||
{
|
||||
return Err(Error::EmptyMessage);
|
||||
}
|
||||
|
||||
@@ -63,10 +65,19 @@ pub async fn req(user: User, target: Ref, message: Json<Data>) -> Result<JsonVal
|
||||
}
|
||||
|
||||
let id = Ulid::new().to_string();
|
||||
let attachments = if let Some(attachment_id) = &message.attachment {
|
||||
Some(vec![
|
||||
File::find_and_use(attachment_id, "attachments", "message", &id).await?,
|
||||
])
|
||||
let attachments = if let Some(ids) = &message.attachments {
|
||||
// ! FIXME: move this to app config
|
||||
if ids.len() >= 5 {
|
||||
return Err(Error::TooManyAttachments)
|
||||
}
|
||||
|
||||
let mut attachments = vec![];
|
||||
for attachment_id in ids {
|
||||
attachments
|
||||
.push(File::find_and_use(attachment_id, "attachments", "message", &id).await?);
|
||||
}
|
||||
|
||||
Some(attachments)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
@@ -77,6 +88,7 @@ pub async fn req(user: User, target: Ref, message: Json<Data>) -> Result<JsonVal
|
||||
mentions.push(captures[1].to_string());
|
||||
}
|
||||
|
||||
|
||||
let msg = Message {
|
||||
id,
|
||||
channel: target.id().to_string(),
|
||||
@@ -87,7 +99,11 @@ pub async fn req(user: User, target: Ref, message: Json<Data>) -> Result<JsonVal
|
||||
nonce: Some(message.nonce.clone()),
|
||||
edited: None,
|
||||
embeds: None,
|
||||
mentions: if mentions.len() > 0 { Some(mentions) } else { None }
|
||||
mentions: if mentions.len() > 0 {
|
||||
Some(mentions)
|
||||
} else {
|
||||
None
|
||||
},
|
||||
};
|
||||
|
||||
msg.clone().publish(&target).await?;
|
||||
|
||||
Reference in New Issue
Block a user