Skip to content

Commit

Permalink
WebUI 接口,获取活动警告 #162
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghost-chu committed Jun 20, 2024
1 parent 3c2db06 commit 343a8b4
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ghostchu.peerbanhelper;

import com.ghostchu.peerbanhelper.alert.AlertManager;
import com.ghostchu.peerbanhelper.btn.BtnNetwork;
import com.ghostchu.peerbanhelper.database.DatabaseHelper;
import com.ghostchu.peerbanhelper.database.DatabaseManager;
Expand Down Expand Up @@ -102,6 +103,8 @@ public class PeerBanHelperServer {
private WatchDog banWaveWatchDog;
@Getter
private JavalinWebContainer webContainer;
@Getter
private AlertManager alertManager;


public PeerBanHelperServer(String pbhServerAddress, YamlConfiguration profile, YamlConfiguration mainConfig) throws SQLException {
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/ghostchu/peerbanhelper/alert/Alert.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.ghostchu.peerbanhelper.alert;

import java.util.logging.Level;

/**
* 警告信息
*
* @param id 相同内容和事件应当使用相同的 ID,避免重复推送
* @param title 警告标题
* @param description 警告内容
* @param level 警告等级
*/
public record Alert(String id, String title, String description, Level level) {
}
31 changes: 31 additions & 0 deletions src/main/java/com/ghostchu/peerbanhelper/alert/AlertManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.ghostchu.peerbanhelper.alert;

import com.ghostchu.peerbanhelper.Main;
import com.ghostchu.peerbanhelper.event.NewAlertCreated;
import com.google.common.collect.ImmutableList;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class AlertManager {
private Map<String, Alert> alerts = new ConcurrentHashMap<>();

public void addAlert(Alert alert) {
if (alerts.put(alert.id(), alert) == null) {
Main.getEventBus().register(new NewAlertCreated(alert));
}
}

public boolean removeAlert(String id) {
return alerts.remove(id) != null;
}

public boolean removeAlert(Alert alert) {
return alerts.remove(alert.id()) != null;
}

public List<Alert> getAlerts() {
return ImmutableList.copyOf(alerts.values());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.ghostchu.peerbanhelper.event;

import com.ghostchu.peerbanhelper.alert.Alert;
import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class NewAlertCreated {
private Alert alert;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.ghostchu.peerbanhelper.module.impl.webapi;

import com.ghostchu.peerbanhelper.PeerBanHelperServer;
import com.ghostchu.peerbanhelper.module.AbstractFeatureModule;
import com.ghostchu.peerbanhelper.web.Role;
import io.javalin.http.Context;
import org.bspfsystems.yamlconfiguration.file.YamlConfiguration;
import org.jetbrains.annotations.NotNull;

public class PBHAlertController extends AbstractFeatureModule {
public PBHAlertController(PeerBanHelperServer server, YamlConfiguration profile) {
super(server, profile);
}

@Override
public boolean isConfigurable() {
return false;
}

@Override
public @NotNull String getName() {
return "WebAPI - Alerts";
}

@Override
public @NotNull String getConfigName() {
return "webapi-alerts";
}

@Override
public void onEnable() {
getServer().getWebContainer().javalin().get("/api/alerts", this::handleListing, Role.USER_READ);
getServer().getWebContainer().javalin().delete("/api/alert/{id}", this::handleDelete, Role.USER_WRITE);
}

private void handleListing(Context ctx) {
ctx.status(200);
ctx.json(getServer().getAlertManager().getAlerts());
}

private void handleDelete(Context ctx) {
if (getServer().getAlertManager().removeAlert(ctx.pathParam("id"))) {
ctx.status(204);
} else {
ctx.status(404);
}
}


@Override
public void onDisable() {

}
}

0 comments on commit 343a8b4

Please sign in to comment.