Added super vote counts and who super voted

This commit is contained in:
Joe Buhlig 2016-04-25 17:07:51 -05:00
parent ea04eb678a
commit f12cef632f
5 changed files with 133 additions and 18 deletions

View File

@ -13,7 +13,14 @@ module DiscourseFeatureVoting
user.custom_fields["votes"] = user.votes.dup.push(params["topic_id"])
user.save
obj = {vote_limit: user.vote_limit, super_vote_limit: user.super_vote_limit, vote_count: topic.custom_fields["vote_count"].to_i}
obj = {
vote_limit: user.vote_limit,
super_vote_limit: user.super_vote_limit,
vote_count: topic.custom_fields["vote_count"].to_i,
super_vote_count: topic.super_vote_count,
who_voted: who_voted(topic),
who_super_voted: who_super_voted(topic)
}
render json: obj
end
@ -31,7 +38,14 @@ module DiscourseFeatureVoting
user.custom_fields["super_votes"] = user.super_votes.dup - [params["topic_id"].to_s]
user.save
obj = {vote_limit: user.vote_limit, super_vote_limit: user.super_vote_limit, vote_count: topic.custom_fields["vote_count"].to_i}
obj = {
vote_limit: user.vote_limit,
super_vote_limit: user.super_vote_limit,
vote_count: topic.custom_fields["vote_count"].to_i,
super_vote_count: topic.super_vote_count,
who_voted: who_voted(topic),
who_super_voted: who_super_voted(topic)
}
render json: obj
end
@ -43,7 +57,14 @@ module DiscourseFeatureVoting
user.custom_fields["super_votes"] = user.super_votes.dup.push(params["topic_id"])
user.save
obj = {vote_limit: user.vote_limit, super_vote_limit: user.super_vote_limit, vote_count: topic.custom_fields["vote_count"].to_i}
obj = {
vote_limit: user.vote_limit,
super_vote_limit: user.super_vote_limit,
vote_count: topic.custom_fields["vote_count"].to_i,
super_vote_count: topic.super_vote_count,
who_voted: who_voted(topic),
who_super_voted: who_super_voted(topic)
}
render json: obj
end
@ -55,9 +76,32 @@ module DiscourseFeatureVoting
user.custom_fields["super_votes"] = user.super_votes.dup - [params["topic_id"].to_s]
user.save
obj = {vote_limit: user.vote_limit, super_vote_limit: user.super_vote_limit, vote_count: topic.custom_fields["vote_count"].to_i}
obj = {
vote_limit: user.vote_limit,
super_vote_limit: user.super_vote_limit,
vote_count: topic.custom_fields["vote_count"].to_i,
super_vote_count: topic.super_vote_count,
who_voted: who_voted(topic),
who_super_voted: who_super_voted(topic)
}
render json: obj
end
def who_voted(topic)
users = []
User.where(id: topic.who_voted).each do |user|
users.push(UserSerializer.new(user, scope: guardian, root: 'user'))
end
return users
end
def who_super_voted(topic)
users = []
User.where(id: topic.who_voted).each do |user|
users.push(UserSerializer.new(user, scope: guardian, root: 'user'))
end
return users
end
end
end

View File

