|
| 1 | +const doAsync = require('doasync'); // rather than utils.promisy to get "free" support for object methods |
| 2 | + |
1 | 3 | const fromUrlToId = url => url.replace(/.*\//, "");
|
2 | 4 |
|
3 | 5 | module.exports = async function iprcheck(w3c, w3cprofileid, name, w3cgroups, store, cb) {
|
4 |
| - return Promise.all( |
5 |
| - w3cgroups.map( |
6 |
| - ({id, type, shortname}) => new Promise((res, rej) => { |
7 |
| - store.getGroup(id, function(err, group) { |
8 |
| - if (err) return rej(err); |
9 |
| - w3c.user(w3cprofileid) |
10 |
| - .participations() |
11 |
| - .fetch({embed: true}, |
12 |
| - function(err, participations) { |
13 |
| - if (err) return rej(err); |
14 |
| - for (var i = 0 ; i < participations.length; i++) { |
15 |
| - var p = participations[i]; |
16 |
| - var org = p._links.organization; |
17 |
| - var affiliation = p.individual ? {id: w3cprofileid, name: name} : {id: fromUrlToId(org.href), name: org.title}; |
18 |
| - if (p._links.group.href === `https://api.w3.org/groups/${type}/${shortname}`) { |
19 |
| - return res({ok: true, affiliation: affiliation}); |
20 |
| - } |
21 |
| - } |
22 |
| - // If we reach here, |
23 |
| - // the user is not participating directly in the group |
24 |
| - // For non WGs, game over |
25 |
| - if (group.groupType != "WG") { |
26 |
| - return res({ok: false}); |
27 |
| - } |
28 |
| - // For WGs, we check if the user is affiliated |
29 |
| - // with an organization that is participating |
30 |
| - w3c.group({type, shortname}) |
31 |
| - .participations() |
32 |
| - .fetch({embed: true}, function(err, participations) { |
33 |
| - if (err) return rej(err); |
34 |
| - var orgids = participations |
35 |
| - .filter(p => !p.individual) |
36 |
| - .map(p => fromUrlToId(p._links.organization.href)); |
37 |
| - w3c.user(w3cprofileid) |
38 |
| - .affiliations() |
39 |
| - .fetch(function(err, affiliations) { |
40 |
| - if (err) return rej(err); |
41 |
| - var affids = affiliations.map(a => a ? fromUrlToId(a.href) : undefined); |
42 |
| - var intersection = orgids.filter(id => affids.includes(id)); |
43 |
| - if (intersection.length) { |
44 |
| - var affiliationId = intersection[0]; |
45 |
| - var affiliationName = affiliations.find(a => a.href == "https://api.w3.org/affiliations/" + affiliationId).title; |
46 |
| - return res({ok: true, affiliation: {id: affiliationId, name: affiliationName}}); |
47 |
| - } |
48 |
| - return res({ok: false}); |
49 |
| - }); |
50 |
| - }); |
51 |
| - }); |
52 |
| - }) |
53 |
| - }))) |
54 |
| - .then(results => { |
55 |
| - for(let res of results) { |
56 |
| - if (res.ok) { |
57 |
| - return {affiliation: res.affiliation, ok: true}; |
58 |
| - } |
| 6 | + for (let {id, type, shortname} of w3cgroups) { |
| 7 | + const group = await doAsync(store).getGroup(id); |
| 8 | + const participations = await w3c.user(w3cprofileid) |
| 9 | + .participations() |
| 10 | + .fetch({embed: true}); |
| 11 | + if (!participations) return {affiliation: null, ok: false}; |
| 12 | + for (let p of participations) { |
| 13 | + const org = p._links.organization; |
| 14 | + const affiliation = p.individual ? {id: w3cprofileid, name: name} : {id: fromUrlToId(org.href), name: org.title}; |
| 15 | + if (p._links.group.href === `https://api.w3.org/groups/${type}/${shortname}`) { |
| 16 | + return {ok: true, affiliation: affiliation}; |
59 | 17 | }
|
60 |
| - return {affiliation: null, ok: false}; |
61 |
| - }); |
| 18 | + } |
| 19 | + // If we reach here, |
| 20 | + // the user is not participating directly in the group |
| 21 | + // For non WGs, game over |
| 22 | + if (group.groupType != "WG") { |
| 23 | + continue; |
| 24 | + } |
| 25 | + // For WGs, we check if the user is affiliated |
| 26 | + // with an organization that is participating |
| 27 | + const orgParticipations = await w3c.group({type, shortname}) |
| 28 | + .participations() |
| 29 | + .fetch({embed: true}); |
| 30 | + const orgids = orgParticipations |
| 31 | + .filter(p => !p.individual) |
| 32 | + .map(p => fromUrlToId(p._links.organization.href)); |
| 33 | + const affiliations = await w3c.user(w3cprofileid) |
| 34 | + .affiliations() |
| 35 | + .fetch(); |
| 36 | + const affids = affiliations.map(a => a ? fromUrlToId(a.href) : undefined); |
| 37 | + const intersection = orgids.filter(id => affids.includes(id)); |
| 38 | + if (intersection.length) { |
| 39 | + const affiliationId = intersection[0]; |
| 40 | + const affiliationName = affiliations.find(a => a.href == "https://api.w3.org/affiliations/" + affiliationId).title; |
| 41 | + return {ok: true, affiliation: {id: affiliationId, name: affiliationName}}; |
| 42 | + } |
| 43 | + } |
| 44 | + return {affiliation: null, ok: false}; |
62 | 45 | };
|
0 commit comments