12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- Posts = new Mongo.Collection('posts');
- Posts.allow({
- update: function (userId, post) {
- return ownsDocument(userId, post);
- },
- remove: function (userId, post) {
- return ownsDocument(userId, post);
- }
- });
- Posts.deny({
- update: function (userId, post, fieldNames) {
-
-
- return (_.without(fieldNames, "url", "title").length > 0);
- }
- });
- Posts.deny({
-
-
-
- update: function (userId, post, fieldNames, modifier) {
- var errors = validatePost(modifier.$set);
- return errors.title || errors.url;
- }
- });
- validatePost = function (post) {
- var errors = {};
- if (!post.title) {
- errors.title = "Please fill in a headline";
- }
- if (!post.url) {
- errors.url = "Please fill in a URL";
- }
- return errors;
- };
- Meteor.methods({
- postInsert: function(postAttributes) {
- "use strict";
- check(this.userId, String);
- check(postAttributes, {
- title: String,
- url: String
- });
- var errors = validatePost(postAttributes);
- if (errors.title || errors.url) {
- throw new Meteor.Error('invalid-post',
- "You must set a title and URL for your post");
- }
- var postWithSameLink = Posts.findOne({ url: postAttributes.url });
- if (postWithSameLink) {
-
- return {
- postExists: true,
- _id: postWithSameLink._id
- };
- }
- var user = Meteor.user();
- var post = _.extend(postAttributes, {
- userId: user._id,
- author: user.username,
- submitted: new Date(),
- commentsCount: 0
- });
- var postId = Posts.insert(post);
- return {
- _id: postId
- };
- }
- });
|