<commit_before># frozen_string_literal: true

#
# Copyright (C) 2018 - present 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 Lti::IMS
  class NamesAndRolesSerializer
    def initialize(page)
      @page = page
    end

    def as_json
      {
        id: page[:url],
        context: serialize_context,
        members: serialize_memberships,
      }.compact
    end

    private

    def serialize_context
      {
        id: Lti::Asset.opaque_identifier_for(unwrap(page[:context])),
        label: page[:context].context_label,
        title: page[:context].context_title,
      }.compact
    end

    def serialize_memberships
      page[:memberships] ? page[:memberships].collect { |m| serialize_membership(m) } : []
    end

    def serialize_membership(enrollment)
      # Inbound model is either an ActiveRecord Enrollment or GroupMembership, with delegations in place
      # to make them behave more or less the same for our purposes
      expander = variable_expander(enrollment)
      member(enrollment, expander).merge!(message(enrollment, expander))
    end

    def variable_expander(enrollment)
      Lti::VariableExpander.new(
        page[:context].root_account,
        Lti::IMS::Providers::MembershipsProvider.unwrap(page[:context]),
        page[:controller],
        {
          current_user: Lti::IMS::Providers::MembershipsProvider.unwrap(enrollment.user),
          tool: page[:tool],
          enrollment: enrollment,
          variable_whitelist: %w[
            Person.name.full
            Person.name.display
            Person.name.family
            Person.name.given
            User.image
            User.id
            Canvas.user.id
            vnd.instructure.User.uuid
            Canvas.user.globalId
            Canvas.user.sisSourceId
            Person.sourcedId
            Message.locale
            vnd.Canvas.Person.email.sis
            Person.email.primary
            Person.address.timezone
            User.username
            Canvas.user.loginId
            Canvas.user.sisIntegrationId<commit_msg>Add New NRPS Variable Expansions<commit_after># frozen_string_literal: true

#
# Copyright (C) 2018 - present 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 Lti::IMS
  class NamesAndRolesSerializer
    def initialize(page)
      @page = page
    end

    def as_json
      {
        id: page[:url],
        context: serialize_context,
        members: serialize_memberships,
      }.compact
    end

    private

    def serialize_context
      {
        id: Lti::Asset.opaque_identifier_for(unwrap(page[:context])),
        label: page[:context].context_label,
        title: page[:context].context_title,
      }.compact
    end

    def serialize_memberships
      page[:memberships] ? page[:memberships].collect { |m| serialize_membership(m) } : []
    end

    def serialize_membership(enrollment)
      # Inbound model is either an ActiveRecord Enrollment or GroupMembership, with delegations in place
      # to make them behave more or less the same for our purposes
      expander = variable_expander(enrollment)
      member(enrollment, expander).merge!(message(enrollment, expander))
    end

    def variable_expander(enrollment)
      Lti::VariableExpander.new(
        page[:context].root_account,
        Lti::IMS::Providers::MembershipsProvider.unwrap(page[:context]),
        page[:controller],
        {
          current_user: Lti::IMS::Providers::MembershipsProvider.unwrap(enrollment.user),
          tool: page[:tool],
          enrollment: enrollment,
          variable_whitelist: %w[
            Person.name.full
            Person.name.display
            Person.name.family
            Person.name.given
            User.image
            User.id
            Canvas.user.id
            vnd.instructure.User.uuid
            com.instructure.User.sectionNames
            Canvas.user.globalId
            Canvas.user.sisSourceId
            Person.sourcedId
            Message.locale
            vnd.Canvas.Person.email.sis
            Person.email.primary
            Person.address.timezone
            User.username
            Canvas.user.loginId
            Canvas.user.sisIntegrationId
            Canvas.course.sectionIds
            Canvas.group.contextIds
{'fraction_non_alphanumeric': 0.07899560117302053, 'fraction_numerical': 0.0018328445747800588, 'mean_word_length': 2.482450542437779, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3396918', 'n_tokens_mistral': 1507, 'n_tokens_neox': 1460, 'n_words': 424}