mirror of
https://github.com/sortedcord/omnia.git
synced 2026-07-22 03:52:48 +05:30
refactor: Move attributes and entity to @omnia/core
This commit is contained in:
114
cli/src/index.ts
114
cli/src/index.ts
@@ -1,113 +1 @@
|
||||
export enum AttributeVisibility {
|
||||
PUBLIC = "PUBLIC",
|
||||
PRIVATE = "PRIVATE",
|
||||
}
|
||||
|
||||
export class Attribute {
|
||||
readonly name: string;
|
||||
private value: string;
|
||||
private visibility: AttributeVisibility;
|
||||
private allowedEntities: Set<string>;
|
||||
|
||||
constructor(name: string, value: string, visibility: AttributeVisibility) {
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
this.visibility = visibility;
|
||||
this.allowedEntities = new Set();
|
||||
}
|
||||
|
||||
setValue(newValue: string) {
|
||||
this.value = newValue;
|
||||
}
|
||||
|
||||
getValue(): string {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
hasAccess(objectId: string): boolean {
|
||||
return (
|
||||
this.visibility === AttributeVisibility.PUBLIC ||
|
||||
this.allowedEntities.has(objectId)
|
||||
);
|
||||
}
|
||||
|
||||
getVisibility(): AttributeVisibility {
|
||||
return this.visibility;
|
||||
}
|
||||
|
||||
setPublic() {
|
||||
this.visibility = AttributeVisibility.PUBLIC;
|
||||
}
|
||||
|
||||
setPrivate() {
|
||||
this.allowedEntities.clear();
|
||||
this.visibility = AttributeVisibility.PRIVATE;
|
||||
}
|
||||
|
||||
grantAccess(objectId: string) {
|
||||
if (this.visibility === AttributeVisibility.PRIVATE) {
|
||||
this.allowedEntities.add(objectId);
|
||||
}
|
||||
}
|
||||
|
||||
revokeAccess(objectId: string) {
|
||||
if (
|
||||
this.visibility === AttributeVisibility.PRIVATE &&
|
||||
this.allowedEntities.has(objectId)
|
||||
) {
|
||||
this.allowedEntities.delete(objectId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export interface IAttribute {
|
||||
id: string;
|
||||
attributes: Map<string, Attribute>;
|
||||
|
||||
addAttribute(
|
||||
name: string,
|
||||
value: string,
|
||||
visibility: AttributeVisibility,
|
||||
): void;
|
||||
getVisibileAttributesFor(viewerId: string): Attribute[];
|
||||
}
|
||||
|
||||
export abstract class AttributableObject implements IAttribute {
|
||||
readonly id: string = crypto.randomUUID();
|
||||
readonly attributes: Map<string, Attribute> = new Map<string, Attribute>();
|
||||
|
||||
addAttribute(
|
||||
name: string,
|
||||
value: string,
|
||||
visibility: AttributeVisibility = AttributeVisibility.PRIVATE,
|
||||
allowedEntities: Set<string> | null = null,
|
||||
): void {
|
||||
if (this.attributes.has(name))
|
||||
throw Error(`Attribute ${name} already exists`);
|
||||
|
||||
this.attributes.set(name, new Attribute(name, value, visibility));
|
||||
if (visibility === AttributeVisibility.PRIVATE && allowedEntities != null) {
|
||||
for (const entity of allowedEntities) {
|
||||
this.attributes.get(name)?.grantAccess(entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
removeAttribute(name: string): void {
|
||||
if (!this.attributes.has(name))
|
||||
throw Error(`Attribute ${name} does not exist`);
|
||||
this.attributes.delete(name);
|
||||
}
|
||||
|
||||
getVisibileAttributesFor(viewerId: string): Attribute[] {
|
||||
return Array.from(this.attributes.values()).filter((attr) =>
|
||||
attr.hasAccess(viewerId),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export class Entity extends AttributableObject {
|
||||
constructor(name: string) {
|
||||
super();
|
||||
}
|
||||
}
|
||||
export {};
|
||||
|
||||
107
packages/core/src/attribute.ts
Normal file
107
packages/core/src/attribute.ts
Normal file
@@ -0,0 +1,107 @@
|
||||
export enum AttributeVisibility {
|
||||
PUBLIC = "PUBLIC",
|
||||
PRIVATE = "PRIVATE",
|
||||
}
|
||||
|
||||
export class Attribute {
|
||||
readonly name: string;
|
||||
private value: string;
|
||||
private visibility: AttributeVisibility;
|
||||
private allowedEntities: Set<string>;
|
||||
|
||||
constructor(name: string, value: string, visibility: AttributeVisibility) {
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
this.visibility = visibility;
|
||||
this.allowedEntities = new Set();
|
||||
}
|
||||
|
||||
setValue(newValue: string) {
|
||||
this.value = newValue;
|
||||
}
|
||||
|
||||
getValue(): string {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
hasAccess(objectId: string): boolean {
|
||||
return (
|
||||
this.visibility === AttributeVisibility.PUBLIC ||
|
||||
this.allowedEntities.has(objectId)
|
||||
);
|
||||
}
|
||||
|
||||
getVisibility(): AttributeVisibility {
|
||||
return this.visibility;
|
||||
}
|
||||
|
||||
setPublic() {
|
||||
this.visibility = AttributeVisibility.PUBLIC;
|
||||
}
|
||||
|
||||
setPrivate() {
|
||||
this.allowedEntities.clear();
|
||||
this.visibility = AttributeVisibility.PRIVATE;
|
||||
}
|
||||
|
||||
grantAccess(objectId: string) {
|
||||
if (this.visibility === AttributeVisibility.PRIVATE) {
|
||||
this.allowedEntities.add(objectId);
|
||||
}
|
||||
}
|
||||
|
||||
revokeAccess(objectId: string) {
|
||||
if (
|
||||
this.visibility === AttributeVisibility.PRIVATE &&
|
||||
this.allowedEntities.has(objectId)
|
||||
) {
|
||||
this.allowedEntities.delete(objectId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export interface IAttribute {
|
||||
id: string;
|
||||
attributes: Map<string, Attribute>;
|
||||
|
||||
addAttribute(
|
||||
name: string,
|
||||
value: string,
|
||||
visibility: AttributeVisibility,
|
||||
): void;
|
||||
getVisibileAttributesFor(viewerId: string): Attribute[];
|
||||
}
|
||||
|
||||
export abstract class AttributableObject implements IAttribute {
|
||||
readonly id: string = crypto.randomUUID();
|
||||
readonly attributes: Map<string, Attribute> = new Map<string, Attribute>();
|
||||
|
||||
addAttribute(
|
||||
name: string,
|
||||
value: string,
|
||||
visibility: AttributeVisibility = AttributeVisibility.PRIVATE,
|
||||
allowedEntities: Set<string> | null = null,
|
||||
): void {
|
||||
if (this.attributes.has(name))
|
||||
throw Error(`Attribute ${name} already exists`);
|
||||
|
||||
this.attributes.set(name, new Attribute(name, value, visibility));
|
||||
if (visibility === AttributeVisibility.PRIVATE && allowedEntities != null) {
|
||||
for (const entity of allowedEntities) {
|
||||
this.attributes.get(name)?.grantAccess(entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
removeAttribute(name: string): void {
|
||||
if (!this.attributes.has(name))
|
||||
throw Error(`Attribute ${name} does not exist`);
|
||||
this.attributes.delete(name);
|
||||
}
|
||||
|
||||
getVisibileAttributesFor(viewerId: string): Attribute[] {
|
||||
return Array.from(this.attributes.values()).filter((attr) =>
|
||||
attr.hasAccess(viewerId),
|
||||
);
|
||||
}
|
||||
}
|
||||
7
packages/core/src/entity.ts
Normal file
7
packages/core/src/entity.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { AttributableObject } from "./attribute.js";
|
||||
|
||||
export class Entity extends AttributableObject {
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
}
|
||||
@@ -1 +1,3 @@
|
||||
export {};
|
||||
export * from "./attribute.js";
|
||||
export * from "./entity.js";
|
||||
export * from "./world.js";
|
||||
|
||||
7
packages/core/src/world.ts
Normal file
7
packages/core/src/world.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { AttributableObject } from "./attribute.js";
|
||||
|
||||
export class World extends AttributableObject {
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user