Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Transfer ownership #396

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions crates/core/models/src/v0/servers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ auto_derived!(
/// Must be enabled in order to show up on [Revolt Discover](https://rvlt.gg).
pub analytics: Option<bool>,

/// User id of the new owner
pub owner: Option<String>,

/// Fields to remove from server object
#[cfg_attr(feature = "validator", validate(length(min = 1)))]
pub remove: Option<Vec<FieldsServer>>,
Expand Down
23 changes: 23 additions & 0 deletions crates/delta/src/routes/servers/server_edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub async fn edit(
&& data.flags.is_none()
&& data.analytics.is_none()
&& data.discoverable.is_none()
&& data.owner.is_none()
&& data.remove.is_none()
{
return Ok(Json(server.into()));
Expand All @@ -57,6 +58,11 @@ pub async fn edit(
permissions.throw_if_lacking_channel_permission(ChannelPermission::ManageServer)?;
}

// Check we are the server owner or privileged if changing sensitive fields
if data.owner.is_some() && (user.id != server.owner && !user.privileged) {
return Err(create_error!(NotOwner));
}

// Check we are privileged if changing sensitive fields
if (data.flags.is_some() /*|| data.nsfw.is_some()*/ || data.discoverable.is_some())
&& !user.privileged
Expand All @@ -80,6 +86,7 @@ pub async fn edit(
// nsfw,
discoverable,
analytics,
owner,
remove,
} = data;

Expand All @@ -92,6 +99,7 @@ pub async fn edit(
// nsfw,
discoverable,
analytics,
owner: owner.clone(),
..Default::default()
};

Expand Down Expand Up @@ -148,6 +156,21 @@ pub async fn edit(
server.banner = partial.banner.clone();
}

// 5. Transfer ownership
if let Some(owner) = owner {
let owner_reference = Reference { id: owner.clone() };
// Check if member exists
owner_reference.as_member(db, &server.id).await?;
let owner_user = owner_reference.as_user(db).await?;

if owner_user.bot.is_some() {
return Err(create_error!(InvalidOperation));
}

server.owner = owner;
partial.owner = Some(server.owner.clone());
}

server
.update(
db,
Expand Down
Loading