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);
}
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);
}
Aggregations