Skip to content

Commit

Permalink
refactor: change Channel::id to return a reference
Browse files Browse the repository at this point in the history
  • Loading branch information
Zomatree committed Jul 30, 2024
1 parent b45ae2c commit 4fc46f7
Show file tree
Hide file tree
Showing 18 changed files with 37 additions and 35 deletions.
4 changes: 2 additions & 2 deletions crates/core/database/src/models/channels/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,13 +417,13 @@ impl Channel {
}

/// Clone this channel's id
pub fn id(&self) -> String {
pub fn id(&self) -> &str {
match self {
Channel::DirectMessage { id, .. }
| Channel::Group { id, .. }
| Channel::SavedMessages { id, .. }
| Channel::TextChannel { id, .. }
| Channel::VoiceChannel { id, .. } => id.clone(),
| Channel::VoiceChannel { id, .. } => id,
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/core/database/src/models/channels/ops/mongodb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ impl AbstractChannels for MongoDb {
.await?;

// Delete the channel itself
query!(self, delete_one_by_id, COL, &channel.id()).map(|_| ())
query!(self, delete_one_by_id, COL, channel.id()).map(|_| ())
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/core/database/src/models/channels/ops/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl AbstractChannels for ReferenceDb {
/// Insert a new channel in the database
async fn insert_channel(&self, channel: &Channel) -> Result<()> {
let mut channels = self.channels.lock().await;
if let Entry::Vacant(entry) = channels.entry(channel.id()) {
if let Entry::Vacant(entry) = channels.entry(channel.id().to_string()) {
entry.insert(channel.clone());
Ok(())
} else {
Expand Down Expand Up @@ -148,7 +148,7 @@ impl AbstractChannels for ReferenceDb {
// Delete a channel
async fn delete_channel(&self, channel: &Channel) -> Result<()> {
let mut channels = self.channels.lock().await;
if channels.remove(&channel.id()).is_some() {
if channels.remove(channel.id()).is_some() {
Ok(())
} else {
Err(create_error!(NotFound))
Expand Down
4 changes: 2 additions & 2 deletions crates/core/database/src/models/messages/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ impl Message {
let message_id = Ulid::new().to_string();
let mut message = Message {
id: message_id.clone(),
channel: channel.id(),
channel: channel.id().to_string(),
masquerade: data.masquerade.map(|masquerade| masquerade.into()),
interactions: data
.interactions
Expand Down Expand Up @@ -479,7 +479,7 @@ impl Message {
PushNotification::from(
self.clone().into_model(None, None),
Some(author),
&channel.id(),
channel.id(),
)
.await,
)
Expand Down
2 changes: 1 addition & 1 deletion crates/core/database/src/models/servers/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ impl Server {
vec![]
};

server.channels = channels.iter().map(|c| c.id()).collect();
server.channels = channels.iter().map(|c| c.id().to_string()).collect();
db.insert_server(&server).await?;
Ok((server, channels))
}
Expand Down
8 changes: 5 additions & 3 deletions crates/delta/src/routes/bots/invite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ mod test {
.client
.post(format!("/bots/{}/invite", bot.id))
.header(ContentType::JSON)
.body(json!(v0::InviteBotDestination::Group { group: group.id() }).to_string())
.body(json!(v0::InviteBotDestination::Group {
group: group.id().to_string()
}).to_string())
.header(Header::new("x-session-token", session.token.to_string()))
.dispatch()
.await;
Expand All @@ -102,8 +104,8 @@ mod test {
drop(response);

let event = harness
.wait_for_event(&group.id(), |event| match event {
EventV1::ChannelGroupJoin { id, .. } => id == &group.id(),
.wait_for_event(group.id(), |event| match event {
EventV1::ChannelGroupJoin { id, .. } => id == group.id(),
_ => false,
})
.await;
Expand Down
2 changes: 1 addition & 1 deletion crates/delta/src/routes/channels/channel_ack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ mod test {

let event = harness
.wait_for_event(&format!("{}!", user.id), |event| match event {
EventV1::ChannelAck { id, .. } => id == &group.id(),
EventV1::ChannelAck { id, .. } => id == group.id(),
_ => false,
})
.await;
Expand Down
4 changes: 2 additions & 2 deletions crates/delta/src/routes/channels/channel_delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ mod test {
drop(response);

harness
.wait_for_event(&group.id(), |event| match event {
EventV1::ChannelDelete { id, .. } => id == &group.id(),
.wait_for_event(group.id(), |event| match event {
EventV1::ChannelDelete { id, .. } => id == group.id(),
_ => false,
})
.await;
Expand Down
6 changes: 3 additions & 3 deletions crates/delta/src/routes/channels/group_add_member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ mod test {
.await;

let event = harness
.wait_for_event(&group.id(), |event| match event {
EventV1::ChannelGroupJoin { id, .. } => id == &group.id(),
.wait_for_event(group.id(), |event| match event {
EventV1::ChannelGroupJoin { id, .. } => id == group.id(),
_ => false,
})
.await;
Expand All @@ -113,7 +113,7 @@ mod test {
_ => unreachable!(),
};

let message = harness.wait_for_message(&group.id()).await;
let message = harness.wait_for_message(group.id()).await;

assert_eq!(
message.system,
Expand Down
6 changes: 3 additions & 3 deletions crates/delta/src/routes/channels/group_remove_member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ mod test {
.await;

let event = harness
.wait_for_event(&group.id(), |event| match event {
EventV1::ChannelGroupJoin { id, .. } => id == &group.id(),
.wait_for_event(group.id(), |event| match event {
EventV1::ChannelGroupJoin { id, .. } => id == group.id(),
_ => false,
})
.await;
Expand All @@ -117,7 +117,7 @@ mod test {
_ => unreachable!(),
};

let message = harness.wait_for_message(&group.id()).await;
let message = harness.wait_for_message(group.id()).await;

assert_eq!(
message.system,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub async fn clear_reactions(
.throw_if_lacking_channel_permission(ChannelPermission::ManageMessages)?;

// Fetch relevant message
let mut message = msg.as_message_in_channel(db, &channel.id()).await?;
let mut message = msg.as_message_in_channel(db, channel.id()).await?;

// Clear reactions
message
Expand Down
2 changes: 1 addition & 1 deletion crates/delta/src/routes/channels/message_edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub async fn edit(

permissions.throw_if_lacking_channel_permission(ChannelPermission::SendMessage)?;

let mut message = msg.as_message_in_channel(db, &channel.id()).await?;
let mut message = msg.as_message_in_channel(db, channel.id()).await?;
if message.author != user.id {
return Err(create_error!(CannotEditMessage));
}
Expand Down
10 changes: 5 additions & 5 deletions crates/delta/src/routes/channels/message_pin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub async fn message_pin(
.await
.throw_if_lacking_channel_permission(ChannelPermission::ManageMessages)?;

let mut message = msg.as_message_in_channel(db, &channel.id()).await?;
let mut message = msg.as_message_in_channel(db, channel.id()).await?;

if message.pinned.unwrap_or_default() {
return Err(create_error!(AlreadyPinned))
Expand All @@ -40,7 +40,7 @@ pub async fn message_pin(
id: message.id.clone(),
by: user.id.clone()
}
.into_message(channel.id())
.into_message(channel.id().to_string())
.send(
db,
MessageAuthor::System {
Expand Down Expand Up @@ -115,7 +115,7 @@ mod test {
assert_eq!(response.status(), Status::NoContent);
drop(response);

harness.wait_for_event(&channel.id(), |event| {
harness.wait_for_event(channel.id(), |event| {
match event {
EventV1::Message(message) => {
match &message.system {
Expand All @@ -131,11 +131,11 @@ mod test {
}
}).await;

harness.wait_for_event(&channel.id(), |event| {
harness.wait_for_event(channel.id(), |event| {
match event {
EventV1::MessageUpdate { id, channel: channel_id, data, .. } => {
assert_eq!(id, &message.id);
assert_eq!(channel_id, &channel.id());
assert_eq!(channel_id, channel.id());
assert_eq!(data.pinned, Some(true));

true
Expand Down
2 changes: 1 addition & 1 deletion crates/delta/src/routes/channels/message_react.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub async fn react_message(
.throw_if_lacking_channel_permission(ChannelPermission::React)?;

// Fetch relevant message
let message = msg.as_message_in_channel(db, &channel.id()).await?;
let message = msg.as_message_in_channel(db, channel.id()).await?;

// Add the reaction
message
Expand Down
8 changes: 4 additions & 4 deletions crates/delta/src/routes/channels/message_unpin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub async fn message_unpin(
.await
.throw_if_lacking_channel_permission(ChannelPermission::ManageMessages)?;

let mut message = msg.as_message_in_channel(db, &channel.id()).await?;
let mut message = msg.as_message_in_channel(db, channel.id()).await?;

if !message.pinned.unwrap_or_default() {
return Err(create_error!(NotPinned))
Expand All @@ -37,7 +37,7 @@ pub async fn message_unpin(
id: message.id.clone(),
by: user.id.clone()
}
.into_message(channel.id())
.into_message(channel.id().to_string())
.send(
db,
MessageAuthor::System {
Expand Down Expand Up @@ -123,7 +123,7 @@ mod test {
assert_eq!(response.status(), Status::NoContent);
drop(response);

harness.wait_for_event(&channel.id(), |event| {
harness.wait_for_event(channel.id(), |event| {
match event {
EventV1::Message(message) => {
match &message.system {
Expand All @@ -139,7 +139,7 @@ mod test {
}
}).await;

harness.wait_for_event(&channel.id(), |event| {
harness.wait_for_event(channel.id(), |event| {
match event {
EventV1::MessageUpdate { id, clear, .. } => {
assert_eq!(&message.id, id);
Expand Down
2 changes: 1 addition & 1 deletion crates/delta/src/routes/channels/message_unreact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub async fn unreact_message(
}

// Fetch relevant message
let message = msg.as_message_in_channel(db, &channel.id()).await?;
let message = msg.as_message_in_channel(db, channel.id()).await?;

// Check if we should wipe all of this reaction
if remove_all {
Expand Down
2 changes: 1 addition & 1 deletion crates/delta/src/routes/channels/webhook_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub async fn create_webhook(
id: webhook_id,
name: data.name,
avatar,
channel_id: channel.id(),
channel_id: channel.id().to_string(),
permissions: *DEFAULT_WEBHOOK_PERMISSIONS,
token: Some(nanoid::nanoid!(64)),
};
Expand Down
2 changes: 1 addition & 1 deletion crates/delta/src/routes/channels/webhook_fetch_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub async fn fetch_webhooks(
.throw_if_lacking_channel_permission(ChannelPermission::ViewChannel)?;

Ok(Json(
db.fetch_webhooks_for_channel(&channel.id())
db.fetch_webhooks_for_channel(channel.id())
.await?
.into_iter()
.map(|v| v.into())
Expand Down

0 comments on commit 4fc46f7

Please sign in to comment.