Skip to content

Commit

Permalink
Update lib/report_generator.rb
Browse files Browse the repository at this point in the history
Co-authored-by: Alan Guo Xiang Tan <gxtan1990@gmail.com>
  • Loading branch information
nattsw and tgxworld committed Jan 3, 2024
1 parent a945423 commit 3bfe684
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions lib/report_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ def initialize(creator_user_id)
def generate(query_id, query_params, recipients)
query = DiscourseDataExplorer::Query.find(query_id)
return [] unless query
return [] if recipients.empty?

creator = User.find(@creator_user_id)
creator = User.find_by(id: @creator_user_id)
return [] unless Guardian.new(creator).can_send_private_messages?

return [] if recipients.empty?

usernames = filter_recipients_by_query_access(recipients, query)
params = params_to_hash(query_params)

Expand All @@ -26,21 +25,6 @@ def generate(query_id, query_params, recipients)
build_report_pms(query, table, usernames)
end

def filter_recipients_by_query_access(recipients, query)
recipients.reduce([]) do |names, recipient|
if (group = Group.find_by(name: recipient)) &&
(
group.id == Group::AUTO_GROUPS[:admins] ||
query.query_groups.exists?(group_id: group.id)
)
next names.concat group.users.pluck(:username)
elsif (user = User.find_by(username: recipient))
next names << recipient if Guardian.new(user).user_can_access_query?(query)
end
next names
end
end

def params_to_hash(query_params)
params = JSON.parse(query_params)
params_hash = {}
Expand Down Expand Up @@ -75,5 +59,24 @@ def build_report_pms(query, table = "", usernames = [])
end
pms
end

private

def filter_recipients_by_query_access(recipients, query)
recipients.reduce([]) do |names, recipient|
if (group = Group.find_by(name: recipient)) &&
(
group.id == Group::AUTO_GROUPS[:admins] ||
query.query_groups.exists?(group_id: group.id)
)
names.concat group.users.pluck(:username)
elsif (user = User.find_by(username: recipient)) &&
Guardian.new(user).user_can_access_query?(query)
names << recipient
end

names
end
end
end
end

0 comments on commit 3bfe684

Please sign in to comment.