<commit_before>#
# Copyright (C) 2011 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#

module SearchHelper
  include AvatarHelper

  ##
  # Loads all the contexts the user belongs to into instance variable @contexts
  # Used for TokenInput.coffee instances
  #
  # If a course is provided, just return it (and its groups/sections)
  def load_all_contexts(options = {})
    context = options[:context]
    @contexts = Rails.cache.fetch(['all_conversation_contexts', @current_user, context].cache_key, :expires_in => 10.minutes) do
      contexts = {:courses => {}, :groups => {}, :sections => {}}

      term_for_course = lambda do |course|
        course.enrollment_term.default_term? ? nil : course.enrollment_term.name
      end

      add_courses = lambda do |courses, type|
        courses.each do |course|
          contexts[:courses][course.id] = {
            :id => course.id,
            :url => course_url(course),
            :name => course.name,
            :type => :course,
            :term => term_for_course.call(course),
            :state => type == :current ? :active : (course.recently_ended? ? :recently_active : :inactive),
            :available => type == :current && course.available?,
            :permissions => course.rights_status(@current_user).select { |key, value| value },
            :default_section_id => course.default_section(no_create: true).try(:id)
          }
        end
      end

      add_sections = lambda do |sections|
        sections.each do |section|
          contexts[:sections][section.id] = {
            :id => section.id,
            :name => section.name,
            :type => :section,
            :term => contexts[:courses][section.course_id][:term],
            :state => section.active? ? :active : :inactive,
            :parent => {:course => section.course_id},
            :context_name =>  contexts[:courses][section.course_id][:name]
            # if we decide to return permissions here, we should ensure those
            # are cached in adheres_to_policy
          }
        end
      end

      add_groups = lambda do |groups|
        Group.send(:preload_associations, groups, :group_category)
        Group.send(:preload_associations, groups, :group_memberships, conditions: { group_memberships: { user_id: @current_user }})
        groups.each do |group|
          group.can_participate = true
          contexts[:groups][group.id] = {
            :id => group.id,
            :name => group.name,
            :type => :group,
            :state => group.active? ? :active : :inactive,
            :parent => group.context_type == 'Course' ? {:course => group.context.id} : nil,
            :context_name => group.context.name,
            :category => group.category,
            :permissions => group.rights_status(@current_user).select { |key, value| value }
          }
        end
      end

      if context.is_a?(Course)
        add_courses.call [context], :current
        visibility = context.enrollment_visibility_level_for(@current_user, context.section_visibilities_for(@current_user), true)
        sections = visibility == :sections ? context.sections_visible_to(@current_user) : context.course_sections
        add_sections.call sections
        add_groups.call context.groups
      elsif context.is_a?(CourseSection)
        add_courses.call [context.course], :current
        add_sections.call context.course.sections_visible_to(@current_user, [context])
      else
        add_courses.call @current_user.concluded_courses.with_each_shard, :concluded
        add_courses.call @current_user.courses.with_each_shard, :current
        add_sections.call @current_user.messageable_sections
        add_groups.call @current_user.messageable_groups
      end
      contexts
    end
    permissions = options[:permissions] || []
    @contexts.each do |type, contexts|
      contexts.each do |id, context|
        context[:permissions] = HashWithIndifferentAccess.new(context[:permissions] || {})
        context[:permissions].slice!(*permissions) unless permissions == :all
      end
    end
    @contexts
  end
end<commit_msg>Don't include soft deleted groups in search filter<commit_after>#
# Copyright (C) 2011 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#

module SearchHelper
  include AvatarHelper

  ##
  # Loads all the contexts the user belongs to into instance variable @contexts
  # Used for TokenInput.coffee instances
  #
  # If a course is provided, just return it (and its groups/sections)
  def load_all_contexts(options = {})
    context = options[:context]
    @contexts = Rails.cache.fetch(['all_conversation_contexts', @current_user, context].cache_key, :expires_in => 10.minutes) do
      contexts = {:courses => {}, :groups => {}, :sections => {}}

      term_for_course = lambda do |course|
        course.enrollment_term.default_term? ? nil : course.enrollment_term.name
      end

      add_courses = lambda do |courses, type|
        courses.each do |course|
          contexts[:courses][course.id] = {
            :id => course.id,
            :url => course_url(course),
            :name => course.name,
            :type => :course,
            :term => term_for_course.call(course),
            :state => type == :current ? :active : (course.recently_ended? ? :recently_active : :inactive),
            :available => type == :current && course.available?,
            :permissions => course.rights_status(@current_user).select { |key, value| value },
            :default_section_id => course.default_section(no_create: true).try(:id)
          }
        end
      end

      add_sections = lambda do |sections|
        sections.each do |section|
          contexts[:sections][section.id] = {
            :id => section.id,
            :name => section.name,
            :type => :section,
            :term => contexts[:courses][section.course_id][:term],
            :state => section.active? ? :active : :inactive,
            :parent => {:course => section.course_id},
            :context_name =>  contexts[:courses][section.course_id][:name]
            # if we decide to return permissions here, we should ensure those
            # are cached in adheres_to_policy
          }
        end
      end

      add_groups = lambda do |groups|
        Group.send(:preload_associations, groups, :group_category)
        Group.send(:preload_associations, groups, :group_memberships, conditions: { group_memberships: { user_id: @current_user }})
        groups.each do |group|
          group.can_participate = true
          contexts[:groups][group.id] = {
            :id => group.id,
            :name => group.name,
            :type => :group,
            :state => group.active? ? :active : :inactive,
            :parent => group.context_type == 'Course' ? {:course => group.context.id} : nil,
            :context_name => group.context.name,
            :category => group.category,
            :permissions => group.rights_status(@current_user).select { |key, value| value }
          }
        end
      end

      if context.is_a?(Course)
        add_courses.call [context], :current
        visibility = context.enrollment_visibility_level_for(@current_user, context.section_visibilities_for(@current_user), true)
        sections = visibility == :sections ? context.sections_visible_to(@current_user) : context.course_sections
        add_sections.call sections
        add_groups.call context.groups.active
      elsif context.is_a?(CourseSection)
        add_courses.call [context.course], :current
        add_sections.call context.course.sections_visible_to(@current_user, [context])
      else
        add_courses.call @current_user.concluded_courses.with_each_shard, :concluded
        add_courses.call @current_user.courses.with_each_shard, :current
        add_sections.call @current_user.messageable_sections
        add_groups.call @current_user.messageable_groups
      end
      contexts
    end
    permissions = options[:permissions] || []
    @contexts.each do |type, contexts|
      contexts.each do |id, context|
        context[:permissions] = HashWithIndifferentAccess.new(context[:permissions] || {})
        context[:permissions].slice!(*permissions) unless permissions == :all
      end
    end
    @contexts
  end
end
{'fraction_non_alphanumeric': 0.10834213305174234, 'fraction_numerical': 0.0014783526927138332, 'mean_word_length': 2.771804062126643, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1754252', 'n_tokens_mistral': 2684, 'n_tokens_neox': 2630, 'n_words': 702}