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


class InvitesController < ApplicationController
  def index
  end

  def invite_room
    @type = params[:type]

    if @type == "webconference"
      @room = BigbluebuttonRoom.find_by_param(params[:room])
    elsif @type == "event"
      @event = Event.find(params[:event])
    end

    tags = []
    members = Profile.where("full_name like ?", "%#{params[:q]}%").select(['full_name', 'user_id']).limit(10)
    members.each do |f|
      tags.push("id"=>f.user_id, "name"=>f.full_name)
    end

    respond_to do |format|
      format.html{
        if request.xhr?
          render :layout => false
        end
      }
      format.json { render :json => tags }
    end
  end

  def send_invite
    @success_messages = Array.new
    @fail_messages = Array.new
    @fail_user_email = Array.new
    @fail_email = Array.new

    success = ""
    error = ""

    if params[:invite][:type] == "webconference"
      send_invite_webconference
    end

    if params[:invite][:type] == "event"
      send_invite_event
    end

    respond_to do |format|
      if @fail_messages.empty?
        if success.size != 0
          flash[:success] = success.html_safe
        end
        if error.size != 0
          flash[:error] = error.html_safe
        end
        format.html { redirect_to request.referer }
        format.xml  { render :xml => @success_messages, :status => :created, :location => @success_messages }
      else
        if success.size != 0
          flash[:success] = success.html_safe
        end
        if error.size != 0
          flash[:error] = error.html_safe
        end
        format.html { redirect_to request.referer }
        format.xml  { render :xml => @fail_messages.map{|m| m.errors}, :status => :unprocessable_entity }
      end
    end

  end

  def send_notification
    @event = Event.find(params[:event_id])

    msg = Hash.new

    msg[:sender] = current_user
    msg[:event] = @event

    @event.participants.each do |p|
      user = User.find(p.user_id)

      if user != current_user
        msg[:receiver] = user
        Notifier.delay.event_notification_email(msg)
      end
    end

    respond_to do |format|
      flash[:success] = t('event.notification_successfully')
      format.html { redirect_to request.referer }
    end
  end

  private

  def send_invite_webconference
    success = ""
    priv_msg = Hash.new
    priv_email = Hash.new

    priv_msg[:sender_id] = current_user.id
    priv_email[:sender_id] = current_user.id

    unless params[:invite][:message].empty?
      priv_email[:body] = params[:invite][:message]
    end
    priv_email[:room_name] = params[:invite][:room_name]
    priv_email[:room_url] = params[:invite][:room_url]
    priv_email[:mobile_url] = params[:invite][:mobile_url]
    priv_email[:user_name] = current_user.name
    priv_email[:locale] = get_user_locale(current_user)

    if params[:invite][:im_check] != "0"
      for receiver in params[:invite][:members_tokens].split(",")
        priv_msg[:receiver_id] = receiver
        user = User.find(receiver)
        locale = get_user_locale(user, false)
        I18n.with_locale(locale) do
          priv_msg[:title] = t('invite.title')
          body = t('invite.message', :sender => current_user.name, :name => params[:invite][:room_name],
                   :invite_url => params[:invite][:room_url], :message => params[:invite][:message],
                   :mobile_url => params[:invite][:mobile_url],
                   :email_sender => current_user.email).html_safe
          priv_msg[:body] = body
        end

        private_message = PrivateMessage.new(priv_msg)
        if private_message.save
          @success_messages << private_message
          success = t('invite.invitation_successfully') << " " << t('invite.user_private_msg', :user => private_message.receiver.full_name)
        else
          error = t('invite.invitation_unsuccessfully') << " " << t('invite.user_private_msg', :user => private_message.receiver.full_name)
          @fail_messages << private_message
        end
      end
    end<commit_msg>Fix private message text for webconf invitations on v2<commit_after># -*- coding: utf-8 -*-
# This file is part of Mconf-Web, a web application that provides access
# to the Mconf webconferencing system. Copyright (C) 2010-2012 Mconf
#
# This file is licensed under the Affero General Public License version
# 3 or later. See the LICENSE file.


