Vulcan/packages/telescope-posts/lib/posts.js
Maxime Quandalle 94c6121d91 Improve jsHint consistency
This commit touch a lot of lines of code with the goal to be more
rigorous about JavaScript code conventions defined in the `.jshintrc`.

Some modification:

* Add a list of used global symbols in the corresponding section of
  `.jshintrc`
* Use local variables instead of global in a lot of places where the
  keyword `var` was mistakenly forgotten
* Add missing semi-colons after instructions
* Add new lines at the end of files
* Remove trailing whitespaces
* Use newer name of some Meteor APIs, eg `addFiles` instead of
  `add_files`
* Add missing `break` statements in `switch` blocks
* Use `===` instead of `==` and `!==` instead of `!=`
* Remove unused variables

This commit should also fix a few bugs due to this lack of rigor. One
example of that was the test `typeof navElements === "array"` that was
never true because in JavaScript, `typeof [] === "object"`, we
replaced this test by the `_.isArray` method provided by underscore.
It might also fix some potential collision related to global
variables.

There is still plenty of work until Telescope code base passes jsHint
validation, but at least this commit is a step in the right direction.
2015-05-01 18:38:27 +02:00

189 lines
3.6 KiB
JavaScript

/**
* The global namespace for Posts.
* @namespace Posts
*/
Posts = new Mongo.Collection("posts");
/**
* Posts schema
* @type {SimpleSchema}
*/
Telescope.schemas.posts = new SimpleSchema({
_id: {
type: String,
optional: true
},
createdAt: {
type: Date,
optional: true
},
postedAt: {
type: Date,
optional: true,
editableBy: ["admin"],
autoform: {
group: 'admin',
type: "bootstrap-datetimepicker"
}
},
url: {
type: String,
optional: true,
editableBy: ["member", "admin"],
autoform: {
type: "bootstrap-url"
}
},
title: {
type: String,
optional: false,
editableBy: ["member", "admin"]
},
body: {
type: String,
optional: true,
editableBy: ["member", "admin"],
autoform: {
rows: 5
}
},
htmlBody: {
type: String,
optional: true
},
viewCount: {
type: Number,
optional: true
},
commentCount: {
type: Number,
optional: true
},
commenters: {
type: [String],
optional: true
},
lastCommentedAt: {
type: Date,
optional: true
},
clickCount: {
type: Number,
optional: true
},
baseScore: {
type: Number,
decimal: true,
optional: true
},
upvotes: {
type: Number,
optional: true
},
upvoters: {
type: [String],
optional: true
},
downvotes: {
type: Number,
optional: true
},
downvoters: {
type: [String],
optional: true
},
score: {
type: Number,
decimal: true,
optional: true
},
status: {
type: Number,
optional: true,
editableBy: ["admin"],
autoValue: function () {
// only provide a default value
// 1) this is an insert operation
// 2) status field is not set in the document being inserted
var user = Meteor.users.findOne(this.userId);
if (this.isInsert && !this.isSet)
return Posts.getDefaultStatus(user);
},
autoform: {
noselect: true,
options: Telescope.config.postStatuses,
group: 'admin'
}
},
sticky: {
type: Boolean,
optional: true,
defaultValue: false,
editableBy: ["admin"],
autoform: {
group: 'admin',
leftLabel: "Sticky"
}
},
inactive: {
type: Boolean,
optional: true
},
author: {
type: String,
optional: true
},
userId: {
type: String, // XXX
optional: true,
editableBy: ["admin"],
autoform: {
group: 'admin',
options: function () {
return Meteor.users.find().map(function (user) {
return {
value: user._id,
label: Users.getDisplayName(user)
};
});
}
}
}
});
// schema transforms
Telescope.schemas.posts.internationalize();
/**
* Attach schema to Posts collection
*/
Posts.attachSchema(Telescope.schemas.posts);
Posts.allow({
update: _.partial(Telescope.allowCheck, Posts),
remove: _.partial(Telescope.allowCheck, Posts)
});
//////////////////////////////////////////////////////
// Collection Hooks //
// https://atmospherejs.com/matb33/collection-hooks //
//////////////////////////////////////////////////////
/**
* Generate HTML body from Markdown on post insert
*/
Posts.before.insert(function (userId, doc) {
if(!!doc.body)
doc.htmlBody = Telescope.utils.sanitize(marked(doc.body));
});
/**
* Generate HTML body from Markdown when post body is updated
*/
Posts.before.update(function (userId, doc, fieldNames, modifier) {
// if body is being modified, update htmlBody too
if (Meteor.isServer && modifier.$set && modifier.$set.body) {
modifier.$set.htmlBody = Telescope.utils.sanitize(marked(modifier.$set.body));
}
});