Refactor likes/flags to simplify things a lot

This commit is contained in:
Robin Ward
2015-07-14 15:50:27 -04:00
parent 5f3c381dc2
commit cc2a33617f
9 changed files with 25 additions and 36 deletions

View File

@@ -56,8 +56,8 @@ const PostMenuComponent = Ember.Component.extend(StringBuffer, {
rerenderTriggers: [
'post.deleted_at',
'likeAction.count',
'likeAction.users.length',
'post.likeAction.count',
'post.likeAction.users.length',
'post.reply_count',
'post.showRepliesBelow',
'post.can_delete',
@@ -69,10 +69,6 @@ const PostMenuComponent = Ember.Component.extend(StringBuffer, {
'post.post_type',
'collapsed'],
likeAction: function() {
return this.get('post.actionByName.like');
}.property('post.actionByName.like'),
_collapsedByDefault: function() {
this.set('collapsed', true);
}.on('init'),
@@ -167,7 +163,7 @@ const PostMenuComponent = Ember.Component.extend(StringBuffer, {
},
clickLikeCount() {
const likeAction = this.get('post.actionByName.like');
const likeAction = this.get('post.likeAction');
if (likeAction) {
const users = likeAction.get('users');
if (users && users.length) {
@@ -233,11 +229,11 @@ const PostMenuComponent = Ember.Component.extend(StringBuffer, {
// Like button
buttonForLike() {
const likeAction = this.get('likeAction');
const likeAction = this.get('post.likeAction');
if (!likeAction) { return; }
const className = likeAction.get('acted') ? 'has-like fade-out' : 'like';
var opts = {className: className};
const opts = {className: className};
if (likeAction.get('canToggle')) {
const descKey = likeAction.get('acted') ? 'post.controls.undo_like' : 'post.controls.like';
@@ -249,16 +245,16 @@ const PostMenuComponent = Ember.Component.extend(StringBuffer, {
},
buttonForLikeCount() {
var likeCount = this.get('post.like_count') || 0;
const likeCount = this.get('post.likeAction.count') || 0;
if (likeCount > 0) {
const likedPost = !!this.get('likeAction.acted');
const likedPost = !!this.get('post.likeAction.acted');
const label = likedPost ? 'post.has_likes_title_you' : 'post.has_likes_title';
return new Button('like-count', label, undefined, {
className: 'like-count highlight-action',
innerHTML: I18n.t("post.has_likes", { count: likeCount }),
labelOptions: {count: likedPost ? (likeCount-1) : likeCount}
className: 'like-count highlight-action',
innerHTML: I18n.t("post.has_likes", { count: likeCount }),
labelOptions: {count: likedPost ? (likeCount-1) : likeCount}
});
}
},
@@ -266,7 +262,7 @@ const PostMenuComponent = Ember.Component.extend(StringBuffer, {
clickLike(post) {
const $heart = this.$('.fa-heart'),
$likeButton = this.$('button[data-action=like]'),
acted = post.get('actionByName.like.acted'),
acted = post.get('likeAction.acted'),
self = this;
if (acted) {

View File

@@ -1,7 +1,7 @@
import StringBuffer from 'discourse/mixins/string-buffer';
export default Ember.Component.extend(StringBuffer, {
likedUsers: Ember.computed.alias('post.actionByName.like.users'),
likedUsers: Ember.computed.alias('post.likeAction.users'),
rerenderTriggers: ['likedUsers.length'],
renderString(buffer) {

View File

@@ -76,11 +76,13 @@ export default ObjectController.extend(ModalFunctionality, {
createFlag(opts) {
const self = this;
let postAction; // an instance of ActionSummary
if (!this.get('flagTopic')) {
postAction = this.get('model.actionByName.' + this.get('selected.name_key'));
postAction = this.get('model.actions_summary').findProperty('id', this.get('selected.id'));
} else {
postAction = this.get('topicActionByName.' + this.get('selected.name_key'));
}
let params = this.get('selected.is_custom_flag') ? {message: this.get('message')} : {};
if (opts) { params = $.extend(params, opts); }

View File

@@ -153,7 +153,7 @@ export default ObjectController.extend(SelectedPostsCount, BufferedContent, {
},
toggleLike(post) {
const likeAction = post.get('actionByName.like');
const likeAction = post.get('likeAction');
if (likeAction && likeAction.get('canToggle')) {
likeAction.toggle(post);
}

View File

@@ -25,10 +25,7 @@ export default RestModel.extend({
}.property('can_undo', 'can_act'),
// Remove it
removeAction: function(post) {
const action = this.get('actionType.name_key');
removeAction: function() {
this.setProperties({
acted: false,
count: this.get('count') - 1,
@@ -36,11 +33,6 @@ export default RestModel.extend({
can_undo: false
});
if (action === 'like' && post) {
post.set('like_count', this.get('count'));
}
if (this.get('usersExpanded')) {
this.get('users').removeObject(Discourse.User.current());
}
@@ -69,11 +61,7 @@ export default RestModel.extend({
can_undo: true
});
if (action === 'like') {
post.set('like_count', this.get('count'));
}
if(action === 'notify_moderators' || action === 'notify_user') {
if (action === 'notify_moderators' || action === 'notify_user') {
this.set('can_undo',false);
this.set('can_defer_flags',false);
}

View File

@@ -367,6 +367,10 @@ Post.reopenClass({
a.count = a.count || 0;
const actionSummary = ActionSummary.create(a);
lookup[a.actionType.name_key] = actionSummary;
if (a.actionType.name_key === "like") {
json.likeAction = actionSummary;
}
return actionSummary;
});