class InvitesController < ApplicationController
  def index
  end

  def invite_room
    @type = params[:type]

    if @type == "webconference"
      @room = BigbluebuttonRoom.find_by_param(params[:room])
    elsif @type == "event"
      @event = Event.find(params[:event])
    end

    tags = []
    members = Profile.where("full_name like ?", "%#{params[:q]}%").select(['full_name', 'user_id']).limit(10)
    members.each do |f|
      tags.push("id"=>f.user_id, "name"=>f.full_name)
    end

    respond_to do |format|
      format.html{
        if request.xhr?
          render :layout => false
        end
      }
      format.json { render :json => tags }
    end
  end

  def send_invite
    @success_messages = Array.new
    @fail_messages = Array.new
    @fail_user_email = Array.new
    @fail_email = Array.new

    success = ""
    error = ""

    if params[:invite][:type] == "webconference"
      send_invite_webconference
    end

    if params[:invite][:type] == "event"
      send_invite_event
    end

    respond_to do |format|
      if @fail_messages.empty?
        if success.size != 0
          flash[:success] = success.html_safe
        end
        if error.size != 0
          flash[:error] = error.html_safe
        end
        format.html { redirect_to request.referer }
        format.xml  { render :xml => @success_messages, :status => :created, :location => @success_messages }
      else
        if success.size != 0
          flash[:success] = success.html_safe
        end
        if error.size != 0
          flash[:error] = error.html_safe
        end
        format.html { redirect_to request.referer }
        format.xml  { render :xml => @fail_messages.map{|m| m.errors}, :status => :unprocessable_entity }
      end
    end

  end

  def send_notification
    @event = Event.find(params[:event_id])

    msg = Hash.new

    msg[:sender] = current_user
    msg[:event] = @event

    @event.participants.each do |p|
      user = User.find(p.user_id)

      if user != current_user
        msg[:receiver] = user
        Notifier.delay.event_notification_email(msg)
      end
    end

    respond_to do |format|
      flash[:success] = t('event.notification_successfully')
      format.html { redirect_to request.referer }
    end
  end

  private

  def send_invite_webconference
    success = ""
    priv_msg = Hash.new
    priv_email = Hash.new

    priv_msg[:sender_id] = current_user.id
    priv_email[:sender_id] = current_user.id

    unless params[:invite][:message].empty?
      priv_email[:body] = params[:invite][:message]
    end
    priv_email[:room_name] = params[:invite][:room_name]
    priv_email[:room_url] = params[:invite][:room_url]
    priv_email[:mobile_url] = params[:invite][:mobile_url]
    priv_email[:user_name] = current_user.name
    priv_email[:locale] = get_user_locale(current_user)

    if params[:invite][:im_check] != "0"
      for receiver in params[:invite][:members_tokens].split(",")
        priv_msg[:receiver_id] = receiver
        user = User.find(receiver)
        locale = get_user_locale(user, false)
        I18n.with_locale(locale) do
          priv_msg[:title] = t('invite.title')
          body = t('invite.message', :sender => current_user.name, :name => params[:invite][:room_name],
                   :invite_url => params[:invite][:room_url],
                   :mobile_url => params[:invite][:mobile_url],
                   :email_sender => current_user.email).html_safe
          body += "</br>\"#{params[:invite][:message]}\"".html_safe
          priv_msg[:body] = body
        end

        private_message = PrivateMessage.new(priv_msg)
        if private_message.save
          @success_messages << private_message
          success = t('invite.invitation_successfully') << " " << t('invite.user_private_msg', :user => private_message.receiver.full_name)
        else
          error = t('invite.invitation_unsuccessfully') << " " << t('invite.user_private_msg', :user => private_message.receiver.full_name)
          @fail_messages << private_message
        end
      end
    end
{'fraction_non_alphanumeric': 0.12348916761687571, 'fraction_numerical': 0.004446978335233751, 'mean_word_length': 2.689945309213294, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '4135955', 'n_tokens_mistral': 2798, 'n_tokens_neox': 2721, 'n_words': 575}