Add notes to hosts, instances, users and other resources (#1000)

This commit is contained in:
Bastiaan authored and GitHub committed 2026-10-18 20:06:15 +03:00
1 parent ee39945c83
commit cbf2b10782
25 files changed
+830 -52

No files matched your search

+74
View File
@@ -0,0 +1,74 @@
import { Type, Static } from "@sinclair/typebox";
/**
* Free text note attached to a resource in the cluster.
*
* Notes are keyed by the type of resource and the id of the resource within
* that type. Core resource types are "host", "instance", "user", "role",
* "mod_pack", "mod" and "save", plugins may define their own.
*/
export default class Note {
constructor(
/** Type of resource this note is attached to. */
public resourceType: string,
/** Id of the resource within its type, as a string. */
public resourceId: string,
/** Text of the note. */
public content: string,
/** Name of the user that last changed the note. */
public updatedBy = "",
/** Millisecond Unix timestamp this entry was last updated at. */
public updatedAtMs = 0,
/** True if this note has been removed. */
public isDeleted = false,
) { }
/** Unique key for this note, resource type and id joined with a slash. */
get id() {
return Note.idFor(this.resourceType, this.resourceId);
}
static idFor(resourceType: string, resourceId: string | number) {
return `${resourceType}/${resourceId}`;
}
static jsonSchema = Type.Object({
"resourceType": Type.String(),
"resourceId": Type.String(),
"content": Type.String(),
"updatedBy": Type.Optional(Type.String()),
"updatedAtMs": Type.Optional(Type.Number()),
"isDeleted": Type.Optional(Type.Boolean()),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(
json.resourceType,
json.resourceId,
json.content,
json.updatedBy,
json.updatedAtMs,
json.isDeleted,
);
}
toJSON() {
const json = {
resourceType: this.resourceType,
resourceId: this.resourceId,
content: this.content,
} as Static<typeof Note.jsonSchema>;
if (this.updatedBy !== "") {
json.updatedBy = this.updatedBy;
}
if (this.updatedAtMs !== 0) {
json.updatedAtMs = this.updatedAtMs;
}
if (this.isDeleted !== false) {
json.isDeleted = this.isDeleted;
}
return json;
}
}
+2
View File
@@ -12,6 +12,7 @@ export { default as ModuleInfo } from "./ModuleInfo.js";
export { default as Permission } from "./Permission.js";
export { default as PlayerStats } from "./PlayerStats.js";
export { default as Role } from "./Role.js";
export { default as Note } from "./Note.js";
export { default as InstanceDetails, InstanceStatus } from "./InstanceDetails.js";
export { default as HostDetails } from "./HostDetails.js";
export { default as UserDetails } from "./UserDetails.js";
@@ -24,5 +25,6 @@ export * from "./messages_instance.js";
export * from "./messages_mod.js";
export * from "./messages_user.js";
export * from "./messages_role.js";
export * from "./messages_note.js";
export * from "./messages_plugin.js";
export * from "./version.js";
+59
View File
@@ -0,0 +1,59 @@
import { Type, Static } from "@sinclair/typebox";
import { jsonArray } from "./composites.js";
import Note from "./Note.js";
export class NoteListRequest {
declare ["constructor"]: typeof NoteListRequest;
static type = "request" as const;
static src = "control" as const;
static dst = "controller" as const;
static permission = "core.note.list" as const;
static Response = jsonArray(Note);
}
export class NoteUpdatesEvent {
declare ["constructor"]: typeof NoteUpdatesEvent;
static type = "event" as const;
static src = "controller" as const;
static dst = "control" as const;
static permission = "core.note.subscribe" as const;
constructor(
public updates: Note[],
) { }
static jsonSchema = Type.Object({
"updates": Type.Array(Note.jsonSchema),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.updates.map(update => Note.fromJSON(update)));
}
}
/**
* Set the note of a resource. An empty content removes the note.
*/
export class NoteSetRequest {
declare ["constructor"]: typeof NoteSetRequest;
static type = "request" as const;
static src = "control" as const;
static dst = "controller" as const;
static permission = "core.note.update" as const;
constructor(
public resourceType: string,
public resourceId: string,
public content: string,
) { }
static jsonSchema = Type.Object({
"resourceType": Type.String(),
"resourceId": Type.String(),
"content": Type.String(),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.resourceType, json.resourceId, json.content);
}
}