Skip to content

Commit

Permalink
Fix the POST /invites/:inviteId/users implementation - delete the inv…
Browse files Browse the repository at this point in the history
…ite after accepting, update existing member level if invite level is higher than current - citizenos/citizenos-fe#112
  • Loading branch information
tiblu committed Nov 20, 2019
1 parent 6ab1b8e commit 3d24306
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 86 deletions.
182 changes: 98 additions & 84 deletions routes/api/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -3390,39 +3390,35 @@ module.exports = function (app) {
});

const createInvitePromises = validUserIdMembers.map(async function (member) {
return await TopicInviteUser
.create(
{
topicId: topicId,
creatorId: userId,
userId: member.userId,
level: member.level
},
{
transaction: t
}
)
.then(function (topicInvite) {
const userInvited = User.build({id: topicInvite.userId});
userInvited.dataValues.level = topicInvite.level; // FIXME: HACK? Invite event, putting level here, not sure it belongs here, but.... https://github.com/citizenos/citizenos-fe/issues/112 https://github.com/w3c/activitystreams/issues/506
userInvited.dataValues.inviteId = topicInvite.id; // FIXME: HACK? Invite event, putting level here, not sure it belongs here, but.... https://github.com/citizenos/citizenos-fe/issues/112 https://github.com/w3c/activitystreams/issues/506
const topicInvite = await TopicInviteUser.create(
{
topicId: topicId,
creatorId: userId,
userId: member.userId,
level: member.level
},
{
transaction: t
}
);

return cosActivities
.inviteActivity(
topic,
userInvited,
{
type: 'User',
id: req.user.id,
ip: req.ip
},
req.method + ' ' + req.path,
t
)
.then(function () {
return topicInvite;
});
});
const userInvited = User.build({id: topicInvite.userId});
userInvited.dataValues.level = topicInvite.level; // FIXME: HACK? Invite event, putting level here, not sure it belongs here, but.... https://github.com/citizenos/citizenos-fe/issues/112 https://github.com/w3c/activitystreams/issues/506
userInvited.dataValues.inviteId = topicInvite.id; // FIXME: HACK? Invite event, pu

await cosActivities.inviteActivity(
topic,
userInvited,
{
type: 'User',
id: req.user.id,
ip: req.ip
},
req.method + ' ' + req.path,
t
);

return topicInvite;
});

return Promise.all(createInvitePromises);
Expand Down Expand Up @@ -3577,68 +3573,86 @@ module.exports = function (app) {
}
);

if (!invite) {
return res.notFound();
}

// Cannot accept the invite for someone else
if (invite.userId !== userId) {
return res.forbidden();
}
// Find out if the User is already a member of the Topic
const memberUserExisting = await TopicMemberUser
.findOne({
where: {
topicId: topicId,
userId: userId
}
});

// Needed just for the activity
const topic = await Topic.findOne({
where: {
id: topicId
if (invite) {
if (invite.userId !== userId) {
return res.forbidden();
}
});

const [memberUser, created] = await db.transaction(function (t) {
return TopicMemberUser
.findOrCreate({
if (memberUserExisting) {
// User already a member, see if we need to update the level
if (TopicMemberUser.LEVELS.indexOf(memberUserExisting.level) < TopicMemberUser.LEVELS.indexOf(invite.level)) {
const memberUserUpdated = await memberUserExisting.update({
level: invite.level
});
return res.ok(memberUserUpdated);
} else {
// No level update, respond with existing member info
return res.ok(memberUserExisting);
}
} else {
// User is not a member, make it happen!

// Topic needed just for the activity
const topic = await Topic.findOne({
where: {
topicId: invite.topicId,
userId: invite.userId
},
defaults: {
level: TopicMemberUser.LEVELS[invite.level]
id: invite.topicId
}
})
.then(function (topicMemberUserResult) {
const [member, wasCreated] = topicMemberUserResult;
});

if (wasCreated) {
const user = User.build({id: member.userId});
user.dataValues.id = member.userId;
const memberUserCreated = await db.transaction(async function (t) {
const member = await TopicMemberUser.create(
{
topicId: invite.topicId,
userId: invite.userId,
level: TopicMemberUser.LEVELS[invite.level]
},
{
transaction: t
}
);

return cosActivities.acceptActivity(
invite,
{
type: 'User',
id: req.user.id,
ip: req.ip
},
{
type: 'User',
id: invite.creatorId
},
topic,
req.method + ' ' + req.path,
t
)
.then(function () {
return topicMemberUserResult;
});
} else {
return topicMemberUserResult;
}
await invite.destroy({transaction: t});

const user = User.build({id: member.userId});
user.dataValues.id = member.userId;

await cosActivities.acceptActivity(
invite,
{
type: 'User',
id: req.user.id,
ip: req.ip
},
{
type: 'User',
id: invite.creatorId
},
topic,
req.method + ' ' + req.path,
t
);

return member;
});
});

if (created) {
return res.created(memberUser);
return res.created(memberUserCreated);
}
} else {
return res.ok(memberUser);
// Already a member, return that membership information
if (memberUserExisting) {
return res.ok(memberUserExisting);
} else { // No invite, not a member - the User is not invited
return res.notFound();
}
}
}));

Expand Down
13 changes: 11 additions & 2 deletions test/api/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -4770,7 +4770,7 @@ suite('Users', function () {

suite('Invites', function () {

suite('Users', function() {
suite('Users', function () {

suite('Create', function () {
let agentCreator = request.agent(app);
Expand Down Expand Up @@ -5554,7 +5554,8 @@ suite('Users', function () {
assert.property(topicMemberUser, 'updatedAt');
assert.property(topicMemberUser, 'deletedAt');

done();
// The invite is supposed to be deleted
_topicInviteUsersRead(agentCreator, topic.id, topicInviteCreated.id, 410, done);
});
});

Expand All @@ -5579,6 +5580,14 @@ suite('Users', function () {
});
});

test('Fail - 40400 - Cannot accept deleted invite', function (done) {
topicInviteUsersDelete(agentCreator, userCreator.id, topic.id, topicInviteCreated.id, function (err, res) {
if (err) return done(err);

_topicInviteUsersAccept(agentUserToInvite, userToInvite.id, topic.id, topicInviteCreated.id, 404, done);
});
});

test('Fail - 40100 - Unauthorized', function (done) {
_topicInviteUsersAccept(request.agent(app), '93857ed7-a81a-4187-85de-234f6d06b011', topic.id, topicInviteCreated.id, 401, done);
});
Expand Down

0 comments on commit 3d24306

Please sign in to comment.