Skip to content

Commit

Permalink
FEATURE: update likes and flags live.
Browse files Browse the repository at this point in the history
  • Loading branch information
SamSaffron committed Mar 24, 2014
1 parent 090f5c9 commit 1cd32ce
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
Expand Up @@ -361,7 +361,9 @@ Discourse.TopicController = Discourse.ObjectController.extend(Discourse.Selected
}

var postStream = topicController.get('postStream');
if (data.type === "revised"){
if (data.type === "revised" || data.type === "acted"){
// TODO we could update less data for "acted"
// (only post actions)
postStream.triggerChangedPost(data.id, data.updated_at);
return;
}
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/posts_controller.rb
Expand Up @@ -212,6 +212,10 @@ def find_post_revision_from_params
def render_post_json(post)
post_serializer = PostSerializer.new(post, scope: guardian, root: false)
post_serializer.add_raw = true
counts = PostAction.counts_for([post], current_user)
if counts && counts = counts[post.id]
post_serializer.post_actions = counts
end
render_json_dump(post_serializer)
end

Expand Down
14 changes: 14 additions & 0 deletions app/models/post_action.rb
Expand Up @@ -20,6 +20,7 @@ class AlreadyActed < StandardError; end

after_save :update_counters
after_save :enforce_rules
after_save :notify_subscribers

def self.update_flagged_posts_count
posts_flagged_count = PostAction.joins(post: :topic)
Expand Down Expand Up @@ -136,6 +137,7 @@ def self.act(user, post, post_action_type_id, opts={})
staff_took_action: opts[:take_action] || false,
related_post_id: related_post_id,
targets_topic: !!targets_topic )

rescue ActiveRecord::RecordNotUnique
# can happen despite being .create
# since already bookmarked
Expand Down Expand Up @@ -259,6 +261,18 @@ def enforce_rules
SpamRulesEnforcer.enforce!(post.user) if post_action_type_key == :spam
end

def notify_subscribers
if (is_like? || is_flag?) && post
MessageBus.publish("/topic/#{post.topic_id}",{
id: post.id,
post_number: post.post_number,
type: "acted"
},
group_ids: post.topic.secure_group_ids
)
end
end

def self.auto_hide_if_needed(post, post_action_type)
return if post.hidden

Expand Down

0 comments on commit 1cd32ce

Please sign in to comment.