diff --git a/assets/javascripts/discourse/initializers/extend-for-solved-button.js.es6 b/assets/javascripts/discourse/initializers/extend-for-solved-button.js.es6 index da80c10..4186d16 100644 --- a/assets/javascripts/discourse/initializers/extend-for-solved-button.js.es6 +++ b/assets/javascripts/discourse/initializers/extend-for-solved-button.js.es6 @@ -3,6 +3,7 @@ import PostView from 'discourse/views/post'; import { Button } from 'discourse/views/post-menu'; import Topic from 'discourse/models/topic'; import User from 'discourse/models/user'; +import TopicStatus from 'discourse/views/topic-status' export default { name: 'extend-for-solved-button', @@ -39,6 +40,21 @@ export default { }.property('accepted_answer', 'id') }); + TopicStatus.reopen({ + statuses: function(){ + var results = this._super(); + if (this.topic.has_accepted_answer) { + results.push({ + openTag: 'span', + closeTag: 'span', + title: I18n.t('solved.has_accepted_answer'), + icon: 'check-square' + }); + } + return results; + }.property() + }); + PostView.reopen({ classNameBindings: ['post.accepted_answer:accepted-answer'] }); diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index d8afdaf..7ad46fa 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -3,6 +3,7 @@ en: solved: allow_accepted_answers: "Allow users to accept answers" accept_answer: "Accept answer" + has_accepted_answer: "This topic has an accepted answer" unaccept_answer: "Unaccept answer" accepted_answer: "Accepted answer" accepted_html: " Solved by {{username}} in post #{{post_number}}" diff --git a/plugin.rb b/plugin.rb index 6a8adf2..33ae297 100644 --- a/plugin.rb +++ b/plugin.rb @@ -173,6 +173,48 @@ after_initialize do def accepted_answer post_custom_fields["is_accepted_answer"] end - end + + require_dependency 'topic_list_item_serializer' + + class ::TopicListItemSerializer + attributes :has_accepted_answer + + def include_has_accepted_answer? + object.has_accepted_answer + end + + def has_accepted_answer + true + end + end + + class ::Topic + attr_accessor :has_accepted_answer + end + + module ::DiscourseSolved::ExtendTopics + def load_topics + topics = super + if topics.present? + # super efficient for front page + with_accepted = Set.new(Topic.exec_sql( + 'SELECT topic_id FROM topic_custom_fields + WHERE topic_id in (:topic_ids) AND + value IS NOT NULL AND + name = \'accepted_answer_post_id\'', + topic_ids: topics.map(&:id) + ).values.flatten.map(&:to_i)) + topics.each do |topic| + topic.has_accepted_answer = true if with_accepted.include? topic.id + end + end + topics + end + end + + class ::TopicList + prepend ::DiscourseSolved::ExtendTopics + end + end