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

Add additional methods to get the target of an audit-log entry #2575

Open
wants to merge 2 commits into
base: master
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
71 changes: 65 additions & 6 deletions src/main/java/net/dv8tion/jda/api/audit/AuditLogEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.ISnowflake;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.ScheduledEvent;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.entities.Webhook;
import net.dv8tion.jda.api.entities.automod.AutoModRule;
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel;
import net.dv8tion.jda.api.events.guild.GuildAuditLogEntryCreateEvent;
import net.dv8tion.jda.internal.entities.GuildImpl;
import net.dv8tion.jda.internal.entities.UserImpl;
import net.dv8tion.jda.internal.entities.WebhookImpl;
import net.dv8tion.jda.internal.utils.Checks;
import net.dv8tion.jda.internal.utils.EntityString;

Expand All @@ -47,15 +51,15 @@ public class AuditLogEntry implements ISnowflake
protected final long userId;
protected final GuildImpl guild;
protected final UserImpl user;
protected final WebhookImpl webhook;
protected final ISnowflake target;
protected final String reason;

protected final Map<String, AuditLogChange> changes;
protected final Map<String, Object> options;
protected final ActionType type;
protected final int rawType;

public AuditLogEntry(ActionType type, int rawType, long id, long userId, long targetId, GuildImpl guild, UserImpl user, WebhookImpl webhook,
public AuditLogEntry(ActionType type, int rawType, long id, long userId, long targetId, GuildImpl guild, UserImpl user, ISnowflake target,
String reason, Map<String, AuditLogChange> changes, Map<String, Object> options)
{
this.type = type;
Expand All @@ -65,7 +69,7 @@ public AuditLogEntry(ActionType type, int rawType, long id, long userId, long ta
this.targetId = targetId;
this.guild = guild;
this.user = user;
this.webhook = webhook;
this.target = target;
this.reason = reason;
this.changes = changes != null && !changes.isEmpty()
? Collections.unmodifiableMap(changes)
Expand Down Expand Up @@ -105,16 +109,71 @@ public String getTargetId()
{
return Long.toUnsignedString(targetId);
}

/**
* The {@link AutoModRule} that the target id of this audit-log entry refers to.
*
* @return Possibly-null AutoModRule instance
*/
@Nullable
public AutoModRule getAutoModRule()
{
return type.getTargetType() != TargetType.AUTO_MODERATION_RULE ? null : (AutoModRule) target;
}

/**
* The {@link GuildChannel} that the target id of this audit-log entry refers to.
*
* @return Possibly-null GuildChannel instance
*/
@Nullable
public GuildChannel getGuildChannel()
{
return type.getTargetType() != TargetType.CHANNEL ? null : (GuildChannel) target;
}

/**
* The {@link Member} that the target id of this audit-log entry refers to.
*
* @return Possibly-null Member instance
*/
@Nullable
public Member getMember()
{
return type.getTargetType() != TargetType.MEMBER ? null : (Member) target;
}

/**
* The {@link ScheduledEvent} that the target id of this audit-log entry refers to.
*
* @return Possibly-null ScheduledEvent instance
*/
@Nullable
public ScheduledEvent getScheduledEvent()
{
return type.getTargetType() != TargetType.SCHEDULED_EVENT ? null : (ScheduledEvent) target;
}

/**
* The {@link ThreadChannel} that the target id of this audit-log entry refers to.
*
* @return Possibly-null ThreadChannel instance
*/
@Nullable
public ThreadChannel getThread()
{
return type.getTargetType() != TargetType.THREAD ? null : (ThreadChannel) target;
}

/**
* The {@link net.dv8tion.jda.api.entities.Webhook Webhook} that the target id of this audit-log entry refers to
* The {@link net.dv8tion.jda.api.entities.Webhook Webhook} that the target id of this audit-log entry refers to.
*
* @return Possibly-null Webhook instance
*/
@Nullable
public Webhook getWebhook()
{
return webhook;
return type.getTargetType() != TargetType.WEBHOOK ? null : (Webhook) target;
}

/**
Expand Down
37 changes: 34 additions & 3 deletions src/main/java/net/dv8tion/jda/internal/entities/EntityBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import net.dv8tion.jda.api.utils.data.DataArray;
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.internal.JDAImpl;
import net.dv8tion.jda.internal.entities.automod.AutoModRuleImpl;
import net.dv8tion.jda.internal.entities.channel.concrete.*;
import net.dv8tion.jda.internal.entities.channel.mixin.attribute.IPermissionContainerMixin;
import net.dv8tion.jda.internal.entities.channel.mixin.attribute.IPostContainerMixin;
Expand Down Expand Up @@ -2513,7 +2514,8 @@ public ApplicationTeam createApplicationTeam(DataObject object)
return new ApplicationTeamImpl(iconId, members, id, ownerId);
}

public AuditLogEntry createAuditLogEntry(GuildImpl guild, DataObject entryJson, DataObject userJson, DataObject webhookJson)
public AuditLogEntry createAuditLogEntry(GuildImpl guild, DataObject entryJson, TLongObjectMap<DataObject> userMap, TLongObjectMap<DataObject> webhookMap,
TLongObjectMap<DataObject> threadMap, TLongObjectMap<DataObject> scheduledEventMap, TLongObjectMap<DataObject> autoModerationRulesMap)
{
final long targetId = entryJson.getLong("target_id", 0);
final long userId = entryJson.getLong("user_id", 0);
Expand All @@ -2523,11 +2525,40 @@ public AuditLogEntry createAuditLogEntry(GuildImpl guild, DataObject entryJson,
final DataObject options = entryJson.isNull("options") ? null : entryJson.getObject("options");
final String reason = entryJson.getString("reason", null);

final DataObject userJson = userMap == null ? null : userMap.get(userId);
final UserImpl user = userJson == null ? null : createUser(userJson);
final WebhookImpl webhook = webhookJson == null ? null : createWebhook(webhookJson);
final Set<AuditLogChange> changesList;
final ActionType type = ActionType.from(typeKey);

ISnowflake target;
DataObject targetJson;
switch (type.getTargetType()) {
case AUTO_MODERATION_RULE:
targetJson = autoModerationRulesMap == null ? null : autoModerationRulesMap.get(targetId);
target = targetJson == null ? null : AutoModRuleImpl.fromData(guild, targetJson);
break;
case CHANNEL:
target = guild.getGuildChannelById(targetId);
break;
case MEMBER:
target = guild.getMemberById(targetId);
break;
case SCHEDULED_EVENT:
targetJson = scheduledEventMap == null ? null : scheduledEventMap.get(targetId);
target = targetJson == null ? null : createScheduledEvent(guild, targetJson);
break;
case THREAD:
targetJson = threadMap == null ? null : threadMap.get(targetId);
target = targetJson == null ? null : createThreadChannel(guild, targetJson, guild.getIdLong());
break;
case WEBHOOK:
targetJson = webhookMap == null ? null : webhookMap.get(targetId);
target = targetJson == null ? null : createWebhook(targetJson);
break;
default:
target = null;
}

if (changes != null)
{
changesList = new HashSet<>(changes.length());
Expand All @@ -2547,7 +2578,7 @@ public AuditLogEntry createAuditLogEntry(GuildImpl guild, DataObject entryJson,
CaseInsensitiveMap<String, Object> optionMap = options != null
? new CaseInsensitiveMap<>(options.toMap()) : null;

return new AuditLogEntry(type, typeKey, id, userId, targetId, guild, user, webhook, reason, changeMap, optionMap);
return new AuditLogEntry(type, typeKey, id, userId, targetId, guild, user, target, reason, changeMap, optionMap);
}

public AuditLogChange createAuditLogChange(DataObject change)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected Long handleInternally(DataObject content)
return null;
}

AuditLogEntry entry = api.getEntityBuilder().createAuditLogEntry(guild, content, null, null);
AuditLogEntry entry = api.getEntityBuilder().createAuditLogEntry(guild, content, null, null, null, null, null);

api.handleEvent(
new GuildAuditLogEntryCreateEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package net.dv8tion.jda.internal.requests.restaction.pagination;

import gnu.trove.map.TLongObjectMap;
import gnu.trove.map.hash.TLongObjectHashMap;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.audit.ActionType;
import net.dv8tion.jda.api.audit.AuditLogEntry;
Expand All @@ -33,11 +32,13 @@
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.internal.entities.EntityBuilder;
import net.dv8tion.jda.internal.entities.GuildImpl;
import net.dv8tion.jda.internal.utils.Helpers;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.function.ToLongFunction;

public class AuditLogPaginationActionImpl
extends PaginationActionImpl<AuditLogEntry, AuditLogPaginationAction>
Expand Down Expand Up @@ -105,35 +106,29 @@ protected Route.CompiledRoute finalizeRoute()
protected void handleSuccess(Response response, Request<List<AuditLogEntry>> request)
{
DataObject obj = response.getObject();
DataArray entries = obj.getArray("audit_log_entries");
DataArray users = obj.getArray("users");
DataArray webhooks = obj.getArray("webhooks");
DataArray entries = obj.getArray("audit_log_entries");
DataArray threads = obj.getArray("threads");
DataArray events = obj.getArray("guild_scheduled_events");
DataArray automodRules = obj.getArray("auto_moderation_rules");

List<AuditLogEntry> list = new ArrayList<>(entries.length());
EntityBuilder builder = api.getEntityBuilder();

TLongObjectMap<DataObject> userMap = new TLongObjectHashMap<>();
for (int i = 0; i < users.length(); i++)
{
DataObject user = users.getObject(i);
userMap.put(user.getLong("id"), user);
}

TLongObjectMap<DataObject> webhookMap = new TLongObjectHashMap<>();
for (int i = 0; i < webhooks.length(); i++)
{
DataObject webhook = webhooks.getObject(i);
webhookMap.put(webhook.getLong("id"), webhook);
}
ToLongFunction<DataObject> getIdFunction = dataObject -> dataObject.getLong("id");
TLongObjectMap<DataObject> userMap = Helpers.convertToMap(getIdFunction, users);
TLongObjectMap<DataObject> webhookMap = Helpers.convertToMap(getIdFunction, webhooks);
TLongObjectMap<DataObject> threadMap = Helpers.convertToMap(getIdFunction, threads);
TLongObjectMap<DataObject> eventMap = Helpers.convertToMap(getIdFunction, events);
TLongObjectMap<DataObject> automodRuleMap = Helpers.convertToMap(getIdFunction, automodRules);

for (int i = 0; i < entries.length(); i++)
{
try
{
DataObject entry = entries.getObject(i);
DataObject user = userMap.get(entry.getLong("user_id", 0));
DataObject webhook = webhookMap.get(entry.getLong("target_id", 0));
AuditLogEntry result = builder.createAuditLogEntry((GuildImpl) guild, entry, user, webhook);
AuditLogEntry result = builder.createAuditLogEntry((GuildImpl) guild, entry, userMap, webhookMap, threadMap, eventMap, automodRuleMap);
list.add(result);
}
catch (ParsingException | NullPointerException e)
Expand Down
Loading