Skip to content

Commit

Permalink
move views logic to createCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
SachaG committed Mar 5, 2017
1 parent 6ab9de0 commit 85dde4b
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 150 deletions.
2 changes: 1 addition & 1 deletion packages/nova-base-components/lib/posts/PostsPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class PostsPage extends Component {

{post.htmlBody ? <div className="posts-page-body" dangerouslySetInnerHTML={htmlBody}></div> : null}

<Components.PostsCommentsThread terms={{postId: post._id}} />
<Components.PostsCommentsThread terms={{postId: post._id, view: 'postComments'}} />

</div>
);
Expand Down
8 changes: 0 additions & 8 deletions packages/nova-comments/lib/parameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,3 @@ const CommentsMaxLimit = (parameters, terms) => {
}

addCallback("comments.parameters", CommentsMaxLimit);

const CommentsBelongingToPost = (parameters, terms) => {
parameters.selector.postId = terms.postId;

return parameters;
};

addCallback("comments.parameters", CommentsBelongingToPost);
31 changes: 8 additions & 23 deletions packages/nova-comments/lib/views.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,20 @@
import Comments from './collection.js';

/**
* @summary Comment views are filters used for subscribing to and viewing comments
* @namespace Comments.views
*/
Comments.views = {};

/**
* @summary Add a module to a comment view
* @param {string} viewName - The name of the view
* @param {function} [viewFunction] - The function used to calculate query terms. Takes terms and baseParameters arguments
*/
Comments.views.add = function (viewName, viewFunction) {
Comments.views[viewName] = viewFunction;
};

// will be common to all other view unless specific properties are overwritten
Comments.views.baseParameters = {
options: {
limit: 10
}
};
Comments.addDefaultView(function (terms) {
return {
options: {limit: 1000}
};
});

