migrate newsletter settings; use Users.getSetting

This commit is contained in:
Sacha Greif 2015-06-08 08:52:41 +09:00
parent 059028fc66
commit 8356f43a8f
6 changed files with 149 additions and 70 deletions

View file

@ -95,7 +95,7 @@ function adminUserCreationNotification (user) {
// send notifications to admins
var admins = Users.adminUsers();
admins.forEach(function(admin){
if (admin.telescope.notifications.users) {
if (Users.getSetting(admin, "telescope.notifications.users", false)) {
var emailProperties = {
profileUrl: Users.getProfileUrl(user),
username: Users.getUserName(user)

View file

@ -634,10 +634,55 @@ var migrationsList = {
if (!!displayName) {
Meteor.users.update(user._id, {$set: {"telescope.displayName": displayName}});
} else {
"displayName not found :("
console.log("displayName not found :(");
}
});
return i;
},
migrateNewsletterSettings: function () {
var i = 0;
var allUsers = Meteor.users.find({
$or: [
{"profile.showBanner": {$exists: true}},
{"profile.subscribedToNewsletter": {$exists: true}}
]
});
console.log('> Found '+allUsers.count()+' users.\n');
allUsers.forEach(function(user){
i++;
var displayName;
if (!!user.profile) {
displayName = user.profile.name || user.profile.username;
} else {
displayName = user.username;
}
console.log('> Updating user '+user._id+' (' + displayName + ')');
if (user.profile) {
var set = {};
var showBanner = user.profile.showBanner;
if (typeof showBanner !== "undefined") {
set["telescope.newsletter.showBanner"] = showBanner;
}
var subscribeToNewsletter = user.profile.subscribedToNewsletter;
if (typeof subscribeToNewsletter !== "undefined") {
set["telescope.newsletter.subscribeToNewsletter"] = subscribeToNewsletter;
}
console.log(set)
if (!_.isEmpty(set)) {
Meteor.users.update(user._id, {$set: set});
}
}
});
return i;
}
};

View file

@ -34,8 +34,8 @@ Meteor.startup(function () {
|| !Users.can.view(Meteor.user())
|| Router.current().location.get().path !== '/'
|| Cookie.get('showBanner') === "no"
|| (Meteor.user() && Users.getUserSetting('showBanner', true) === false)
|| (Meteor.user() && Users.getUserSetting('subscribedToNewsletter', false) === true)
|| (Meteor.user() && Meteor.user().getSetting('telescope.newsletter.showBanner', true) === false)
|| (Meteor.user() && Meteor.user().getSetting('telescope.newsletter.subscribeToNewsletter', false) === true)
){
return false;
}else{

View file

@ -40,6 +40,32 @@ Posts.addField({
}
});
Users.addField([
{
fieldName: 'telescope.newsletter.showBanner',
fieldSchema: {
label: 'Show banner',
type: Boolean,
optional: true,
editableBy: ['admin', 'member'],
autoform: {
omit: true
}
}
},
{
fieldName: 'telescope.newsletter.subscribeToNewsletter',
fieldSchema: {
label: 'Subscribe to newsletter',
type: Boolean,
optional: true,
editableBy: ['admin', 'member'],
autoform: {
group: 'Newsletter'
}
}
}
]);
// Users
// TODO: add "subscribe to newsletter" checkbox or setting to account page

View file

@ -5,8 +5,8 @@ function postSubmitNotification (post) {
var notifiedUserIds = _.pluck(Users.find({'telescope.notifications.posts': true}, {fields: {_id:1}}).fetch(), '_id');
// remove post author ID from arrays
var adminIds = _.without(adminIds, post.userId);
var notifiedUserIds = _.without(notifiedUserIds, post.userId);
adminIds = _.without(adminIds, post.userId);
notifiedUserIds = _.without(notifiedUserIds, post.userId);
if (post.status === Posts.config.STATUS_PENDING && !!adminIds.length) {
// if post is pending, only notify admins
@ -41,11 +41,9 @@ function addCommentNotification (comment) {
// 1. Notify author of post (if they have new comment notifications turned on)
// but do not notify author of post if they're the ones posting the comment
if (!!postAuthor.telescope.notifications.comments && comment.userId !== postAuthor._id) {
if (Users.getSetting(postAuthor, "telescope.notifications.comments", true) && comment.userId !== postAuthor._id) {
Herald.createNotification(post.userId, {courier: 'newComment', data: notificationData});
userIdsNotified.push(post.userId);
}
// 2. Notify author of comment being replied to
@ -60,7 +58,7 @@ function addCommentNotification (comment) {
var parentCommentAuthor = Users.findOne(parentComment.userId);
// do not notify parent comment author if they have reply notifications turned off
if (!!parentCommentAuthor.telescope.notifications.replies) {
if (Users.getSetting(parentCommentAuthor, "telescope.notifications.replies", true)) {
// add parent comment to notification data
notificationData.parentComment = _.pick(parentComment, '_id', 'userId', 'author');
@ -122,57 +120,60 @@ Comments.addField(
);
// Add notifications options to user profile settings
Users.addField({
fieldName: 'telescope.notifications.users',
fieldSchema: {
label: 'New users',
type: Boolean,
optional: true,
editableBy: ['admin'],
autoform: {
group: 'Email Notifications'
Users.addField([
{
fieldName: 'telescope.notifications.users',
fieldSchema: {
label: 'New users',
type: Boolean,
optional: true,
defaultValue: false,
editableBy: ['admin'],
autoform: {
group: 'Email Notifications'
}
}
},
{
fieldName: 'telescope.notifications.posts',
fieldSchema: {
label: 'New posts',
type: Boolean,
optional: true,
defaultValue: false,
editableBy: ['admin', 'member'],
autoform: {
group: 'Email Notifications'
}
}
},
{
fieldName: 'telescope.notifications.comments',
fieldSchema: {
label: 'Comments on my posts',
type: Boolean,
optional: true,
defaultValue: true,
editableBy: ['admin', 'member'],
autoform: {
group: 'Email Notifications'
}
}
},
{
fieldName: 'telescope.notifications.replies',
fieldSchema: {
label: 'Replies to my comments',
type: Boolean,
optional: true,
defaultValue: true,
editableBy: ['admin', 'member'],
autoform: {
group: 'Email Notifications'
}
}
}
});
Users.addField({
fieldName: 'telescope.notifications.posts',
fieldSchema: {
label: 'New posts',
type: Boolean,
optional: true,
editableBy: ['admin', 'member'],
autoform: {
group: 'Email Notifications'
}
}
});
Users.addField({
fieldName: 'telescope.notifications.comments',
fieldSchema: {
label: 'Comments on my posts',
type: Boolean,
optional: true,
editableBy: ['admin', 'member'],
autoform: {
group: 'Email Notifications'
}
}
});
Users.addField({
fieldName: 'telescope.notifications.replies',
fieldSchema: {
label: 'Replies to my comments',
type: Boolean,
optional: true,
editableBy: ['admin', 'member'],
autoform: {
group: 'Email Notifications'
}
}
});
]);
function setNotificationDefaults (user) {
// set notifications default preferences

View file

@ -124,6 +124,24 @@ Users.userProfileComplete = function (user) {
Users.helpers({userProfileComplete: function () {return Users.userProfileComplete(this);}});
Users.userProfileCompleteById = function (userId) {return Users.userProfileComplete(Meteor.users.findOne(userId));};
/**
* Get a user setting
* @param {Object} user
* @param {String} settingName
* @param {Object} defaultValue
*/
Users.getSetting = function (user, settingName, defaultValue) {
user = user || Meteor.user();
defaultValue = defaultValue || null;
if (user.telescope && user.telescope.settings) {
var settingValue = this.getProperty(user.telescope.settings, settingName);
return (settingValue === null) ? defaultValue : settingValue;
} else {
return defaultValue;
}
};
Users.helpers({getSetting: function () {return Users.getSetting(this);}});
///////////////////
// Other Helpers //
///////////////////
@ -151,17 +169,6 @@ Users.numberOfItemsInPast24Hours = function (user, collection) {
return items.count();
};
Users.getUserSetting = function (settingName, defaultValue, user) {
user = user || Meteor.user();
defaultValue = defaultValue || null;
if (user.telescope && user.telescope.settings) {
var settingValue = this.getProperty(user.telescope.settings, settingName);
return (settingValue === null) ? defaultValue : settingValue;
} else {
return defaultValue;
}
};
Users.setUserSetting = function (settingName, value, userArgument) {
// note: for some very weird reason, doesn't work when called from Accounts.onCreateUser