71 lines
1.9 KiB
Rust
71 lines
1.9 KiB
Rust
use crate::util::result::{Error, Result};
|
|
use crate::util::variables::MAX_GROUP_SIZE;
|
|
use crate::{database::*, notifications::events::ClientboundNotification};
|
|
|
|
use mongodb::bson::doc;
|
|
|
|
#[put("/<target>/recipients/<member>")]
|
|
pub async fn req(user: User, target: Ref, member: Ref) -> Result<()> {
|
|
if get_relationship(&user, &member.id) != RelationshipStatus::Friend {
|
|
Err(Error::NotFriends)?
|
|
}
|
|
|
|
let channel = target.fetch_channel().await?;
|
|
let perm = permissions::PermissionCalculator::new(&user)
|
|
.with_channel(&channel)
|
|
.for_channel()
|
|
.await?;
|
|
|
|
if !perm.get_invite_others() {
|
|
Err(Error::MissingPermission)?
|
|
}
|
|
|
|
if let Channel::Group { id, recipients, .. } = &channel {
|
|
if recipients.len() >= *MAX_GROUP_SIZE {
|
|
Err(Error::GroupTooLarge {
|
|
max: *MAX_GROUP_SIZE,
|
|
})?
|
|
}
|
|
|
|
if recipients.iter().find(|x| *x == &member.id).is_some() {
|
|
Err(Error::AlreadyInGroup)?
|
|
}
|
|
|
|
get_collection("channels")
|
|
.update_one(
|
|
doc! {
|
|
"_id": &id
|
|
},
|
|
doc! {
|
|
"$push": {
|
|
"recipients": &member.id
|
|
}
|
|
},
|
|
None,
|
|
)
|
|
.await
|
|
.map_err(|_| Error::DatabaseError {
|
|
operation: "update_one",
|
|
with: "channel",
|
|
})?;
|
|
|
|
ClientboundNotification::ChannelGroupJoin {
|
|
id: id.clone(),
|
|
user: member.id.clone(),
|
|
}
|
|
.publish(id.clone());
|
|
|
|
Content::SystemMessage(SystemMessage::UserAdded {
|
|
id: member.id,
|
|
by: user.id,
|
|
})
|
|
.send_as_system(&channel)
|
|
.await
|
|
.ok();
|
|
|
|
Ok(())
|
|
} else {
|
|
Err(Error::InvalidOperation)
|
|
}
|
|
}
|