Skip to content

Commit

Permalink
fixing get/set user setting
Browse files Browse the repository at this point in the history
  • Loading branch information
SachaG committed Jun 8, 2015
1 parent 1dea108 commit c268d14
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 55 deletions.
1 change: 0 additions & 1 deletion .meteor/versions
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
Expand Up @@ -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”

Expand Down
2 changes: 1 addition & 1 deletion packages/telescope-email/lib/server/email.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
3 changes: 2 additions & 1 deletion packages/telescope-lib/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ <h4 class="newsletter-tagline">{{_ "receive_the_best_of"}} {{siteName}} {{_ "rig
{{#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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion packages/telescope-newsletter/lib/server/mailchimp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
15 changes: 1 addition & 14 deletions packages/telescope-newsletter/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions packages/telescope-notifications/lib/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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');
Expand Down
4 changes: 3 additions & 1 deletion packages/telescope-releases/releases/0.20.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
73 changes: 44 additions & 29 deletions packages/telescope-users/lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 //
Expand Down Expand Up @@ -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('.');
Expand Down

0 comments on commit c268d14

Please sign in to comment.