Search in sources :

Example 1 with DiscordClient

use of org.javacord.api.entity.DiscordClient in project Javacord by BtoBastian.

the class PresenceUpdateHandler method dispatchUserStatusChangeEventIfChangeDetected.

private void dispatchUserStatusChangeEventIfChangeDetected(long userId, UserStatus newStatus, UserStatus oldStatus, Map<DiscordClient, UserStatus> newClientStatus, Map<DiscordClient, UserStatus> oldClientStatus) {
    UserImpl user = api.getCachedUserById(userId).map(UserImpl.class::cast).orElse(null);
    // Only dispatch the event if something changed
    boolean shouldDispatch = false;
    if (newClientStatus != oldClientStatus) {
        shouldDispatch = true;
    }
    for (DiscordClient client : DiscordClient.values()) {
        if (newClientStatus.get(client) != oldClientStatus.get(client)) {
            shouldDispatch = true;
        }
    }
    if (!shouldDispatch) {
        return;
    }
    UserChangeStatusEvent event = new UserChangeStatusEventImpl(api, userId, newStatus, oldStatus, newClientStatus, oldClientStatus);
    api.getEventDispatcher().dispatchUserChangeStatusEvent(api, user == null ? Collections.emptySet() : user.getMutualServers(), user == null ? Collections.emptySet() : Collections.singleton(user), event);
}
Also used : DiscordClient(org.javacord.api.entity.DiscordClient) UserChangeStatusEventImpl(org.javacord.core.event.user.UserChangeStatusEventImpl) UserChangeStatusEvent(org.javacord.api.event.user.UserChangeStatusEvent) UserImpl(org.javacord.core.entity.user.UserImpl)

Example 2 with DiscordClient

use of org.javacord.api.entity.DiscordClient in project Javacord by BtoBastian.

the class PresenceUpdateHandler method handle.

@Override
public void handle(JsonNode packet) {
    // ignore the guild_id and send to all mutual servers instead, or we must track the properties per server
    // or all packets after the first do not detect a change and will not send around an event for the server
    long userId = packet.get("user").get("id").asLong();
    AtomicReference<UserPresence> presence = new AtomicReference<>(api.getEntityCache().get().getUserPresenceCache().getPresenceByUserId(userId).orElseGet(() -> new UserPresence(userId, null, null, io.vavr.collection.HashMap.empty())));
    if (packet.hasNonNull("activities")) {
        Set<Activity> newActivities = new HashSet<>();
        for (JsonNode activityJson : packet.get("activities")) {
            if (!activityJson.isNull()) {
                newActivities.add(new ActivityImpl(api, activityJson));
            }
        }
        Set<Activity> oldActivities = api.getEntityCache().get().getUserPresenceCache().getPresenceByUserId(userId).map(UserPresence::getActivities).orElse(Collections.emptySet());
        presence.set(presence.get().setActivities(newActivities));
        if (!Objects.deepEquals(newActivities.toArray(), oldActivities.toArray())) {
            dispatchUserActivityChangeEvent(userId, newActivities, oldActivities);
        }
    }
    UserStatus oldStatus = api.getEntityCache().get().getUserPresenceCache().getPresenceByUserId(userId).map(UserPresence::getStatus).orElse(UserStatus.OFFLINE);
    UserStatus newStatus;
    if (packet.has("status")) {
        newStatus = UserStatus.fromString(packet.get("status").asText(null));
        presence.set(presence.get().setStatus(newStatus));
    } else {
        newStatus = oldStatus;
    }
    Map<DiscordClient, UserStatus> oldClientStatus = api.getEntityCache().get().getUserPresenceCache().getPresenceByUserId(userId).map(UserPresence::getClientStatus).orElse(HashMap.empty());
    for (DiscordClient client : DiscordClient.values()) {
        if (packet.has("client_status")) {
            JsonNode clientStatus = packet.get("client_status");
            if (clientStatus.hasNonNull(client.getName())) {
                UserStatus status = UserStatus.fromString(clientStatus.get(client.getName()).asText());
                presence.set(presence.get().setClientStatus(presence.get().getClientStatus().put(client, status)));
            } else {
                presence.set(presence.get().setClientStatus(presence.get().getClientStatus().put(client, UserStatus.OFFLINE)));
            }
        }
    }
    Map<DiscordClient, UserStatus> newClientStatus = api.getEntityCache().get().getUserPresenceCache().getPresenceByUserId(userId).map(UserPresence::getClientStatus).orElse(HashMap.empty());
    api.updateUserPresence(userId, p -> presence.get());
    dispatchUserStatusChangeEventIfChangeDetected(userId, newStatus, oldStatus, newClientStatus, oldClientStatus);
}
Also used : DiscordClient(org.javacord.api.entity.DiscordClient) ActivityImpl(org.javacord.core.entity.activity.ActivityImpl) Activity(org.javacord.api.entity.activity.Activity) AtomicReference(java.util.concurrent.atomic.AtomicReference) JsonNode(com.fasterxml.jackson.databind.JsonNode) UserPresence(org.javacord.core.entity.user.UserPresence) UserStatus(org.javacord.api.entity.user.UserStatus) HashSet(java.util.HashSet)

Aggregations

DiscordClient (org.javacord.api.entity.DiscordClient)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 HashSet (java.util.HashSet)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Activity (org.javacord.api.entity.activity.Activity)1 UserStatus (org.javacord.api.entity.user.UserStatus)1 UserChangeStatusEvent (org.javacord.api.event.user.UserChangeStatusEvent)1 ActivityImpl (org.javacord.core.entity.activity.ActivityImpl)1 UserImpl (org.javacord.core.entity.user.UserImpl)1 UserPresence (org.javacord.core.entity.user.UserPresence)1 UserChangeStatusEventImpl (org.javacord.core.event.user.UserChangeStatusEventImpl)1