Skip to content

Commit

Permalink
feat(application): implemented auto model decoding for ApplicationRol…
Browse files Browse the repository at this point in the history
…eConnectionMetadata.java
  • Loading branch information
seailz committed Sep 19, 2023
1 parent 8c89623 commit 560604b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.seailz.discordjar.utils.model.DiscordJarProp;
import com.seailz.discordjar.utils.model.JSONProp;
import com.seailz.discordjar.utils.model.Model;
import com.seailz.discordjar.utils.model.ModelDecoder;
import com.seailz.discordjar.utils.rest.DiscordRequest;
import com.seailz.discordjar.utils.rest.DiscordResponse;
import com.seailz.discordjar.utils.flag.BitwiseUtil;
Expand Down Expand Up @@ -223,7 +224,7 @@ public List<ApplicationRoleConnectionMetadata> getRoleConnections() {

if (response.code() == 200) {
return new JSONArray(response.body()).toList().stream()
.map(o -> ApplicationRoleConnectionMetadata.decompile((JSONObject) o))
.map(o -> (ApplicationRoleConnectionMetadata) ModelDecoder.decodeObject((JSONObject) o, ApplicationRoleConnectionMetadata.class, discordJar))
.toList();
} else {
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.seailz.discordjar.model.application;

import com.seailz.discordjar.core.Compilerable;
import com.seailz.discordjar.utils.model.JSONProp;
import com.seailz.discordjar.utils.model.Model;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.function.Function;

/**
* A representation of role connection metadata for an application. <p>
Expand All @@ -16,73 +19,80 @@
* its metadata will appear in the role verification configuration when the
* application has been added as a verification method to the role. <p>
*
* When a user connects their account using the bot's role_connections_verification_url,
* When a user connects their account using the bots role_connections_verification_url,
* the bot will update a user's role connection with metadata
* using the OAuth2 {@code role_connections.write} scope.
*
* @author Seailz
* @since 1.0
* @see Application
*/
public record ApplicationRoleConnectionMetadata(
Type type,
String key,
String name,
HashMap<String, String> nameLocalizations,
String description,
HashMap<String, String> descriptionLocalizations
) implements Compilerable {

public class ApplicationRoleConnectionMetadata implements Model {
@JSONProp("type")
private Type type;
@JSONProp("key")
private String key;
@JSONProp("name")
private String name;
@JSONProp("name_localizations")
private HashMap<String, String> nameLocalizations;
@JSONProp("description")
private String description;
@JSONProp("description_localizations")
private HashMap<String, String> descriptionLocalizations;

@Override
public JSONObject compile() {
JSONObject json = new JSONObject();
json.put("type", type.getCode());
json.put("key", key);
json.put("name", name);
if (!nameLocalizations.isEmpty()) json.put("name_localizations", new JSONObject(nameLocalizations));
json.put("description", description);
if (!descriptionLocalizations.isEmpty()) json.put("description_localizations", new JSONObject(descriptionLocalizations));
return json;
public HashMap<String, Function<JSONObject, ?>> customDecoders() {
return new HashMap<>(){{
put("name_localizations", (json) -> {
if (json.has("name_localizations")) {
JSONObject nameLocalizationsJson = json.getJSONObject("name_localizations");
HashMap<String, String> nameLocalizations = new HashMap<>();
for (String key1 : nameLocalizationsJson.keySet()) {
nameLocalizations.put(key1, nameLocalizationsJson.getString(key1));
}
return nameLocalizations;
}
return null;
});

put("description_localizations", (json) -> {
if (json.has("description_localizations")) {
JSONObject descriptionLocalizationsJson = json.getJSONObject("description_localizations");
HashMap<String, String> descriptionLocalizations = new HashMap<>();
for (String key1 : descriptionLocalizationsJson.keySet()) {
descriptionLocalizations.put(key1, descriptionLocalizationsJson.getString(key1));
}
return descriptionLocalizations;
}
return null;
});
}};
}

@NotNull
@Contract("_ -> new")
public static ApplicationRoleConnectionMetadata decompile(@NotNull JSONObject json) {
Type type = json.has("type") ? Type.fromCode(json.getInt("type")) : null;
String key = json.has("key") ? json.getString("key") : null;
String name = json.has("name") ? json.getString("name") : null;
HashMap<String, String> nameLocalizations;

if (json.has("name_localizations")) {
JSONObject nameLocalizationsJson = json.getJSONObject("name_localizations");
nameLocalizations = new HashMap<>();
for (String key1 : nameLocalizationsJson.keySet()) {
nameLocalizations.put(key1, nameLocalizationsJson.getString(key1));
}
} else {
nameLocalizations = null;
}
public Type type() {
return type;
}

public String key() {
return key;
}

String description = json.has("description") ? json.getString("description") : null;
HashMap<String, String> descriptionLocalizations;
public String name() {
return name;
}

if (json.has("description_localizations")) {
JSONObject descriptionLocalizationsJson = json.getJSONObject("description_localizations");
descriptionLocalizations = new HashMap<>();
for (String key1 : descriptionLocalizationsJson.keySet()) {
descriptionLocalizations.put(key1, descriptionLocalizationsJson.getString(key1));
}
} else {
descriptionLocalizations = null;
}
public HashMap<String, String> nameLocalizations() {
return nameLocalizations;
}

public String description() {
return description;
}

return new ApplicationRoleConnectionMetadata(
type == null ? Type.UNKNOWN : type,
key,
name,
nameLocalizations,
description,
descriptionLocalizations
);
public HashMap<String, String> descriptionLocalizations() {
return descriptionLocalizations;
}

public ApplicationRoleConnectionMetadata(Type type, String key, String name, HashMap<String, String> nameLocalizations, String description, HashMap<String, String> descriptionLocalizations) {
Expand All @@ -98,6 +108,8 @@ public ApplicationRoleConnectionMetadata(Type type, String key, String name, Str
this(type, key, name, new HashMap<>(), description, new HashMap<>());
}

private ApplicationRoleConnectionMetadata() {}

public ApplicationRoleConnectionMetadata addNameLocalization(String language, String name) {
this.nameLocalizations.put(language, name);
return this;
Expand Down

0 comments on commit 560604b

Please sign in to comment.