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

@@ -100,35 +100,6 @@ let CreatureSchema = new SimpleSchema({
type: SimpleSchema.Integer,
defaultValue: 0,
},
// Inventory
'denormalizedStats.weightTotal': {
type: Number,
defaultValue: 0,
},
'denormalizedStats.weightEquipment': {
type: Number,
defaultValue: 0,
},
'denormalizedStats.weightCarried': {
type: Number,
defaultValue: 0,
},
'denormalizedStats.valueTotal': {
type: Number,
defaultValue: 0,
},
'denormalizedStats.valueEquipment': {
type: Number,
defaultValue: 0,
},
'denormalizedStats.valueCarried': {
type: Number,
defaultValue: 0,
},
'denormalizedStats.itemsAttuned': {
type: Number,
defaultValue: 0,
},
// Version of computation engine that was last used to compute this creature
computeVersion: {
type: String,

View File

@@ -1,8 +1,4 @@
import SimpleSchema from 'simpl-schema';
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
import { assertUserHasPaidBenefits } from '/imports/api/users/patreon/tiers.js';
import Creatures from '/imports/api/creature/creatures/Creatures.js';
let Tabletops = new Mongo.Collection('tabletops');
@@ -52,150 +48,4 @@ let TabletopSchema = new SimpleSchema({
Tabletops.attachSchema(TabletopSchema);
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');
}
}
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');
}
}
function assertUserIsAdmin(userId){
if (!Meteor.users.isAdmin(userId)){
throw new Meteor.Error('Admin only',
'This is restricted to admins for now');
}
}
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);
assertUserIsAdmin(this.userId);
return Tabletops.insert({
gameMaster: this.userId,
});
},
});
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);
assertUserIsAdmin(this.userId);
let removed = Tabletops.remove({
_id: tabletopId,
});
Creatures.update({
tabletop: tabletopId,
}, {
$unset: {tabletop: 1},
});
return removed;
},
});
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);
assertUserIsAdmin(this.userId);
Creatures.update({
_id: {$in: creatureIds},
$or: [
{writers: this.userId},
{owner: this.userId},
],
}, {
$set: {tabletop: tabletopId},
}, {
multi: true,
});
},
});
export default Tabletops;
export { insertTabletop, removeTabletop, addCreaturesToTabletop };

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');
}
}

View File

@@ -168,12 +168,6 @@ Meteor.users.sendVerificationEmail = new ValidatedMethod({
}
});
Meteor.users.isAdmin = function(userId){
userId = this.userId || userId;
let user = Meteor.users.findOne(userId);
return user && user.roles.includes('admin');
}
Meteor.users.canPickUsername = new ValidatedMethod({
name: 'users.canPickUsername',
validate: userSchema.pick('username').validator(),