diff --git a/plugins/poll/assets/javascripts/initializers/extend-for-poll.js.es6 b/plugins/poll/assets/javascripts/initializers/extend-for-poll.js.es6 index 944b29d48..1153308eb 100644 --- a/plugins/poll/assets/javascripts/initializers/extend-for-poll.js.es6 +++ b/plugins/poll/assets/javascripts/initializers/extend-for-poll.js.es6 @@ -21,6 +21,15 @@ export default { initialize(container) { + const messageBus = container.lookup("message-bus:main"); + + // listen for back-end to tell us when a post has a poll + messageBus.subscribe("/polls", data => { + const post = container.lookup("controller:topic").get("postStream").findLoadedPost(data.post_id); + // HACK to trigger the "postViewUpdated" event + post.set("cooked", post.get("cooked") + " "); + }); + // overwrite polls PostView.reopen({ _createPollViews: function($post) { diff --git a/plugins/poll/plugin.rb b/plugins/poll/plugin.rb index e0b403fe8..7d286aaa0 100644 --- a/plugins/poll/plugin.rb +++ b/plugins/poll/plugin.rb @@ -273,6 +273,12 @@ after_initialize do whitelisted end + # tells the front-end we have a poll for that post + on(:post_created) do |post| + next if post.is_first_post? || post.custom_fields[POLLS_CUSTOM_FIELD].blank? + DiscourseBus.publish("/polls", { post_id: post.id }) + end + add_to_serializer(:post, :polls, false) { post_custom_fields[POLLS_CUSTOM_FIELD] } add_to_serializer(:post, :include_polls?) { post_custom_fields.present? && post_custom_fields[POLLS_CUSTOM_FIELD].present? }