2016-11-26 02:46:55 +08:00
|
|
|
import { Injected } from 'meteor/meteorhacks:inject-initial';
|
2016-06-07 15:03:00 +09:00
|
|
|
import moment from 'moment';
|
2017-03-23 16:27:59 +09:00
|
|
|
import { addCallback, Utils } from 'meteor/vulcan:core';
|
2015-09-17 14:51:14 +09:00
|
|
|
|
2016-11-26 02:46:55 +08:00
|
|
|
// Add "after" and "before" properties to terms which can be used to limit posts in time.
|
2017-02-04 11:46:40 +09:00
|
|
|
function addTimeParameter (parameters, terms, apolloClient) {
|
2015-09-17 14:51:14 +09:00
|
|
|
|
2016-05-13 15:52:33 +09:00
|
|
|
// console.log("// addTimeParameter")
|
|
|
|
|
2016-02-17 19:39:43 +09:00
|
|
|
if (typeof parameters.selector.postedAt === "undefined") {
|
2016-11-26 02:46:55 +08:00
|
|
|
|
2016-05-13 15:52:33 +09:00
|
|
|
let postedAt = {}, mAfter, mBefore, startOfDay, endOfDay, clientTimezoneOffset, serverTimezoneOffset, timeDifference;
|
|
|
|
|
2016-11-26 02:46:55 +08:00
|
|
|
/*
|
2016-05-13 15:52:33 +09:00
|
|
|
|
|
|
|
If we're on the client, add the time difference between client and server
|
2016-11-26 02:46:55 +08:00
|
|
|
|
|
|
|
Example: client is on Japanese time (+9 hours),
|
2016-05-13 15:52:33 +09:00
|
|
|
server on UCT (Greenwich) time (+0 hours), for a total difference of +9 hours.
|
|
|
|
|
|
|
|
So the time "00:00, UCT" is equivalent to "09:00, JST".
|
|
|
|
|
2016-11-26 02:46:55 +08:00
|
|
|
So if we want to express the timestamp "00:00, UCT" on the client,
|
2016-05-13 15:52:33 +09:00
|
|
|
we *add* 9 hours to "00:00, JST" on the client to get "09:00, JST" and
|
|
|
|
sync up both times.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (Meteor.isClient) {
|
|
|
|
clientTimezoneOffset = -1 * new Date().getTimezoneOffset();
|
|
|
|
serverTimezoneOffset = -1 * Injected.obj('serverTimezoneOffset').offset;
|
|
|
|
timeDifference = clientTimezoneOffset - serverTimezoneOffset;
|
2016-11-26 02:46:55 +08:00
|
|
|
|
2016-05-13 15:52:33 +09:00
|
|
|
// console.log("client time:"+clientTimezoneOffset);
|
|
|
|
// console.log("server time:"+serverTimezoneOffset);
|
|
|
|
// console.log("difference: "+timeDifference);
|
|
|
|
}
|
2015-09-03 14:22:51 +09:00
|
|
|
|
2015-09-17 14:51:14 +09:00
|
|
|
if (terms.after) {
|
2016-05-13 15:52:33 +09:00
|
|
|
|
|
|
|
// console.log("// after: "+terms.after);
|
|
|
|
|
|
|
|
mAfter = moment(terms.after, "YYYY-MM-DD");
|
|
|
|
startOfDay = mAfter.startOf('day');
|
|
|
|
|
|
|
|
// console.log("// normal ", mAfter.toDate(), mAfter.valueOf());
|
|
|
|
// console.log("// startOfDay ", startOfDay.toDate(), startOfDay.valueOf());
|
|
|
|
|
|
|
|
if (Meteor.isClient) {
|
|
|
|
startOfDay.add(timeDifference, "minutes");
|
|
|
|
// console.log("// after add ", startOfDay.toDate(), startOfDay.valueOf());
|
2017-01-21 10:02:03 +09:00
|
|
|
// note: on the client, dates are stored as strings,
|
|
|
|
// so use strings for MongoDB filtering options too
|
|
|
|
postedAt.$gte = startOfDay.toISOString();
|
|
|
|
} else {
|
|
|
|
postedAt.$gte = startOfDay.toDate();
|
2016-05-13 15:52:33 +09:00
|
|
|
}
|
|
|
|
|
2015-09-17 14:51:14 +09:00
|
|
|
}
|
2015-09-03 14:22:51 +09:00
|
|
|
|
2015-09-17 14:51:14 +09:00
|
|
|
if (terms.before) {
|
2016-05-13 15:52:33 +09:00
|
|
|
|
|
|
|
mBefore = moment(terms.before, "YYYY-MM-DD");
|
|
|
|
endOfDay = mBefore.endOf('day');
|
|
|
|
|
|
|
|
if (Meteor.isClient) {
|
|
|
|
endOfDay.add(timeDifference, "minutes");
|
2017-01-21 10:02:03 +09:00
|
|
|
postedAt.$lt = endOfDay.toISOString();
|
|
|
|
} else {
|
|
|
|
postedAt.$lt = endOfDay.toDate();
|
2016-05-13 15:52:33 +09:00
|
|
|
}
|
|
|
|
|
2015-09-17 14:51:14 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!_.isEmpty(postedAt)) {
|
2016-02-17 19:39:43 +09:00
|
|
|
parameters.selector.postedAt = postedAt;
|
2015-09-17 14:51:14 +09:00
|
|
|
}
|
2015-09-03 14:22:51 +09:00
|
|
|
|
2015-09-17 14:51:14 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
return parameters;
|
2015-09-03 14:22:51 +09:00
|
|
|
}
|
2016-12-13 11:40:24 +09:00
|
|
|
addCallback("posts.parameters", addTimeParameter);
|
2015-05-06 17:38:19 +09:00
|
|
|
|
2015-09-03 14:30:35 +09:00
|
|
|
// limit the number of items that can be requested at once
|
2017-02-04 11:46:40 +09:00
|
|
|
function limitPosts (parameters, terms, apolloClient) {
|
2015-09-03 14:22:51 +09:00
|
|
|
var maxLimit = 200;
|
2016-02-27 16:41:50 +09:00
|
|
|
|
2016-04-09 10:22:44 +09:00
|
|
|
// 1. set default limit to 10
|
|
|
|
let limit = 10;
|
|
|
|
|
|
|
|
// 2. look for limit on terms.limit
|
|
|
|
if (terms.limit) {
|
|
|
|
limit = parseInt(terms.limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3. look for limit on terms.options.limit
|
|
|
|
if (terms.options && terms.options.limit) {
|
|
|
|
limit = parseInt(terms.options.limit);
|
2016-02-27 16:41:50 +09:00
|
|
|
}
|
|
|
|
|
2016-04-09 10:22:44 +09:00
|
|
|
// 4. make sure limit is not greater than 200
|
|
|
|
if (limit > maxLimit) {
|
|
|
|
limit = maxLimit;
|
2015-09-03 14:22:51 +09:00
|
|
|
}
|
2015-05-06 17:38:19 +09:00
|
|
|
|
2016-04-09 10:22:44 +09:00
|
|
|
// 5. initialize parameters.options if needed
|
|
|
|
if (!parameters.options) {
|
|
|
|
parameters.options = {};
|
2015-05-06 17:38:19 +09:00
|
|
|
}
|
2016-04-09 10:22:44 +09:00
|
|
|
|
|
|
|
// 6. set limit
|
|
|
|
parameters.options.limit = limit;
|
|
|
|
|
2015-09-03 14:22:51 +09:00
|
|
|
return parameters;
|
|
|
|
}
|
2016-12-13 11:40:24 +09:00
|
|
|
addCallback("posts.parameters", limitPosts);
|