feat: add group ownership transfer

This commit is contained in:
Paul Makles
2022-07-15 17:58:08 +01:00
parent 5aebda2d16
commit ab2af9b5e5
6 changed files with 54 additions and 3 deletions

View File

@@ -16,19 +16,18 @@ use validator::Validate;
pub struct DataEditChannel { pub struct DataEditChannel {
/// Channel name /// Channel name
#[validate(length(min = 1, max = 32))] #[validate(length(min = 1, max = 32))]
#[serde(skip_serializing_if = "Option::is_none")]
name: Option<String>, name: Option<String>,
/// Channel description /// Channel description
#[validate(length(min = 0, max = 1024))] #[validate(length(min = 0, max = 1024))]
#[serde(skip_serializing_if = "Option::is_none")]
description: Option<String>, description: Option<String>,
/// Group owner
owner: Option<String>,
/// Icon /// Icon
/// ///
/// Provide an Autumn attachment Id. /// Provide an Autumn attachment Id.
#[validate(length(min = 1, max = 128))] #[validate(length(min = 1, max = 128))]
icon: Option<String>, icon: Option<String>,
/// Whether this channel is age-restricted /// Whether this channel is age-restricted
#[serde(skip_serializing_if = "Option::is_none")]
nsfw: Option<bool>, nsfw: Option<bool>,
#[validate(length(min = 1))] #[validate(length(min = 1))]
remove: Option<Vec<FieldsChannel>>, remove: Option<Vec<FieldsChannel>>,
@@ -59,12 +58,47 @@ pub async fn req(
&& data.description.is_none() && data.description.is_none()
&& data.icon.is_none() && data.icon.is_none()
&& data.nsfw.is_none() && data.nsfw.is_none()
&& data.owner.is_none()
&& data.remove.is_none() && data.remove.is_none()
{ {
return Ok(Json(channel)); return Ok(Json(channel));
} }
let mut partial: PartialChannel = Default::default(); let mut partial: PartialChannel = Default::default();
// Transfer group ownership
if let Some(new_owner) = data.owner {
if let Channel::Group {
owner, recipients, ..
} = &mut channel
{
// Make sure we are the owner of this group
if owner != &user.id {
return Err(Error::NotOwner);
}
// Ensure user is part of group
if !recipients.contains(&new_owner) {
return Err(Error::NotInGroup);
}
// Transfer ownership
*owner = new_owner.to_string();
// Notify clients
SystemMessage::ChannelOwnershipChanged {
from: owner.to_string(),
to: new_owner,
}
} else {
return Err(Error::InvalidOperation);
}
.into_message(channel.id().to_string())
.create(db, &channel, None)
.await
.ok();
}
match &mut channel { match &mut channel {
Channel::Group { Channel::Group {
id, id,

View File

@@ -305,6 +305,15 @@ impl Channel {
vec![], vec![],
) )
.await?; .await?;
SystemMessage::ChannelOwnershipChanged {
from: owner.to_string(),
to: new_owner.into(),
}
.into_message(id.to_string())
.create(db, self, None)
.await
.ok();
} else { } else {
db.delete_channel(self).await?; db.delete_channel(self).await?;
return Ok(()); return Ok(());

View File

@@ -262,6 +262,9 @@ impl From<SystemMessage> for String {
"Channel description changed.".to_string() "Channel description changed.".to_string()
} }
SystemMessage::ChannelIconChanged { .. } => "Channel icon changed.".to_string(), SystemMessage::ChannelIconChanged { .. } => "Channel icon changed.".to_string(),
SystemMessage::ChannelOwnershipChanged { .. } => {
"Channel ownership changed.".to_string()
}
} }
} }
} }

View File

@@ -63,6 +63,8 @@ pub enum SystemMessage {
ChannelDescriptionChanged { by: String }, ChannelDescriptionChanged { by: String },
#[serde(rename = "channel_icon_changed")] #[serde(rename = "channel_icon_changed")]
ChannelIconChanged { by: String }, ChannelIconChanged { by: String },
#[serde(rename = "channel_ownership_changed")]
ChannelOwnershipChanged { from: String, to: String },
} }
/// Name and / or avatar override information /// Name and / or avatar override information

View File

@@ -72,6 +72,7 @@ pub enum Error {
}, },
NotElevated, NotElevated,
CannotGiveMissingPermissions, CannotGiveMissingPermissions,
NotOwner,
// ? General errors. // ? General errors.
DatabaseError { DatabaseError {
@@ -167,6 +168,7 @@ impl<'r> Responder<'r, 'static> for Error {
Error::MissingUserPermission { .. } => Status::Forbidden, Error::MissingUserPermission { .. } => Status::Forbidden,
Error::NotElevated => Status::Forbidden, Error::NotElevated => Status::Forbidden,
Error::CannotGiveMissingPermissions => Status::Forbidden, Error::CannotGiveMissingPermissions => Status::Forbidden,
Error::NotOwner => Status::Forbidden,
Error::DatabaseError { .. } => Status::InternalServerError, Error::DatabaseError { .. } => Status::InternalServerError,
Error::InternalError => Status::InternalServerError, Error::InternalError => Status::InternalServerError,

View File

@@ -50,6 +50,7 @@ services:
/usr/bin/mc mb minio/backgrounds; /usr/bin/mc mb minio/backgrounds;
/usr/bin/mc mb minio/icons; /usr/bin/mc mb minio/icons;
/usr/bin/mc mb minio/banners; /usr/bin/mc mb minio/banners;
/usr/bin/mc mb minio/emojis;
exit 0; exit 0;
" "
# File server (autumn) # File server (autumn)