forked from activitypods/activitypods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocation.js
70 lines (67 loc) · 2.14 KB
/
location.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
const { ControlledContainerMixin } = require('@semapps/ldp');
const { MIME_TYPES } = require('@semapps/mime-types');
module.exports = {
name: 'profiles.location',
mixins: [ControlledContainerMixin],
settings: {
path: '/locations',
acceptedTypes: ['vcard:Location'],
excludeFromMirror: true,
permissions: {},
newResourcesPermissions: {}
},
actions: {
async getHomeLocation(ctx) {
const { webId } = ctx.params;
const results = await ctx.call('triplestore.query', {
query: `
PREFIX vcard: <http://www.w3.org/2006/vcard/ns#>
PREFIX as: <https://www.w3.org/ns/activitystreams#>
SELECT ?homeLocation
WHERE {
<${webId}> as:url ?profile .
?profile vcard:hasAddress ?homeLocation .
}
`,
accept: MIME_TYPES.JSON,
webId
});
return results.length > 0 ? results[0].homeLocation.value : null;
},
async clearHomeLocation(ctx) {
const { webId } = ctx.params;
await ctx.call('triplestore.update', {
query: `
PREFIX vcard: <http://www.w3.org/2006/vcard/ns#>
PREFIX as: <https://www.w3.org/ns/activitystreams#>
DELETE {
?profile vcard:hasAddress ?homeLocation .
?hasGeo vcard:latitude ?latitude .
?hasGeo vcard:longitude ?longitude .
?profile vcard:hasGeo ?hasGeo .
}
WHERE {
<${webId}> as:url ?profile .
?profile vcard:hasAddress ?homeLocation .
?profile vcard:hasGeo ?hasGeo .
?hasGeo vcard:latitude ?latitude .
?hasGeo vcard:longitude ?longitude .
}
`,
webId
});
}
},
hooks: {
after: {
async delete(ctx, res) {
// If the deleted location is the home location of the current user, clear it from profile
const homeLocation = await this.actions.getHomeLocation({ webId: res.webId }, { parentCtx: ctx });
if (homeLocation === res.resourceUri) {
await this.actions.clearHomeLocation({ webId: res.webId }, { parentCtx: ctx });
}
return res;
}
}
}
};