use of org.javacord.api.entity.permission.Permissions in project Javacord by BtoBastian.
the class RegularServerChannelImpl method getEffectiveOverwrittenPermissions.
@Override
public Permissions getEffectiveOverwrittenPermissions(User user) {
PermissionsBuilder builder = new PermissionsBuilder(PermissionsImpl.EMPTY_PERMISSIONS);
Server server = getServer();
Role everyoneRole = server.getEveryoneRole();
Permissions everyoneRolePermissionOverwrites = getOverwrittenPermissions(everyoneRole);
for (PermissionType type : PermissionType.values()) {
if (everyoneRolePermissionOverwrites.getState(type) == PermissionState.DENIED) {
builder.setState(type, PermissionState.DENIED);
}
if (everyoneRolePermissionOverwrites.getState(type) == PermissionState.ALLOWED) {
builder.setState(type, PermissionState.ALLOWED);
}
}
List<Role> rolesOfUser = new ArrayList<>(server.getRoles(user));
rolesOfUser.remove(everyoneRole);
List<Permissions> permissionOverwrites = rolesOfUser.stream().map(this::getOverwrittenPermissions).collect(Collectors.toList());
for (Permissions permissions : permissionOverwrites) {
for (PermissionType type : PermissionType.values()) {
if (permissions.getState(type) == PermissionState.DENIED) {
builder.setState(type, PermissionState.DENIED);
}
}
}
for (Permissions permissions : permissionOverwrites) {
for (PermissionType type : PermissionType.values()) {
if (permissions.getState(type) == PermissionState.ALLOWED) {
builder.setState(type, PermissionState.ALLOWED);
}
}
}
for (PermissionType type : PermissionType.values()) {
Permissions permissions = overwrittenUserPermissions.getOrDefault(user.getId(), PermissionsImpl.EMPTY_PERMISSIONS);
if (permissions.getState(type) == PermissionState.DENIED) {
builder.setState(type, PermissionState.DENIED);
}
if (permissions.getState(type) == PermissionState.ALLOWED) {
builder.setState(type, PermissionState.ALLOWED);
}
}
return builder.build();
}
use of org.javacord.api.entity.permission.Permissions in project Javacord by BtoBastian.
the class RegularServerChannel method getEffectivePermissions.
/**
* Gets the effective permissions of a user in this channel.
* The returned permission object will only have {@link PermissionState#ALLOWED} and
* {@link PermissionState#DENIED} states!
* It takes into account global permissions and the effective overwritten permissions of a user.
* Remember, that some permissions affect others!
* E.g. a user who has {@link PermissionType#SEND_MESSAGES} but not {@link PermissionType#READ_MESSAGES} cannot
* send messages, even though he has the {@link PermissionType#SEND_MESSAGES} permission.
*
* @param user The user.
* @return The effective permissions of the user in this channel.
*/
default Permissions getEffectivePermissions(User user) {
if (getServer().isOwner(user)) {
return getServer().getPermissions(user);
}
PermissionsBuilder builder = new PermissionsBuilder(getServer().getPermissions(user));
Permissions effectiveOverwrittenPermissions = getEffectiveOverwrittenPermissions(user);
Arrays.stream(PermissionType.values()).filter(type -> effectiveOverwrittenPermissions.getState(type) != PermissionState.UNSET).forEachOrdered(type -> builder.setState(type, effectiveOverwrittenPermissions.getState(type)));
Arrays.stream(PermissionType.values()).filter(type -> builder.getState(type) == PermissionState.UNSET).forEachOrdered(type -> builder.setState(type, PermissionState.DENIED));
return builder.build();
}
use of org.javacord.api.entity.permission.Permissions in project Javacord by BtoBastian.
the class ChannelUpdateHandler method handleRegularServerChannel.
private void handleRegularServerChannel(JsonNode jsonChannel) {
long channelId = jsonChannel.get("id").asLong();
Optional<RegularServerChannel> optionalChannel = api.getRegularServerChannelById(channelId);
if (!optionalChannel.isPresent()) {
LoggerUtil.logMissingChannel(logger, channelId);
return;
}
RegularServerChannelImpl channel = (RegularServerChannelImpl) optionalChannel.get();
ServerImpl server = (ServerImpl) channel.getServer();
final AtomicBoolean areYouAffected = new AtomicBoolean(false);
ChannelCategory oldCategory = channel.asCategorizable().flatMap(Categorizable::getCategory).orElse(null);
ChannelCategory newCategory = jsonChannel.hasNonNull("parent_id") ? channel.getServer().getChannelCategoryById(jsonChannel.get("parent_id").asLong(-1)).orElse(null) : null;
final RegularServerChannelImpl regularServerChannel = (RegularServerChannelImpl) channel;
final int oldRawPosition = regularServerChannel.getRawPosition();
final int newRawPosition = jsonChannel.get("position").asInt();
if (oldRawPosition != newRawPosition || !Objects.deepEquals(oldCategory, newCategory)) {
final int oldPosition = regularServerChannel.getPosition();
if (regularServerChannel instanceof ServerTextChannelImpl) {
((ServerTextChannelImpl) regularServerChannel).setParentId(newCategory == null ? -1 : newCategory.getId());
} else if (regularServerChannel instanceof ServerVoiceChannelImpl) {
((ServerVoiceChannelImpl) regularServerChannel).setParentId(newCategory == null ? -1 : newCategory.getId());
}
regularServerChannel.setRawPosition(newRawPosition);
final int newPosition = regularServerChannel.getPosition();
final ServerChannelChangePositionEvent event = new ServerChannelChangePositionEventImpl(regularServerChannel, newPosition, oldPosition, newRawPosition, oldRawPosition, newCategory, oldCategory);
if (server.isReady()) {
api.getEventDispatcher().dispatchServerChannelChangePositionEvent((DispatchQueueSelector) regularServerChannel.getServer(), regularServerChannel.getServer(), regularServerChannel, event);
}
}
Collection<Long> rolesWithOverwrittenPermissions = new HashSet<>();
Collection<Long> usersWithOverwrittenPermissions = new HashSet<>();
if (jsonChannel.has("permission_overwrites") && !jsonChannel.get("permission_overwrites").isNull()) {
for (JsonNode permissionOverwriteJson : jsonChannel.get("permission_overwrites")) {
Permissions oldOverwrittenPermissions;
ConcurrentHashMap<Long, Permissions> overwrittenPermissions;
long entityId = permissionOverwriteJson.get("id").asLong();
Optional<DiscordEntity> entity;
switch(permissionOverwriteJson.get("type").asInt()) {
case 0:
Role role = server.getRoleById(entityId).orElseThrow(() -> new IllegalStateException("Received channel update event with unknown role!"));
entity = Optional.of(role);
oldOverwrittenPermissions = regularServerChannel.getOverwrittenPermissions(role);
overwrittenPermissions = regularServerChannel.getInternalOverwrittenRolePermissions();
rolesWithOverwrittenPermissions.add(entityId);
break;
case 1:
oldOverwrittenPermissions = regularServerChannel.getOverwrittenUserPermissions().getOrDefault(entityId, PermissionsImpl.EMPTY_PERMISSIONS);
entity = api.getCachedUserById(entityId).map(DiscordEntity.class::cast);
overwrittenPermissions = regularServerChannel.getInternalOverwrittenUserPermissions();
usersWithOverwrittenPermissions.add(entityId);
break;
default:
throw new IllegalStateException("Permission overwrite object with unknown type: " + permissionOverwriteJson);
}
long allow = permissionOverwriteJson.get("allow").asLong(0);
long deny = permissionOverwriteJson.get("deny").asLong(0);
Permissions newOverwrittenPermissions = new PermissionsImpl(allow, deny);
if (!newOverwrittenPermissions.equals(oldOverwrittenPermissions)) {
overwrittenPermissions.put(entityId, newOverwrittenPermissions);
if (server.isReady()) {
dispatchServerChannelChangeOverwrittenPermissionsEvent(channel, newOverwrittenPermissions, oldOverwrittenPermissions, entityId, entity.orElse(null));
areYouAffected.compareAndSet(false, entityId == api.getYourself().getId());
entity.filter(e -> e instanceof Role).map(Role.class::cast).ifPresent(role -> areYouAffected.compareAndSet(false, role.getUsers().stream().anyMatch(User::isYourself)));
}
}
}
}
ConcurrentHashMap<Long, Permissions> overwrittenRolePermissions;
ConcurrentHashMap<Long, Permissions> overwrittenUserPermissions;
overwrittenRolePermissions = regularServerChannel.getInternalOverwrittenRolePermissions();
overwrittenUserPermissions = regularServerChannel.getInternalOverwrittenUserPermissions();
Iterator<Map.Entry<Long, Permissions>> userIt = overwrittenUserPermissions.entrySet().iterator();
while (userIt.hasNext()) {
Map.Entry<Long, Permissions> entry = userIt.next();
if (usersWithOverwrittenPermissions.contains(entry.getKey())) {
continue;
}
Permissions oldPermissions = entry.getValue();
userIt.remove();
if (server.isReady()) {
dispatchServerChannelChangeOverwrittenPermissionsEvent(channel, PermissionsImpl.EMPTY_PERMISSIONS, oldPermissions, entry.getKey(), api.getCachedUserById(entry.getKey()).orElse(null));
areYouAffected.compareAndSet(false, entry.getKey() == api.getYourself().getId());
}
}
Iterator<Map.Entry<Long, Permissions>> roleIt = overwrittenRolePermissions.entrySet().iterator();
while (roleIt.hasNext()) {
Map.Entry<Long, Permissions> entry = roleIt.next();
if (rolesWithOverwrittenPermissions.contains(entry.getKey())) {
continue;
}
api.getRoleById(entry.getKey()).ifPresent(role -> {
Permissions oldPermissions = entry.getValue();
roleIt.remove();
if (server.isReady()) {
dispatchServerChannelChangeOverwrittenPermissionsEvent(channel, PermissionsImpl.EMPTY_PERMISSIONS, oldPermissions, role.getId(), role);
areYouAffected.compareAndSet(false, role.getUsers().stream().anyMatch(User::isYourself));
}
});
}
if (areYouAffected.get() && !channel.canYouSee()) {
api.forEachCachedMessageWhere(msg -> msg.getChannel().getId() == channelId, msg -> {
api.removeMessageFromCache(msg.getId());
((MessageCacheImpl) ((TextChannel) channel).getMessageCache()).removeMessage(msg);
});
}
}
use of org.javacord.api.entity.permission.Permissions in project Javacord by BtoBastian.
the class RegularServerChannelUpdaterDelegateImpl method prepareUpdateBody.
@Override
protected boolean prepareUpdateBody(ObjectNode body) {
boolean patchChannel = super.prepareUpdateBody(body);
if (position != null) {
body.put("position", position.intValue());
patchChannel = true;
}
ArrayNode permissionOverwrites = null;
if (overwrittenUserPermissions != null || overwrittenRolePermissions != null) {
permissionOverwrites = body.putArray("permission_overwrites");
patchChannel = true;
}
if (overwrittenUserPermissions != null) {
for (Map.Entry<Long, Permissions> entry : overwrittenUserPermissions.entrySet()) {
permissionOverwrites.addObject().put("id", Long.toUnsignedString(entry.getKey())).put("type", 1).put("allow", entry.getValue().getAllowedBitmask()).put("deny", entry.getValue().getDeniedBitmask());
}
}
if (overwrittenRolePermissions != null) {
for (Map.Entry<Long, Permissions> entry : overwrittenRolePermissions.entrySet()) {
permissionOverwrites.addObject().put("id", Long.toUnsignedString(entry.getKey())).put("type", 0).put("allow", entry.getValue().getAllowedBitmask()).put("deny", entry.getValue().getDeniedBitmask());
}
}
return patchChannel;
}
use of org.javacord.api.entity.permission.Permissions in project Javacord by BtoBastian.
the class GuildRoleUpdateHandler method handle.
@Override
public void handle(JsonNode packet) {
JsonNode roleJson = packet.get("role");
long roleId = roleJson.get("id").asLong();
api.getRoleById(roleId).map(role -> (RoleImpl) role).ifPresent(role -> {
Color oldColorObject = role.getColor().orElse(null);
int oldColor = role.getColorAsInt();
int newColor = roleJson.get("color").asInt(0);
if (oldColor != newColor) {
role.setColor(newColor);
RoleChangeColorEvent event = new RoleChangeColorEventImpl(role, role.getColor().orElse(null), oldColorObject);
api.getEventDispatcher().dispatchRoleChangeColorEvent((DispatchQueueSelector) role.getServer(), role, role.getServer(), event);
}
boolean oldHoist = role.isDisplayedSeparately();
boolean newHoist = roleJson.get("hoist").asBoolean(false);
if (oldHoist != newHoist) {
role.setHoist(newHoist);
RoleChangeHoistEvent event = new RoleChangeHoistEventImpl(role, oldHoist);
api.getEventDispatcher().dispatchRoleChangeHoistEvent((DispatchQueueSelector) role.getServer(), role, role.getServer(), event);
}
boolean oldMentionable = role.isMentionable();
boolean newMentionable = roleJson.get("mentionable").asBoolean(false);
if (oldMentionable != newMentionable) {
role.setMentionable(newMentionable);
RoleChangeMentionableEvent event = new RoleChangeMentionableEventImpl(role, oldMentionable);
api.getEventDispatcher().dispatchRoleChangeMentionableEvent((DispatchQueueSelector) role.getServer(), role, role.getServer(), event);
}
String oldName = role.getName();
String newName = roleJson.get("name").asText();
if (!oldName.equals(newName)) {
role.setName(newName);
RoleChangeNameEvent event = new RoleChangeNameEventImpl(role, newName, oldName);
api.getEventDispatcher().dispatchRoleChangeNameEvent((DispatchQueueSelector) role.getServer(), role, role.getServer(), event);
}
Permissions oldPermissions = role.getPermissions();
PermissionsImpl newPermissions = new PermissionsImpl(roleJson.get("permissions").asLong(), 0);
if (!oldPermissions.equals(newPermissions)) {
role.setPermissions(newPermissions);
RoleChangePermissionsEvent event = new RoleChangePermissionsEventImpl(role, newPermissions, oldPermissions);
api.getEventDispatcher().dispatchRoleChangePermissionsEvent((DispatchQueueSelector) role.getServer(), role, role.getServer(), event);
// If bot is affected remove messages from cache that are no longer visible
if (role.getUsers().stream().anyMatch(User::isYourself)) {
Set<Long> unreadableChannels = role.getServer().getTextChannels().stream().filter(((Predicate<ServerTextChannel>) ServerTextChannel::canYouSee).negate()).map(ServerTextChannel::getId).collect(Collectors.toSet());
api.forEachCachedMessageWhere(msg -> unreadableChannels.contains(msg.getChannel().getId()), msg -> {
api.removeMessageFromCache(msg.getId());
((MessageCacheImpl) msg.getChannel().getMessageCache()).removeMessage(msg);
});
}
}
int oldPosition = role.getPosition();
int oldRawPosition = role.getRawPosition();
int newRawPosition = roleJson.get("position").asInt();
if (oldRawPosition != newRawPosition) {
role.setRawPosition(newRawPosition);
int newPosition = role.getPosition();
RoleChangePositionEvent event = new RoleChangePositionEventImpl(role, newRawPosition, oldRawPosition, newPosition, oldPosition);
api.getEventDispatcher().dispatchRoleChangePositionEvent((DispatchQueueSelector) role.getServer(), role, role.getServer(), event);
}
});
}
Aggregations