-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathregistration.js
127 lines (113 loc) · 4.41 KB
/
registration.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
const { MoleculerError } = require('moleculer').Errors;
const { ActivitiesHandlerMixin, OBJECT_TYPES } = require('@semapps/activitypub');
const { getAnnouncesGroupUri } = require('@activitypods/announcer');
const { JOIN_EVENT, LEAVE_EVENT } = require('../config/patterns');
const { JOIN_EVENT_MAPPING, LEAVE_EVENT_MAPPING } = require('../config/mappings');
module.exports = {
name: 'events.registration',
mixins: [ActivitiesHandlerMixin],
dependencies: ['activitypub.registry', 'activity-mapping', 'ldp', 'webacl'],
async started() {
await this.broker.call('activitypub.registry.register', {
path: '/attendees',
attachToTypes: [OBJECT_TYPES.EVENT],
attachPredicate: 'http://activitypods.org/ns/core#attendees',
ordered: false,
dereferenceItems: false,
});
await this.broker.call('activity-mapping.addMapper', {
match: JOIN_EVENT,
mapping: JOIN_EVENT_MAPPING,
});
await this.broker.call('activity-mapping.addMapper', {
match: LEAVE_EVENT,
mapping: LEAVE_EVENT_MAPPING,
});
},
actions: {
async addCreatorToAttendees(ctx) {
const { resourceUri, newData } = ctx.params;
await ctx.call('activitypub.collection.attach', {
collectionUri: newData['apods:attendees'],
item: newData['dc:creator'],
});
},
async givePermissionsForAttendeesCollection(ctx) {
const { resourceUri, newData } = ctx.params;
await ctx.call('webacl.resource.addRights', {
resourceUri: newData['apods:attendees'],
additionalRights: {
group: {
uri: getAnnouncesGroupUri(resourceUri),
read: true,
},
},
webId: newData['dc:creator'],
});
},
},
activities: {
joinEvent: {
match: JOIN_EVENT,
async onEmit(ctx, activity, emitterUri) {
const emitter = await ctx.call('activitypub.actor.get', { actorUri: emitterUri, webId: 'system' });
// If the emitter has a profile, it means the contacts app is activated
if (emitter.url) {
const organizerUri = activity.object['dc:creator'];
// Ensure the organizer is in the contacts WebACL group of the emitter so he can see his profile (and write to him)
// TODO put this in the contacts app ?
await ctx.call('webacl.group.addMember', {
groupSlug: new URL(emitterUri).pathname + '/contacts',
memberUri: organizerUri,
webId: emitterUri,
});
}
},
async onReceive(ctx, activity) {
const event = activity.object;
if (await ctx.call('events.status.isFinished', { event })) {
throw new MoleculerError('This event is finished', 403, 'FORBIDDEN');
} else if (await ctx.call('events.status.isClosed', { event })) {
throw new MoleculerError('Registrations for this event are closed', 403, 'FORBIDDEN');
}
if (
!(await ctx.call('activitypub.collection.includes', {
collectionUri: event['apods:announces'],
itemUri: activity.actor,
}))
) {
throw new MoleculerError('You have not been invited to this event', 400, 'BAD REQUEST');
}
await ctx.call('activitypub.collection.attach', {
collectionUri: event['apods:attendees'],
item: activity.actor,
});
// Tag event as closed if max attendees has been reached
await ctx.call('events.status.tagUpdatedEvent', { eventUri: event.id });
// TODO send confirmation mail to participant
},
},
leaveEvent: {
match: LEAVE_EVENT,
async onReceive(ctx, activity) {
const event = activity.object;
if (
!(await ctx.call('activitypub.collection.includes', {
collectionUri: event['apods:attendees'],
itemUri: activity.actor,
}))
) {
throw new MoleculerError('You are not attending this event', 400);
} else if (await ctx.call('events.status.isFinished', { event })) {
throw new MoleculerError('This event is closed', 403, 'FORBIDDEN');
}
await ctx.call('activitypub.collection.detach', {
collectionUri: event['apods:attendees'],
item: activity.actor,
});
// Tag event as open if the number of attendees is now lower than max attendees
await ctx.call('events.status.tagUpdatedEvent', { eventUri: event.id });
},
},
},
};