-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstatus.js
201 lines (188 loc) · 7.19 KB
/
status.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
const CronMixin = require('moleculer-cron');
const { MIME_TYPES } = require('@semapps/mime-types');
const { defaultToArray } = require('@semapps/ldp');
const EVENT_STATUS_COMING = 'apods:Coming';
const EVENT_STATUS_FINISHED = 'apods:Finished';
const EVENT_STATUS_OPEN = 'apods:Open';
const EVENT_STATUS_CLOSED = 'apods:Closed';
const COMING_FINISHED_STATUSES = [EVENT_STATUS_COMING, EVENT_STATUS_FINISHED];
const OPEN_CLOSED_STATUSES = [EVENT_STATUS_OPEN, EVENT_STATUS_CLOSED];
module.exports = {
name: 'events.status',
mixins: [CronMixin],
dependencies: ['api', 'ldp', 'webacl'],
actions: {
async set(ctx) {
const { eventUri, newStatus } = ctx.params;
// Ensure event is complete (we may have concurrency bugs otherwise)
const event = await ctx.call('activitypub.object.awaitCreateComplete', {
objectUri: eventUri,
predicates: [
'dc:creator',
'dc:modified',
'dc:created',
'apods:attendees',
'apods:announces',
'apods:announcers',
],
});
let otherStatus;
if (COMING_FINISHED_STATUSES.includes(newStatus)) {
otherStatus =
event['apods:hasStatus'] &&
defaultToArray(event['apods:hasStatus']).find((s) => OPEN_CLOSED_STATUSES.includes(s));
} else if (OPEN_CLOSED_STATUSES.includes(newStatus)) {
otherStatus =
event['apods:hasStatus'] &&
defaultToArray(event['apods:hasStatus']).find((s) => COMING_FINISHED_STATUSES.includes(s));
} else {
throw new Error('Invalid status ' + newStatus);
}
await ctx.call('events.event.put', {
resource: { ...event, 'apods:hasStatus': [newStatus, otherStatus] },
contentType: MIME_TYPES.JSON,
webId: 'system',
});
},
isFinished(ctx) {
const { event } = ctx.params;
const status = defaultToArray(event['apods:hasStatus']) || [];
return status.includes(EVENT_STATUS_FINISHED);
},
isClosed(ctx) {
const { event } = ctx.params;
const status = defaultToArray(event['apods:hasStatus']) || [];
return status.includes(EVENT_STATUS_CLOSED);
},
async tagNewEvent(ctx) {
const { eventUri } = ctx.params;
// TODO ensure that the event is indeed coming and open
await this.actions.set({ eventUri, newStatus: EVENT_STATUS_COMING });
await this.actions.set({ eventUri, newStatus: EVENT_STATUS_OPEN });
},
async tagUpdatedEvent(ctx) {
const { eventUri } = ctx.params;
const event = await ctx.call('events.event.get', {
resourceUri: eventUri,
accept: MIME_TYPES.JSON,
webId: 'system',
});
if (event['apods:maxAttendees']) {
// TODO add a activitypub.collection.count action
const attendeesCollection = await ctx.call('activitypub.collection.get', {
collectionUri: event['apods:attendees'],
webId: 'system',
});
if (
!event['apods:hasStatus'].includes(EVENT_STATUS_CLOSED) &&
attendeesCollection.items.length >= event['apods:maxAttendees']
) {
await this.actions.set({ eventUri, newStatus: EVENT_STATUS_CLOSED });
} else if (
!event['apods:hasStatus'].includes(EVENT_STATUS_OPEN) &&
attendeesCollection.items.length < event['apods:maxAttendees']
) {
await this.actions.set({ eventUri, newStatus: EVENT_STATUS_OPEN });
}
}
},
async tagComing(ctx) {
for (let dataset of await ctx.call('pod.list')) {
// TODO do not return cached events
const results = await ctx.call('triplestore.query', {
query: `
PREFIX apods: <http://activitypods.org/ns/core#>
PREFIX as: <https://www.w3.org/ns/activitystreams#>
PREFIX ldp: <http://www.w3.org/ns/ldp#>
SELECT ?eventUri
WHERE {
?eventUri a as:Event .
?eventUri as:endTime ?endTime .
FILTER(NOW() < ?endTime) .
FILTER NOT EXISTS { ?eventUri apods:hasStatus ${EVENT_STATUS_COMING} . }
}
`,
dataset,
webId: 'system',
});
for (let eventUri of results.map((node) => node.eventUri.value)) {
await this.actions.set({ eventUri, newStatus: EVENT_STATUS_COMING });
await ctx.emit('events.status.coming', { eventUri });
}
}
},
async tagClosed(ctx) {
for (let dataset of await ctx.call('pod.list')) {
// TODO do not return cached events
const results = await ctx.call('triplestore.query', {
query: `
PREFIX apods: <http://activitypods.org/ns/core#>
PREFIX ldp: <http://www.w3.org/ns/ldp#>
PREFIX as: <https://www.w3.org/ns/activitystreams#>
SELECT ?eventUri
WHERE {
?eventUri a as:Event .
?eventUri as:endTime ?endTime .
OPTIONAL { ?eventUri apods:closingTime ?closingTime }
OPTIONAL { ?eventUri apods:maxAttendees ?maxAttendees }
# Subquery to count participants
{
SELECT (COUNT(?attendees) AS ?numAttendees) ?eventUri
WHERE {
?eventUri apods:attendees ?attendeesCollectionUri .
?attendeesCollectionUri as:items ?attendees
}
GROUP BY ?eventUri
}
FILTER(( NOW() > ?closingTime && NOW() < ?endTime ) || (NOW() < ?endTime && ?numAttendees >= ?maxAttendees)) .
FILTER NOT EXISTS { ?eventUri apods:hasStatus ${EVENT_STATUS_CLOSED} . }
}
`,
dataset,
webId: 'system',
});
for (let eventUri of results.map((node) => node.eventUri.value)) {
await this.actions.set({ eventUri, newStatus: EVENT_STATUS_CLOSED });
await ctx.emit('events.status.closed', { eventUri });
}
}
},
async tagFinished(ctx) {
for (let dataset of await ctx.call('pod.list')) {
// TODO do not return cached events
const results = await ctx.call('triplestore.query', {
query: `
PREFIX apods: <http://activitypods.org/ns/core#>
PREFIX ldp: <http://www.w3.org/ns/ldp#>
PREFIX as: <https://www.w3.org/ns/activitystreams#>
SELECT ?eventUri
WHERE {
?eventUri a as:Event .
?eventUri as:endTime ?endTime .
FILTER(NOW() > ?endTime) .
FILTER NOT EXISTS { ?eventUri apods:hasStatus ${EVENT_STATUS_FINISHED} . }
}
`,
dataset,
webId: 'system',
});
for (let eventUri of results.map((node) => node.eventUri.value)) {
await this.actions.set({ eventUri, newStatus: EVENT_STATUS_FINISHED });
await this.actions.set({ eventUri, newStatus: EVENT_STATUS_CLOSED });
await ctx.emit('events.status.finished', { eventUri });
}
}
},
},
crons: [
{
cronTime: '*/15 * * * *',
onTick: function () {
this.call('events.status.tagComing');
this.call('events.status.tagClosed');
this.call('events.status.tagFinished');
},
timeZone: 'Europe/Paris',
},
],
};