IpAddrSearch: fix pool_versions exception.

Fix pool_versions exception if PoolArchive db isn't set up. Prefer
ActiveRecord over raw sql so we can get real user objects so that the
view can use link_to_user.
This commit is contained in:
evazion
2017-01-12 05:32:04 +00:00
parent fde7829bf6
commit 4848f18455
3 changed files with 22 additions and 27 deletions

View File

@@ -19,20 +19,16 @@ module Moderator
end
private
def select_all_sql(sql, source, *params)
source.select_all_sql(sql, *params)
end
def search_by_ip_addr(ip_addrs)
sums = Hash.new {|h, k| h[k] = 0}
add_row(sums, "select id as k, 1 as count from users where last_ip_addr in (?)", ip_addrs)
add_row(sums, "select creator_id as k, count(*) from comments where ip_addr in (?) group by k", ip_addrs)
add_row(sums, "select updater_id as k, count(*) from post_versions where updater_ip_addr in (?) group by k", ip_addrs)
add_row(sums, "select updater_id as k, count(*) from note_versions where updater_ip_addr in (?) group by k", ip_addrs)
add_row(sums, "select updater_id as k, count(*) from pool_versions where updater_ip_addr in (?) group by k", ip_addrs, PoolArchive)
add_row(sums, "select updater_id as k, count(*) from wiki_page_versions where updater_ip_addr in (?) group by k", ip_addrs)
add_row(sums, "select from_id as k, count(*) from dmails where creator_ip_addr in (?) group by k", ip_addrs)
add_row(sums, NoteVersion.where(updater_ip_addr: ip_addrs).group(:updater).count)
add_row(sums, PoolArchive.where(updater_ip_addr: ip_addrs).group(:updater).count) if PoolArchive.enabled?
add_row(sums, PostVersion.where(updater_ip_addr: ip_addrs).group(:updater).count)
add_row(sums, WikiPageVersion.where(updater_ip_addr: ip_addrs).group(:updater).count)
add_row(sums, Comment.where(ip_addr: ip_addrs).group(:creator).count)
add_row(sums, Dmail.where(creator_ip_addr: ip_addrs).group(:from).count)
add_row(sums, Hash[User.where(last_ip_addr: ip_addrs).collect { |user| [user, 1] }])
sums
end
@@ -44,21 +40,20 @@ module Moderator
def search_by_user_id(user_ids)
sums = Hash.new {|h, k| h[k] = 0}
users = User.find(user_ids)
add_row(sums, "select ip_addr as k, count(*) from comments where creator_id in (?) group by k", user_ids)
add_row(sums, "select updater_ip_addr as k, count(*) from post_versions where updater_id in (?) group by k", user_ids)
add_row(sums, "select updater_ip_addr as k, count(*) from note_versions where updater_id in (?) group by k", user_ids)
add_row(sums, "select updater_ip_addr as k, count(*) from pool_versions where updater_id in (?) group by k", user_ids, PoolArchive)
add_row(sums, "select updater_ip_addr as k, count(*) from wiki_page_versions where updater_id in (?) group by k", user_ids)
add_row(sums, "select creator_ip_addr as k, count(*) from dmails where from_id in (?) group by k", user_ids)
add_row(sums, NoteVersion.where(updater: users).group(:updater_ip_addr).count)
add_row(sums, PoolArchive.where(updater: users).group(:updater_ip_addr).count) if PoolArchive.enabled?
add_row(sums, PostVersion.where(updater: users).group(:updater_ip_addr).count)
add_row(sums, WikiPageVersion.where(updater: users).group(:updater_ip_addr).count)
add_row(sums, Comment.where(creator: users).group(:ip_addr).count)
add_row(sums, Dmail.where(from: users).group(:creator_ip_addr).count)
sums
end
def add_row(sums, sql, ip_addrs, source = ActiveRecord::Base)
select_all_sql(sql, source, ip_addrs).each do |row|
sums[row["k"]] += row["count"].to_i
end
def add_row(sums, counts)
sums.merge!(counts) { |key, oldcount, newcount| oldcount + newcount }
end
end
end

View File

@@ -1,4 +1,4 @@
<p><%= link_to "Search for users with these IP addresses", moderator_ip_addrs_path(:search => {:ip_addr => @results.map {|k, count| k}.reject{|ip| ip == "127.0.0.1"}.join(",")}) %></p>
<p><%= link_to "Search for users with these IP addresses", moderator_ip_addrs_path(:search => {:ip_addr => @results.keys.reject{|ip| ip == "127.0.0.1"}.join(",")}) %></p>
<table class="striped">
<thead>

View File

@@ -1,4 +1,4 @@
<p><%= link_to "Search for IP addresses with these users", moderator_ip_addrs_path(:search => {:user_id => @results.map{|k, count| k}.join(",")}) %></p>
<p><%= link_to "Search for IP addresses with these users", moderator_ip_addrs_path(:search => {:user_id => @results.map{|user, count| user.id}.join(",")}) %></p>
<table class="striped">
<thead>
@@ -9,12 +9,12 @@
</tr>
</thead>
<tbody>
<% @results.each do |user_id, count| %>
<% @results.each do |user, count| %>
<tr>
<td><%= link_to User.id_to_name(user_id), user_path(user_id) %></td>
<td><%= link_to_user user %></td>
<td><%= count %></td>
<td><%= link_to "Show IP addresses", moderator_ip_addrs_path(:search => {:user_id => user_id}) %></td>
<td><%= link_to "Show IP addresses", moderator_ip_addrs_path(:search => {:user_id => user.id}) %></td>
</tr>
<% end %>
</tbody>
</table>
</table>