Skip to content

Commit

Permalink
Pass tenant to search_params
Browse files Browse the repository at this point in the history
  • Loading branch information
mirrec committed Oct 12, 2023
1 parent 698612f commit 42dfb90
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 11 deletions.
4 changes: 2 additions & 2 deletions app/controllers/message_threads_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ def message_thread_policy_scope
end

def search_permissions
result = { tenant_id: Current.tenant }
result[:box_id] = Current.box if Current.box
result = { tenant: Current.tenant }
result[:box] = Current.box if Current.box
result[:tag_ids] = policy_scope(Tag).pluck(:id) unless Current.user.admin?
result
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/message_thread_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def self.all(scope: nil, search_permissions:, query: nil, cursor:)
parsed_query = Searchable::MessageThreadQuery.parse(query.to_s)
filter = Searchable::MessageThreadQuery.labels_to_ids(
parsed_query,
tenant_id: search_permissions.fetch(:tenant_id)
tenant: search_permissions.fetch(:tenant)
)

ids, next_cursor, highlights = Searchable::MessageThread.search_ids(
Expand Down
4 changes: 2 additions & 2 deletions app/models/searchable/message_thread.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def self.fulltext_search(query)
def self.search_ids(query_filter, search_permissions:, cursor:, per_page:, direction: )
scope = self

scope = scope.where(tenant_id: search_permissions.fetch(:tenant_id))
scope = scope.where(box_id: search_permissions.fetch(:box_id)) if search_permissions[:box_id]
scope = scope.where(tenant_id: search_permissions.fetch(:tenant))
scope = scope.where(box_id: search_permissions.fetch(:box)) if search_permissions[:box]

if search_permissions.key?(:tag_ids)
if search_permissions[:tag_ids].any?
Expand Down
12 changes: 6 additions & 6 deletions app/models/searchable/message_thread_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ def self.parse(query)
}
end

def self.labels_to_ids(parsed_query, tenant_id:)
def self.labels_to_ids(parsed_query, tenant:)
fulltext, filter_labels, filter_out_labels =
parsed_query.fetch_values(:fulltext, :filter_labels, :filter_out_labels)

found_all, filter_tag_ids = label_names_to_tag_ids(tenant_id, filter_labels)
_, filter_out_tag_ids = label_names_to_tag_ids(tenant_id, filter_out_labels)
found_all, filter_tag_ids = label_names_to_tag_ids(tenant, filter_labels)
_, filter_out_tag_ids = label_names_to_tag_ids(tenant, filter_out_labels)

result = {}

Expand All @@ -51,11 +51,11 @@ def self.labels_to_ids(parsed_query, tenant_id:)
result
end

def self.label_names_to_tag_ids(tenant_id, label_names)
def self.label_names_to_tag_ids(tenant, label_names)
if label_names.find { |name| name == "*" }.present?
[true, Tag.where(tenant_id: tenant_id, visible: true).pluck(:id)]
[true, tenant.tags.visible.pluck(:id)]
else
ids = Tag.where(tenant_id: tenant_id, name: label_names).pluck(:id)
ids = tenant.tags.where(name: label_names).pluck(:id)
[ids.length == label_names.length, ids]
end
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class Tag < ApplicationRecord
validates :name, presence: true
validates :name, uniqueness: { scope: :tenant_id, case_sensitive: false }

scope :visible, -> { where(visible: true) }

after_create_commit ->(tag) { tag.mark_readable_by_groups(tag.tenant.admin_groups) }
after_update_commit ->(tag) { EventBus.publish(:tag_renamed, tag) if previous_changes.key?("name") }
after_destroy ->(tag) { EventBus.publish(:tag_removed, tag) }
Expand Down

0 comments on commit 42dfb90

Please sign in to comment.