UNPKG

2.31 kBJavaScriptView Raw
1'use strict';
2
3const guid = require('guid');
4const tensify = require('tensify');
5
6let activityClass = class JSONActivityStreamish {
7 constructor(args) {
8 this._id = guid.raw();
9 this._name = args && args.name ? args.name : '';
10 this._context = 'http://www.w3.org/ns/activitystreams#';
11 this._published = new Date().toISOString();
12 this._actor = args && 'actor' in args ? args.actor : {};
13 this._target = args && 'target' in args ? args.target : {};
14 this._type = args && 'type' in args ? args.type : '';
15 this._object = args && 'object' in args ? args.object: {};
16 if (args && args.content) {
17 this._content = args.content;
18 }
19 }
20
21 // getter and setter in one. Kind of ugly though.
22 actor(newActor) {
23 if (newActor) {
24 this._actor = newActor;
25 return this;
26 }
27 return this._actor;
28 }
29
30 object(newObject) {
31 if (newObject) {
32 this._object = newObject;
33 return this;
34 }
35 return this._object;
36 }
37
38 target(newTarget) {
39 if (newTarget) {
40 this._target = newTarget;
41 return this;
42 }
43 return this._target;
44 }
45
46 type(newType) {
47 if (newType) {
48 this._type = newType;
49 return this;
50 }
51 return this._type;
52 }
53
54 content(newContent) {
55 if (newContent) {
56 this._content = newContent;
57 return this;
58 }
59 return this._content;
60 }
61
62 meta(newMeta) {
63 if (newMeta) {
64 this._meta = newMeta;
65 return this;
66 }
67 return this._meta;
68 }
69
70 name(newName) {
71 if (newName) {
72 this._name = newName;
73 return this;
74 }
75 return this._name;
76 }
77
78 toJSON() {
79 let object = {
80 '@context': this._context,
81 id: this._id,
82 actor: this._actor,
83 object: this._object,
84 type: this._type,
85 published: this._published
86 };
87 if (!this._name) {
88 this._name = `${this._actor.name} has ${tensify(this._type.toLowerCase()).past} ${this._object.name}`;
89 }
90 object.name = this._name;
91
92 if (Object.keys(this._target).length) {
93 object.target = this._target;
94 }
95
96 if (this._content) {
97 object.content = this._content;
98 }
99
100 if (this._meta) {
101 object.meta = this._meta;
102 }
103 return object;
104 }
105
106 toString() {
107 return JSON.stringify(this.toJSON(), null, 2);
108 }
109}
110
111module.exports = activityClass;