working on post schema and resolvers check

This commit is contained in:
Sacha Greif 2016-11-30 16:55:34 +09:00
parent 868194de59
commit 0c2b9da97c
3 changed files with 58 additions and 49 deletions

View file

@ -80,6 +80,15 @@ Posts.getDefaultStatus = function (user) {
} }
}; };
/**
* @summary Get status name
* @param {Object} user
*/
Posts.getStatusName = function (post) {
// TODO: write function
return 'approved';
};
/** /**
* @summary Check if a post is approved * @summary Check if a post is approved
* @param {Object} post * @param {Object} post

View file

@ -23,7 +23,12 @@ const resolvers = {
name: 'postsList', name: 'postsList',
check(user, documents, context) {
return true // TODO: write check function
},
resolver(root, {terms, offset, limit}, context, info) { resolver(root, {terms, offset, limit}, context, info) {
// TODO: call check function
let {selector, options} = context.Posts.parameters.get(terms); let {selector, options} = context.Posts.parameters.get(terms);
options.limit = (limit < 1 || limit > 10) ? 10 : limit; options.limit = (limit < 1 || limit > 10) ? 10 : limit;
options.skip = offset; options.skip = offset;
@ -38,8 +43,15 @@ const resolvers = {
name: 'postsSingle', name: 'postsSingle',
resolver(root, args, context) { check(user, document, context) {
return context.Posts.findOne({_id: args._id}, { fields: context.getViewableFields(context.currentUser, context.Posts) }); return true // TODO: write check function
const status = context.Posts.getStatusName(document);
return Users.isOwner(user, document) ? Users.canDo(`posts.view.${status}.own`) : Users.canDo(`posts.view.${status}.all`)
},
resolver(root, {documentId}, context) {
// TODO: call check function
return context.Posts.findOne({_id: documentId}, { fields: context.getViewableFields(context.currentUser, context.Posts) });
}, },
}, },

View file

@ -8,7 +8,6 @@ import Posts from './collection.js';
* @summary Posts config namespace * @summary Posts config namespace
* @type {Object} * @type {Object}
*/ */
const formGroups = { const formGroups = {
admin: { admin: {
name: "admin", name: "admin",
@ -16,17 +15,6 @@ const formGroups = {
} }
}; };
// check if user can create a new post
const canInsert = user => Users.canDo(user, "posts.new");
// check if user can edit a post
const canEdit = mutations.edit.check;
// check if user can edit *all* posts
const canEditAll = user => Users.canDo(user, "posts.edit.all"); // we don't use the mutations.edit check here, to be changed later with ability to give options to mutations.edit.check?
const alwaysPublic = user => true;
/** /**
* @summary Posts schema * @summary Posts schema
* @type {Object} * @type {Object}
@ -39,7 +27,7 @@ const schema = {
type: String, type: String,
optional: true, optional: true,
publish: true, publish: true,
viewableIf: alwaysPublic, viewableIf: ['anonymous'],
}, },
/** /**
Timetstamp of post creation Timetstamp of post creation
@ -47,7 +35,7 @@ const schema = {
createdAt: { createdAt: {
type: Date, type: Date,
optional: true, optional: true,
viewableIf: canEditAll, viewableIf: ['admins'],
publish: true, // publish so that admins can sort pending posts by createdAt publish: true, // publish so that admins can sort pending posts by createdAt
autoValue: (documentOrModifier) => { autoValue: (documentOrModifier) => {
if (documentOrModifier && !documentOrModifier.$set) return new Date() // if this is an insert, set createdAt to current timestamp if (documentOrModifier && !documentOrModifier.$set) return new Date() // if this is an insert, set createdAt to current timestamp
@ -59,9 +47,9 @@ const schema = {
postedAt: { postedAt: {
type: Date, type: Date,
optional: true, optional: true,
viewableIf: alwaysPublic, viewableIf: ['anonymous'],
insertableIf: canEditAll, insertableIf: ['admins'],
editableIf: canEditAll, editableIf: ['admins'],
publish: true, publish: true,
control: "datetime", control: "datetime",
group: formGroups.admin group: formGroups.admin
@ -73,9 +61,9 @@ const schema = {
type: String, type: String,
optional: true, optional: true,
max: 500, max: 500,
viewableIf: alwaysPublic, viewableIf: ['anonymous'],
insertableIf: canInsert, insertableIf: ['default'],
editableIf: canEdit, editableIf: ['default'],
control: "text", control: "text",
publish: true, publish: true,
order: 10 order: 10
@ -87,9 +75,9 @@ const schema = {
type: String, type: String,
optional: false, optional: false,
max: 500, max: 500,
viewableIf: alwaysPublic, viewableIf: ['anonymous'],
insertableIf: canInsert, insertableIf: ['default'],
editableIf: canEdit, editableIf: ['default'],
control: "text", control: "text",
publish: true, publish: true,
order: 20 order: 20
@ -100,7 +88,7 @@ const schema = {
slug: { slug: {
type: String, type: String,
optional: true, optional: true,
viewableIf: alwaysPublic, viewableIf: ['anonymous'],
publish: true, publish: true,
autoValue: (documentOrModifier) => { autoValue: (documentOrModifier) => {
// if title is changing, return new slug // if title is changing, return new slug
@ -117,9 +105,9 @@ const schema = {
type: String, type: String,
optional: true, optional: true,
max: 3000, max: 3000,
viewableIf: alwaysPublic, viewableIf: ['anonymous'],
insertableIf: canInsert, insertableIf: ['default'],
editableIf: canEdit, editableIf: (user, document) => Users.isAdmin(user) || Users.isOwner(user, document),
control: "textarea", control: "textarea",
publish: true, publish: true,
order: 30 order: 30
@ -131,7 +119,7 @@ const schema = {
type: String, type: String,
optional: true, optional: true,
publish: true, publish: true,
viewableIf: alwaysPublic, viewableIf: ['anonymous'],
autoValue(documentOrModifier) { autoValue(documentOrModifier) {
const body = documentOrModifier.body || documentOrModifier.$set && documentOrModifier.$set.body; const body = documentOrModifier.body || documentOrModifier.$set && documentOrModifier.$set.body;
if (body) { if (body) {
@ -149,7 +137,7 @@ const schema = {
optional: true, optional: true,
max: 255, //should not be changed the 255 is max we should load for each post/item max: 255, //should not be changed the 255 is max we should load for each post/item
publish: true, publish: true,
viewableIf: alwaysPublic, viewableIf: ['anonymous'],
autoValue(documentOrModifier) { autoValue(documentOrModifier) {
const body = documentOrModifier.body || documentOrModifier.$set && documentOrModifier.$set.body; const body = documentOrModifier.body || documentOrModifier.$set && documentOrModifier.$set.body;
if (body) { if (body) {
@ -166,7 +154,7 @@ const schema = {
type: Number, type: Number,
optional: true, optional: true,
publish: true, publish: true,
viewableIf: alwaysPublic, viewableIf: (user, document) => Users.isAdmin(user) || Users.isOwner(user, document),
defaultValue: 0 defaultValue: 0
}, },
/** /**
@ -176,7 +164,7 @@ const schema = {
type: Date, type: Date,
optional: true, optional: true,
publish: true, publish: true,
viewableIf: alwaysPublic, viewableIf: ['anonymous'],
}, },
/** /**
Count of how many times the post's link was clicked Count of how many times the post's link was clicked
@ -185,7 +173,7 @@ const schema = {
type: Number, type: Number,
optional: true, optional: true,
publish: true, publish: true,
viewableIf: canEditAll, viewableIf: ['admins'],
defaultValue: 0 defaultValue: 0
}, },
/** /**
@ -194,9 +182,9 @@ const schema = {
status: { status: {
type: Number, type: Number,
optional: true, optional: true,
viewableIf: alwaysPublic, viewableIf: ['anonymous'],
insertableIf: canEditAll, insertableIf: ['admins'],
editableIf: canEditAll, editableIf: ['admins'],
control: "select", control: "select",
publish: true, publish: true,
autoValue(documentOrModifier) { autoValue(documentOrModifier) {
@ -219,7 +207,7 @@ const schema = {
isFuture: { isFuture: {
type: Boolean, type: Boolean,
optional: true, optional: true,
viewableIf: alwaysPublic, viewableIf: ['anonymous'],
publish: true publish: true
}, },
/** /**
@ -229,9 +217,9 @@ const schema = {
type: Boolean, type: Boolean,
optional: true, optional: true,
defaultValue: false, defaultValue: false,
viewableIf: alwaysPublic, viewableIf: ['anonymous'],
insertableIf: canEditAll, insertableIf: ['admins'],
editableIf: canEditAll, editableIf: ['admins'],
control: "checkbox", control: "checkbox",
publish: true, publish: true,
group: formGroups.admin group: formGroups.admin
@ -251,19 +239,19 @@ const schema = {
userIP: { userIP: {
type: String, type: String,
optional: true, optional: true,
viewableIf: canEditAll, viewableIf: ['admins'],
publish: false publish: false
}, },
userAgent: { userAgent: {
type: String, type: String,
optional: true, optional: true,
viewableIf: canEditAll, viewableIf: ['admins'],
publish: false publish: false
}, },
referrer: { referrer: {
type: String, type: String,
optional: true, optional: true,
viewableIf: canEditAll, viewableIf: ['admins'],
publish: false publish: false
}, },
/** /**
@ -272,7 +260,7 @@ const schema = {
author: { author: {
type: String, type: String,
optional: true, optional: true,
viewableIf: alwaysPublic, viewableIf: ['anonymous'],
publish: true, publish: true,
autoValue: (documentOrModifier) => { autoValue: (documentOrModifier) => {
// if userId is changing, change the author name too // if userId is changing, change the author name too
@ -287,14 +275,14 @@ const schema = {
type: String, type: String,
optional: true, optional: true,
control: "select", control: "select",
viewableIf: alwaysPublic, viewableIf: ['anonymous'],
insertableIf: canInsert, insertableIf: ['default'],
hidden: true, hidden: true,
resolveAs: 'user: User', resolveAs: 'user: User',
// publish: true, // publish: true,
// regEx: SimpleSchema.RegEx.Id, // regEx: SimpleSchema.RegEx.Id,
// insertableIf: canEditAll, // insertableIf: ['admins'],
// editableIf: canEditAll, // editableIf: ['admins'],
// form: { // form: {
// group: 'admin', // group: 'admin',
// options: function () { // options: function () {