FEATURE: Warn a user when they have few likes remaining

This commit is contained in:
Robin Ward
2016-03-18 11:17:51 -04:00
parent 1fba835d4f
commit 5d4ee2ca1d
9 changed files with 75 additions and 17 deletions
@@ -66,6 +66,10 @@ Discourse.Ajax = Em.Mixin.create({
});
}
if (args.returnXHR) {
data = { result: data, xhr };
}
Ember.run(null, resolve, data);
};
@@ -18,10 +18,7 @@ export default RestModel.extend({
},
togglePromise(post) {
if (!this.get('acted')) {
return this.act(post).then(() => true);
}
return this.undo(post).then(() => false);
return this.get('acted') ? this.undo(post) : this.act(post);
},
toggle(post) {
@@ -64,11 +61,15 @@ export default RestModel.extend({
message: opts.message,
take_action: opts.takeAction,
flag_topic: this.get('flagTopic') ? true : false
}
}).then(function(result) {
},
returnXHR: true,
}).then(function(data) {
if (!self.get('flagTopic')) {
return post.updateActionsSummary(result);
post.updateActionsSummary(data.result);
}
const remaining = parseInt(data.xhr.getResponseHeader('Discourse-Actions-Remaining') || 0);
const max = parseInt(data.xhr.getResponseHeader('Discourse-Actions-Max') || 0);
return { acted: true, remaining, max };
}).catch(function(error) {
popupAjaxError(error);
self.removeAction(post);
@@ -83,7 +84,10 @@ export default RestModel.extend({
return Discourse.ajax("/post_actions/" + post.get('id'), {
type: 'DELETE',
data: { post_action_type_id: this.get('id') }
}).then(result => post.updateActionsSummary(result));
}).then(result => {
post.updateActionsSummary(result);
return { acted: false };
});
},
deferFlags(post) {
@@ -423,7 +423,27 @@ export default createWidget('post', {
const likeAction = post.get('likeAction');
if (likeAction && likeAction.get('canToggle')) {
return likeAction.togglePromise(post);
return likeAction.togglePromise(post).then(result => this._warnIfClose(result));
}
},
_warnIfClose(result) {
if (!result || !result.acted) { return; }
const kvs = this.keyValueStore;
const lastWarnedLikes = kvs.get('lastWarnedLikes');
// only warn once per day
const yesterday = new Date().getTime() - 1000 * 60 * 60 * 24;
if (lastWarnedLikes && parseInt(lastWarnedLikes) > yesterday) {
return;
}
const { remaining, max } = result;
const threshold = Math.ceil(max * 0.1);
if (remaining === threshold) {
bootbox.alert(I18n.t('post.few_likes_left'));
kvs.set({ key: 'lastWarnedLikes', value: new Date().getTime() });
}
},
@@ -119,6 +119,7 @@ export default class Widget {
this.currentUser = container.lookup('current-user:main');
this.store = container.lookup('store:main');
this.appEvents = container.lookup('app-events:main');
this.keyValueStore = container.lookup('key-value-store:main');
if (this.name) {
const custom = _customSettings[this.name];