-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathActivitiesJSONAdapter.js
164 lines (148 loc) · 4.75 KB
/
ActivitiesJSONAdapter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import {ActivitiesAdapter} from '@webex/component-adapter-interfaces';
import {Observable} from 'rxjs';
const EMPTY_ACTION = {
actionID: null,
personID: null,
roomID: null,
type: 'submit',
activityID: null,
inputs: null,
created: null,
};
// TODO: Figure out how to import JS Doc definitions and remove duplication.
/**
* An activity a person performs in Webex.
*
* @external Activity
* @see {@link https://github.com/webex/component-adapter-interfaces/blob/master/src/ActivitiesAdapter.js#L6}
*/
/**
* @typedef ActivitiesJSON
* @param {object} datasource An object that contains activities keyed by ID
* @example
* {
* "activity-1": {
* "ID": "activity-1",
* "roomID": "roomID",
* "text": "Hey Barbara, how is it going?",
* "personID": "user-2",
* "created": "created",
* "displayAuthor": false
* },
* "activity-2": {
* "ID": "activity-2",
* "roomID": "roomID",
* "text": "Oh no! That's terrible 😔",
* "personID": "user-2",
* "created": "created",
* "displayAuthor": true
* }
* }
*/
/**
* `ActivitiesJSONAdapter` is an implementation of the `ActivitiesAdapter` interface.
* This implementation utilizes a JSON object as its source of activity data.
*
* @see {@link ActivitiesJSON}
* @implements {ActivitiesAdapter}
*/
export default class ActivitiesJSONAdapter extends ActivitiesAdapter {
/**
* Returns an observable that emits activity data of the given ID.
* For this implementation, once the data is emitted, the observable completes.
*
* @param {string} ID ID of activity to get
* @returns {Observable.<Activity>} Observable that emits data of the given ID
*/
getActivity(ID) {
return Observable.create((observer) => {
if (this.datasource[ID]) {
observer.next(this.datasource[ID]);
} else {
observer.error(new Error(`Could not find activity with ID "${ID}"`));
}
observer.complete();
});
}
/**
* Posts an attachment action, returns an observable that emits the created action
*
* @param {string} activityID ID of the activity corresponding to this submit action
* @param {object} inputs The message content
* @returns {Observable.<object>} Observable stream that emits data of the newly created action
*/
postAction(activityID, inputs) {
return new Observable((observer) => {
const activity = this.datasource[activityID];
if (activity) {
const actionID = `action-${activityID}-${activity?.actions.length + 1}`;
const personID = 'user1';
const newAction = {
...EMPTY_ACTION,
actionID,
personID,
roomID: activity.roomID,
activityID,
inputs,
created: new Date().toISOString(),
};
const activityActions = this.datasource[activityID].actions;
activityActions.push(newAction);
observer.next(newAction);
observer.complete();
} else {
observer.error(new Error(`Unable to create an attachment action for activity with id "${activityID}"`));
}
});
}
/**
* Posts an activity and returns an observable to the new activity data
*
* @param {object} activity The activity to post
* @returns {Observable.<Activity>} Observable that emits the posted activity (including id)
*/
postActivity(activity) {
const activity$ = new Observable((observer) => {
const activities = this.datasource;
if (activity.roomID) {
const ID = `activity${Object.keys(activities).length + 1}`;
const newActivity = {
attachments: [],
actions: [],
cards: [],
...activity,
ID,
personID: 'user1',
created: new Date().toISOString(),
};
activities[ID] = newActivity;
observer.next(newActivity);
observer.complete();
} else {
observer.error(new Error(`Unable to post an activity in room with id ${activity.roomID}`));
}
});
return activity$;
}
/**
* A function that checks whether or not an Activity object contains at least one adaptive card.
*
* @param {Activity} activity Activity object
* @returns {boolean} True if received Activity object contains at least one adaptive card
*/
// eslint-disable-next-line class-methods-use-this
hasAdaptiveCards(activity) {
return activity.cards.length > 0;
}
/**
* A function that returns adaptive card data of an Activity object.
*
* @param {Activity} activity Activity object
* @param {number} cardIndex Index of the adaptive card to get
* @returns {object|undefined} Adaptive card data object
*/
// eslint-disable-next-line class-methods-use-this
getAdaptiveCard(activity, cardIndex) {
return activity.cards[cardIndex];
}
}