-
-
Notifications
You must be signed in to change notification settings - Fork 724
/
ChannelUpdateHandler.java
344 lines (315 loc) · 14.7 KB
/
ChannelUpdateHandler.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
* Copyright 2015-2017 Austin Keener & Michael Ritter & Florian Spieß
*
* 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.core.handle;
import gnu.trove.TDecorators;
import gnu.trove.list.TLongList;
import gnu.trove.list.linked.TLongLinkedList;
import gnu.trove.map.TLongObjectMap;
import net.dv8tion.jda.client.entities.impl.GroupImpl;
import net.dv8tion.jda.client.events.group.update.GroupUpdateIconEvent;
import net.dv8tion.jda.client.events.group.update.GroupUpdateNameEvent;
import net.dv8tion.jda.client.events.group.update.GroupUpdateOwnerEvent;
import net.dv8tion.jda.core.entities.*;
import net.dv8tion.jda.core.entities.impl.*;
import net.dv8tion.jda.core.events.channel.text.update.TextChannelUpdateNameEvent;
import net.dv8tion.jda.core.events.channel.text.update.TextChannelUpdatePermissionsEvent;
import net.dv8tion.jda.core.events.channel.text.update.TextChannelUpdatePositionEvent;
import net.dv8tion.jda.core.events.channel.text.update.TextChannelUpdateTopicEvent;
import net.dv8tion.jda.core.events.channel.voice.update.*;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class ChannelUpdateHandler extends SocketHandler
{
public ChannelUpdateHandler(JDAImpl api)
{
super(api);
}
@Override
protected Long handleInternally(JSONObject content)
{
ChannelType type = ChannelType.fromId(content.getInt("type"));
if (type == ChannelType.GROUP)
{
handleGroup(content);
return null;
}
List<IPermissionHolder> changed = new ArrayList<>();
List<IPermissionHolder> contained = new ArrayList<>();
final long channelId = content.getLong("id");
final int position = content.getInt("position");
final String name = content.getString("name");
JSONArray permOverwrites = content.getJSONArray("permission_overwrites");
switch (type)
{
case TEXT:
{
String topic = content.isNull("topic") ? null : content.getString("topic");
TextChannelImpl textChannel = (TextChannelImpl) api.getTextChannelMap().get(channelId);
if (textChannel == null)
{
api.getEventCache().cache(EventCache.Type.CHANNEL, channelId, () -> handle(responseNumber, allContent));
EventCache.LOG.debug("CHANNEL_UPDATE attempted to update a TextChannel that does not exist. JSON: " + content);
return null;
}
//If any properties changed, update the values and fire the proper events.
final String oldName = textChannel.getName();
final String oldTopic = textChannel.getTopic();
final int oldPosition = textChannel.getPositionRaw();
if (!StringUtils.equals(oldName, name))
{
textChannel.setName(name);
api.getEventManager().handle(
new TextChannelUpdateNameEvent(
api, responseNumber,
textChannel, oldName));
}
if (!StringUtils.equals(oldTopic, topic))
{
textChannel.setTopic(topic);
api.getEventManager().handle(
new TextChannelUpdateTopicEvent(
api, responseNumber,
textChannel, oldTopic));
}
if (oldPosition != position)
{
textChannel.setRawPosition(position);
api.getEventManager().handle(
new TextChannelUpdatePositionEvent(
api, responseNumber,
textChannel, oldPosition));
}
applyPermissions(textChannel, content, permOverwrites, contained, changed);
//If this update modified permissions in any way.
if (!changed.isEmpty())
{
api.getEventManager().handle(
new TextChannelUpdatePermissionsEvent(
api, responseNumber,
textChannel, changed));
}
break; //Finish the TextChannelUpdate case
}
case VOICE:
{
VoiceChannelImpl voiceChannel = (VoiceChannelImpl) api.getVoiceChannelMap().get(channelId);
int userLimit = content.getInt("user_limit");
int bitrate = content.getInt("bitrate");
if (voiceChannel == null)
{
api.getEventCache().cache(EventCache.Type.CHANNEL, channelId, () -> handle(responseNumber, allContent));
EventCache.LOG.debug("CHANNEL_UPDATE attempted to update a VoiceChannel that does not exist. JSON: " + content);
return null;
}
//If any properties changed, update the values and fire the proper events.
final String oldName = voiceChannel.getName();
final int oldPosition = voiceChannel.getPositionRaw();
final int oldLimit = voiceChannel.getUserLimit();
final int oldBitrate = voiceChannel.getBitrate();
if (!StringUtils.equals(oldName, name))
{
voiceChannel.setName(name);
api.getEventManager().handle(
new VoiceChannelUpdateNameEvent(
api, responseNumber,
voiceChannel, oldName));
}
if (oldPosition != position)
{
voiceChannel.setRawPosition(position);
api.getEventManager().handle(
new VoiceChannelUpdatePositionEvent(
api, responseNumber,
voiceChannel, oldPosition));
}
if (oldLimit != userLimit)
{
voiceChannel.setUserLimit(userLimit);
api.getEventManager().handle(
new VoiceChannelUpdateUserLimitEvent(
api, responseNumber,
voiceChannel, oldLimit));
}
if (oldBitrate != bitrate)
{
voiceChannel.setBitrate(bitrate);
api.getEventManager().handle(
new VoiceChannelUpdateBitrateEvent(
api, responseNumber,
voiceChannel, oldBitrate));
}
applyPermissions(voiceChannel, content, permOverwrites, contained, changed);
//If this update modified permissions in any way.
if (!changed.isEmpty())
{
api.getEventManager().handle(
new VoiceChannelUpdatePermissionsEvent(
api, responseNumber,
voiceChannel, changed));
}
break; //Finish the VoiceChannelUpdate case
}
default:
throw new IllegalArgumentException("CHANNEL_UPDATE provided an unrecognized channel type JSON: " + content);
}
return null;
}
private long getIdLong(IPermissionHolder permHolder)
{
if (permHolder instanceof Member)
return ((Member) permHolder).getUser().getIdLong();
else
return ((Role) permHolder).getIdLong();
}
private void applyPermissions(AbstractChannelImpl<?> channel, JSONObject content,
JSONArray permOverwrites, List<IPermissionHolder> contained, List<IPermissionHolder> changed)
{
//Determines if a new PermissionOverride was created or updated.
//If a PermissionOverride was created or updated it stores it in the proper Map to be reported by the Event.
for (int i = 0; i < permOverwrites.length(); i++)
{
handlePermissionOverride(permOverwrites.getJSONObject(i), channel, content, changed, contained);
}
//Check if any overrides were deleted because of this event.
//Get the current overrides. (we copy them to a new list because the Set returned is backed by the Map, meaning our removes would remove from the Map. Not good.
//Loop through all of the json defined overrides. If we find a match, remove the User or Role from our lists.
//Any entries remaining in these lists after this for loop is over will be removed from the Channel's overrides.
final TLongList toRemove = new TLongLinkedList();
final TLongObjectMap<PermissionOverride> overridesMap = channel.getOverrideMap();
TDecorators.wrap(overridesMap.keySet()).stream()
.map(id -> mapPermissionHolder(id, channel.getGuild()))
.filter(Objects::nonNull)
.filter(permHolder -> !contained.contains(permHolder))
.forEach(permHolder ->
{
changed.add(permHolder);
toRemove.add(getIdLong(permHolder));
});
toRemove.forEach((id) ->
{
overridesMap.remove(id);
return true;
});
}
private IPermissionHolder mapPermissionHolder(long id, Guild guild)
{
final Role holder = guild.getRoleById(id);
return holder == null ? guild.getMemberById(id) : holder;
}
private void handlePermissionOverride(JSONObject override, AbstractChannelImpl<?> channel, JSONObject content,
List<IPermissionHolder> changedPermHolders, List<IPermissionHolder> containedPermHolders)
{
final long id = override.getLong("id");
final long allow = override.getLong("allow");
final long deny = override.getLong("deny");
final IPermissionHolder permHolder;
switch (override.getString("type"))
{
case "role":
{
permHolder = channel.getGuild().getRoleById(id);
if (permHolder == null)
{
api.getEventCache().cache(EventCache.Type.ROLE, id, () ->
handlePermissionOverride(override, channel, content, changedPermHolders, containedPermHolders));
EventCache.LOG.debug("CHANNEL_UPDATE attempted to create or update a PermissionOverride for a Role that doesn't exist! RoleId: " + id + " JSON: " + content);
return;
}
break;
}
case "member":
{
permHolder = channel.getGuild().getMemberById(id);
if (permHolder == null)
{
api.getEventCache().cache(EventCache.Type.USER, id, () ->
handlePermissionOverride(override, channel, content, changedPermHolders, containedPermHolders));
EventCache.LOG.debug("CHANNEL_UPDATE attempted to create or update a PermissionOverride for Member that doesn't exist in this Guild! MemberId: " + id + " JSON: " + content);
return;
}
break;
}
default:
throw new IllegalArgumentException("CHANNEL_UPDATE provided an unrecognized PermissionOverride type. JSON: " + content);
}
PermissionOverrideImpl permOverride = (PermissionOverrideImpl) channel.getOverrideMap().get(id);
if (permOverride == null) //Created
{
api.getEntityBuilder().createPermissionOverride(override, channel);
changedPermHolders.add(permHolder);
}
else if (permOverride.getAllowedRaw() != allow || permOverride.getDeniedRaw() != deny) //Updated
{
permOverride.setAllow(allow);
permOverride.setDeny(deny);
changedPermHolders.add(permHolder);
}
containedPermHolders.add(permHolder);
}
private void handleGroup(JSONObject content)
{
final long groupId = content.getLong("id");
final long ownerId = content.getLong("owner_id");
final String name = content.isNull("name") ? null : content.getString("name");
final String iconId = content.isNull("icon") ? null : content.getString("icon");
GroupImpl group = (GroupImpl) api.asClient().getGroupById(groupId);
if (group == null)
{
api.getEventCache().cache(EventCache.Type.CHANNEL, groupId, () -> handle(responseNumber, allContent));
EventCache.LOG.debug("Received CHANNEL_UPDATE for a group that was not yet cached. JSON: " + content);
return;
}
final User owner = group.getUserMap().get(ownerId);
final User oldOwner = group.getOwner();
final String oldName = group.getName();
final String oldIconId = group.getIconId();
if (owner == null)
{
EventCache.LOG.warn("Received CHANNEL_UPDATE for a group with an owner_id for a user that is not cached. owner_id: " + ownerId);
}
else
{
if (!Objects.equals(owner, oldOwner))
{
group.setOwner(owner);
api.getEventManager().handle(
new GroupUpdateOwnerEvent(
api, responseNumber,
group, oldOwner));
}
}
if (!Objects.equals(name, oldName))
{
group.setName(name);
api.getEventManager().handle(
new GroupUpdateNameEvent(
api, responseNumber,
group, oldName));
}
if (!Objects.equals(iconId, oldIconId))
{
group.setIconId(iconId);
api.getEventManager().handle(
new GroupUpdateIconEvent(
api, responseNumber,
group, oldIconId));
}
}
}