mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-12-11 16:21:24 -05:00
Refactor likes/flags to simplify things a lot
This commit is contained in:
parent
5f3c381dc2
commit
cc2a33617f
9 changed files with 25 additions and 36 deletions
|
@ -56,8 +56,8 @@ const PostMenuComponent = Ember.Component.extend(StringBuffer, {
|
||||||
|
|
||||||
rerenderTriggers: [
|
rerenderTriggers: [
|
||||||
'post.deleted_at',
|
'post.deleted_at',
|
||||||
'likeAction.count',
|
'post.likeAction.count',
|
||||||
'likeAction.users.length',
|
'post.likeAction.users.length',
|
||||||
'post.reply_count',
|
'post.reply_count',
|
||||||
'post.showRepliesBelow',
|
'post.showRepliesBelow',
|
||||||
'post.can_delete',
|
'post.can_delete',
|
||||||
|
@ -69,10 +69,6 @@ const PostMenuComponent = Ember.Component.extend(StringBuffer, {
|
||||||
'post.post_type',
|
'post.post_type',
|
||||||
'collapsed'],
|
'collapsed'],
|
||||||
|
|
||||||
likeAction: function() {
|
|
||||||
return this.get('post.actionByName.like');
|
|
||||||
}.property('post.actionByName.like'),
|
|
||||||
|
|
||||||
_collapsedByDefault: function() {
|
_collapsedByDefault: function() {
|
||||||
this.set('collapsed', true);
|
this.set('collapsed', true);
|
||||||
}.on('init'),
|
}.on('init'),
|
||||||
|
@ -167,7 +163,7 @@ const PostMenuComponent = Ember.Component.extend(StringBuffer, {
|
||||||
},
|
},
|
||||||
|
|
||||||
clickLikeCount() {
|
clickLikeCount() {
|
||||||
const likeAction = this.get('post.actionByName.like');
|
const likeAction = this.get('post.likeAction');
|
||||||
if (likeAction) {
|
if (likeAction) {
|
||||||
const users = likeAction.get('users');
|
const users = likeAction.get('users');
|
||||||
if (users && users.length) {
|
if (users && users.length) {
|
||||||
|
@ -233,11 +229,11 @@ const PostMenuComponent = Ember.Component.extend(StringBuffer, {
|
||||||
|
|
||||||
// Like button
|
// Like button
|
||||||
buttonForLike() {
|
buttonForLike() {
|
||||||
const likeAction = this.get('likeAction');
|
const likeAction = this.get('post.likeAction');
|
||||||
if (!likeAction) { return; }
|
if (!likeAction) { return; }
|
||||||
|
|
||||||
const className = likeAction.get('acted') ? 'has-like fade-out' : 'like';
|
const className = likeAction.get('acted') ? 'has-like fade-out' : 'like';
|
||||||
var opts = {className: className};
|
const opts = {className: className};
|
||||||
|
|
||||||
if (likeAction.get('canToggle')) {
|
if (likeAction.get('canToggle')) {
|
||||||
const descKey = likeAction.get('acted') ? 'post.controls.undo_like' : 'post.controls.like';
|
const descKey = likeAction.get('acted') ? 'post.controls.undo_like' : 'post.controls.like';
|
||||||
|
@ -249,16 +245,16 @@ const PostMenuComponent = Ember.Component.extend(StringBuffer, {
|
||||||
},
|
},
|
||||||
|
|
||||||
buttonForLikeCount() {
|
buttonForLikeCount() {
|
||||||
var likeCount = this.get('post.like_count') || 0;
|
const likeCount = this.get('post.likeAction.count') || 0;
|
||||||
if (likeCount > 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';
|
const label = likedPost ? 'post.has_likes_title_you' : 'post.has_likes_title';
|
||||||
|
|
||||||
return new Button('like-count', label, undefined, {
|
return new Button('like-count', label, undefined, {
|
||||||
className: 'like-count highlight-action',
|
className: 'like-count highlight-action',
|
||||||
innerHTML: I18n.t("post.has_likes", { count: likeCount }),
|
innerHTML: I18n.t("post.has_likes", { count: likeCount }),
|
||||||
labelOptions: {count: likedPost ? (likeCount-1) : likeCount}
|
labelOptions: {count: likedPost ? (likeCount-1) : likeCount}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -266,7 +262,7 @@ const PostMenuComponent = Ember.Component.extend(StringBuffer, {
|
||||||
clickLike(post) {
|
clickLike(post) {
|
||||||
const $heart = this.$('.fa-heart'),
|
const $heart = this.$('.fa-heart'),
|
||||||
$likeButton = this.$('button[data-action=like]'),
|
$likeButton = this.$('button[data-action=like]'),
|
||||||
acted = post.get('actionByName.like.acted'),
|
acted = post.get('likeAction.acted'),
|
||||||
self = this;
|
self = this;
|
||||||
|
|
||||||
if (acted) {
|
if (acted) {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import StringBuffer from 'discourse/mixins/string-buffer';
|
import StringBuffer from 'discourse/mixins/string-buffer';
|
||||||
|
|
||||||
export default Ember.Component.extend(StringBuffer, {
|
export default Ember.Component.extend(StringBuffer, {
|
||||||
likedUsers: Ember.computed.alias('post.actionByName.like.users'),
|
likedUsers: Ember.computed.alias('post.likeAction.users'),
|
||||||
rerenderTriggers: ['likedUsers.length'],
|
rerenderTriggers: ['likedUsers.length'],
|
||||||
|
|
||||||
renderString(buffer) {
|
renderString(buffer) {
|
||||||
|
|
|
@ -76,11 +76,13 @@ export default ObjectController.extend(ModalFunctionality, {
|
||||||
createFlag(opts) {
|
createFlag(opts) {
|
||||||
const self = this;
|
const self = this;
|
||||||
let postAction; // an instance of ActionSummary
|
let postAction; // an instance of ActionSummary
|
||||||
|
|
||||||
if (!this.get('flagTopic')) {
|
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 {
|
} else {
|
||||||
postAction = this.get('topicActionByName.' + this.get('selected.name_key'));
|
postAction = this.get('topicActionByName.' + this.get('selected.name_key'));
|
||||||
}
|
}
|
||||||
|
|
||||||
let params = this.get('selected.is_custom_flag') ? {message: this.get('message')} : {};
|
let params = this.get('selected.is_custom_flag') ? {message: this.get('message')} : {};
|
||||||
if (opts) { params = $.extend(params, opts); }
|
if (opts) { params = $.extend(params, opts); }
|
||||||
|
|
||||||
|
|
|
@ -153,7 +153,7 @@ export default ObjectController.extend(SelectedPostsCount, BufferedContent, {
|
||||||
},
|
},
|
||||||
|
|
||||||
toggleLike(post) {
|
toggleLike(post) {
|
||||||
const likeAction = post.get('actionByName.like');
|
const likeAction = post.get('likeAction');
|
||||||
if (likeAction && likeAction.get('canToggle')) {
|
if (likeAction && likeAction.get('canToggle')) {
|
||||||
likeAction.toggle(post);
|
likeAction.toggle(post);
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,10 +25,7 @@ export default RestModel.extend({
|
||||||
}.property('can_undo', 'can_act'),
|
}.property('can_undo', 'can_act'),
|
||||||
|
|
||||||
// Remove it
|
// Remove it
|
||||||
removeAction: function(post) {
|
removeAction: function() {
|
||||||
|
|
||||||
const action = this.get('actionType.name_key');
|
|
||||||
|
|
||||||
this.setProperties({
|
this.setProperties({
|
||||||
acted: false,
|
acted: false,
|
||||||
count: this.get('count') - 1,
|
count: this.get('count') - 1,
|
||||||
|
@ -36,11 +33,6 @@ export default RestModel.extend({
|
||||||
can_undo: false
|
can_undo: false
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
if (action === 'like' && post) {
|
|
||||||
post.set('like_count', this.get('count'));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.get('usersExpanded')) {
|
if (this.get('usersExpanded')) {
|
||||||
this.get('users').removeObject(Discourse.User.current());
|
this.get('users').removeObject(Discourse.User.current());
|
||||||
}
|
}
|
||||||
|
@ -69,11 +61,7 @@ export default RestModel.extend({
|
||||||
can_undo: true
|
can_undo: true
|
||||||
});
|
});
|
||||||
|
|
||||||
if (action === 'like') {
|
if (action === 'notify_moderators' || action === 'notify_user') {
|
||||||
post.set('like_count', this.get('count'));
|
|
||||||
}
|
|
||||||
|
|
||||||
if(action === 'notify_moderators' || action === 'notify_user') {
|
|
||||||
this.set('can_undo',false);
|
this.set('can_undo',false);
|
||||||
this.set('can_defer_flags',false);
|
this.set('can_defer_flags',false);
|
||||||
}
|
}
|
||||||
|
|
|
@ -367,6 +367,10 @@ Post.reopenClass({
|
||||||
a.count = a.count || 0;
|
a.count = a.count || 0;
|
||||||
const actionSummary = ActionSummary.create(a);
|
const actionSummary = ActionSummary.create(a);
|
||||||
lookup[a.actionType.name_key] = actionSummary;
|
lookup[a.actionType.name_key] = actionSummary;
|
||||||
|
|
||||||
|
if (a.actionType.name_key === "like") {
|
||||||
|
json.likeAction = actionSummary;
|
||||||
|
}
|
||||||
return actionSummary;
|
return actionSummary;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,6 @@ class PostSerializer < BasicPostSerializer
|
||||||
attributes :post_number,
|
attributes :post_number,
|
||||||
:post_type,
|
:post_type,
|
||||||
:updated_at,
|
:updated_at,
|
||||||
:like_count,
|
|
||||||
:reply_count,
|
:reply_count,
|
||||||
:reply_to_post_number,
|
:reply_to_post_number,
|
||||||
:quote_count,
|
:quote_count,
|
||||||
|
|
|
@ -19,7 +19,7 @@ describe PostSerializer do
|
||||||
serializer.post_actions = PostAction.counts_for([post], actor)[post.id] if user.try(:id) == actor.id
|
serializer.post_actions = PostAction.counts_for([post], actor)[post.id] if user.try(:id) == actor.id
|
||||||
actions = serializer.as_json[:actions_summary]
|
actions = serializer.as_json[:actions_summary]
|
||||||
lookup = PostActionType.types.invert
|
lookup = PostActionType.types.invert
|
||||||
actions.keep_if{|a| a[:count] > 0}.map{|a| lookup[a[:id]]}
|
actions.keep_if{|a| (a[:count] || 0) > 0}.map{|a| lookup[a[:id]]}
|
||||||
end
|
end
|
||||||
|
|
||||||
before do
|
before do
|
||||||
|
@ -39,7 +39,7 @@ describe PostSerializer do
|
||||||
it "can't flag your own post to notify yourself" do
|
it "can't flag your own post to notify yourself" do
|
||||||
serializer = PostSerializer.new(post, scope: Guardian.new(post.user), root: false)
|
serializer = PostSerializer.new(post, scope: Guardian.new(post.user), root: false)
|
||||||
notify_user_action = serializer.actions_summary.find { |a| a[:id] == PostActionType.types[:notify_user] }
|
notify_user_action = serializer.actions_summary.find { |a| a[:id] == PostActionType.types[:notify_user] }
|
||||||
expect(notify_user_action[:can_act]).to eq(false)
|
expect(notify_user_action).to be_blank
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,7 @@ function setup(store) {
|
||||||
});
|
});
|
||||||
|
|
||||||
this.on('toggleLike', function() {
|
this.on('toggleLike', function() {
|
||||||
post.toggleProperty('actionByName.like.acted');
|
post.toggleProperty('likeAction.acted');
|
||||||
});
|
});
|
||||||
|
|
||||||
this.set('post', post);
|
this.set('post', post);
|
||||||
|
|
Loading…
Reference in a new issue