diff --git a/lib/collections/anonymous-user.js b/lib/collections/anonymous-user.js index b506c2e..302ef45 100644 --- a/lib/collections/anonymous-user.js +++ b/lib/collections/anonymous-user.js @@ -1,3 +1,7 @@ +import SimpleSchema from 'simpl-schema'; +import { AutoForm } from 'meteor/aldeed:autoform'; +SimpleSchema.extendOptions(['autoform']); + AnonymousUserCollection = new Mongo.Collection('commentsui-anonymoususer') AnonymousUserCollection.allow({ diff --git a/lib/collections/comments.js b/lib/collections/comments.js index c255310..cac841a 100644 --- a/lib/collections/comments.js +++ b/lib/collections/comments.js @@ -1,15 +1,20 @@ -import { Mongo } from 'meteor/mongo' +import { Mongo } from "meteor/mongo"; -import linkifyStr from 'linkifyjs/string' -import timeTickService from '../services/time-tick' -import userService from '../services/user' -import comment from '../services/comment' -import { addCollectionMethods } from './CommentCollectionMethods' -import { addReplyMethods } from './ReplyCollectionMethods' +import SimpleSchema from "simpl-schema"; -export const CommentsCollection = new Mongo.Collection('comments') +SimpleSchema.extendOptions(["autoform"]); -CommentsCollection.schemas = {} +import moment from 'moment'; +import linkifyStr from "linkifyjs/string"; +import timeTickService from "../services/time-tick"; +import userService from "../services/user"; +import comment from "../services/comment"; +import { addCollectionMethods } from "./CommentCollectionMethods"; +import { addReplyMethods } from "./ReplyCollectionMethods"; + +export const CommentsCollection = new Mongo.Collection("comments"); + +CommentsCollection.schemas = {}; CommentsCollection.schemas.StarRatingSchema = new SimpleSchema({ userId: { @@ -18,17 +23,7 @@ CommentsCollection.schemas.StarRatingSchema = new SimpleSchema({ rating: { type: Number } -}) - -const likeableConfig = { - type: [String], - autoValue: function() { - if (this.isInsert) { - return [] - } - }, - optional: true -} +}); /** * Return a comment schema enhanced with the given schema config. @@ -45,11 +40,14 @@ function getCommonCommentSchema(additionalSchemaConfig = {}) { isAnonymous: { type: Boolean }, - 'media.type': { + media: { + type: Object + }, + "media.type": { type: String, optional: true }, - 'media.content': { + "media.content": { type: String, optional: true }, @@ -60,40 +58,68 @@ function getCommonCommentSchema(additionalSchemaConfig = {}) { }, status: { type: String, - autoValue: function () { + autoValue: function() { if (this.isInsert) { - return Comments.config().defaultCommentStatus + return Comments.config().defaultCommentStatus; } }, - optional: true, + optional: true }, + replies: { - type: [Object], - autoValue: function () { + type: Array + }, + "replies.$": { + type: Object, + autoValue: function() { if (this.isInsert) { - return [] + return []; } }, optional: true }, - likes: { ...likeableConfig }, - dislikes: { ...likeableConfig }, - starRatings: { - type: [CommentsCollection.schemas.StarRatingSchema], + likes: { + type: Array, + optional: true + }, + "likes.$": { + type: String, autoValue: function() { if (this.isInsert) { - return [] + return []; } - }, + } + }, + dislikes: { + type: Array, + optional: true + }, + "dislikes.$": { + type: String, + autoValue: function() { + if (this.isInsert) { + return []; + } + } + }, + starRatings: { + type: Array, optional: true }, + "starRatings.$": { + type: CommentsCollection.schemas.StarRatingSchema, + autoValue: function() { + if (this.isInsert) { + return []; + } + } + }, // general rating number that is used for sorting ratingScore: { type: Number, - decimal: true, autoValue: function() { if (this.isInsert) { - return 0 + return 0; } } }, @@ -101,11 +127,11 @@ function getCommonCommentSchema(additionalSchemaConfig = {}) { type: Date, autoValue: function() { if (this.isInsert) { - return new Date + return new Date(); } else if (this.isUpsert) { - return { $setOnInsert: new Date } + return { $setOnInsert: new Date() }; } else { - this.unset() + this.unset(); } } }, @@ -113,14 +139,13 @@ function getCommonCommentSchema(additionalSchemaConfig = {}) { type: Date, autoValue: function() { if (this.isUpdate) { - return new Date() + return new Date(); } }, - denyInsert: true, optional: true }, ...additionalSchemaConfig - } + }; } /** @@ -133,11 +158,11 @@ function getCommonCommentSchema(additionalSchemaConfig = {}) { */ function enhanceReplies(scope, position) { if (!position) { - position = [] + position = []; } - return _.map(scope.replies, function (reply, index) { - position.push(index) + return _.map(scope.replies, function(reply, index) { + position.push(index); reply = Object.assign(reply, { position: position.slice(0), @@ -148,7 +173,7 @@ function enhanceReplies(scope, position) { createdAgo: scope.createdAgo.bind(reply), getStarRating: scope.getStarRating.bind(reply), enhancedContent: scope.enhancedContent.bind(reply) - }) + }); if (reply.replies) { // recursive! @@ -157,69 +182,75 @@ function enhanceReplies(scope, position) { null, _.extend(_.clone(scope), { replies: reply.replies }), position - )() + )(); } - position.pop() + position.pop(); - return reply - }) + return reply; + }); } -CommentsCollection.schemas.ReplySchema = new SimpleSchema(getCommonCommentSchema({ - replyId: { - type: String - } -})) +CommentsCollection.schemas.ReplySchema = new SimpleSchema( + getCommonCommentSchema({ + replyId: { + type: String + } + }) +); -CommentsCollection.schemas.CommentSchema = new SimpleSchema(getCommonCommentSchema({ - referenceId: { - type: String - } -})) +CommentsCollection.schemas.CommentSchema = new SimpleSchema( + getCommonCommentSchema({ + referenceId: { + type: String + } + }) +); -CommentsCollection.attachSchema(CommentsCollection.schemas.CommentSchema) +CommentsCollection.attachSchema(CommentsCollection.schemas.CommentSchema); // Is handled with Meteor.methods CommentsCollection.allow({ insert: () => false, update: () => false, remove: () => false -}) +}); -const calculateAverageRating = (ratings) => _.reduce( - ratings, - (averageRating, rating) => (averageRating + rating.rating / ratings.length), - 0 -) +const calculateAverageRating = ratings => + _.reduce( + ratings, + (averageRating, rating) => averageRating + rating.rating / ratings.length, + 0 + ); -CommentsCollection._calculateAverageRating = calculateAverageRating +CommentsCollection._calculateAverageRating = calculateAverageRating; -const getCount = (scope, field) => scope[field] && scope[field].length ? scope[field].length : 0 +const getCount = (scope, field) => + scope[field] && scope[field].length ? scope[field].length : 0; -addCollectionMethods(CommentsCollection) -addReplyMethods(CommentsCollection) +addCollectionMethods(CommentsCollection); +addReplyMethods(CommentsCollection); CommentsCollection.helpers({ - likesCount: function () { - return getCount(this, 'likes'); + likesCount: function() { + return getCount(this, "likes"); }, - dislikesCount: function () { - return getCount(this, 'dislikes'); + dislikesCount: function() { + return getCount(this, "dislikes"); }, - user: function () { - return userService.getUserById(this.userId) + user: function() { + return userService.getUserById(this.userId); }, - createdAgo: function () { - return timeTickService.fromNowReactive(moment(this.createdAt)) + createdAgo: function() { + return timeTickService.fromNowReactive(moment(this.createdAt)); }, - filteredReplies: function () { + filteredReplies: function() { return comment.filterOutNotApprovedReplies( this.replies, - userService.getUserId(), - ) + userService.getUserId() + ); }, - enhancedReplies: function (position) { + enhancedReplies: function(position) { const { _id, user, @@ -228,50 +259,56 @@ CommentsCollection.helpers({ createdAgo, getStarRating, enhancedContent, - referenceId, - } = this + referenceId + } = this; - const userId = userService.getUserId() - let replies = this.replies + const userId = userService.getUserId(); + let replies = this.replies; if (!Comments.config().canSeePendingComments(referenceId, userId)) { - replies = this.filteredReplies() + replies = this.filteredReplies(); } - return enhanceReplies({ - _id, - user, - likesCount, - dislikesCount, - createdAgo, - getStarRating, - enhancedContent, - replies, - }, position) + return enhanceReplies( + { + _id, + user, + likesCount, + dislikesCount, + createdAgo, + getStarRating, + enhancedContent, + replies + }, + position + ); }, - enhancedContent: function () { - return linkifyStr((this.content || '')) + enhancedContent: function() { + return linkifyStr(this.content || ""); }, - getStarRating: function () { + getStarRating: function() { if (_.isArray(this.starRatings)) { - const ownRating = _.find(this.starRatings, rating => rating.userId === Meteor.userId()) + const ownRating = _.find( + this.starRatings, + rating => rating.userId === Meteor.userId() + ); if (ownRating) { return { - type: 'user', + type: "user", rating: ownRating.rating - } + }; } return { - type: 'average', + type: "average", rating: calculateAverageRating(this.starRatings) - } + }; } return { - type: 'average', + type: "average", rating: 0 - } + }; } -}) +}); diff --git a/lib/services/time-tick.js b/lib/services/time-tick.js index a0eb66f..7af83a7 100644 --- a/lib/services/time-tick.js +++ b/lib/services/time-tick.js @@ -1,3 +1,5 @@ +import moment from 'moment'; + const timeTickService = (() => { const timeTick = new Tracker.Dependency() diff --git a/package.js b/package.js index 040d95e..fe743da 100644 --- a/package.js +++ b/package.js @@ -1,89 +1,104 @@ Package.describe({ - name: 'arkham:comments-ui', - summary: 'Simple templates for disqus-like comment functionality', - version: '1.4.3', - git: 'https://github.com/komentify/meteor-comments-ui.git' + name: "arkham:comments-ui", + summary: "Simple templates for disqus-like comment functionality", + version: "1.4.3", + git: "https://github.com/komentify/meteor-comments-ui.git" }); Npm.depends({ - linkifyjs: '2.0.2', + "simpl-schema": "1.5.5", + "linkifyjs": "2.0.2", }); Package.onUse(function(api) { // Meteor Version - api.versionsFrom('METEOR@1.4.1.2'); + api.versionsFrom("METEOR@1.4.1.2"); // Meteor Core Dependencies - api.use(['accounts-password@1.0.1'], { weak: true }); + api.use(["accounts-password@1.0.1"], { weak: true }); api.use([ - 'accounts-base', - 'ecmascript', - 'underscore', - 'mongo-livedata', - 'templating', - 'jquery', - 'check', - 'less@2.5.0_2', - 'tracker', - 'check', - 'random', - 'markdown', - 'reactive-dict', + "accounts-base", + "ecmascript", + "underscore", + "mongo-livedata", + "templating", + "jquery", + "check", + "less@2.5.0_2", + "tracker", + "check", + "random", + "markdown", + "reactive-dict" ]); // Atmosphere Package Dependencies api.use([ - 'aldeed:collection2@2.5.0', 'aldeed:simple-schema@1.3.3', 'dburles:collection-helpers@1.0.3', - 'momentjs:moment@2.10.6', 'reywood:publish-composite@1.4.2', - 'aldeed:template-extension@4.0.0', 'barbatus:stars-rating@1.0.7' + "aldeed:collection2@3.0.0", + "dburles:collection-helpers", + "reywood:publish-composite", + "aldeed:template-extension@4.0.0", + "barbatus:stars-rating@1.0.7" ]); // Package specific globals and files api.addFiles([ - 'lib/collections/anonymous-user.js', - 'lib/collections/comments.js', - 'lib/collections/methods/anonymous-user.js', - 'lib/collections/methods/comments.js', + "lib/collections/anonymous-user.js", + "lib/collections/comments.js", + "lib/collections/methods/anonymous-user.js", + "lib/collections/methods/comments.js" ]); api.addFiles([ - 'lib/services/media-analyzers/image.js', - 'lib/services/media-analyzers/youtube.js', - 'lib/services/user.js', - 'lib/services/time-tick.js', - 'lib/services/media.js', - 'lib/components/commentsBox/commentsBox.html', - 'lib/components/commentsBox/commentsBox.less', - 'lib/components/commentsSingleComment/commentsSingleComment.html', - 'lib/components/commentsTextarea/commentsTextarea.html', - 'lib/components/commentsSubheader/commentsSubheader.html', - 'lib/components/commentsList/commentsList.html', - 'lib/api.js', + "lib/services/media-analyzers/image.js", + "lib/services/media-analyzers/youtube.js", + "lib/services/user.js", + "lib/services/time-tick.js", + "lib/services/media.js", + "lib/components/commentsBox/commentsBox.html", + "lib/components/commentsBox/commentsBox.less", + "lib/components/commentsSingleComment/commentsSingleComment.html", + "lib/components/commentsTextarea/commentsTextarea.html", + "lib/components/commentsSubheader/commentsSubheader.html", + "lib/components/commentsList/commentsList.html", + "lib/api.js" ]); - api.addFiles([ - 'lib/components/helpers.js', - 'lib/components/commentsBox/commentsBox.js', - 'lib/components/commentsSingleComment/commentsSingleComment.js', - 'lib/components/commentsTextarea/commentsTextarea.js', - 'lib/components/commentsSubheader/commentsSubheader.js', - 'lib/components/commentsList/commentsList.js', - ], 'client'); + api.addFiles( + [ + "lib/components/helpers.js", + "lib/components/commentsBox/commentsBox.js", + "lib/components/commentsSingleComment/commentsSingleComment.js", + "lib/components/commentsTextarea/commentsTextarea.js", + "lib/components/commentsSubheader/commentsSubheader.js", + "lib/components/commentsList/commentsList.js" + ], + "client" + ); - api.addFiles([ - 'lib/server/publish.js', - 'lib/services/hashing.js', - 'lib/comment-status-api.js', - 'lib/server/api.js', - ], 'server'); + api.addFiles( + [ + "lib/server/publish.js", + "lib/services/hashing.js", + "lib/comment-status-api.js", + "lib/server/api.js" + ], + "server" + ); - api.export('Comments'); + api.export("Comments"); }); Package.onTest(function(api) { - api.use(['tinytest', 'accounts-password', 'ecmascript', 'audit-argument-checks', 'check']); - api.use('arkham:comments-ui'); + api.use([ + "tinytest", + "accounts-password", + "ecmascript", + "audit-argument-checks", + "check" + ]); + api.use("arkham:comments-ui"); - api.addFiles(['tests/api-tests.js', 'tests/reply-service-tests.js']); - api.addFiles('tests/ui-tests.js', 'client'); + api.addFiles(["tests/api-tests.js", "tests/reply-service-tests.js"]); + api.addFiles("tests/ui-tests.js", "client"); });