Mester-Ulquiorra / web

The frontend of the Ulquiorra UCP (User Control Panel). This does not contain the backend API!
https://ucp.mester.info
BSD Zero Clause License
1 stars 0 forks source link

alert system #6

Closed MesterMan03 closed 1 year ago

MesterMan03 commented 1 year ago

Goal: provide a one-directional (server -> client) way of sending alerts and other types of data, either real-time or async. 
Types AlertType = "appeal" | "punishment" AlertMessage<T extends AlertType> =

  1. type: same as T
  2. data: AlertData

AlertData<"appeal"> (used for appeals that have been processed by the staff team) =

  1. status: "accepted" or "rejected"
  2. reason: string

AlertData<"punishment"> (used for new punishments, always in real-time) = same as the punishment object from /user/punishments

Example

{
  "type": "appeal",
  "data": {
    "status": "accepted",
    "reason": "thx for the bribe"
  }
}

Route changes /user may return a new alerts field with type AlertMessage<unknown>[] when there are unread alerts. /sse returns a Server-Sent Events stream which may occasionally send a alert event with a AlertMessage<unknown> object.

MesterMan03 commented 1 year ago

API route update /sse now returns a SSE stream. This doesn't do much on its own, only sending a heartbeat ping every 15 seconds, but if you include ?test=something at the end of the URL, it'll send a test alert every 10 seconds. In JavaScript, you can read it like this:

const es = new EventSource("https://ucp-api.mester.info/sse?test=hi");
es.addEventListener("alert", (event) => {
  const message = event.data;
  // do something with it
});

The data is sent with JSON encoding, so you might want to JSON.parse() it first.

MesterMan03 commented 1 year ago

API update /sse now returns an appeal alert in production mode.

MesterMan03 commented 1 year ago

Closed in favour of #8