Commit 3f001cad60ab8127ba7321585a32d080326631c9
1 parent
004dd02a
Exists in
master
and in
1 other branch
Add points endpoint
Showing
2 changed files
with
96 additions
and
76 deletions
Show diff stats
lib/gamification_plugin/api.rb
| @@ -6,11 +6,15 @@ class GamificationPlugin::API < Grape::API | @@ -6,11 +6,15 @@ class GamificationPlugin::API < Grape::API | ||
| 6 | environment.gamification_plugin_badges.group(:name).count | 6 | environment.gamification_plugin_badges.group(:name).count |
| 7 | end | 7 | end |
| 8 | 8 | ||
| 9 | - resource :my do | 9 | + resource :my do |
| 10 | get 'badges' do | 10 | get 'badges' do |
| 11 | authenticate! | 11 | authenticate! |
| 12 | present current_person.badges, :with => Noosfero::API::Entities::Badge | 12 | present current_person.badges, :with => Noosfero::API::Entities::Badge |
| 13 | end | 13 | end |
| 14 | + get 'points' do | ||
| 15 | + authenticate! | ||
| 16 | + {points: current_person.points} | ||
| 17 | + end | ||
| 14 | 18 | ||
| 15 | get 'level' do | 19 | get 'level' do |
| 16 | authenticate! | 20 | authenticate! |
| @@ -25,6 +29,12 @@ class GamificationPlugin::API < Grape::API | @@ -25,6 +29,12 @@ class GamificationPlugin::API < Grape::API | ||
| 25 | present person.badges | 29 | present person.badges |
| 26 | end | 30 | end |
| 27 | 31 | ||
| 32 | + get ':id/points' do | ||
| 33 | + person = environment.people.visible_for_person(current_person).find_by_id(params[:id]) | ||
| 34 | + return not_found! if person.blank? | ||
| 35 | + {:points => person.points} | ||
| 36 | + end | ||
| 37 | + | ||
| 28 | get ':id/level' do | 38 | get ':id/level' do |
| 29 | person = environment.people.visible_for_person(current_person).find_by_id(params[:id]) | 39 | person = environment.people.visible_for_person(current_person).find_by_id(params[:id]) |
| 30 | return not_found! if person.blank? | 40 | return not_found! if person.blank? |
lib/merit/point_rules.rb
| @@ -3,90 +3,100 @@ module Merit | @@ -3,90 +3,100 @@ module Merit | ||
| 3 | include Merit::PointRulesMethods | 3 | include Merit::PointRulesMethods |
| 4 | 4 | ||
| 5 | AVAILABLE_RULES = { | 5 | AVAILABLE_RULES = { |
| 6 | - :comment_author => { | ||
| 7 | - :action => 'comment#create', | ||
| 8 | - :undo_action => 'comment#destroy', | ||
| 9 | - :to => :author, | ||
| 10 | - :value => 1, | ||
| 11 | - :description => _('Comment author'), | ||
| 12 | - :default_weight => 150 | 6 | + comment_author: { |
| 7 | + action: 'comment#create', | ||
| 8 | + undo_action: 'comment#destroy', | ||
| 9 | + to: :author, | ||
| 10 | + value: 1, | ||
| 11 | + description: _('Comment author'), | ||
| 12 | + default_weight: 150 | ||
| 13 | }, | 13 | }, |
| 14 | - :comment_article_author => { | ||
| 15 | - :action => 'comment#create', | ||
| 16 | - :undo_action => 'comment#destroy', | ||
| 17 | - :to => lambda {|comment| comment.source.author}, | ||
| 18 | - :value => 1, | ||
| 19 | - :description => _('Article author of a comment'), | ||
| 20 | - :default_weight => 50 | 14 | + comment_article_author: { |
| 15 | + action: 'comment#create', | ||
| 16 | + undo_action: 'comment#destroy', | ||
| 17 | + to: lambda {|comment| comment.source.author}, | ||
| 18 | + value: 1, | ||
| 19 | + description: _('Article author of a comment'), | ||
| 20 | + default_weight: 50 | ||
| 21 | }, | 21 | }, |
| 22 | - :comment_article => { | ||
| 23 | - :action => 'comment#create', | ||
| 24 | - :undo_action => 'comment#destroy', | ||
| 25 | - :to => lambda {|comment| comment.source}, | ||
| 26 | - :value => 1, | ||
| 27 | - :description => _('Source article of a comment'), | ||
| 28 | - :default_weight => 50 | 22 | + comment_article: { |
| 23 | + action: 'comment#create', | ||
| 24 | + undo_action: 'comment#destroy', | ||
| 25 | + to: lambda {|comment| comment.source}, | ||
| 26 | + value: 1, | ||
| 27 | + description: _('Source article of a comment'), | ||
| 28 | + default_weight: 50 | ||
| 29 | }, | 29 | }, |
| 30 | - :comment_community => { | ||
| 31 | - :action => 'comment#create', | ||
| 32 | - :undo_action => 'comment#destroy', | ||
| 33 | - :to => lambda {|comment| comment.profile}, | ||
| 34 | - :value => 1, | ||
| 35 | - :description => _('Article community of a comment'), | ||
| 36 | - :default_weight => 50, | ||
| 37 | - :condition => lambda {|target| target.profile.community? } | 30 | + comment_community: { |
| 31 | + action: 'comment#create', | ||
| 32 | + undo_action: 'comment#destroy', | ||
| 33 | + to: lambda {|comment| comment.profile}, | ||
| 34 | + value: 1, | ||
| 35 | + description: _('Article community of a comment'), | ||
| 36 | + default_weight: 50, | ||
| 37 | + condition: lambda {|target| target.profile.community? } | ||
| 38 | }, | 38 | }, |
| 39 | - :article_author => { | ||
| 40 | - :action => 'article#create', | ||
| 41 | - :undo_action => 'article#destroy', | ||
| 42 | - :to => :author, | ||
| 43 | - :value => 1, | ||
| 44 | - :description => _('Article author'), | ||
| 45 | - :default_weight => 500 | 39 | + article_author: { |
| 40 | + action: 'article#create', | ||
| 41 | + undo_action: 'article#destroy', | ||
| 42 | + to: :author, | ||
| 43 | + value: 1, | ||
| 44 | + description: _('Article author'), | ||
| 45 | + default_weight: 500 | ||
| 46 | }, | 46 | }, |
| 47 | - :article_community => { | ||
| 48 | - :action => 'article#create', | ||
| 49 | - :undo_action => 'article#destroy', | ||
| 50 | - :to => :profile, | ||
| 51 | - :value => 1, | ||
| 52 | - :description => _('Article community'), | ||
| 53 | - :default_weight => 600, | ||
| 54 | - :condition => lambda {|target| target.profile.community? } | 47 | + article_community: { |
| 48 | + action: 'article#create', | ||
| 49 | + undo_action: 'article#destroy', | ||
| 50 | + to: :profile, | ||
| 51 | + value: 1, | ||
| 52 | + description: _('Article community'), | ||
| 53 | + default_weight: 600, | ||
| 54 | + condition: lambda {|target| target.profile.community? } | ||
| 55 | }, | 55 | }, |
| 56 | - :vote_voteable_author => { | ||
| 57 | - :action => 'vote#create', | ||
| 58 | - :undo_action => 'vote#destroy', | ||
| 59 | - :to => lambda {|vote| vote.voteable.author}, | ||
| 60 | - :profile => lambda {|vote| vote.voteable.profile}, | ||
| 61 | - :value => lambda {|vote| vote.vote}, | ||
| 62 | - :description => _('Author of a voted content'), | ||
| 63 | - :default_weight => 50, | 56 | + vote_voteable_author: { |
| 57 | + action: 'vote#create', | ||
| 58 | + undo_action: 'vote#destroy', | ||
| 59 | + to: lambda {|vote| vote.voteable.author}, | ||
| 60 | + profile: lambda {|vote| vote.voteable.profile}, | ||
| 61 | + value: lambda {|vote| vote.vote}, | ||
| 62 | + description: _('Author of a voted content'), | ||
| 63 | + default_weight: 50, | ||
| 64 | }, | 64 | }, |
| 65 | - :vote_voteable => { | ||
| 66 | - :action => 'vote#create', | ||
| 67 | - :undo_action => 'vote#destroy', | ||
| 68 | - :to => lambda {|vote| vote.voteable}, | ||
| 69 | - :profile => lambda {|vote| vote.voteable.profile}, | ||
| 70 | - :value => lambda {|vote| vote.vote}, | ||
| 71 | - :description => _('Voted content'), | ||
| 72 | - :default_weight => 50 | 65 | + vote_voteable: { |
| 66 | + action: 'vote#create', | ||
| 67 | + undo_action: 'vote#destroy', | ||
| 68 | + to: lambda {|vote| vote.voteable}, | ||
| 69 | + profile: lambda {|vote| vote.voteable.profile}, | ||
| 70 | + value: lambda {|vote| vote.vote}, | ||
| 71 | + description: _('Voted content'), | ||
| 72 | + default_weight: 50 | ||
| 73 | }, | 73 | }, |
| 74 | - :vote_voter => { | ||
| 75 | - :action => 'vote#create', | ||
| 76 | - :undo_action => 'vote#destroy', | ||
| 77 | - :to => lambda {|vote| vote.voter}, | ||
| 78 | - :value => lambda {|vote| 1}, | ||
| 79 | - :description => _('Voter'), | ||
| 80 | - :default_weight => 10 | 74 | + vote_voter: { |
| 75 | + action: 'vote#create', | ||
| 76 | + undo_action: 'vote#destroy', | ||
| 77 | + to: lambda {|vote| vote.voter}, | ||
| 78 | + value: lambda {|vote| 1}, | ||
| 79 | + description: _('Voter'), | ||
| 80 | + default_weight: 10 | ||
| 81 | }, | 81 | }, |
| 82 | - :friends => { | ||
| 83 | - :action => 'friendship#create', | ||
| 84 | - :undo_action => 'friendship#destroy', | ||
| 85 | - :to => lambda {|friendship| friendship.person}, | ||
| 86 | - :value => 1, | ||
| 87 | - :description => _('Friends'), | ||
| 88 | - :default_weight => 5 | 82 | + friends: { |
| 83 | + action: 'friendship#create', | ||
| 84 | + undo_action: 'friendship#destroy', | ||
| 85 | + to: lambda {|friendship| friendship.person}, | ||
| 86 | + value: 1, | ||
| 87 | + description: _('Friends'), | ||
| 88 | + default_weight: 5 | ||
| 89 | }, | 89 | }, |
| 90 | + profile_completion: { | ||
| 91 | + action: ['account#create', 'account#update'], | ||
| 92 | + undo_action: 'account#destroy', | ||
| 93 | + to: lambda {|user| user.person}, | ||
| 94 | + value: 1, | ||
| 95 | + description: _('Profile Completion'), | ||
| 96 | + default_weight: 5, | ||
| 97 | + model_name: "User", | ||
| 98 | + condition: lambda {|user| user.person.profile_completion_score_condition } | ||
| 99 | + } | ||
| 90 | } | 100 | } |
| 91 | 101 | ||
| 92 | def weight(category) | 102 | def weight(category) |
| @@ -110,7 +120,7 @@ module Merit | @@ -110,7 +120,7 @@ module Merit | ||
| 110 | 120 | ||
| 111 | AVAILABLE_RULES.each do |category, setting| | 121 | AVAILABLE_RULES.each do |category, setting| |
| 112 | [setting[:action], setting[:undo_action]].compact.zip([1, -1]).each do |action, signal| | 122 | [setting[:action], setting[:undo_action]].compact.zip([1, -1]).each do |action, signal| |
| 113 | - score lambda {|target| signal * calculate_score(target, category, setting[:value])}, :on => action, :to => setting[:to], :category => category do |target| | 123 | + score lambda {|target| signal * calculate_score(target, category, setting[:value])}, on: action, to: setting[:to], category: category do |target| |
| 114 | condition(setting, target) | 124 | condition(setting, target) |
| 115 | end | 125 | end |
| 116 | end | 126 | end |