<commit_before># This file is part of Mconf-Web, a web application that provides access
# to the Mconf webconferencing system. Copyright (C) 2010-2015 Mconf.
#
# This file is licensed under the Affero General Public License version
# 3 or later. See the LICENSE file.

class Invitation < ActiveRecord::Base
  include Mconf::LocaleControllerModule

  belongs_to :target, :polymorphic => true
  belongs_to :sender, :class_name => "User"
  belongs_to :recipient, :class_name => "User"

  # Sends the invitation to the recipient.
  # Respects the preferences of the user, sending the notification either via
  # email or private message.
  # Uses the mailer variable to build the correct emails
  def send_invitation
    mailer = if self.is_a? WebConferenceInvitation
               WebConferenceMailer
             elsif self.is_a? EventInvitation
               EventMailer
             else
               nil
             end
    return false if mailer.nil?

    mailer.invitation_email(self.id).deliver
    true
  end

  def self.create_invitations(user_list, params)
    # creates an invitation for each user
    users = user_list.try(:split, ",") || []
    users.map do |user_str|
      user = User.where(:id => user_str).first
      if user
        params[:recipient] = user
      else
        params[:recipient_email] = user_str
      end
      self.create(params)
    end
  end

  # Builds a flash message containing all the names of invited users
  def self.build_flash(list, message)
    msg = message + " "
    msg += list.map { |user|
      user.is_a?(User) ? ActionController::Base.helpers.strip_tags(user.full_name) : user
    }.join(", ")
  end

  # Checks if the invitations in `invitations` are valid or will fail when sent.
  # Returns two arrays:
  #   [0] The Users and/or emails that received the invitation successfully
  #   [1] The Users and/or emails that did not receive the invitation
  def self.check_invitations(invitations)
    success = []
    error = []

    # the invitation will only be invalid if the user or their email is invalid
    for invitation in invitations
      if invitation.recipient.nil?
        mail = invitation.recipient_email
        if ValidateEmail.valid?(mail)
          success << mail
        else
          error << mail
        end
      else
        user = invitation.recipient
        if ValidateEmail.valid?(user.email)
          success << user
        else
          error << user
        end
      end
    end

    return success, error
  end

  def to_ical
    if self.is_a? EventInvitation
      cal = Icalendar::Calendar.new
      cal.add_event(target.to_ics)
      cal.to_ical
    else
      event = Icalendar::Event.new

      # We send the dates always in UTC to make it easier. The 'Z' in the ends denotes
      # that it's in UTC.
      event.dtstart = self.starts_on.in_time_zone('UTC').strftime("%Y%m%dT%H%M%SZ") unless self.starts_on.blank?
      event.dtend = self.ends_on.in_time_zone('UTC').strftime("%Y%m%dT%H%M%SZ") unless self.ends_on.blank?
      event.organizer = sender.email
      event.ip_class = "PUBLIC"<commit_msg>Call event.to_ical directlty in invitation.to_ical<commit_after># This file is part of Mconf-Web, a web application that provides access
# to the Mconf webconferencing system. Copyright (C) 2010-2015 Mconf.
#
# This file is licensed under the Affero General Public License version
# 3 or later. See the LICENSE file.

class Invitation < ActiveRecord::Base
  include Mconf::LocaleControllerModule

  belongs_to :target, :polymorphic => true
  belongs_to :sender, :class_name => "User"
  belongs_to :recipient, :class_name => "User"

  # Sends the invitation to the recipient.
  # Respects the preferences of the user, sending the notification either via
  # email or private message.
  # Uses the mailer variable to build the correct emails
  def send_invitation
    mailer = if self.is_a? WebConferenceInvitation
               WebConferenceMailer
             elsif self.is_a? EventInvitation
               EventMailer
             else
               nil
             end
    return false if mailer.nil?

    mailer.invitation_email(self.id).deliver
    true
  end

  def self.create_invitations(user_list, params)
    # creates an invitation for each user
    users = user_list.try(:split, ",") || []
    users.map do |user_str|
      user = User.where(:id => user_str).first
      if user
        params[:recipient] = user
      else
        params[:recipient_email] = user_str
      end
      self.create(params)
    end
  end

  # Builds a flash message containing all the names of invited users
  def self.build_flash(list, message)
    msg = message + " "
    msg += list.map { |user|
      user.is_a?(User) ? ActionController::Base.helpers.strip_tags(user.full_name) : user
    }.join(", ")
  end

  # Checks if the invitations in `invitations` are valid or will fail when sent.
  # Returns two arrays:
  #   [0] The Users and/or emails that received the invitation successfully
  #   [1] The Users and/or emails that did not receive the invitation
  def self.check_invitations(invitations)
    success = []
    error = []

    # the invitation will only be invalid if the user or their email is invalid
    for invitation in invitations
      if invitation.recipient.nil?
        mail = invitation.recipient_email
        if ValidateEmail.valid?(mail)
          success << mail
        else
          error << mail
        end
      else
        user = invitation.recipient
        if ValidateEmail.valid?(user.email)
          success << user
        else
          error << user
        end
      end
    end

    return success, error
  end

  def to_ical
    if self.is_a? EventInvitation
      target.to_ical
    else
      event = Icalendar::Event.new

      # We send the dates always in UTC to make it easier. The 'Z' in the ends denotes
      # that it's in UTC.
      event.dtstart = self.starts_on.in_time_zone('UTC').strftime("%Y%m%dT%H%M%SZ") unless self.starts_on.blank?
      event.dtend = self.ends_on.in_time_zone('UTC').strftime("%Y%m%dT%H%M%SZ") unless self.ends_on.blank?
      event.organizer = sender.email
      event.ip_class = "PUBLIC"
{'fraction_non_alphanumeric': 0.08229386035963066, 'fraction_numerical': 0.0035639073384092015, 'mean_word_length': 2.610526315789474, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6166411', 'n_tokens_mistral': 1859, 'n_tokens_neox': 1812, 'n_words': 586}