Skip to content

Commit 29575b8

Browse files
committed
fix(move): move command dit not synchronize permissions with the destination category
1 parent 8510539 commit 29575b8

2 files changed

Lines changed: 36 additions & 6 deletions

File tree

rustmail/src/commands/move_thread/common.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::prelude::config::*;
22
use crate::prelude::db::*;
3-
use serenity::all::{ChannelId, CommandInteraction, Context, EditChannel, GuildId, Message};
3+
use crate::prelude::utils::*;
4+
use serenity::all::{ChannelId, CommandInteraction, Context, EditChannel, GuildChannel, GuildId, Message};
5+
use crate::errors::{ModmailError, ModmailResult};
46

57
pub async fn is_in_thread(msg: &Message, pool: &sqlx::SqlitePool) -> bool {
68
let channel_id = msg.channel_id.to_string();
@@ -46,21 +48,27 @@ pub async fn move_channel_to_category_by_msg(
4648
ctx: &Context,
4749
msg: &Message,
4850
category_id: ChannelId,
49-
) -> Result<serenity::model::channel::GuildChannel, serenity::Error> {
51+
) -> ModmailResult<GuildChannel> {
52+
let permissions = get_category_permissions_overwrites(ctx, category_id).await?;
53+
5054
msg.channel_id
51-
.edit(&ctx.http, EditChannel::new().category(category_id))
55+
.edit(&ctx.http, EditChannel::new().category(category_id).permissions(permissions))
5256
.await
57+
.map_err(ModmailError::from)
5358
}
5459

5560
pub async fn move_channel_to_category_by_command_option(
5661
ctx: &Context,
5762
command: &CommandInteraction,
5863
category_id: ChannelId,
59-
) -> Result<serenity::model::channel::GuildChannel, serenity::Error> {
64+
) -> ModmailResult<GuildChannel> {
65+
let permissions = get_category_permissions_overwrites(ctx, category_id).await?;
66+
6067
command
6168
.channel_id
62-
.edit(&ctx.http, EditChannel::new().category(category_id))
69+
.edit(&ctx.http, EditChannel::new().category(category_id).permissions(permissions))
6370
.await
71+
.map_err(ModmailError::from)
6472
}
6573

6674
pub fn find_best_match_category(

rustmail/src/utils/command/category.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use serenity::all::CommandInteraction;
1+
use serenity::all::{ChannelId, CommandInteraction, PermissionOverwrite};
22
use serenity::all::{Channel, Context, PermissionOverwriteType, RoleId};
3+
use crate::errors::{CommandError, ModmailError, ModmailResult};
34

45
pub async fn get_category_id_from_command(ctx: &Context, command: &CommandInteraction) -> String {
56
match command.channel_id.to_channel(&ctx.http).await {
@@ -58,3 +59,24 @@ pub async fn get_required_permissions_channel_from_command(
5859
_ => 0u64,
5960
}
6061
}
62+
63+
pub async fn get_category_permissions_overwrites(
64+
ctx: &Context,
65+
category_id: ChannelId,
66+
) -> ModmailResult<Vec<PermissionOverwrite>> {
67+
let category = match category_id.to_channel(&ctx.http).await {
68+
Ok(channel) => {
69+
channel.category()
70+
}
71+
Err(..) => {
72+
return Err(ModmailError::Command(CommandError::NotInThread()));
73+
}
74+
};
75+
76+
let permissions = match category {
77+
Some(category) => category.permission_overwrites.clone(),
78+
None => return Err(ModmailError::Command(CommandError::NotInThread())),
79+
};
80+
81+
Ok(permissions)
82+
}

0 commit comments

Comments
 (0)