mirror of
https://github.com/discourse/discourse.git
synced 2024-11-27 03:10:46 -06:00
Refactor likes/flags to simplify things a lot
This commit is contained in:
parent
5f3c381dc2
commit
cc2a33617f
@ -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,9 +245,9 @@ 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';
|
||||
|
||||
@ -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) {
|
||||
|
@ -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) {
|
||||
|
@ -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); }
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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;
|
||||
});
|
||||
|
||||
|
@ -18,7 +18,6 @@ class PostSerializer < BasicPostSerializer
|
||||
attributes :post_number,
|
||||
:post_type,
|
||||
:updated_at,
|
||||
:like_count,
|
||||
:reply_count,
|
||||
:reply_to_post_number,
|
||||
: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
|
||||
actions = serializer.as_json[:actions_summary]
|
||||
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
|
||||
|
||||
before do
|
||||
@ -39,7 +39,7 @@ describe PostSerializer do
|
||||
it "can't flag your own post to notify yourself" do
|
||||
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] }
|
||||
expect(notify_user_action[:can_act]).to eq(false)
|
||||
expect(notify_user_action).to be_blank
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -15,7 +15,7 @@ function setup(store) {
|
||||
});
|
||||
|
||||
this.on('toggleLike', function() {
|
||||
post.toggleProperty('actionByName.like.acted');
|
||||
post.toggleProperty('likeAction.acted');
|
||||
});
|
||||
|
||||
this.set('post', post);
|
||||
|
Loading…
Reference in New Issue
Block a user