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: save used invite #330

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions crates/core/database/src/models/server_members/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ auto_derived_partial!(
/// Time at which this user joined the server
pub joined_at: Timestamp,

/// Invite used by member to join server
#[serde(skip_serializing_if = "Option::is_none")]
pub used_invite: Option<String>,

/// Member's nickname
#[serde(skip_serializing_if = "Option::is_none")]
pub nickname: Option<String>,
Expand Down Expand Up @@ -69,6 +73,7 @@ impl Default for Member {
avatar: None,
roles: vec![],
timeout: None,
used_invite: None,
}
}
}
Expand All @@ -81,6 +86,7 @@ impl Member {
server: &Server,
user: &User,
channels: Option<Vec<Channel>>,
used_invite: Option<String>,
) -> Result<Vec<Channel>> {
if db.fetch_ban(&server.id, &user.id).await.is_ok() {
return Err(create_error!(Banned));
Expand All @@ -95,6 +101,7 @@ impl Member {
server: server.id.to_string(),
user: user.id.to_string(),
},
used_invite,
..Default::default()
};

Expand Down
4 changes: 4 additions & 0 deletions crates/core/database/src/util/bridge/v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,7 @@ impl From<crate::Member> for Member {
avatar: value.avatar.map(|f| f.into()),
roles: value.roles,
timeout: value.timeout,
used_invite: value.used_invite,
}
}
}
Expand All @@ -629,6 +630,7 @@ impl From<Member> for crate::Member {
avatar: value.avatar.map(|f| f.into()),
roles: value.roles,
timeout: value.timeout,
used_invite: value.used_invite,
}
}
}
Expand All @@ -642,6 +644,7 @@ impl From<crate::PartialMember> for PartialMember {
avatar: value.avatar.map(|f| f.into()),
roles: value.roles,
timeout: value.timeout,
used_invite: value.used_invite,
}
}
}
Expand All @@ -655,6 +658,7 @@ impl From<PartialMember> for crate::PartialMember {
avatar: value.avatar.map(|f| f.into()),
roles: value.roles,
timeout: value.timeout,
used_invite: value.used_invite,
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions crates/core/models/src/v0/server_members.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ auto_derived_partial!(
/// Time at which this user joined the server
pub joined_at: Timestamp,

/// Invite used by member to join server
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub used_invite: Option<String>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would expose the used invite to any member of the server, which is unintended behaviour and could be abused.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to remove this field, or return Default in this case?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean if it were removed there's no way it would get to a client where it'd be useful, we would need to have some sort of permission (let's just suppose KickMembers for now) that it checks for when transforming members from database to API models.

Luckily for us, in most cases where we fetch members, we already check whether we can access the server (so permissions are already calculated), information about whether we have a specific permission (let's say KickMembers as before) can be passed through to where we transform the models which controls the visibility of the used_invite field (this can later be re-used for any other fields we might want to keep private to moderators).


/// Member's nickname
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub nickname: Option<String>,
Expand Down
2 changes: 1 addition & 1 deletion crates/delta/src/routes/bots/invite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub async fn invite_bot(
.await
.throw_if_lacking_channel_permission(ChannelPermission::ManageServer)?;

Member::create(db, &server, &bot_user, None)
Member::create(db, &server, &bot_user, None, None)
.await
.map(|_| EmptyResponse)
}
Expand Down
4 changes: 2 additions & 2 deletions crates/delta/src/routes/invites/invite_join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ pub async fn join(

let invite = target.as_invite(db).await?;
match &invite {
Invite::Server { server, .. } => {
Invite::Server { server, code, .. } => {
let server = db.fetch_server(server).await?;
let channels = Member::create(db, &server, &user, None).await?;
let channels = Member::create(db, &server, &user, None, Some(code.to_string())).await?;
Ok(Json(InviteJoinResponse::Server {
channels: channels.into_iter().map(|c| c.into()).collect(),
server: server.into(),
Expand Down
2 changes: 1 addition & 1 deletion crates/delta/src/routes/servers/server_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub async fn create_server(
user.can_acquire_server(db).await?;

let (server, channels) = Server::create(db, data, &user, true).await?;
let channels = Member::create(db, &server, &user, Some(channels)).await?;
let channels = Member::create(db, &server, &user, Some(channels), None).await?;

Ok(Json(v0::CreateServerLegacyResponse {
server: server.into(),
Expand Down