Search in sources :

Example 1 with MemberPresenceImpl

use of net.dv8tion.jda.internal.entities.MemberPresenceImpl in project JDA by DV8FromTheWorld.

the class PresenceUpdateHandler method handleInternally.

@Override
protected Long handleInternally(DataObject content) {
    // Ignore events for relationships, presences are guild only to us
    if (content.isNull("guild_id")) {
        log.debug("Received PRESENCE_UPDATE without guild_id. Ignoring event.");
        return null;
    }
    if (api.getCacheFlags().stream().noneMatch(CacheFlag::isPresence))
        return null;
    // Do a pre-check to see if this is for a Guild, and if it is, if the guild is currently locked or not cached.
    final long guildId = content.getUnsignedLong("guild_id");
    if (getJDA().getGuildSetupController().isLocked(guildId))
        return guildId;
    GuildImpl guild = (GuildImpl) getJDA().getGuildById(guildId);
    if (guild == null) {
        getJDA().getEventCache().cache(EventCache.Type.GUILD, guildId, responseNumber, allContent, this::handle);
        EventCache.LOG.debug("Received a PRESENCE_UPDATE for a guild that is not yet cached! GuildId:{} UserId: {}", guildId, content.getObject("user").get("id"));
        return null;
    }
    CacheView.SimpleCacheView<MemberPresenceImpl> presences = guild.getPresenceView();
    if (presences == null)
        // technically this should be impossible
        return null;
    DataObject jsonUser = content.getObject("user");
    final long userId = jsonUser.getUnsignedLong("id");
    MemberImpl member = (MemberImpl) guild.getMemberById(userId);
    MemberPresenceImpl presence = presences.get(userId);
    OnlineStatus status = OnlineStatus.fromKey(content.getString("status"));
    if (status == OnlineStatus.OFFLINE)
        presences.remove(userId);
    if (presence == null) {
        presence = new MemberPresenceImpl();
        if (status != OnlineStatus.OFFLINE) {
            try (UnlockHook lock = presences.writeLock()) {
                presences.getMap().put(userId, presence);
            }
        }
    }
    // Now that we've update the User's info, lets see if we need to set the specific Presence information.
    // This is stored in the Member objects.
    // We set the activities to null to prevent parsing if the cache was disabled
    final DataArray activityArray = !getJDA().isCacheFlagSet(CacheFlag.ACTIVITY) || content.isNull("activities") ? null : content.getArray("activities");
    List<Activity> newActivities = new ArrayList<>();
    boolean parsedActivity = parseActivities(userId, activityArray, newActivities);
    if (getJDA().isCacheFlagSet(CacheFlag.CLIENT_STATUS) && !content.isNull("client_status"))
        handleClientStatus(content, presence);
    // Check if activities changed
    if (parsedActivity)
        handleActivities(newActivities, member, presence);
    if (presence.getOnlineStatus() != status) {
        OnlineStatus oldStatus = presence.getOnlineStatus();
        presence.setOnlineStatus(status);
        if (member != null) {
            getJDA().getEntityBuilder().updateMemberCache(member);
            getJDA().handleEvent(new UserUpdateOnlineStatusEvent(getJDA(), responseNumber, member, oldStatus));
        }
    }
    return null;
}
Also used : CacheFlag(net.dv8tion.jda.api.utils.cache.CacheFlag) MemberImpl(net.dv8tion.jda.internal.entities.MemberImpl) MemberPresenceImpl(net.dv8tion.jda.internal.entities.MemberPresenceImpl) OnlineStatus(net.dv8tion.jda.api.OnlineStatus) ArrayList(java.util.ArrayList) Activity(net.dv8tion.jda.api.entities.Activity) DataArray(net.dv8tion.jda.api.utils.data.DataArray) GuildImpl(net.dv8tion.jda.internal.entities.GuildImpl) DataObject(net.dv8tion.jda.api.utils.data.DataObject) CacheView(net.dv8tion.jda.api.utils.cache.CacheView) UnlockHook(net.dv8tion.jda.internal.utils.UnlockHook) UserUpdateOnlineStatusEvent(net.dv8tion.jda.api.events.user.update.UserUpdateOnlineStatusEvent)

Aggregations

ArrayList (java.util.ArrayList)1 OnlineStatus (net.dv8tion.jda.api.OnlineStatus)1 Activity (net.dv8tion.jda.api.entities.Activity)1 UserUpdateOnlineStatusEvent (net.dv8tion.jda.api.events.user.update.UserUpdateOnlineStatusEvent)1 CacheFlag (net.dv8tion.jda.api.utils.cache.CacheFlag)1 CacheView (net.dv8tion.jda.api.utils.cache.CacheView)1 DataArray (net.dv8tion.jda.api.utils.data.DataArray)1 DataObject (net.dv8tion.jda.api.utils.data.DataObject)1 GuildImpl (net.dv8tion.jda.internal.entities.GuildImpl)1 MemberImpl (net.dv8tion.jda.internal.entities.MemberImpl)1 MemberPresenceImpl (net.dv8tion.jda.internal.entities.MemberPresenceImpl)1 UnlockHook (net.dv8tion.jda.internal.utils.UnlockHook)1