Skip to content

Commit

Permalink
fix: many database issues
Browse files Browse the repository at this point in the history
  • Loading branch information
qixils committed Dec 21, 2023
1 parent 00a0180 commit 69ba2f9
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import java.util.Locale;

import static com.mongodb.client.model.Filters.and;
import static com.mongodb.client.model.Filters.eq;

public abstract class ConfigCommand {
Expand All @@ -36,7 +35,7 @@ protected Mono<?> setLocale(
// TODO: probably remove per-channel locale config i think

var collection = library.getDatabaseManager().collection(LocaleConfig.class);
var filter = and(eq("id", snowflake.getIdLong()), eq("entryType", entryType));
var filter = eq("_id", LocaleConfig.createId(entryType, snowflake.getIdLong()));
Publisher<?> result;
if (locale == null) {
result = collection.deleteOne(filter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public Mono<QuasiMessage> setLocaleCommand(
? Text.single(Key.library("user-config.language.output.removed"))
: Text.single(Key.library("user-config.language.output.updated"), locale.getDisplayName(locale)))
.map(text -> new QuasiMessage(text, request -> {
if (request instanceof ReplyCallbackAction action && channel.getType() == ChannelType.PRIVATE) {
if (request instanceof ReplyCallbackAction action && channel.getType() != ChannelType.PRIVATE) {
//noinspection ResultOfMethodCallIgnored
action.setEphemeral(true);
}
Expand Down Expand Up @@ -90,7 +90,7 @@ public Mono<QuasiMessage> setTimeZoneCommand(
? Text.single(Key.library("user-config.timezone.output.removed"))
: Text.single(Key.library("user-config.timezone.output.updated"), (Text) locale -> tz.getDisplayName(TextStyle.FULL_STANDALONE, locale)))
.map(text -> new QuasiMessage(text, request -> {
if (request instanceof ReplyCallbackAction action && channel.getType() == ChannelType.PRIVATE) {
if (request instanceof ReplyCallbackAction action && channel.getType() != ChannelType.PRIVATE) {
//noinspection ResultOfMethodCallIgnored
action.setEphemeral(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class ZonedDateTimeConverter implements Converter<String, ZonedDateTime>

@Override
public @NonNull ZonedDateTime convert(@NonNull Interaction interaction, @NonNull String input) {
TimeZoneConfig config = library.getDatabaseManager().getBySnowflake(interaction.getUser(), TimeZoneConfig.class).block();
TimeZoneConfig config = library.getDatabaseManager().getById(interaction.getUser().getIdLong(), TimeZoneConfig.class).block();
ZoneId zone = config == null ? ZoneOffset.UTC : config.getTimeZone();
int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0, nanos = 0;
char meridiem = 'x';
Expand Down
16 changes: 4 additions & 12 deletions src/main/java/dev/qixils/quasicord/db/DatabaseManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
import com.mongodb.reactivestreams.client.MongoDatabase;
import dev.qixils.quasicord.Environment;
import dev.qixils.quasicord.Quasicord;
import net.dv8tion.jda.api.entities.ISnowflake;
import org.bson.codecs.configuration.CodecRegistries;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.codecs.pojo.PojoCodecProvider;
import org.bson.conversions.Bson;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.jetbrains.annotations.NotNull;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
Expand Down Expand Up @@ -97,18 +97,10 @@ private static String collectionNameOf(Class<?> clazz) {
return getAllBy(filter, tClass);
}

// getBySnowflake
// getById

public <T extends ISnowflake> @NotNull Mono<T> getBySnowflake(String snowflake, Class<T> tClass) {
return Mono.from(collection(tClass).find(Filters.eq("snowflake", snowflake)).first());
}

public <T extends ISnowflake> @NotNull Mono<T> getBySnowflake(long snowflake, Class<T> tClass) {
return getBySnowflake(String.valueOf(snowflake), tClass);
}

public <T extends ISnowflake> @NotNull Mono<T> getBySnowflake(ISnowflake snowflake, Class<T> tClass) {
return getBySnowflake(snowflake.getId(), tClass);
public <T> @NonNull Mono<T> getById(Object id, Class<T> tClass) {
return Mono.from(collection(tClass).find(Filters.eq("_id", id)));
}

// misc
Expand Down
34 changes: 0 additions & 34 deletions src/main/java/dev/qixils/quasicord/db/ISettableSnowflake.java

This file was deleted.

96 changes: 81 additions & 15 deletions src/main/java/dev/qixils/quasicord/db/collection/LocaleConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,68 +7,134 @@
package dev.qixils.quasicord.db.collection;

import dev.qixils.quasicord.db.CollectionName;
import net.dv8tion.jda.api.entities.ISnowflake;
import org.bson.codecs.pojo.annotations.BsonCreator;
import org.bson.codecs.pojo.annotations.BsonId;
import org.bson.codecs.pojo.annotations.BsonProperty;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.jetbrains.annotations.ApiStatus;

import java.util.Locale;

/**
* An entry in the locale configuration database collection.
* This stores the selected locale for a user, channel, or guild.
*/
@CollectionName(name = "locale")
public class LocaleConfig implements ISnowflake {
private long snowflake;
private @Nullable EntryType entryType;
private @Nullable String languageCode;
public class LocaleConfig {

@BsonId
private final @NonNull String id;
private final long snowflake;
private final @NonNull EntryType entryType;
private final @NonNull String languageCode;

/**
* No-arg constructor for MongoDB.
* Creates an ID for this object.
*
* @param entryType the type of this object
* @param snowflake the snowflake of this object
* @return object ID
*/
LocaleConfig() {
@NonNull
public static String createId(@NonNull EntryType entryType, long snowflake) {
return entryType.name() + '/' + Long.toHexString(snowflake);
}

/**
* Constructor for MongoDB.
*/
@ApiStatus.Internal
@BsonCreator
public LocaleConfig(
@BsonId @NonNull String id,
@BsonProperty("snowflake") long snowflake,
@BsonProperty("entryType") @NonNull EntryType entryType,
@BsonProperty("languageCode") @NonNull String languageCode
) {
this.id = id;
this.snowflake = snowflake;
this.entryType = entryType;
this.languageCode = languageCode;
}

/**
* Constructs a new LocaleConfig entry.
*
* @param snowflake the object's snowflake ID
* @param snowflake the object's snowflake ID
* @param entryType the type of entry
* @param languageCode the object's configured language code
*/
public LocaleConfig(long snowflake, @NonNull EntryType entryType, @NonNull String languageCode) {
this.id = createId(entryType, snowflake);
this.snowflake = snowflake;
this.entryType = entryType;
this.languageCode = languageCode;
}

@Override
public long getIdLong() {
/**
* Constructs a new LocaleConfig entry.
*
* @param snowflake the object's snowflake ID
* @param entryType the type of entry
* @param language the object's configured language
*/
public LocaleConfig(long snowflake, @NonNull EntryType entryType, @NonNull Locale language) {
this(snowflake, entryType, language.toLanguageTag());
}

/**
* Returns the database ID of this object.
*
* @return ID
*/
@NonNull
@BsonId
public String getId() {
return id;
}

/**
* Returns the snowflake for this object.
*
* @return snowflake
*/
public long getSnowflake() {
return snowflake;
}

/**
* Returns the type of this object.
*
* @return type
*/
public @NonNull EntryType getEntryType() {
if (entryType == null)
throw new IllegalStateException("Entry type is null");
return entryType;
}

/**
* Returns the code of the configured language of this object.
*
* @return language code
*/
public @NonNull String getLanguageCode() {
if (languageCode == null)
throw new IllegalStateException("Language code is null");
return languageCode;
}

/**
* A type of entry used to distinguish between snowflake IDs of users, channels, and guilds.
*/
public enum EntryType {

/**
* A user's entry.
*/
USER,

/**
* A channel's entry.
*/
CHANNEL,

/**
* A guild's entry.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
package dev.qixils.quasicord.db.collection;

import dev.qixils.quasicord.db.CollectionName;
import net.dv8tion.jda.api.entities.ISnowflake;
import org.bson.codecs.pojo.annotations.BsonId;
import org.bson.codecs.pojo.annotations.BsonIgnore;
import org.bson.codecs.pojo.annotations.BsonProperty;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.jetbrains.annotations.ApiStatus;

import java.time.ZoneId;

Expand All @@ -18,23 +20,23 @@
* This stores the selected timezone for a user.
*/
@CollectionName(name = "timezone")
public class TimeZoneConfig implements ISnowflake {
private long snowflake;
private @Nullable String tzCode;
public class TimeZoneConfig {

/**
* No-arg constructor for MongoDB.
*/
TimeZoneConfig() {
}
@BsonId
private final long snowflake; // _id
private final @NonNull String tzCode;

/**
* Constructs a new TimeZoneConfig entry.
*
* @param snowflake the user's snowflake ID
* @param tzCode the user's configured timezone
* @param snowflake the user's snowflake ID
* @param tzCode the user's configured timezone
*/
TimeZoneConfig(long snowflake, @NonNull String tzCode) {
@ApiStatus.Internal
public TimeZoneConfig(
@BsonId long snowflake,
@BsonProperty("tzCode") @NonNull String tzCode
) {
this.snowflake = snowflake;
this.tzCode = tzCode;
}
Expand All @@ -49,20 +51,33 @@ public TimeZoneConfig(long snowflake, @NonNull ZoneId tz) {
this(snowflake, tz.getId());
}

@Override
public long getIdLong() {
/**
* Gets the snowflake/ID of this config.
*
* @return snowflake/ID
*/
@BsonId
public long getId() {
return snowflake;
}

/**
* Gets the timezone code of this config.
*
* @return timezone code
*/
@BsonProperty("tzCode")
public @NonNull String getTimeZoneCode() {
if (tzCode == null)
throw new IllegalStateException("Language code is null");
return tzCode;
}

/**
* Gets the timezone of this config.
*
* @return timezone
*/
@BsonIgnore
public @NonNull ZoneId getTimeZone() {
if (tzCode == null)
throw new IllegalStateException("Time zone code is null");
return ZoneId.of(tzCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public static void consumeCommandResult(@NonNull CommandInteraction interaction,
case Mono<?> mono -> mono.subscribe(res -> consumeCommandResult(interaction, res));
case CompletableFuture<?> fut -> fut.thenAccept(res -> consumeCommandResult(interaction, res));
// terminal cases:
case null -> {}
case null -> interaction.deferReply().queue();
case Boolean ephemeral -> interaction.deferReply(ephemeral).queue();
case QuasiMessage message -> message.text().asString(Context.fromInteraction(interaction)).subscribe(string -> {
var action = interaction.reply(string);
message.modifier().accept(action);
Expand Down

0 comments on commit 69ba2f9

Please sign in to comment.