use of de.btobastian.javacord.listener.channel.ChannelChangeTopicListener in project Javacord by BtoBastian.
the class ChannelUpdateHandler method handleServerTextChannel.
/**
* Handles the server text channels.
*
* @param packet The packet (the "d"-object).
* @param server The server of the channel.
*/
private void handleServerTextChannel(JSONObject packet, Server server) {
final Channel channel = server.getChannelById(packet.getString("id"));
if (channel == null) {
// no channel with the given id was found
return;
}
String name = packet.getString("name");
if (!channel.getName().equals(name)) {
final String oldName = channel.getName();
((ImplChannel) channel).setName(name);
listenerExecutorService.submit(new Runnable() {
@Override
public void run() {
List<ChannelChangeNameListener> listeners = api.getListeners(ChannelChangeNameListener.class);
synchronized (listeners) {
for (ChannelChangeNameListener listener : listeners) {
try {
listener.onChannelChangeName(api, channel, oldName);
} catch (Throwable t) {
logger.warn("Uncaught exception in ChannelChangeNameListener!", t);
}
}
}
}
});
}
String topic = packet.get("topic") == null ? null : packet.get("topic").toString();
if ((channel.getTopic() != null && topic == null) || (channel.getTopic() == null && topic != null) || (channel.getTopic() != null && !channel.getTopic().equals(topic))) {
final String oldTopic = channel.getTopic();
((ImplChannel) channel).setTopic(topic);
listenerExecutorService.submit(new Runnable() {
@Override
public void run() {
List<ChannelChangeTopicListener> listeners = api.getListeners(ChannelChangeTopicListener.class);
synchronized (listeners) {
for (ChannelChangeTopicListener listener : listeners) {
try {
listener.onChannelChangeTopic(api, channel, oldTopic);
} catch (Throwable t) {
logger.warn("Uncaught exception in ChannelChangeTopicListener!", t);
}
}
}
}
});
}
int position = packet.getInt("position");
if (channel.getPosition() != position) {
final int oldPosition = channel.getPosition();
((ImplChannel) channel).setPosition(position);
listenerExecutorService.submit(new Runnable() {
@Override
public void run() {
List<ChannelChangePositionListener> listeners = api.getListeners(ChannelChangePositionListener.class);
synchronized (listeners) {
for (ChannelChangePositionListener listener : listeners) {
try {
listener.onChannelChangePosition(api, channel, oldPosition);
} catch (Throwable t) {
logger.warn("Uncaught exception in ChannelChangePositionListener!", t);
}
}
}
}
});
}
JSONArray permissionOverwrites = packet.getJSONArray("permission_overwrites");
for (int i = 0; i < permissionOverwrites.length(); i++) {
JSONObject permissionOverwrite = permissionOverwrites.getJSONObject(i);
int allow = permissionOverwrite.getInt("allow");
int deny = permissionOverwrite.getInt("deny");
String id = permissionOverwrite.getString("id");
String type = permissionOverwrite.getString("type");
// permissions overwritten by users
if (type.equals("member")) {
final User user;
try {
user = api.getUserById(id).get();
} catch (InterruptedException | ExecutionException e) {
continue;
}
ImplPermissions permissions = new ImplPermissions(allow, deny);
final Permissions oldPermissions = channel.getOverwrittenPermissions(user);
if (!oldPermissions.equals(permissions)) {
((ImplChannel) channel).setOverwrittenPermissions(user, permissions);
listenerExecutorService.submit(new Runnable() {
@Override
public void run() {
List<UserChangeOverwrittenPermissionsListener> listeners = api.getListeners(UserChangeOverwrittenPermissionsListener.class);
synchronized (listeners) {
for (UserChangeOverwrittenPermissionsListener listener : listeners) {
try {
listener.onUserChangeOverwrittenPermissions(api, user, channel, oldPermissions);
} catch (Throwable t) {
logger.warn("Uncaught exception in UserChangeOverwrittenPermissionsListener!", t);
}
}
}
}
});
}
}
// permissions overwritten by roles
if (type.equals("role")) {
final Role role = channel.getServer().getRoleById(id);
ImplPermissions permissions = new ImplPermissions(allow, deny);
final Permissions oldPermissions = role.getOverwrittenPermissions(channel);
if (!permissions.equals(oldPermissions)) {
((ImplRole) role).setOverwrittenPermissions(channel, permissions);
listenerExecutorService.submit(new Runnable() {
@Override
public void run() {
List<RoleChangeOverwrittenPermissionsListener> listeners = api.getListeners(RoleChangeOverwrittenPermissionsListener.class);
synchronized (listeners) {
for (RoleChangeOverwrittenPermissionsListener listener : listeners) {
try {
listener.onRoleChangeOverwrittenPermissions(api, role, channel, oldPermissions);
} catch (Throwable t) {
logger.warn("Uncaught exception in RoleChangeOverwrittenPermissionsListener!", t);
}
}
}
}
});
}
}
}
String parentId = packet.isNull("parent_id") ? null : packet.getString("parent_id");
if (!Objects.deepEquals(channel.getParentId(), parentId)) {
((ImplChannel) channel).setParentId(parentId);
}
}
Aggregations