@@ -2,9 +2,9 @@ defmodule GroupherServer.Accounts.Delegate.Achievements do
22 @ moduledoc """
33 user achievements related
44 acheiveements formula:
5- 1. create content been stared by other user + 1
5+ 1. create content been upvoteed by other user + 1
66 2. create content been watched by other user + 1
7- 3. create content been favorited by other user + 2
7+ 3. create content been colleced by other user + 2
88 4. followed by other user + 3
99 """
1010 import Ecto.Query , warn: false
@@ -14,16 +14,16 @@ defmodule GroupherServer.Accounts.Delegate.Achievements do
1414 alias Helper . { ORM , SpecType }
1515 alias GroupherServer.Accounts . { Achievement , User }
1616
17- @ favorite_weight get_config ( :general , :user_achieve_favorite_weight )
18- @ star_weight get_config ( :general , :user_achieve_star_weight )
17+ @ collect_weight get_config ( :general , :user_achieve_collect_weight )
18+ @ upvote_weight get_config ( :general , :user_achieve_upvote_weight )
1919 # @watch_weight get_config(:general, :user_achieve_watch_weight)
2020 @ follow_weight get_config ( :general , :user_achieve_follow_weight )
2121
2222 @ doc """
23- add user's achievement by add followers_count of favorite_weight
23+ inc user's achievement by inc followers_count of collect_weight
2424 """
2525 @ spec achieve ( User . t ( ) , atom , atom ) :: SpecType . done ( )
26- def achieve ( % User { id: user_id } , :add , :follow ) do
26+ def achieve ( % User { id: user_id } , :inc , :follow ) do
2727 with { :ok , achievement } <- ORM . findby_or_insert ( Achievement , ~m( user_id) a , ~m( user_id) a ) do
2828 followers_count = achievement . followers_count + 1
2929 reputation = achievement . reputation + @ follow_weight
@@ -34,9 +34,9 @@ defmodule GroupherServer.Accounts.Delegate.Achievements do
3434 end
3535
3636 @ doc """
37- minus user's achievement by add followers_count of favorite_weight
37+ dec user's achievement by inc followers_count of collect_weight
3838 """
39- def achieve ( % User { id: user_id } , :minus , :follow ) do
39+ def achieve ( % User { id: user_id } , :dec , :follow ) do
4040 with { :ok , achievement } <- ORM . findby_or_insert ( Achievement , ~m( user_id) a , ~m( user_id) a ) do
4141 followers_count = max ( achievement . followers_count - 1 , 0 )
4242 reputation = max ( achievement . reputation - @ follow_weight , 0 )
@@ -47,55 +47,55 @@ defmodule GroupherServer.Accounts.Delegate.Achievements do
4747 end
4848
4949 @ doc """
50- add user's achievement by contents_stared_count of star_weight
50+ inc user's achievement by articles_upvotes_count of upvote_weight
5151 """
52- def achieve ( % User { id: user_id } = _user , :add , :star ) do
52+ def achieve ( % User { id: user_id } = _user , :inc , :upvote ) do
5353 with { :ok , achievement } <- ORM . findby_or_insert ( Achievement , ~m( user_id) a , ~m( user_id) a ) do
54- contents_stared_count = achievement . contents_stared_count + 1
55- reputation = achievement . reputation + @ star_weight
54+ articles_upvotes_count = achievement . articles_upvotes_count + 1
55+ reputation = achievement . reputation + @ upvote_weight
5656
5757 achievement
58- |> ORM . update ( ~m( contents_stared_count reputation) a )
58+ |> ORM . update ( ~m( articles_upvotes_count reputation) a )
5959 end
6060 end
6161
6262 @ doc """
63- minus user's achievement by contents_stared_count of star_weight
63+ dec user's achievement by articles_upvotes_count of upvote_weight
6464 """
65- def achieve ( % User { id: user_id } = _user , :minus , :star ) do
65+ def achieve ( % User { id: user_id } = _user , :dec , :upvote ) do
6666 with { :ok , achievement } <- ORM . findby_or_insert ( Achievement , ~m( user_id) a , ~m( user_id) a ) do
67- contents_stared_count = max ( achievement . contents_stared_count - 1 , 0 )
68- reputation = max ( achievement . reputation - @ star_weight , 0 )
67+ articles_upvotes_count = max ( achievement . articles_upvotes_count - 1 , 0 )
68+ reputation = max ( achievement . reputation - @ upvote_weight , 0 )
6969
7070 achievement
71- |> ORM . update ( ~m( contents_stared_count reputation) a )
71+ |> ORM . update ( ~m( articles_upvotes_count reputation) a )
7272 end
7373 end
7474
7575 @ doc """
76- minus user's achievement by contents_favorited_count of favorite_weight
76+ dec user's achievement by articles_collects_count of collect_weight
7777 """
78- def achieve ( % User { id: user_id } = _user , :add , :favorite ) do
78+ def achieve ( % User { id: user_id } = _user , :inc , :collect ) do
7979 with { :ok , achievement } <- ORM . findby_or_insert ( Achievement , ~m( user_id) a , ~m( user_id) a ) do
80- contents_favorited_count = achievement . contents_favorited_count + 1
81- reputation = achievement . reputation + @ favorite_weight
80+ articles_collects_count = achievement . articles_collects_count + 1
81+ reputation = achievement . reputation + @ collect_weight
8282
8383 achievement
84- |> ORM . update ( ~m( contents_favorited_count reputation) a )
84+ |> ORM . update ( ~m( articles_collects_count reputation) a )
8585 end
8686 end
8787
8888 @ doc """
89- add user's achievement by contents_favorited_count of favorite_weight
89+ inc user's achievement by articles_collects_count of collect_weight
9090 """
91- def achieve ( % User { id: user_id } = _user , :minus , :favorite ) do
91+ def achieve ( % User { id: user_id } = _user , :dec , :collect ) do
9292 with { :ok , achievement } <- ORM . findby_or_insert ( Achievement , ~m( user_id) a , ~m( user_id) a ) do
93- contents_favorited_count = max ( achievement . contents_favorited_count - 1 , 0 )
93+ articles_collects_count = max ( achievement . articles_collects_count - 1 , 0 )
9494
95- reputation = max ( achievement . reputation - @ favorite_weight , 0 )
95+ reputation = max ( achievement . reputation - @ collect_weight , 0 )
9696
9797 achievement
98- |> ORM . update ( ~m( contents_favorited_count reputation) a )
98+ |> ORM . update ( ~m( articles_collects_count reputation) a )
9999 end
100100 end
101101
@@ -113,27 +113,15 @@ defmodule GroupherServer.Accounts.Delegate.Achievements do
113113 @ doc """
114114 only used for user delete the farorited category, other case is auto
115115 """
116- def downgrade_achievement ( % User { id: user_id } , :favorite , count ) do
116+ def downgrade_achievement ( % User { id: user_id } , :collect , count ) do
117117 with { :ok , achievement } <- ORM . find_by ( Achievement , user_id: user_id ) do
118- contents_favorited_count = max ( achievement . contents_favorited_count - count , 0 )
119- reputation = max ( achievement . reputation - count * @ favorite_weight , 0 )
118+ articles_collects_count = max ( achievement . articles_collects_count - count , 0 )
119+ reputation = max ( achievement . reputation - count * @ collect_weight , 0 )
120120
121- achievement
122- |> ORM . update ( ~m( contents_favorited_count reputation) a )
121+ achievement |> ORM . update ( ~m( articles_collects_count reputation) a )
123122 end
124123 end
125124
126- # @spec safe_minus(non_neg_integer(), non_neg_integer()) :: non_neg_integer()
127- # defp safe_minus(count, unit) when is_integer(count) and is_integer(unit) and unit > 0 do
128- # case count <= 0 do
129- # true ->
130- # 0
131-
132- # false ->
133- # count - unit
134- # end
135- # end
136-
137125 @ doc """
138126 list communities which the user is editor in it
139127 """
0 commit comments