diff --git a/src/common/common.constants.ts b/src/common/common.constants.ts new file mode 100644 index 0000000..b1d6b3c --- /dev/null +++ b/src/common/common.constants.ts @@ -0,0 +1,3 @@ +export const contexts = { + activityStreamsV2: 'https://www.w3.org/ns/activitystreams' +} \ No newline at end of file diff --git a/src/common/common.types.ts b/src/common/common.types.ts new file mode 100644 index 0000000..aa61f30 --- /dev/null +++ b/src/common/common.types.ts @@ -0,0 +1,34 @@ +import { Link } from "../models/Link.model"; +import {ASObject} from "../models/asObject/ASObject.model"; + +// TODO: implement +export type Image = 'placeholder'; +export type Article = 'placeholder'; +export type Audio = 'placeholder'; +export type Document = 'placeholder'; +export type Event = 'placeholder'; +export type Note = 'placeholder'; +export type Page = 'placeholder'; +export type Place = 'placeholder'; +export type Profile = 'placeholder'; +export type Relationship = 'placeholder'; +export type Tombstone = 'placeholder'; +export type Video = 'placeholder'; +export type Mention = 'placeholder'; +export type AnyASObject = ASObject | Article | Audio | Document | Event | Image | Note | Page | Place | Profile | Relationship | Tombstone | Video; +export type DateTime = string; +export type Duration = string; +export type UrlField = string | Link; +export type IconField = string | Image | Link; +export type ImageField = string | Image | Link; +export type AttachmentField = string | AnyASObject | Link; +export type AudienceField = string | AnyASObject | Link; +export type InReplyToField = string | AnyASObject | Link; +export type LocationField = string | AnyASObject | Link; +export type PreviewField = string | AnyASObject | Link; +export type ToField = string | AnyASObject | Link; +export type BtoField = string | AnyASObject | Link; +export type CcField = string | AnyASObject | Link; +export type BccField = string | AnyASObject | Link; +export type AttributedToField = string | AnyASObject | Link | Mention; +export type TagField = string | AnyASObject | Link | Mention; \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 8228f4f..8b13789 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,12 +1 @@ -import { ASObject } from './models/ASObject.model'; -const obj = new ASObject({ - id: 'someId', -}); -const obj2 = new ASObject({ - cc: '123', - content: obj, -}).addContext(); - -console.log(obj2.plain()); -console.log(obj2.json()); diff --git a/src/models/ASBase.model.ts b/src/models/ASBase.model.ts index a1c7cda..d7cb0f5 100644 --- a/src/models/ASBase.model.ts +++ b/src/models/ASBase.model.ts @@ -1,3 +1,5 @@ +import { contexts } from "../common/common.constants"; + // TODO: types // TODO: renaming/refactor export type Context = string | any[]; @@ -17,9 +19,10 @@ export class ASBase { } // TODO: rewrite (better option to handle multiple contexts) - public addContext(context?: Context): this { + // some comment + public setContext(context?: Context): this { this.fields = { - ['@context']: context || 'https://www.w3.org/ns/activitystreams', + ['@context']: context || contexts.activityStreamsV2, ...this.fields } return this; diff --git a/src/models/ASCollectionBase.model.ts b/src/models/ASCollectionBase.model.ts index 048746a..b180cbd 100644 --- a/src/models/ASCollectionBase.model.ts +++ b/src/models/ASCollectionBase.model.ts @@ -1,4 +1,4 @@ -import { ASObject, ASObjectFields } from "./ASObject.model"; +import { ASObject, ASObjectFields } from "./asObject/ASObject.model"; // TODO: types // https://www.w3.org/TR/activitystreams-core/#collection diff --git a/src/models/ASObject.model.ts b/src/models/ASObject.model.ts deleted file mode 100644 index 4ec192d..0000000 --- a/src/models/ASObject.model.ts +++ /dev/null @@ -1,41 +0,0 @@ -import {ASBase} from "./ASBase.model"; - -// TODO: types -// https://www.w3.org/TR/activitystreams-core/#object -export interface ASObjectFields { - /** @remarks Example remark for id */ - 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 ASObject extends ASBase{ - constructor(fields: ASObjectFields) { - super(fields); - } -} \ No newline at end of file diff --git a/src/models/Actor.model.ts b/src/models/Actor.model.ts index f1f7cb7..c8a4c51 100644 --- a/src/models/Actor.model.ts +++ b/src/models/Actor.model.ts @@ -1,5 +1,5 @@ import {ASBase} from "./ASBase.model"; -import {ASObjectFields} from "./ASObject.model"; +import {ASObjectFields} from "./asObject/ASObject.model"; export enum ActorTypes { Application = 'Application', diff --git a/src/models/IntransitiveActivity.model.ts b/src/models/IntransitiveActivity.model.ts index 96baf0d..2e16d92 100644 --- a/src/models/IntransitiveActivity.model.ts +++ b/src/models/IntransitiveActivity.model.ts @@ -1,5 +1,5 @@ import {ASBase} from "./ASBase.model"; -import {ASObjectFields} from "./ASObject.model"; +import {ASObjectFields} from "./asObject/ASObject.model"; // TODO: types // https://www.w3.org/TR/activitystreams-core/#intransitiveactivities diff --git a/src/models/Link.model.ts b/src/models/Link.model.ts index fdb1fc0..aaa3261 100644 --- a/src/models/Link.model.ts +++ b/src/models/Link.model.ts @@ -3,7 +3,7 @@ import {ASBase} from "./ASBase.model"; // TODO: types // https://www.w3.org/TR/activitystreams-core/#dfn-link export interface LinkFields { - id?: any; + id?: string; name?: any; hreflang?: any; mediaType?: any; diff --git a/src/models/asObject/ASObject.model.ts b/src/models/asObject/ASObject.model.ts new file mode 100644 index 0000000..a8ad586 --- /dev/null +++ b/src/models/asObject/ASObject.model.ts @@ -0,0 +1,8 @@ +import {ASBase} from "../ASBase.model"; +import { ASObjectFields } from "./ASObject.types"; + +export class ASObject extends ASBase{ + constructor(fields: ASObjectFields) { + super(fields); + } +} \ No newline at end of file diff --git a/src/models/asObject/ASObject.types.ts b/src/models/asObject/ASObject.types.ts new file mode 100644 index 0000000..38d62c5 --- /dev/null +++ b/src/models/asObject/ASObject.types.ts @@ -0,0 +1,318 @@ +import {Link} from "../Link.model"; +import {Collection} from "../Collection.model"; +import { ASObject } from "./ASObject.model"; +import { + DateTime, + Duration, + UrlField, + IconField, + ImageField, + AttachmentField, + AudienceField, + InReplyToField, + LocationField, + PreviewField, + ToField, + BtoField, + CcField, + BccField, + AttributedToField, + TagField, +} from "../../common/common.types"; + +// TODO: types +// https://www.w3.org/TR/activitystreams-core/#object +export interface ASObjectFields { + /** + * Provides the globally unique identifier for + * an {@link https://www.w3.org/TR/activitystreams-vocabulary/#dfn-object Object} + * + * {@link https://www.w3.org/TR/activitystreams-vocabulary/#dfn-object Docs (@id)} + */ + id?: string; + + /** + * Identifies the {@link https://www.w3.org/TR/activitystreams-vocabulary/#dfn-object Object} + * type. Multiple values may be specified. + * + * {@link https://www.w3.org/TR/activitystreams-vocabulary/#dfn-object Docs (@type)} + */ + type?: string | string[]; + + /** + * Identifies resources attached or related to an object that potentially requires special handling. + * The intent is to provide a model that is at least semantically similar to attachments in email. + * + * {@link https://www.w3.org/ns/activitystreams#attachment Docs} + */ + attachment?: AttachmentField | AttachmentField[]; + + + /** + * Identifies one or more entities to which this object is attributed. + * The attributed entities might not be Actors. For instance, + * an object might be attributed to the completion of another activity. + * + * {@link https://www.w3.org/ns/activitystreams#attributedTo Docs} + */ + attributedTo?: AttributedToField | AttributedToField[]; + + /** + * Identifies one or more entities that represent the total population of entities + * for which the object can be considered to be relevant. + * + * {@link https://www.w3.org/ns/activitystreams#audience Docs} + */ + audience?: AudienceField | AudienceField[]; + + // TODO: only content or contentMap at one time + + /** + * The content or textual representation of the Object encoded as a JSON string. + * By default, the value of content is HTML. The mediaType property can be + * used in the object to indicate a different content type. + * [For multiple language-tagged values use contentMap property]. + * + * {@link https://www.w3.org/ns/activitystreams#content Docs} + */ + content?: string; + + /** + * The content or textual representation of the Object encoded as a JSON string. + * By default, the value of content is HTML. The mediaType property can be + * used in the object to indicate a different content type. + * [For a single value use content property]. + * + * {@link https://www.w3.org/ns/activitystreams#content Docs} + */ + contentMap?: Record; + + // TODO: make type as MIME Media Type + /** + * When used on an Object, identifies the MIME media type of the value of the content property. + * If not specified, the content property is assumed to contain text/html content. + * Works different with Link + * + * {@link https://www.w3.org/ns/activitystreams#mediaType Docs} + */ + mediaType?: string; + + // TODO: only name or nameMap at one time + + /** + * A simple, human-readable, plain-text name for the object. + * HTML markup MUST NOT be included. + * [For multiple language-tagged values use nameMap property] + * + * {@link https://www.w3.org/ns/activitystreams#name Docs} + */ + name?: string; + + /** + * A simple, human-readable, plain-text name for the object. + * HTML markup MUST NOT be included. + * [For a single value use name property] + * + * {@link https://www.w3.org/ns/activitystreams#name Docs} + */ + nameMap?: Record; + + /** + * The date and time describing the actual or expected ending time of the object. + * When used with an Activity object, for instance, the endTime property specifies + * the moment the activity concluded or is expected to conclude. + * + * Must be provided in {@link https://www.w3schools.blog/xsd-date-and-time-data-types xsd:dateTime} + * + * {@link https://www.w3.org/ns/activitystreams#endTime Docs} + * + * @example + * 2020-08-08T08:30:11-03:00Z + * 2020-08-08T08:30:11-03:00 + * 2020-08-08T08:30:11Z + * 2020-08-08T08:30:11 + * 2020-08-08 + */ + endTime?: DateTime; + + /** + * The date and time describing the actual or expected starting time of the object. + * When used with an Activity object, for instance, the startTime property specifies + * the moment the activity began or is scheduled to begin. + * + * Must be provided in {@link https://www.w3schools.blog/xsd-date-and-time-data-types xsd:dateTime} + * + * {@link https://www.w3.org/ns/activitystreams#startTime Docs} + * + * @example + * 2020-08-08T08:30:11-03:00Z + * 2020-08-08T08:30:11-03:00 + * 2020-08-08T08:30:11Z + * 2020-08-08T08:30:11 + * 2020-08-08 + */ + startTime?: DateTime; + + /** + * Identifies the entity (e.g. an application) that generated the object. + * + * {@link https://www.w3.org/ns/activitystreams#generator Docs} + */ + generator?: (string | ASObject | Link); + + /** + * Indicates an entity (or entities) that describes an icon for this object. + * Unlike image property, the icon should have an aspect ratio of one (horizontal) + * to one (vertical) and should be suitable for presentation at a small size. + * + * {@link https://www.w3.org/ns/activitystreams#icon Docs} + */ + icon?: IconField | IconField[]; + + /** + * Indicates an entity that describes an image for this object. + * Unlike the icon property, there are no aspect ratio or display size limitations assumed. + * + * {@link https://www.w3.org/ns/activitystreams#image Docs} + */ + image?: ImageField | ImageField[]; + + /** + * Indicates one or more entities for which this object is considered a response. + * + * {@link https://www.w3.org/ns/activitystreams#inReplyTo Docs} + */ + inReplyTo?: InReplyToField | InReplyToField[]; + + /** + * Indicates one or more physical or logical locations associated with the object. + * + * {@link https://www.w3.org/ns/activitystreams#location Docs} + */ + location?: LocationField | LocationField[]; + + /** + * Identifies an entity that provides a preview of this object. + * + * {@link https://www.w3.org/ns/activitystreams#preview Docs} + */ + preview?: PreviewField; + + /** + * The date and time at which the object was published. + * + * Must be provided in {@link https://www.w3schools.blog/xsd-date-and-time-data-types xsd:dateTime} + * + * {@link https://www.w3.org/ns/activitystreams#published Docs} + * + * @example + * 2020-08-08T08:30:11-03:00Z + * 2020-08-08T08:30:11-03:00 + * 2020-08-08T08:30:11Z + * 2020-08-08T08:30:11 + * 2020-08-08 + */ + published?: DateTime; + + /** + * Identifies a Collection containing objects considered to be responses to this object. + * + * {@link https://www.w3.org/ns/activitystreams#replies Docs} + */ + replies?: Collection; + + // TODO: only summary or summaryMap at one time + + /** + * A natural language summarization of the object encoded as HTML. + * Multiple language tagged summaries MAY be provided. + * [For multiple language-tagged values use summaryMap property] + * + * {@link https://www.w3.org/ns/activitystreams#summary Docs} + */ + summary?: string; + + /** + * A natural language summarization of the object encoded as HTML. + * Multiple language tagged summaries MAY be provided. + * [For a single value use summary property] + * + * {@link https://www.w3.org/ns/activitystreams#summary Docs} + */ + summaryMap?: Record; + + /** + * One or more "tags" that have been associated with an objects. A tag can be any kind of Object. + * The key difference between attachment and tag is that the former implies association by + * inclusion, while the latter implies associated by reference. + * + * {@link https://www.w3.org/ns/activitystreams#tag Docs} + */ + tag?: TagField | TagField[]; + + /** + * The date and time at which the object was updated. + * + * {@link https://www.w3.org/ns/activitystreams#published Docs} + * + * Must be provided in {@link https://www.w3schools.blog/xsd-date-and-time-data-types xsd:dateTime} + * + * @example + * 2020-08-08T08:30:11-03:00Z + * 2020-08-08T08:30:11-03:00 + * 2020-08-08T08:30:11Z + * 2020-08-08T08:30:11 + * 2020-08-08 + */ + updated?: DateTime; + + /** + * Identifies one or more links to representations of the object. + * + * {@link https://www.w3.org/ns/activitystreams#url Docs} + */ + url?: UrlField | UrlField[]; + + /** + * Identifies an entity or entities considered to be part of the public primary audience of an Object. + * + * {@link https://www.w3.org/ns/activitystreams#to Docs} + */ + to?: ToField | ToField[]; + + /** + * Identifies an Object that is part of the private primary audience of this Object. + * + * {@link https://www.w3.org/ns/activitystreams#bto Docs} + */ + bto?: BtoField | BtoField[]; + + /** + * Identifies an Object that is part of the public secondary audience of this Object. + * + * {@link https://www.w3.org/ns/activitystreams#cc Docs} + */ + cc?: CcField | CcField[]; + + /** + * Identifies one or more Objects that are part of the private secondary audience of this Object. + * + * {@link https://www.w3.org/ns/activitystreams#bcc Docs} + */ + bcc?: BccField | BccField[]; + + /** + * When the object describes a time-bound resource, such as an audio or video, a meeting, etc., + * the duration property indicates the object's approximate duration. + * + * {@link https://www.w3.org/ns/activitystreams#duration Docs} + * + * Must be provided in {@link http://www.datypic.com/sc/xsd/t-xsd_duration.html xsd:duration} + * + * @example + * PT20M (20 minutes) + * P1DT2H (1 day, 2 hours) + * P2Y6M5DT12H35M30S (2 years, 6 months, 5 days, 12 hours, 35 minutes, 30 seconds) + */ + duration?: Duration; +} \ No newline at end of file