FEATURE: New API for tag extension

PERF: stop querying category custom fields when rendering home page
FIX: listing votes instead of supervotes
This commit is contained in:
Sam 2017-03-01 13:15:22 -05:00
parent 30eeac1671
commit 20be727c7f
3 changed files with 68 additions and 29 deletions

View File

@ -1,10 +1,39 @@
import property from 'ember-addons/ember-computed-decorators';
import Category from 'discourse/models/category';
import { withPluginApi } from 'discourse/lib/plugin-api';
function initialize(api) {
api.addTagsHtmlCallback((topic) => {
if (!topic.can_vote) { return; }
var buffer = [];
buffer.push("<span class='list-vote-count-new discourse-tag'>");
if (topic.user_super_voted) {
buffer.push("<i class='fa fa-star'></i>");
} else if (topic.user_voted) {
buffer.push("<i class='fa fa-star-o'></i>");
}
buffer.push(I18n.t('feature_voting.vote.multiple'));
buffer.push(`: <span class='vote-count-new'>${topic.vote_count}</span>`);
buffer.push("</span>");
if (buffer.length > 0){
return buffer.join("");
}
}, {priority: -100});
}
export default {
name: 'extend-category-for-voting',
before: 'inject-discourse-objects',
initialize() {
initialize(container) {
withPluginApi('0.8.3', api => {
initialize(api, container);
});
Category.reopen({
@ -22,4 +51,4 @@ export default {
});
}
};
};

View File

@ -88,10 +88,15 @@
.list-vote-count{
font-size: 0.75em;
}
.list-vote-count-new {
.fa {
margin-right: 2px;
}
}
.user-voted-topics{
height: 34px;
line-height: 34px;
a {
padding: 8px 13px;
color: #7a7a7a;
@ -139,4 +144,4 @@
border-bottom: 1px solid #e9e9e9;
padding-bottom: 10px;
margin-bottom: 5px;
}
}

View File

@ -72,7 +72,7 @@ after_initialize do
end
def who_super_voted
users = User.find(UserCustomField.where(name: "votes", value: object.topic.id).pluck(:user_id))
users = User.find(UserCustomField.where(name: "super_votes", value: object.topic.id).pluck(:user_id))
ActiveModel::ArraySerializer.new(users, scope: scope, each_serializer: UserSerializer)
end
end
@ -89,31 +89,36 @@ after_initialize do
}
class ::Category
def self.reset_voting_cache
@allowed_voting_cache["allowed"] =
begin
Set.new(
CategoryCustomField
.where(name: "enable_topic_voting", value: "true")
.pluck(:category_id)
)
end
end
@allowed_voting_cache = DistributedCache.new("allowed_voting")
def self.can_vote?(category_id)
unless set = @allowed_voting_cache["allowed"]
set = reset_voting_cache
end
set.include?(category_id)
end
after_save :reset_voting_cache
protected
def reset_voting_cache
::Guardian.reset_voting_cache
::Category.reset_voting_cache
end
end
class ::Guardian
@@allowed_voting_cache = DistributedCache.new("allowed_voting")
def self.reset_voting_cache
@@allowed_voting_cache["allowed"] =
begin
Set.new(
CategoryCustomField
.where(name: "enable_topic_voting", value: "true")
.pluck(:category_id)
)
end
end
end
require_dependency 'user'
class ::User
def vote_count
@ -203,13 +208,13 @@ after_initialize do
object.super_vote_count
end
end
end
require_dependency 'topic'
class ::Topic
def can_vote
self.category.respond_to?(:custom_fields) and SiteSetting.feature_voting_enabled and self.category.custom_fields["enable_topic_voting"].eql?("true")
SiteSetting.feature_voting_enabled and Category.can_vote?(category_id)
end
def vote_count
@ -275,8 +280,8 @@ after_initialize do
end
def list_votes
topics = create_list(:votes, {order: "votes"})
end
create_list(:votes, {order: "votes"})
end
end
require_dependency "jobs/base"
@ -284,7 +289,7 @@ after_initialize do
class VoteRelease < Jobs::Base
def execute(args)
if topic = Topic.find_by(id: args[:topic_id])
if Topic.find_by(id: args[:topic_id])
UserCustomField.where(name: "votes", value: args[:topic_id]).find_each do |user_field|
user = User.find(user_field.user_id)
user.custom_fields["votes"] = user.votes.dup - [args[:topic_id].to_s]
@ -297,7 +302,7 @@ after_initialize do
class VoteReclaim < Jobs::Base
def execute(args)
if topic = Topic.find_by(id: args[:topic_id])
if Topic.find_by(id: args[:topic_id])
UserCustomField.where(name: "votes_archive", value: args[:topic_id]).find_each do |user_field|
user = User.find(user_field.user_id)
user.custom_fields["votes"] = user.votes.dup.push(args[:topic_id])