Skip to content

Commit

Permalink
More object update checks
Browse files Browse the repository at this point in the history
  • Loading branch information
SNWCreations committed Jun 18, 2024
1 parent c705817 commit 6da9cac
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
23 changes: 22 additions & 1 deletion src/main/java/snw/kookbc/impl/event/EventFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@
import snw.kookbc.impl.serializer.event.role.RoleDeleteEventDeserializer;
import snw.kookbc.impl.serializer.event.role.RoleInfoUpdateEventDeserializer;
import snw.kookbc.impl.serializer.event.user.*;
import snw.kookbc.impl.storage.EntityStorage;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.BiPredicate;

import static snw.kookbc.impl.serializer.EventDeserializeUtils.getBody;
import static snw.kookbc.util.GsonUtil.get;
import static snw.kookbc.util.GsonUtil.has;

Expand All @@ -64,18 +66,37 @@ private static BiPredicate<EventFactory, JsonObject> subscribed(Class<? extends
return (eventFactory, json) -> eventFactory.eventManager.isSubscribed(type);
}

private static BiPredicate<EventFactory, JsonObject> cachedChannel() {
return (eventFactory, jsonObject) -> {
final JsonObject body = getBody(jsonObject);
final String channelId = get(body, "channel_id").getAsString();
final EntityStorage storage = eventFactory.client.getStorage();
return storage.containsChannel(channelId);
};
}

public static BiPredicate<EventFactory, JsonObject> cachedGuild() {
return (eventFactory, jsonObject) -> {
final JsonObject body = getBody(jsonObject);
final String channelId = get(body, "id").getAsString();
final EntityStorage storage = eventFactory.client.getStorage();
return storage.containsChannel(channelId);
};
}

static {
FORCE_CREATE_TYPES = new HashSet<Class<? extends Event>>() {
{
add(ChannelMessageEvent.class);
add(PrivateMessageReceivedEvent.class);
add(ChannelInfoUpdateEvent.class); // what #85 did
}
};
CREATE_CONDITIONS = new HashMap<Class<? extends Event>, BiPredicate<EventFactory, JsonObject>>() {
{
put(ChannelMessageDeleteEvent.class, subscribed(ChannelMessageEvent.class));
put(PrivateMessageDeleteEvent.class, subscribed(PrivateMessageReceivedEvent.class));
put(ChannelInfoUpdateEvent.class, cachedChannel()); // what #85 did
put(GuildInfoUpdateEvent.class, cachedGuild());
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package snw.kookbc.impl.serializer;

import com.google.gson.JsonObject;

import static snw.kookbc.util.GsonUtil.get;

public class EventDeserializeUtils {
public static JsonObject getBody(JsonObject object) {
return get(get(object, "extra").getAsJsonObject(), "body").getAsJsonObject();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import java.lang.reflect.Type;

import static snw.kookbc.impl.serializer.EventDeserializeUtils.getBody;
import static snw.kookbc.util.GsonUtil.get;

public abstract class NormalEventDeserializer<T extends Event> extends BaseEventDeserializer<T> {
Expand All @@ -41,7 +42,7 @@ protected T deserialize(JsonObject object, Type type, JsonDeserializationContext
type,
ctx,
get(object, "msg_timestamp").getAsLong(),
get(get(object, "extra").getAsJsonObject(), "body").getAsJsonObject()
getBody(object)
);
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/snw/kookbc/impl/storage/EntityStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ public void removeEmoji(CustomEmoji emoji) {
emojis.invalidate(emoji.getId());
}

public boolean containsChannel(String id) {
return channels.asMap().containsKey(id);
}

private static Caffeine<Object, Object> newCaffeineBuilderWithWeakRef() {
return Caffeine.newBuilder()
.weakValues()
Expand Down

0 comments on commit 6da9cac

Please sign in to comment.