diff --git a/app/controllers/message_threads_controller.rb b/app/controllers/message_threads_controller.rb index dc1b3b136..05bf8dece 100644 --- a/app/controllers/message_threads_controller.rb +++ b/app/controllers/message_threads_controller.rb @@ -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 diff --git a/app/models/message_thread_collection.rb b/app/models/message_thread_collection.rb index 40fc33994..327cc85f3 100644 --- a/app/models/message_thread_collection.rb +++ b/app/models/message_thread_collection.rb @@ -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( diff --git a/app/models/searchable/message_thread.rb b/app/models/searchable/message_thread.rb index 5f9bc596b..b4022ea1e 100644 --- a/app/models/searchable/message_thread.rb +++ b/app/models/searchable/message_thread.rb @@ -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? diff --git a/app/models/searchable/message_thread_query.rb b/app/models/searchable/message_thread_query.rb index 09f42c55d..1166a8882 100644 --- a/app/models/searchable/message_thread_query.rb +++ b/app/models/searchable/message_thread_query.rb @@ -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 = {} @@ -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 diff --git a/app/models/tag.rb b/app/models/tag.rb index aa324596f..a0d7eabd6 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -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) }