Skip to content

Commit

Permalink
Move checkCanAccess to ChannelMixin
Browse files Browse the repository at this point in the history
  • Loading branch information
MinnDevelopment committed May 12, 2024
1 parent f37cc49 commit ffeabdd
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,6 @@ public PrivateChannelImpl setLatestMessageIdLong(long latestMessageId)
return this;
}

// -- Object --

@Override
public int hashCode()
{
Expand Down Expand Up @@ -195,4 +193,7 @@ private void checkBot()
if (getUser() != null && getUser().isBot())
throw new UnsupportedOperationException("Cannot send a private message between bots.");
}

@Override
public void checkCanAccess() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,7 @@ default RestAction<Void> delete()

// ---- State Accessors ----
T setName(String name);

// ---- Hooks ----
void checkCanAccess();
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public interface GuildMessageChannelMixin<T extends GuildMessageChannelMixin<T>>
@CheckReturnValue
default RestAction<Void> deleteMessagesByIds(@Nonnull Collection<String> messageIds)
{
checkCanAccess();
checkPermission(Permission.MESSAGE_MANAGE, "Must have MESSAGE_MANAGE in order to bulk delete messages in this channel regardless of author.");

if (messageIds.size() < 2 || messageIds.size() > 100)
Expand All @@ -66,6 +67,7 @@ default RestAction<Void> removeReactionById(@Nonnull String messageId, @Nonnull
Checks.notNull(emoji, "Emoji");
Checks.notNull(user, "User");

checkCanAccess();
if (!getJDA().getSelfUser().equals(user))
checkPermission(Permission.MESSAGE_MANAGE);

Expand All @@ -85,6 +87,7 @@ default RestAction<Void> clearReactionsById(@Nonnull String messageId)
{
Checks.isSnowflake(messageId, "Message ID");

checkCanAccess();
checkPermission(Permission.MESSAGE_MANAGE);

final Route.CompiledRoute route = Route.Messages.REMOVE_ALL_REACTIONS.compile(getId(), messageId);
Expand All @@ -98,6 +101,7 @@ default RestAction<Void> clearReactionsById(@Nonnull String messageId, @Nonnull
Checks.notNull(messageId, "Message ID");
Checks.notNull(emoji, "Emoji");

checkCanAccess();
checkPermission(Permission.MESSAGE_MANAGE);

Route.CompiledRoute route = Route.Messages.CLEAR_EMOJI_REACTIONS.compile(getId(), messageId, emoji.getAsReactionCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import net.dv8tion.jda.api.utils.messages.MessageCreateData;
import net.dv8tion.jda.api.utils.messages.MessageEditData;
import net.dv8tion.jda.api.utils.messages.MessagePollData;
import net.dv8tion.jda.internal.entities.channel.mixin.ChannelMixin;
import net.dv8tion.jda.internal.requests.RestActionImpl;
import org.jetbrains.annotations.NotNull;

Expand All @@ -51,12 +52,14 @@

public interface MessageChannelMixin<T extends MessageChannelMixin<T>> extends
MessageChannel,
MessageChannelUnion
MessageChannelUnion,
ChannelMixin<T>
{
// ---- Default implementations of interface ----
@Nonnull
default List<CompletableFuture<Void>> purgeMessages(@Nonnull List<? extends Message> messages)
{
checkCanAccess();
if (messages == null || messages.isEmpty())
return Collections.emptyList();

Expand All @@ -80,6 +83,7 @@ default List<CompletableFuture<Void>> purgeMessages(@Nonnull List<? extends Mess
@Nonnull
default List<CompletableFuture<Void>> purgeMessagesById(@Nonnull long... messageIds)
{
checkCanAccess();
if (messageIds == null || messageIds.length == 0)
return Collections.emptyList();

Expand Down Expand Up @@ -207,6 +211,7 @@ default RestAction<Message> retrieveMessageById(@Nonnull String messageId)
@CheckReturnValue
default AuditableRestAction<Void> deleteMessageById(@Nonnull String messageId)
{
checkCanAccess();
//We don't know if this is a Message sent by us or another user, so we can't run checks for Permission.MESSAGE_MANAGE
return MessageChannelUnion.super.deleteMessageById(messageId);
}
Expand Down Expand Up @@ -263,6 +268,7 @@ default MessageHistory.MessageRetrieveAction getHistoryFromBeginning(int limit)
@CheckReturnValue
default RestAction<Void> sendTyping()
{
checkCanAccess();
return MessageChannelUnion.super.sendTyping();
}

Expand Down Expand Up @@ -310,6 +316,7 @@ default RestAction<Void> unpinMessageById(@Nonnull String messageId)
@CheckReturnValue
default RestAction<List<Message>> retrievePinnedMessages()
{
checkCanAccess();
return MessageChannelUnion.super.retrievePinnedMessages();
}

Expand Down

0 comments on commit ffeabdd

Please sign in to comment.