Comments.views.add("postComments", function (terms) {
Comments.addView("postComments", function (terms) {
return {
selector: {postId: terms.postId},
options: {limit: 0, sort: {postedAt: -1}}
options: {sort: {postedAt: -1}}
};
});

Comments.views.add("userComments", function (terms) {
Comments.addView("userComments", function (terms) {
return {
selector: {userId: terms.userId},
options: {sort: {postedAt: -1}}
Expand Down
22 changes: 22 additions & 0 deletions packages/nova-lib/lib/modules/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ Mongo.Collection.prototype.getPublicFields = function () {
return fields;
};


Mongo.Collection.prototype.addDefaultView = function (view) {
this.defaultView = view;
};

Mongo.Collection.prototype.addView = function (viewName, view) {
this.views[viewName] = view;
};

export const createCollection = options => {

// initialize new Mongo collection
Expand All @@ -106,6 +115,9 @@ export const createCollection = options => {
// add typeName
collection.typeName = options.typeName;

// add views
collection.views = [];

if (options.schema) {
// attach schema to collection
collection.attachSchema(new SimpleSchema(options.schema));
Expand Down Expand Up @@ -178,6 +190,16 @@ export const createCollection = options => {
options: {}
};

if (collection.defaultView) {
parameters = Utils.deepExtend(true, parameters, collection.defaultView(terms, apolloClient));
}

// handle view option
if (terms.view && collection.views[terms.view]) {
const view = collection.views[terms.view];
parameters = Utils.deepExtend(true, parameters, view(terms, apolloClient));
}

// iterate over posts.parameters callbacks
parameters = runCallbacks(`${options.collectionName}.parameters`, parameters, _.clone(terms), apolloClient);

Expand Down
20 changes: 9 additions & 11 deletions packages/nova-newsletter/lib/server/newsletter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,15 @@ import Newsletter from '../namespace.js';
import { Utils, getSetting } from 'meteor/nova:core';

// create new "newsletter" view for all posts from the past X days that haven't been scheduled yet
Posts.views.add("newsletter", function (terms) {
return {
selector: {
scheduledAt: {$exists: false}
},
options: {
sort: {baseScore: -1},
limit: terms.limit
}
};
});
Posts.addView("newsletter", terms => ({
selector: {
scheduledAt: {$exists: false}
},
options: {
sort: {baseScore: -1},
limit: terms.limit
}
}));

/**
* @summary Return an array containing the latest n posts that can be sent in a newsletter
Expand Down
21 changes: 1 addition & 20 deletions packages/nova-posts/lib/parameters.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,7 @@
import { Injected } from 'meteor/meteorhacks:inject-initial';
import moment from 'moment';
import Posts from './collection.js'
import { addCallback, Utils } from 'meteor/nova:core';
import { addCallback } from 'meteor/nova:core';

// Parameter callbacks

// View Parameter
// Add a "view" property to terms which can be used to filter posts.
function addViewParameter (parameters, terms, apolloClient) {

// if view is not defined, default to "new"
var view = !!terms.view ? Utils.dashToCamel(terms.view) : 'new';

// get query parameters according to current view
if (typeof Posts.views[view] !== 'undefined')
parameters = Utils.deepExtend(true, parameters, Posts.views[view](terms, apolloClient));

return parameters;
}
addCallback("posts.parameters", addViewParameter);

// View Parameter
// Add "after" and "before" properties to terms which can be used to limit posts in time.
function addTimeParameter (parameters, terms, apolloClient) {

Expand Down
142 changes: 56 additions & 86 deletions packages/nova-posts/lib/views.js
Original file line number Diff line number Diff line change
@@ -1,121 +1,101 @@
import Users from 'meteor/nova:users';
import Posts from './collection.js'

/**
* @summary Post views are filters used for subscribing to and viewing posts
* @namespace Posts.views
*/
Posts.views = {};

/**
* @summary Add a post view
* @param {string} viewName - The name of the view
* @param {function} [viewFunction] - The function used to calculate query terms. Takes terms and baseParameters arguments
*/
Posts.views.add = function (viewName, viewFunction) {
Posts.views[viewName] = viewFunction;
};

/**
* @summary Base parameters that will be common to all other view unless specific properties are overwritten
*/
Posts.views.baseParameters = {
Posts.addDefaultView(terms => ({
selector: {
status: Posts.config.STATUS_APPROVED,
isFuture: {$ne: true} // match both false and undefined
}
};
}));

/**
* @summary Top view
*/
Posts.views.add("top", function (terms) {
return {
...Posts.views.baseParameters,
options: {sort: {sticky: -1, score: -1}}
};
});
Posts.addView("top", terms => ({
options: {
sort: {sticky: -1, score: -1}
}
}));

/**
* @summary New view
*/
Posts.views.add("new", function (terms) {
return {
...Posts.views.baseParameters,
options: {sort: {sticky: -1, postedAt: -1}}
};
});
Posts.addView("new", terms => ({
options: {
sort: {sticky: -1, postedAt: -1}
}
}));

/**
* @summary Best view
*/
Posts.views.add("best", function (terms) {
return {
...Posts.views.baseParameters,
options: {sort: {sticky: -1, baseScore: -1}}
};
});
Posts.addView("best", terms => ({
options: {
sort: {sticky: -1, baseScore: -1}
}
}));

/**
* @summary Pending view
*/
Posts.views.add("pending", function (terms) {
return {
selector: {
status: Posts.config.STATUS_PENDING
},
options: {sort: {createdAt: -1}}
};
});
Posts.addView("pending", terms => ({
selector: {
status: Posts.config.STATUS_PENDING
},
options: {
sort: {createdAt: -1}
}
}));

/**
* @summary Rejected view
*/
Posts.views.add("rejected", function (terms) {
return {
selector: {
status: Posts.config.STATUS_REJECTED
},
options: {sort: {createdAt: -1}}
};
});
Posts.addView("rejected", terms => ({
selector: {
status: Posts.config.STATUS_REJECTED
},
options: {
sort: {createdAt: -1}
}
}));

/**
* @summary Scheduled view
*/
Posts.views.add("scheduled", function (terms) {
return {
selector: {
status: Posts.config.STATUS_APPROVED,
isFuture: true
},
options: {sort: {postedAt: -1}}
};
});
Posts.addView("scheduled", terms => ({
selector: {
status: Posts.config.STATUS_APPROVED,
isFuture: true
},
options: {
sort: {postedAt: -1}
}
}));

/**
* @summary User posts view
*/
Posts.views.add("userPosts", function (terms) {
return {
selector: {
userId: terms.userId,
status: Posts.config.STATUS_APPROVED,
isFuture: {$ne: true}
},
options: {
limit: 5,
sort: {
postedAt: -1
}
Posts.addView("userPosts", terms => ({
selector: {
userId: terms.userId,
status: Posts.config.STATUS_APPROVED,
isFuture: {$ne: true}
},
options: {
limit: 5,
sort: {
postedAt: -1
}
};
});
}
}));

/**
* @summary User upvoted posts view
*/
Posts.views.add("userUpvotedPosts", function (terms, apolloClient) {
Posts.addView("userUpvotedPosts", (terms, apolloClient) => {
var user = apolloClient ? Users.findOneInStore(apolloClient.store, terms.userId) : Users.findOne(terms.userId);

var postsIds = _.pluck(user.upvotedPosts, "itemId");
Expand All @@ -128,7 +108,7 @@ Posts.views.add("userUpvotedPosts", function (terms, apolloClient) {
/**
* @summary User downvoted posts view
*/
Posts.views.add("userDownvotedPosts", function (terms, apolloClient) {
Posts.addView("userDownvotedPosts", (terms, apolloClient) => {
var user = apolloClient ? Users.findOneInStore(apolloClient.store, terms.userId) : Users.findOne(terms.userId);

var postsIds = _.pluck(user.downvotedPosts, "itemId");
Expand All @@ -138,13 +118,3 @@ Posts.views.add("userDownvotedPosts", function (terms, apolloClient) {
options: {limit: 5, sort: {postedAt: -1}}
};
});


Posts.views.add("test", function (terms) {
return {
selector: {
title: {$regex: "newsletter", $options: 'i'}
},
options: {sort: {sticky: -1, baseScore: -1}}
};
});
2 changes: 1 addition & 1 deletion packages/nova-rss/lib/server/rss.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Posts from "meteor/nova:posts";
import Comments from "meteor/nova:comments";
import { Utils, getSetting } from 'meteor/nova:core';

Posts.views.rss = Posts.views.new; // default to "new" view for RSS feed
Posts.addView('rss', Posts.views.new); // default to "new" view for RSS feed

const getMeta = (url) => {
const siteUrl = getSetting('siteUrl', Meteor.absoluteUrl());
Expand Down

0 comments on commit 85dde4b

Please sign in to comment.