-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
31
src/main/java/com/ghostchu/peerbanhelper/alert/AlertManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/ghostchu/peerbanhelper/event/NewAlertCreated.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
54 changes: 54 additions & 0 deletions
54
src/main/java/com/ghostchu/peerbanhelper/module/impl/webapi/PBHAlertController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
|
||
} | ||
} |