diff --git a/app/assets/javascripts/discourse/components/topic-list.js.es6 b/app/assets/javascripts/discourse/components/topic-list.js.es6 index 2ea6befbb..11dd9e422 100644 --- a/app/assets/javascripts/discourse/components/topic-list.js.es6 +++ b/app/assets/javascripts/discourse/components/topic-list.js.es6 @@ -15,6 +15,10 @@ export default Ember.Component.extend({ return this.get('order') === "likes"; }.property(), + showOpLikes: function(){ + return this.get('order') === "op_likes"; + }.property(), + click: function(e){ var self = this; var on = function(sel, callback){ diff --git a/app/assets/javascripts/discourse/templates/components/topic-list-header.raw.hbs b/app/assets/javascripts/discourse/templates/components/topic-list-header.raw.hbs index 5111bb48c..b26ae0305 100644 --- a/app/assets/javascripts/discourse/templates/components/topic-list-header.raw.hbs +++ b/app/assets/javascripts/discourse/templates/components/topic-list-header.raw.hbs @@ -17,5 +17,8 @@ {{#if showLikes}} {{raw "components/topic-list-header-column" sortable=sortable number='true' order='likes' name='likes'}} {{/if}} +{{#if showOpLikes}} + {{raw "components/topic-list-header-column" sortable=sortable number='true' order='op_likes' name='likes'}} +{{/if}} {{raw "components/topic-list-header-column" sortable=sortable number='true' order='views' name='views'}} {{raw "components/topic-list-header-column" sortable=sortable number='true' order='activity' name='activity'}} diff --git a/app/assets/javascripts/discourse/templates/components/topic-list.hbs b/app/assets/javascripts/discourse/templates/components/topic-list.hbs index 5232c50c5..357b14c07 100644 --- a/app/assets/javascripts/discourse/templates/components/topic-list.hbs +++ b/app/assets/javascripts/discourse/templates/components/topic-list.hbs @@ -7,6 +7,7 @@ hideCategory=hideCategory showPosters=showPosters showLikes=showLikes + showOpLikes=showOpLikes showParticipants=showParticipants order=order ascending=ascending diff --git a/app/assets/javascripts/discourse/templates/list/topic_list_item.raw.hbs b/app/assets/javascripts/discourse/templates/list/topic_list_item.raw.hbs index 2a404e077..83eedde3a 100644 --- a/app/assets/javascripts/discourse/templates/list/topic_list_item.raw.hbs +++ b/app/assets/javascripts/discourse/templates/list/topic_list_item.raw.hbs @@ -38,6 +38,15 @@ {{/if}} {{/if}} +{{#if controller.showOpLikes}} +<td class="num likes"> + {{#if hasOpLikes}} + <a href='{{topic.summaryUrl}}'> + {{number topic.op_like_count}} <i class='fa fa-heart'></i></td> + </a> + {{/if}} +{{/if}} + <td class="num views {{topic.viewsHeat}}">{{number topic.views numberKey="views_long"}}</td> {{raw "list/activity-column" topic=topic class="num" tagName="td"}} diff --git a/app/assets/javascripts/discourse/views/topic-list-item.js.es6 b/app/assets/javascripts/discourse/views/topic-list-item.js.es6 index d1ea9fa80..941913264 100644 --- a/app/assets/javascripts/discourse/views/topic-list-item.js.es6 +++ b/app/assets/javascripts/discourse/views/topic-list-item.js.es6 @@ -18,6 +18,10 @@ export default Discourse.View.extend(StringBuffer, { return this.get('topic.like_count') > 0; }, + hasOpLikes: function(){ + return this.get('topic.op_like_count') > 0; + }, + click: function(e){ var target = $(e.target); diff --git a/app/models/topic.rb b/app/models/topic.rb index b1a49b225..2eab5fe2d 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -101,6 +101,8 @@ class Topic < ActiveRecord::Base has_one :warning + has_one :first_post, -> {where post_number: 1}, class_name: Post + # When we want to temporarily attach some data to a forum topic (usually before serialization) attr_accessor :user_data attr_accessor :posters # TODO: can replace with posters_summary once we remove old list code diff --git a/app/serializers/topic_list_item_serializer.rb b/app/serializers/topic_list_item_serializer.rb index 7ff2d8ead..e7c850317 100644 --- a/app/serializers/topic_list_item_serializer.rb +++ b/app/serializers/topic_list_item_serializer.rb @@ -6,7 +6,8 @@ class TopicListItemSerializer < ListableTopicSerializer :has_summary, :archetype, :last_poster_username, - :category_id + :category_id, + :op_like_count has_many :posters, serializer: TopicPosterSerializer, embed: :objects has_many :participants, serializer: TopicPosterSerializer, embed: :objects @@ -21,6 +22,10 @@ class TopicListItemSerializer < ListableTopicSerializer object.posters || [] end + def op_like_count + object.first_post && object.first_post.like_count + end + def last_poster_username posters.find { |poster| poster.user.id == object.last_post_user_id }.try(:user).try(:username) end @@ -33,4 +38,8 @@ class TopicListItemSerializer < ListableTopicSerializer object.private_message? end + def include_op_like_count? + object.association(:first_post).loaded? + end + end diff --git a/lib/topic_query.rb b/lib/topic_query.rb index cbf36f52b..78175bf0c 100644 --- a/lib/topic_query.rb +++ b/lib/topic_query.rb @@ -220,7 +220,7 @@ class TopicQuery end if sort_column == 'op_likes' - return result.order("(SELECT like_count FROM posts p3 WHERE p3.topic_id = topics.id AND p3.post_number = 1) #{sort_dir}") + return result.includes(:first_post).order("(SELECT like_count FROM posts p3 WHERE p3.topic_id = topics.id AND p3.post_number = 1) #{sort_dir}") end result.order("topics.#{sort_column} #{sort_dir}")