WIP: report implementation for varo
This commit is contained in:
@ -0,0 +1,28 @@
|
||||
package eu.mhsl.craftattack.spawn.common.api;
|
||||
|
||||
import eu.mhsl.craftattack.spawn.core.config.Configuration;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.util.Objects;
|
||||
|
||||
public class VaroApi {
|
||||
private final static ConfigurationSection apiConfig = Objects.requireNonNull(Configuration.cfg.getConfigurationSection("varoApi"));
|
||||
public final static String basePath = apiConfig.getString("endpoint");
|
||||
public final static String apiSecret = apiConfig.getString("auth");
|
||||
|
||||
public static URI getBaseUri() {
|
||||
Objects.requireNonNull(basePath);
|
||||
try {
|
||||
return new URI(basePath);
|
||||
} catch(URISyntaxException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void authorizationHeader(HttpRequest.Builder builder) {
|
||||
builder.header("Authorization", apiSecret);
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package eu.mhsl.craftattack.spawn.common.api.repositories;
|
||||
|
||||
import eu.mhsl.craftattack.spawn.core.api.client.ReqResp;
|
||||
import eu.mhsl.craftattack.spawn.common.api.CraftAttackApi;
|
||||
import java.util.UUID;
|
||||
|
||||
public class CraftAttackReportRepository extends ReportRepository {
|
||||
public CraftAttackReportRepository() {
|
||||
super(CraftAttackApi.getBaseUri(), new RequestModifier(CraftAttackApi::withAuthorizationSecret, null));
|
||||
}
|
||||
|
||||
public ReqResp<PlayerReports> queryReports(UUID player) {
|
||||
return this.get(
|
||||
"report",
|
||||
(parameters) -> parameters.addParameter("uuid", player.toString()),
|
||||
PlayerReports.class
|
||||
);
|
||||
}
|
||||
|
||||
public ReqResp<ReportUrl> createReport(ReportCreationInfo data) {
|
||||
return this.post(
|
||||
"report",
|
||||
data,
|
||||
ReportUrl.class
|
||||
);
|
||||
}
|
||||
}
|
@ -1,17 +1,18 @@
|
||||
package eu.mhsl.craftattack.spawn.common.api.repositories;
|
||||
|
||||
import eu.mhsl.craftattack.spawn.core.api.client.HttpRepository;
|
||||
import eu.mhsl.craftattack.spawn.core.api.client.ReqResp;
|
||||
import eu.mhsl.craftattack.spawn.common.api.CraftAttackApi;
|
||||
import eu.mhsl.craftattack.spawn.core.api.client.RepositoryLoader;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class ReportRepository extends HttpRepository {
|
||||
public ReportRepository() {
|
||||
super(CraftAttackApi.getBaseUri(), new RequestModifier(CraftAttackApi::withAuthorizationSecret, null));
|
||||
@RepositoryLoader.Abstraction
|
||||
public abstract class ReportRepository extends HttpRepository {
|
||||
public ReportRepository(URI basePath, RequestModifier... baseRequestModifier) {
|
||||
super(basePath, baseRequestModifier);
|
||||
}
|
||||
|
||||
public record ReportCreationInfo(@NotNull UUID reporter, @Nullable UUID reported, String reason) {
|
||||
@ -38,20 +39,4 @@ public class ReportRepository extends HttpRepository {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ReqResp<PlayerReports> queryReports(UUID player) {
|
||||
return this.get(
|
||||
"report",
|
||||
(parameters) -> parameters.addParameter("uuid", player.toString()),
|
||||
PlayerReports.class
|
||||
);
|
||||
}
|
||||
|
||||
public ReqResp<ReportUrl> createReport(ReportCreationInfo data) {
|
||||
return this.post(
|
||||
"report",
|
||||
data,
|
||||
ReportUrl.class
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,45 @@
|
||||
package eu.mhsl.craftattack.spawn.common.api.repositories;
|
||||
|
||||
import eu.mhsl.craftattack.spawn.common.api.VaroApi;
|
||||
import eu.mhsl.craftattack.spawn.core.api.client.ReqResp;
|
||||
import org.apache.commons.lang3.NotImplementedException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class VaroReportRepository extends ReportRepository {
|
||||
public VaroReportRepository() {
|
||||
super(VaroApi.getBaseUri(), new RequestModifier(null, VaroApi::authorizationHeader));
|
||||
}
|
||||
|
||||
public ReqResp<PlayerReports> queryReports(UUID player) {
|
||||
throw new NotImplementedException("Report querying is not supported in Varo!");
|
||||
}
|
||||
|
||||
public ReqResp<ReportUrl> createReport(ReportCreationInfo data) {
|
||||
return this.post(
|
||||
"report",
|
||||
data,
|
||||
ReportUrl.class
|
||||
);
|
||||
}
|
||||
|
||||
public record StrikeCreationInfo(
|
||||
@Nullable UUID reporter, // null for automatic creations
|
||||
@NotNull UUID reported,
|
||||
@NotNull String reason,
|
||||
@Nullable String body,
|
||||
@Nullable String notice,
|
||||
@Nullable String statement,
|
||||
int strike_reason_id // internal strike mapping
|
||||
) {
|
||||
}
|
||||
public ReqResp<Void> createStrike(StrikeCreationInfo data) {
|
||||
return this.put(
|
||||
"report",
|
||||
data,
|
||||
Void.class
|
||||
);
|
||||
}
|
||||
}
|
@ -1,8 +1,10 @@
|
||||
package eu.mhsl.craftattack.spawn.common.appliances.metaGameplay.report;
|
||||
|
||||
import eu.mhsl.craftattack.spawn.common.api.repositories.ReportRepository;
|
||||
import eu.mhsl.craftattack.spawn.common.api.repositories.VaroReportRepository;
|
||||
import eu.mhsl.craftattack.spawn.core.Main;
|
||||
import eu.mhsl.craftattack.spawn.core.api.client.ReqResp;
|
||||
import eu.mhsl.craftattack.spawn.common.api.repositories.ReportRepository;
|
||||
import eu.mhsl.craftattack.spawn.common.api.repositories.CraftAttackReportRepository;
|
||||
import eu.mhsl.craftattack.spawn.core.appliance.Appliance;
|
||||
import eu.mhsl.craftattack.spawn.core.appliance.ApplianceCommand;
|
||||
import net.kyori.adventure.text.Component;
|
||||
@ -36,7 +38,7 @@ public class Report extends Appliance {
|
||||
}
|
||||
|
||||
public void reportToUnknown(@NotNull Player issuer) {
|
||||
ReportRepository.ReportCreationInfo request = new ReportRepository.ReportCreationInfo(issuer.getUniqueId(), null, "");
|
||||
CraftAttackReportRepository.ReportCreationInfo request = new CraftAttackReportRepository.ReportCreationInfo(issuer.getUniqueId(), null, "");
|
||||
Bukkit.getScheduler().runTaskAsynchronously(
|
||||
Main.instance(),
|
||||
() -> this.createReport(issuer, request)
|
||||
@ -62,10 +64,11 @@ public class Report extends Appliance {
|
||||
}
|
||||
|
||||
private void createReport(Player issuer, ReportRepository.ReportCreationInfo reportRequest) {
|
||||
ReqResp<ReportRepository.ReportUrl> createdReport = this.queryRepository(ReportRepository.class)
|
||||
ReqResp<ReportRepository.ReportUrl> createdReport = this.queryRepository(VaroReportRepository.class)
|
||||
.createReport(reportRequest);
|
||||
|
||||
switch(createdReport.status()) {
|
||||
case 200: // varo-endpoint specific
|
||||
case 201:
|
||||
issuer.sendMessage(
|
||||
Component.text()
|
||||
@ -112,7 +115,7 @@ public class Report extends Appliance {
|
||||
}
|
||||
|
||||
public void queryReports(Player issuer) {
|
||||
ReqResp<ReportRepository.PlayerReports> userReports = this.queryRepository(ReportRepository.class)
|
||||
ReqResp<ReportRepository.PlayerReports> userReports = this.queryRepository(VaroReportRepository.class)
|
||||
.queryReports(issuer.getUniqueId());
|
||||
|
||||
if(userReports.status() != 200) {
|
||||
|
Reference in New Issue
Block a user