Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compute_reputation query #363

Merged
merged 1 commit into from
Aug 15, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions apps/cf/lib/actions/actions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,19 @@ defmodule CF.Actions do

@spec compute_reputation!(%User{}) :: integer
def compute_reputation!(user) do
DB.Schema.User
|> where([u], u.id == ^user.id)
|> join(:inner, [u], action in UserAction, on: action.user_id == u.id)
|> join(:inner, [u, _], action in UserAction, on: action.target_user_id == u.id)
DB.Schema.UserAction
|> where([a], a.user_id == ^user.id or a.target_user_id == ^user.id)
|> select(
[_, actions_authored, actions_targeted],
coalesce(sum(actions_authored.author_reputation_change), 0) +
coalesce(sum(actions_targeted.target_reputation_change), 0)
[a],
coalesce(
sum(
fragment(
"CASE WHEN user_id = ? THEN author_reputation_change ELSE target_reputation_change END",
^user.id
)
),
0
)
)
|> DB.Repo.one!()
end
Expand Down