fixing get/set user setting

This commit is contained in:
Sacha Greif 2015-06-08 09:54:42 +09:00
parent 1dea1083ba
commit c268d142cb
12 changed files with 61 additions and 55 deletions

View file

@ -79,7 +79,6 @@ mobile-status-bar@1.0.3
momentjs:moment@2.10.3
mongo@1.1.0
mongo-livedata@1.0.8
mrt:cookies@0.3.0
npm-bcrypt@0.7.8_2
oauth@1.1.4
oauth1@1.1.4

View file

@ -9,6 +9,8 @@ Just a couple minor bug fixes.
* Fixed video lightbox issue.
* Updated Getting Started content.
* Enforcing better URL formatting for Site URL setting.
* Fixed notification settings.
* Reworked user settings.
## v0.20.4 “RefactorScope”

View file

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

View file

@ -125,7 +125,7 @@
</tr>
<tr>
<td class="footer-container">
<!-- <a href="{{accountLink}}">Change your notifications settings</a><br/><br/> -->
<a href="{{accountLink}}">Change your notifications settings</a><br/><br/>
{{{footer}}}
</td>
</tr>

View file

@ -49,7 +49,8 @@ Package.onUse(function (api) {
'dburles:collection-helpers@1.0.3',
'jparker:gravatar@0.3.1',
'sanjo:meteor-files-helpers@1.1.0_4',
'cmather:handlebars-server@0.2.0'
'cmather:handlebars-server@0.2.0',
'chuangbo:cookie@1.1.0'
];
api.use(packages);

View file

@ -6,7 +6,7 @@
{{#if isNotConnected}}
<input class="newsletter-email" type="email" placeholder="Your Email">
{{/if}}
<button class="button newsletter-button btn btn-primary">{{_ "get_newsletter"}}<span class="button-loader"><img src="/img/loading-balls.svg"/></span></button>
<button class="button newsletter-button btn btn-primary">{{_ "get_newsletter"}}<span class="button-loader"><img src="/packages/telescope_core/public/img/loading-balls.svg"/></span></button>
</form>
<h4 class="newsletter-subscribed">{{_ "thanks_for_subscribing"}}</h4>
<a href="#" class="newsletter-dismiss banner-dismiss">×</a>

View file

@ -11,7 +11,7 @@ var dismissBanner = function () {
$('.newsletter-banner').fadeOut('fast', function () {
if(Meteor.user()){
// if user is connected, change setting in their account
Users.setUserSetting('showBanner', false);
Users.setSetting(Meteor.user(), 'newsletter.showBanner', false);
}else{
// set cookie
Cookie.set('showBanner', "no");
@ -34,8 +34,8 @@ Meteor.startup(function () {
|| !Users.can.view(Meteor.user())
|| Router.current().location.get().path !== '/'
|| Cookie.get('showBanner') === "no"
|| (Meteor.user() && Meteor.user().getSetting('telescope.newsletter.showBanner', true) === false)
|| (Meteor.user() && Meteor.user().getSetting('telescope.newsletter.subscribeToNewsletter', false) === true)
|| (Meteor.user() && Meteor.user().getSetting('newsletter.showBanner', true) === false)
|| (Meteor.user() && Meteor.user().getSetting('newsletter.subscribeToNewsletter', false) === true)
){
return false;
}else{

View file

@ -114,7 +114,7 @@ addToMailChimpList = function(userOrEmail, confirm, done){
// mark user as subscribed
if(!!user)
Users.setUserSetting('subscribedToNewsletter', true, user);
Users.setSetting(user, 'newsletter.subscribeToNewsletter', true);
console.log("// User subscribed");

View file

@ -16,20 +16,7 @@ Package.onUse(function (api) {
api.use([
'telescope:core@0.20.5',
'miro:mailchimp@1.0.4',
], ['client', 'server']);
api.use([
'jquery',
'underscore',
'templating',
'telescope:messages@0.1.0',
'mrt:cookies@0.3.0'
], 'client');
api.use([
'percolatestudio:synced-cron@1.1.0',
'cmather:handlebars-server@0.2.0'
], ['server']);
]);
api.addFiles([
'package-tap.i18n',

View file

@ -41,7 +41,7 @@ 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 (Users.getSetting(postAuthor, "telescope.notifications.comments", true) && comment.userId !== postAuthor._id) {
if (Users.getSetting(postAuthor, "notifications.comments", true) && comment.userId !== postAuthor._id) {
Herald.createNotification(post.userId, {courier: 'newComment', data: notificationData});
userIdsNotified.push(post.userId);
}
@ -58,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 (Users.getSetting(parentCommentAuthor, "telescope.notifications.replies", true)) {
if (Users.getSetting(parentCommentAuthor, "notifications.replies", true)) {
// add parent comment to notification data
notificationData.parentComment = _.pick(parentComment, '_id', 'userId', 'author');

View file

@ -8,4 +8,6 @@ Just a couple minor bug fixes.
* Generate public user properties list from schema.
* Fixed video lightbox issue.
* Updated Getting Started content.
* Enforcing better URL formatting for Site URL setting.
* Enforcing better URL formatting for Site URL setting.
* Fixed notification settings.
* Reworked user settings.

View file

@ -133,14 +133,55 @@ Users.userProfileCompleteById = function (userId) {return Users.userProfileCompl
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);
// all settings should be in the user.telescope namespace, so add "telescope." if needed
settingName = settingName.slice(0,10) === "telescope." ? settingName : "telescope." + settingName;
if (user.telescope) {
var settingValue = this.getProperty(user, settingName);
return (settingValue === null) ? defaultValue : settingValue;
} else {
return defaultValue;
}
};
Users.helpers({getSetting: function () {return Users.getSetting(this);}});
Users.helpers({getSetting: function (settingName, defaultValue) {return Users.getSetting(this, settingName, defaultValue);}});
/**
* Set a user setting
* @param {Object} user
* @param {String} settingName
* @param {Object} defaultValue
*/
Users.setSetting = function (userArgument, settingName, value) {
// note: for some very weird reason, doesn't work when called from Accounts.onCreateUser
var user;
if(Meteor.isClient){
user = (typeof userArgument === "undefined") ? Meteor.user() : userArgument; // on client, default to current user
}else if (Meteor.isServer){
user = userArgument; // on server, use argument
}
if(!user)
throw new Meteor.Error(500, 'User not defined');
Meteor.call('setUserSetting', settingName, value, user);
};
Users.helpers({setSetting: function () {return Users.setSetting(this);}});
Meteor.methods({
setUserSetting: function (settingName, value, user) {
console.log('Setting user setting "' + settingName + '" to "' + value + '" for ' + Users.getUserName(user));
// all settings should be in the user.telescope namespace, so add "telescope." if needed
var field = settingName.slice(0,10) === "telescope." ? settingName : "telescope." + settingName;
var modifier = {$set: {}};
modifier.$set[field] = value;
Meteor.users.update(user._id, modifier);
}
});
///////////////////
// Other Helpers //
@ -169,32 +210,6 @@ Users.numberOfItemsInPast24Hours = function (user, collection) {
return items.count();
};
Users.setUserSetting = function (settingName, value, userArgument) {
// note: for some very weird reason, doesn't work when called from Accounts.onCreateUser
var user;
if(Meteor.isClient){
user = (typeof userArgument === "undefined") ? Meteor.user() : userArgument; // on client, default to current user
}else if (Meteor.isServer){
user = userArgument; // on server, use argument
}
if(!user)
throw new Meteor.Error(500, 'User not defined');
Meteor.call('setUserSetting', settingName, value, user);
};
Meteor.methods({
setUserSetting: function (settingName, value, user) {
// console.log('Setting user setting "' + setting + '" to "' + value + '" for ' + Users.getUserName(user));
var field = 'telescope.settings.'+settingName;
var modifier = {$set: {}};
modifier.$set[field] = value;
Meteor.users.update(user._id, modifier);
}
});
Users.getProperty = function (object, property) {
// recursive function to get nested properties
var array = property.split('.');