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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix showing spaces in Last Activity page #12881

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ def ordered_users_with_activities
Decidim::ParticipatorySpaceLastActivity
.new(resource).query
.where.not(user: nil)
.select("decidim_user_id, MAX(decidim_action_logs.created_at)")
.group("decidim_user_id")
.select("decidim_action_logs.decidim_user_id, MAX(decidim_action_logs.created_at)")
.group("decidim_action_logs.decidim_user_id")
.reorder("MAX(decidim_action_logs.created_at) DESC")
end

Expand Down
43 changes: 27 additions & 16 deletions decidim-core/app/queries/decidim/last_activity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,36 @@ def filter_moderated(query)
end

def filter_spaces(query)
conditions = []

current_user_id = current_user&.id.to_i
Decidim.participatory_space_manifests.map do |manifest|
klass = manifest.model_class_name.constantize

condition = if klass.include?(Decidim::HasPrivateUsers)
Arel.sql(
[
"decidim_action_logs.participatory_space_type = '#{manifest.model_class_name}'",
"decidim_action_logs.participatory_space_id IN (#{Arel.sql(klass.visible_for(current_user).select(:id).to_sql)})"
].join(" AND ")
).to_s
else
Arel.sql("decidim_action_logs.participatory_space_type = '#{manifest.model_class_name}'").to_s
end

conditions << "(#{condition})"
next unless klass.include?(Decidim::HasPrivateUsers)

table = klass.table_name
query = query.joins(
Arel.sql(
<<~SQL.squish
LEFT JOIN #{table}
ON decidim_action_logs.participatory_space_type = '#{manifest.model_class_name}'
AND #{table}.id = decidim_action_logs.participatory_space_id
LEFT JOIN decidim_participatory_space_private_users AS #{manifest.name}_private_users
ON #{manifest.name}_private_users.privatable_to_type = '#{manifest.model_class_name}'
AND #{table}.id = #{manifest.name}_private_users.privatable_to_id
AND #{table}.private_space = 't'
SQL
).to_s
).where(
Arel.sql(
<<~SQL.squish
#{table}.id IS NULL OR
#{table}.private_space = 'f' OR
#{manifest.name}_private_users.decidim_user_id = #{current_user_id}
SQL
).to_s
)
end
query.where(Arel.sql(conditions.join(" OR ")).to_s)

query
end

def filter_deleted(query)
Expand Down
Loading