mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-18 07:33:12 +09:00
Add notes to hosts, instances, users and other resources (#1000)
This commit is contained in:
1 parent
ee39945c83
commit
cbf2b10782
25 files changed
+830
-52
No files matched your search
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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";
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import * as instance from "../data/messages_instance.js";
|
||||
import * as mod from "../data/messages_mod.js";
|
||||
import * as user from "../data/messages_user.js";
|
||||
import * as role from "../data/messages_role.js";
|
||||
import * as note from "../data/messages_note.js";
|
||||
import * as plugin from "../data/messages_plugin.js";
|
||||
import * as subscriptions from "../subscriptions.js";
|
||||
import type { RequestClass, EventClass } from "./link.js";
|
||||
@@ -126,6 +127,10 @@ export const dataClasses: (RequestClass<unknown, unknown> | EventClass<unknown>)
|
||||
role.RoleGrantDefaultPermissionsRequest,
|
||||
role.RoleDeleteRequest,
|
||||
|
||||
note.NoteListRequest,
|
||||
note.NoteUpdatesEvent,
|
||||
note.NoteSetRequest,
|
||||
|
||||
user.UserGetRequest,
|
||||
user.UserListRequest,
|
||||
user.UserCreateRequest,
|
||||
|
||||
@@ -462,6 +462,24 @@ export const corePermissions = [
|
||||
description: "Permanently delete roles.",
|
||||
},
|
||||
|
||||
{
|
||||
name: "core.note.list",
|
||||
title: "List notes",
|
||||
description: "Get the full list of notes attached to resources in the cluster.",
|
||||
grantByDefault: true,
|
||||
},
|
||||
{
|
||||
name: "core.note.subscribe",
|
||||
title: "Subscribe to note updates",
|
||||
description: "Subscribe to be notified when notes attached to resources are updated.",
|
||||
grantByDefault: true,
|
||||
},
|
||||
{
|
||||
name: "core.note.update",
|
||||
title: "Update notes",
|
||||
description: "Write, edit and remove notes attached to resources in the cluster.",
|
||||
},
|
||||
|
||||
{
|
||||
name: "core.user.get",
|
||||
title: "Get user",
|
||||
|
||||
Reference in new issue
Block a user