pull/3/head
SiRanWeb 2022-10-23 19:12:17 +03:00
commit 609485b378
11 zmienionych plików z 2043 dodań i 0 usunięć

3
.gitignore vendored 100644
Wyświetl plik

@ -0,0 +1,3 @@
node_modules/
build/
.idea/

1831
package-lock.json wygenerowano 100644

Plik diff jest za duży Load Diff

18
package.json 100644
Wyświetl plik

@ -0,0 +1,18 @@
{
"name": "activitypub-js-models",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"ts:watch": "tsc -p ./tsconfig.json",
"start": "node --es-module-specifier-resolution=node build/index.js",
"start:dev": "nodemon --es-module-specifier-resolution=node build/index.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"nodemon": "^2.0.20",
"typescript": "^4.8.4"
}
}

16
src/index.ts 100644
Wyświetl plik

@ -0,0 +1,16 @@
import express from 'express';
import { ObjectAP } from './models/Object.model';
// const app = express();
// app.listen(3000, () => console.log('Listening on port 3000!'));
const obj = new ObjectAP({
id: 'someId',
});
const obj2 = new ObjectAP({
cc: '123',
content: obj,
}).addContext();
console.log(obj2.plain());
console.log(obj2.json());

Wyświetl plik

@ -0,0 +1,14 @@
import {ModelBaseAP} from "./Base.model";
import {IntransitiveActivityAPFields} from "./IntransitiveActivity.model";
// TODO: types
// https://www.w3.org/TR/activitystreams-core/#activities
export interface ActivityAPFields extends IntransitiveActivityAPFields {
object?: any; // Object?
}
export class ActivityAP extends ModelBaseAP<ActivityAPFields>{
constructor(fields: ActivityAPFields) {
super(fields);
}
}

Wyświetl plik

@ -0,0 +1,27 @@
import {ModelBaseAP} from "./Base.model";
import {ObjectAPFields} from "./Object.model";
export enum ActorTypes {
Application = 'Application',
Group = 'Group',
Organization = 'Organization',
Person = 'Person',
Service = 'Service'
}
// TODO: types
// https://www.w3.org/TR/activitystreams-core/#actors
export interface ActorAPFields extends ObjectAPFields {
type?: any | ActorTypes; // ?
actor?: any; // Actor?
target?: any;
origin?: any;
result?: any;
instrument?: any;
}
export class ActorAP extends ModelBaseAP<ActorAPFields>{
constructor(fields: ActorAPFields) {
super(fields);
}
}

Wyświetl plik

@ -0,0 +1,43 @@
// TODO: types
export type Context = string | any[];
export interface ModelBaseAPWithContext {
'@context'?: Context;
}
export class ModelBaseAP<T> {
public fields: T & ModelBaseAPWithContext;
constructor(fields: T) {
// TODO: make recursive copy
this.fields = {
...fields,
} as T & ModelBaseAPWithContext;
}
// TODO: rewrite (better option to handle multiple contexts)
public addContext(context?: Context): this {
this.fields = {
['@context']: context || 'https://www.w3.org/ns/activitystreams',
...this.fields
}
return this;
}
public plain(): Record<string, any> {
const result = {} as Record<string, any>;
for (const [key, node] of Object.entries(this.fields)) {
if (node instanceof ModelBaseAP) {
result[key] = node.plain();
} else {
result[key] = node;
}
}
// TODO make recursive copy
return result;
}
public json() {
return JSON.stringify(this.plain());
}
}

Wyświetl plik

@ -0,0 +1,18 @@
import {ModelBaseAP} from "./Base.model";
import {ObjectAPFields} from "./Object.model";
// TODO: types
// https://www.w3.org/TR/activitystreams-core/#intransitiveactivities
export interface IntransitiveActivityAPFields extends ObjectAPFields {
actor?: any; // Actor?
target?: any;
origin?: any;
result?: any;
instrument?: any;
}
export class IntransitiveActivityAP extends ModelBaseAP<IntransitiveActivityAPFields>{
constructor(fields: IntransitiveActivityAPFields) {
super(fields);
}
}

Wyświetl plik

@ -0,0 +1,19 @@
import {ModelBaseAP} from "./Base.model";
// TODO: types
// https://www.w3.org/TR/activitystreams-core/#dfn-link
export interface ObjectAPFields {
id?: any;
name?: any;
hreflang?: any;
mediaType?: any;
rel?: any;
height?: any;
width?: any;
}
export class LinkAP extends ModelBaseAP<ObjectAPFields>{
constructor(fields: ObjectAPFields) {
super(fields);
}
}

Wyświetl plik

@ -0,0 +1,39 @@
import {ModelBaseAP} from "./Base.model";
// TODO: types
// https://www.w3.org/TR/activitystreams-core/#object
export interface ObjectAPFields {
id?: any;
type?: any;
attachment?: any;
attributedTo?: any;
audience?: any;
content?: any;
name?: any;
endTime?: any;
generator?: any;
icon?: any;
image?: any; // Link?
inReplyTo?: any;
location?: any;
preview?: any;
published?: any;
replies?: any;
startTime?: any;
summary?: any;
tag?: any;
updated?: any;
url?: any;
to?: any;
bto?: any;
cc?: any;
bcc?: any;
mediaType?: any;
duration?: any;
}
export class ObjectAP extends ModelBaseAP<ObjectAPFields>{
constructor(fields: ObjectAPFields) {
super(fields);
}
}

15
tsconfig.json 100644
Wyświetl plik

@ -0,0 +1,15 @@
{
"compilerOptions": {
"watch": true,
"outDir": "./build",
"target": "es2016",
"module": "ES6",
"moduleResolution": "Node",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "**/*.spec.ts"],
}