@ -9,7 +9,7 @@ export default createWidget('vote-box', {
},
defaultState() {
return { whoVotedUsers: [], allowClick: true, initialVote: false };
return { allowClick: true, initialVote: false };
},
html(attrs, state){
@ -32,6 +32,7 @@ export default createWidget('vote-box', {
topic.set('vote_count', result.vote_count);
topic.set('user_voted', true);
Discourse.User.current().set('vote_limit', result.vote_limit);
topic.set('who_voted', result.who_voted);
state.allowClick = true;
}).catch(function(error) {
console.log(error);
@ -49,10 +50,16 @@ export default createWidget('vote-box', {
}
}).then(function(result) {
topic.set('vote_count', result.vote_count);
topic.set('super_vote_count', result.super_vote_count);
if (result.super_vote_count == 0){
topic.set('has_super_votes', false);
}
topic.set('user_voted', false);
topic.set('user_super_voted', false);
Discourse.User.current().set('vote_limit', result.vote_limit);
Discourse.User.current().set('super_vote_limit', result.super_vote_limit);
topic.set('who_voted', result.who_voted);
topic.set('who_super_voted', result.who_super_voted);
state.allowClick = true;
}).catch(function(error) {
console.log(error);
@ -70,8 +77,11 @@ export default createWidget('vote-box', {
}
}).then(function(result) {
topic.set('vote_count', result.vote_count);
topic.set('super_vote_count', result.super_vote_count);
topic.set('has_super_votes', true);
topic.set('user_super_voted', true);
Discourse.User.current().set('super_vote_limit', result.super_vote_limit);
topic.set('who_super_voted', result.who_super_voted);
state.allowClick = true;
}).catch(function(error) {
console.log(error);
@ -89,8 +99,13 @@ export default createWidget('vote-box', {
}
}).then(function(result) {
topic.set('vote_count', result.vote_count);
topic.set('super_vote_count', result.super_vote_count);
if (result.super_vote_count == 0){
topic.set('has_super_votes', false);
}
topic.set('user_super_voted', false);
Discourse.User.current().set('super_vote_limit', result.super_vote_limit);
topic.set('who_super_voted', result.who_super_voted);
state.allowClick = true;
}).catch(function(error) {
console.log(error);

View File

@ -5,14 +5,19 @@ export default createWidget('vote-count', {
tagName: 'div.vote-count-wrapper',
buildClasses(attrs, state) {
if (!attrs.has_votes){
return "no-votes";
}
},
defaultState() {
return { voteCount: 0, whoVotedUsers: [] };
return { voteCount: 0, whoVotedUsers: [], whoSuperVotedUsers: [] };
},
html(attrs, state){
if (!attrs.has_votes){
return
}
var voteCount = h('div.vote-count', attrs.vote_count.toString());
if (attrs.single_vote){
var voteDescription = I18n.t('feature_voting.vote.one');
@ -22,19 +27,43 @@ export default createWidget('vote-count', {
}
var voteLabel = h('div.vote-label', voteDescription);
var whoVoted = [];
if (Discourse.SiteSettings.feature_voting_show_who_voted) {
if (Discourse.SiteSettings.feature_voting_show_who_voted && attrs.has_votes) {
whoVoted = this.attach('small-user-list', {
users: state.whoVotedUsers,
users: this.state.whoVotedUsers,
addSelf: attrs.liked,
listClassName: 'who-voted popup-menu voting-popup-menu hidden',
listClassName: 'regular-votes',
description: 'feature_voting.who_voted'
})
}
return [voteCount, voteLabel, whoVoted];
if (attrs.single_super_vote){
var superVoteDescription = I18n.t('feature_voting.vote.one');
}
else {
var superVoteDescription = I18n.t('feature_voting.vote.multiple');
}
var superVoteCount = [];
var whoSuperVoted = [];
if (Discourse.SiteSettings.feature_voting_show_who_voted && attrs.has_super_votes) {
if (attrs.single_super_vote){
var superVoteDescription = I18n.t('feature_voting.vote.one');
}
else {
var superVoteDescription = I18n.t('feature_voting.vote.multiple');
}
superVoteCount = h('div.super-vote-count', [attrs.super_vote_count.toString(), superVoteDescription]);
var whoSuperVoted = [];
whoSuperVoted = this.attach('small-user-list', {
users: this.state.whoSuperVotedUsers,
addSelf: attrs.liked,
listClassName: 'super-votes',
description: 'feature_voting.who_super_voted'
})
}
return [voteCount, voteLabel, h('div.who-voted.popup-menu.voting-popup-menu.hidden', [superVoteCount, whoSuperVoted, whoVoted])];
},
click(attrs){
if (Discourse.SiteSettings.feature_voting_show_who_voted) {
click(){
if (Discourse.SiteSettings.feature_voting_show_who_voted && this.attrs.has_votes) {
this.getWhoVoted();
$(".who-voted").toggle();
}
@ -45,13 +74,20 @@ export default createWidget('vote-count', {
},
getWhoVoted() {
const { attrs, state } = this;
var users = attrs.who_voted;
var users = this.attrs.who_voted;
var superUsers = this.attrs.who_super_voted;
if (users.length){
return state.whoVotedUsers = users.map(whoVotedAvatars);
this.state.whoVotedUsers = users.map(whoVotedAvatars);
}
else{
return state.whoVotedUsers = [];
this.state.whoVotedUsers = [];
}
if (superUsers.length){
this.state.whoSuperVotedUsers = superUsers.map(whoVotedAvatars);
}
else{
this.state.whoSuperVotedUsers = [];
}
},
});

View File

@ -10,6 +10,10 @@ en:
vote:
one: "vote"
multiple: "votes"
super_vote:
one: "super vote"
multiple: "super votes"
who_voted: "voted for this"
who_super_voted: "super voted for this"
upgrade_question: "Would you like to make that a Super Vote?"
upgrade_answer: "Yes!"

View File

@ -16,7 +16,7 @@ after_initialize do
require_dependency 'topic_view_serializer'
class ::TopicViewSerializer
attributes :can_vote, :single_vote, :vote_count, :has_votes, :user_voted, :user_super_voted, :who_voted, :who_super_voted
attributes :can_vote, :single_vote, :vote_count, :has_votes, :super_vote_count, :has_super_votes, :user_voted, :user_super_voted, :who_voted, :who_super_voted
def can_vote
object.topic.can_vote
@ -26,6 +26,10 @@ after_initialize do
object.topic.vote_count.to_i == 1
end
def single_super_vote
object.topic.super_vote_count.to_i == 1
end
def vote_count
object.topic.vote_count
end
@ -34,6 +38,14 @@ after_initialize do
object.topic.vote_count.to_i > 0
end
def super_vote_count
object.topic.super_vote_count
end
def has_super_votes
object.topic.super_vote_count.to_i > 0
end
def user_voted
object.topic.user_voted(scope.user.id)
end
@ -187,6 +199,10 @@ after_initialize do
end
end
def super_vote_count
UserCustomField.where(name: "super_votes", value: self.id).count
end
def who_voted
UserCustomField.where(name: "votes", value: self.id).pluck(:user_id)
end