Refactored tabletop methods

This commit is contained in:
Stefan Zermatten
2021-10-10 19:58:41 +02:00
parent 51d3fbbcb7
commit eebfbfd636
7 changed files with 165 additions and 185 deletions

View File

@@ -0,0 +1,57 @@
import SimpleSchema from 'simpl-schema';
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
import { assertUserInTabletop } from './shared/tabletopPermissions.js';
import { assertAdmin } from '/imports/api/sharing/sharingPermissions.js';
import { assertUserHasPaidBenefits } from '/imports/api/users/patreon/tiers.js';
import Creatures from '/imports/api/creature/creatures/Creatures.js';
const addCreaturesToTabletop = new ValidatedMethod({
name: 'tabletops.addCreatures',
validate: new SimpleSchema({
'creatureIds': {
type: Array,
},
'creatureIds.$': {
type: String,
regEx: SimpleSchema.RegEx.id,
},
tabletopId: {
type: String,
regEx: SimpleSchema.RegEx.id,
},
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 10,
timeInterval: 5000,
},
run({tabletopId, creatureIds}) {
if (!this.userId) {
throw new Meteor.Error('tabletops.addCreatures.denied',
'You need to be logged in to remove a tabletop');
}
assertUserHasPaidBenefits(this.userId);
assertUserInTabletop(tabletopId, this.userId);
assertAdmin(this.userId);
Creatures.update({
_id: {$in: creatureIds},
$or: [
{writers: this.userId},
{owner: this.userId},
],
}, {
$set: {tabletop: tabletopId},
}, {
multi: true,
});
},
});
export default addCreaturesToTabletop;

View File

@@ -0,0 +1,34 @@
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
import Tabletops from '../Tabletops.js';
import { assertAdmin } from '/imports/api/sharing/sharingPermissions.js';
import { assertUserHasPaidBenefits } from '/imports/api/users/patreon/tiers.js';
const insertTabletop = new ValidatedMethod({
name: 'tabletops.insert',
validate: null,
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run() {
if (!this.userId) {
throw new Meteor.Error('tabletops.insert.denied',
'You need to be logged in to insert a tabletop');
}
assertUserHasPaidBenefits(this.userId);
assertAdmin(this.userId);
return Tabletops.insert({
gameMaster: this.userId,
});
},
});
export default insertTabletop;

View File

@@ -0,0 +1,49 @@
import SimpleSchema from 'simpl-schema';
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
import Tabletops from '../Tabletops.js';
import { assertAdmin } from '/imports/api/sharing/sharingPermissions.js';
import { assertUserHasPaidBenefits } from '/imports/api/users/patreon/tiers.js';
import { assertUserIsTabletopOwner } from './shared/tabletopPermissions/js';
import Creatures from '/imports/api/creature/creatures/Creatures.js';
const removeTabletop = new ValidatedMethod({
name: 'tabletops.remove',
validate: new SimpleSchema({
tabletopId: {
type: String,
regEx: SimpleSchema.RegEx.id,
},
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({tabletopId}) {
if (!this.userId) {
throw new Meteor.Error('tabletops.remove.denied',
'You need to be logged in to remove a tabletop');
}
assertUserHasPaidBenefits(this.userId);
assertUserIsTabletopOwner(tabletopId, this.userId);
assertAdmin(this.userId);
let removed = Tabletops.remove({
_id: tabletopId,
});
Creatures.update({
tabletop: tabletopId,
}, {
$unset: {tabletop: 1},
});
return removed;
},
});
export default removeTabletop;

View File

@@ -0,0 +1,25 @@
import Tabletops from '../../Tabletops.js';
export function assertUserInTabletop(tabletopId, userId){
let tabletop = Tabletops.findOne(tabletopId);
if (!tabletop){
throw new Meteor.Error('Tabletop does not exist',
'No tabletop could be found for the given tabletop id');
}
if (tabletop.gameMaster !== userId && !tabletop.players.includes(userId)){
throw new Meteor.Error('Not in tabletop',
'The user is not the gamemaster or a player in the given tabletop');
}
}
export function assertUserIsTabletopOwner(tabletopId, userId){
let tabletop = Tabletops.findOne(tabletopId);
if (!tabletop){
throw new Meteor.Error('Tabletop does not exist',
'No tabletop could be found for the given tabletop id');
}
if (tabletop.gameMaster !== userId){
throw new Meteor.Error('Not the owner',
'The user is not the owner of the given tabletop');
}
}