Search in sources :

Example 1 with ServerTextChannelImpl

use of org.javacord.core.entity.channel.ServerTextChannelImpl in project Javacord by BtoBastian.

the class ChannelUpdateHandler method handleServerTextChannel.

/**
 * Handles a server text channel update.
 *
 * @param jsonChannel The json channel data.
 */
private void handleServerTextChannel(JsonNode jsonChannel) {
    long channelId = jsonChannel.get("id").asLong();
    Optional<ServerTextChannel> optionalChannel = api.getServerTextChannelById(channelId);
    if (!optionalChannel.isPresent()) {
        LoggerUtil.logMissingChannel(logger, channelId);
        return;
    }
    ServerTextChannelImpl channel = (ServerTextChannelImpl) optionalChannel.get();
    String oldTopic = channel.getTopic();
    String newTopic = jsonChannel.has("topic") && !jsonChannel.get("topic").isNull() ? jsonChannel.get("topic").asText() : "";
    if (!oldTopic.equals(newTopic)) {
        channel.setTopic(newTopic);
        ServerTextChannelChangeTopicEvent event = new ServerTextChannelChangeTopicEventImpl(channel, newTopic, oldTopic);
        api.getEventDispatcher().dispatchServerTextChannelChangeTopicEvent((DispatchQueueSelector) channel.getServer(), channel.getServer(), channel, event);
    }
    boolean oldNsfwFlag = channel.isNsfw();
    boolean newNsfwFlag = jsonChannel.get("nsfw").asBoolean();
    if (oldNsfwFlag != newNsfwFlag) {
        channel.setNsfwFlag(newNsfwFlag);
        ServerChannelChangeNsfwFlagEvent event = new ServerChannelChangeNsfwFlagEventImpl(channel, newNsfwFlag, oldNsfwFlag);
        api.getEventDispatcher().dispatchServerChannelChangeNsfwFlagEvent((DispatchQueueSelector) channel.getServer(), null, channel.getServer(), channel, event);
    }
    int oldSlowmodeDelay = channel.getSlowmodeDelayInSeconds();
    // Check if "rate_limit_per_user" exists as a temporary fix until SERVER_NEWS_CHANNEL is handled separately.
    int newSlowmodeDelay = jsonChannel.has("rate_limit_per_user") ? jsonChannel.get("rate_limit_per_user").asInt(0) : 0;
    if (oldSlowmodeDelay != newSlowmodeDelay) {
        channel.setSlowmodeDelayInSeconds(newSlowmodeDelay);
        ServerTextChannelChangeSlowmodeEvent event = new ServerTextChannelChangeSlowmodeEventImpl(channel, oldSlowmodeDelay, newSlowmodeDelay);
        api.getEventDispatcher().dispatchServerTextChannelChangeSlowmodeEvent((DispatchQueueSelector) channel.getServer(), channel.getServer(), channel, event);
    }
    int oldDefaultAutoArchiveDuration = channel.getDefaultAutoArchiveDuration();
    int newDefaultAutoArchiveDuration = jsonChannel.has("default_auto_archive_duration") ? jsonChannel.get("default_auto_archive_duration").asInt() : 1440;
    if (oldDefaultAutoArchiveDuration != newDefaultAutoArchiveDuration) {
        channel.setDefaultAutoArchiveDuration(newDefaultAutoArchiveDuration);
        ServerTextChannelChangeDefaultAutoArchiveDurationEvent event = new ServerTextChannelChangeDefaultAutoArchiveDurationEventImpl(channel, oldDefaultAutoArchiveDuration, newDefaultAutoArchiveDuration);
        api.getEventDispatcher().dispatchServerTextChannelChangeDefaultAutoArchiveDurationEvent((DispatchQueueSelector) channel.getServer(), channel.getServer(), channel, event);
    }
}
Also used : ServerChannelChangeNsfwFlagEventImpl(org.javacord.core.event.channel.server.ServerChannelChangeNsfwFlagEventImpl) ServerTextChannelChangeTopicEvent(org.javacord.api.event.channel.server.text.ServerTextChannelChangeTopicEvent) ServerTextChannelChangeTopicEventImpl(org.javacord.core.event.channel.server.text.ServerTextChannelChangeTopicEventImpl) ServerTextChannelChangeDefaultAutoArchiveDurationEventImpl(org.javacord.core.event.channel.server.text.ServerTextChannelChangeDefaultAutoArchiveDurationEventImpl) ServerChannelChangeNsfwFlagEvent(org.javacord.api.event.channel.server.ServerChannelChangeNsfwFlagEvent) ServerTextChannel(org.javacord.api.entity.channel.ServerTextChannel) ServerTextChannelChangeSlowmodeEvent(org.javacord.api.event.channel.server.text.ServerTextChannelChangeSlowmodeEvent) ServerTextChannelChangeDefaultAutoArchiveDurationEvent(org.javacord.api.event.channel.server.text.ServerTextChannelChangeDefaultAutoArchiveDurationEvent) ServerTextChannelChangeSlowmodeEventImpl(org.javacord.core.event.channel.server.text.ServerTextChannelChangeSlowmodeEventImpl) ServerTextChannelImpl(org.javacord.core.entity.channel.ServerTextChannelImpl)

