/
AuditLogPaginationActionImpl.java
161 lines (140 loc) · 5.19 KB
/
AuditLogPaginationActionImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.UserSnowflake;
import net.dv8tion.jda.api.exceptions.InsufficientPermissionException;
import net.dv8tion.jda.api.exceptions.ParsingException;
import net.dv8tion.jda.api.requests.Request;
import net.dv8tion.jda.api.requests.Response;
import net.dv8tion.jda.api.requests.restaction.pagination.AuditLogPaginationAction;
import net.dv8tion.jda.api.utils.data.DataArray;
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.requests.Route;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
public class AuditLogPaginationActionImpl
extends PaginationActionImpl<AuditLogEntry, AuditLogPaginationAction>
implements AuditLogPaginationAction
{
protected final Guild guild;
// filters
protected ActionType type = null;
protected String userId = null;
public AuditLogPaginationActionImpl(Guild guild)
{
super(guild.getJDA(), Route.Guilds.GET_AUDIT_LOGS.compile(guild.getId()), 1, 100, 100);
if (!guild.getSelfMember().hasPermission(Permission.VIEW_AUDIT_LOGS))
throw new InsufficientPermissionException(guild, Permission.VIEW_AUDIT_LOGS);
this.guild = guild;
super.order(PaginationOrder.BACKWARD);
}
@Nonnull
@Override
public AuditLogPaginationActionImpl type(ActionType type)
{
this.type = type;
return this;
}
@Nonnull
@Override
public AuditLogPaginationActionImpl user(UserSnowflake user)
{
this.userId = user == null ? null : user.getId();
return this;
}
@Nonnull
@Override
public Guild getGuild()
{
return guild;
}
@Nonnull
@Override
public EnumSet<PaginationOrder> getSupportedOrders()
{
return EnumSet.of(PaginationOrder.BACKWARD, PaginationOrder.FORWARD);
}
@Override
protected Route.CompiledRoute finalizeRoute()
{
Route.CompiledRoute route = super.finalizeRoute();
if (type != null)
route = route.withQueryParams("action_type", String.valueOf(type.getKey()));
if (userId != null)
route = route.withQueryParams("user_id", userId);
return route;
}
@Override
protected void handleSuccess(Response response, Request<List<AuditLogEntry>> request)
{
DataObject obj = response.getObject();
DataArray users = obj.getArray("users");
DataArray webhooks = obj.getArray("webhooks");
DataArray entries = obj.getArray("audit_log_entries");
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);
}
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);
list.add(result);
}
catch (ParsingException | NullPointerException e)
{
LOG.warn("Encountered exception in AuditLogPagination", e);
}
}
if (!list.isEmpty())
{
if (this.useCache)
this.cached.addAll(list);
this.last = list.get(list.size() - 1);
this.lastKey = last.getIdLong();
}
request.onSuccess(list);
}
@Override
protected long getKey(AuditLogEntry it)
{
return it.getIdLong();
}
}