Example 2 with ServerTextChannelImpl

use of org.javacord.core.entity.channel.ServerTextChannelImpl 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);
        });
    }
}
Also used : User(org.javacord.api.entity.user.User) RegularServerChannel(org.javacord.api.entity.channel.RegularServerChannel) RegularServerChannelImpl(org.javacord.core.entity.channel.RegularServerChannelImpl) JsonNode(com.fasterxml.jackson.databind.JsonNode) ServerChannelChangePositionEvent(org.javacord.api.event.channel.server.ServerChannelChangePositionEvent) ServerImpl(org.javacord.core.entity.server.ServerImpl) PermissionsImpl(org.javacord.core.entity.permission.PermissionsImpl) Permissions(org.javacord.api.entity.permission.Permissions) ServerTextChannelImpl(org.javacord.core.entity.channel.ServerTextChannelImpl) HashSet(java.util.HashSet) ServerVoiceChannelImpl(org.javacord.core.entity.channel.ServerVoiceChannelImpl) Role(org.javacord.api.entity.permission.Role) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MessageCacheImpl(org.javacord.core.util.cache.MessageCacheImpl) ServerChannelChangePositionEventImpl(org.javacord.core.event.channel.server.ServerChannelChangePositionEventImpl) ChannelCategory(org.javacord.api.entity.channel.ChannelCategory) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DiscordEntity(org.javacord.api.entity.DiscordEntity)

Example 3 with ServerTextChannelImpl

use of org.javacord.core.entity.channel.ServerTextChannelImpl in project Javacord by BtoBastian.

the class ServerImpl method getOrCreateServerTextChannel.

/**
 * Gets or creates a server text channel.
 *
 * @param data The json data of the channel.
 * @return The server text channel.
 */
public ServerTextChannel getOrCreateServerTextChannel(JsonNode data) {
    long id = Long.parseLong(data.get("id").asText());
    ChannelType type = ChannelType.fromId(data.get("type").asInt());
    synchronized (this) {
        switch(type) {
            case SERVER_TEXT_CHANNEL:
            case // TODO Treat news channels differently
            SERVER_NEWS_CHANNEL:
                return getTextChannelById(id).orElseGet(() -> new ServerTextChannelImpl(api, this, data));
            default:
                // Invalid channel type
                return null;
        }
    }
}
Also used : ChannelType(org.javacord.api.entity.channel.ChannelType) ServerTextChannelImpl(org.javacord.core.entity.channel.ServerTextChannelImpl)

Aggregations

ServerTextChannelImpl (org.javacord.core.entity.channel.ServerTextChannelImpl)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 DiscordEntity (org.javacord.api.entity.DiscordEntity)1 ChannelCategory (org.javacord.api.entity.channel.ChannelCategory)1 ChannelType (org.javacord.api.entity.channel.ChannelType)1 RegularServerChannel (org.javacord.api.entity.channel.RegularServerChannel)1 ServerTextChannel (org.javacord.api.entity.channel.ServerTextChannel)1 Permissions (org.javacord.api.entity.permission.Permissions)1 Role (org.javacord.api.entity.permission.Role)1 User (org.javacord.api.entity.user.User)1 ServerChannelChangeNsfwFlagEvent (org.javacord.api.event.channel.server.ServerChannelChangeNsfwFlagEvent)1 ServerChannelChangePositionEvent (org.javacord.api.event.channel.server.ServerChannelChangePositionEvent)1 ServerTextChannelChangeDefaultAutoArchiveDurationEvent (org.javacord.api.event.channel.server.text.ServerTextChannelChangeDefaultAutoArchiveDurationEvent)1 ServerTextChannelChangeSlowmodeEvent (org.javacord.api.event.channel.server.text.ServerTextChannelChangeSlowmodeEvent)1 ServerTextChannelChangeTopicEvent (org.javacord.api.event.channel.server.text.ServerTextChannelChangeTopicEvent)1 RegularServerChannelImpl (org.javacord.core.entity.channel.RegularServerChannelImpl)1