<commit_before>class MessagesController < OrganizationAwareController

  add_breadcrumb "Home", :root_path

  before_action :set_message, :only => [:show, :edit, :update, :destroy, :tag, :reply]
  before_action :check_for_cancel, :only => [:create]

  # Session Variables
  INDEX_KEY_LIST_VAR          = "messages_list_cache_var"

  NEW_MESSAGE_FILTER          = '1'
  FLAGGED_MESSAGE_FILTER      = '2'
  ALL_MESSAGE_FILTER          = '3'
  SENT_MESSAGE_FILTER         = '4'

  def index

    add_breadcrumb "My Messages", user_messages_path(current_user)

    # Start to set up the query
    conditions  = []
    values      = []

    # New messages must be for the current user
    conditions << 'to_user_id = ?'
    values << current_user.id

    # Get the messages
    @messages = Message.where(conditions.join(' AND '), *values).order("created_at DESC")
    @all_messages = @messages.where('opened_at IS NOT NULL')
    @new_messages = @messages.where('opened_at IS NULL')
    @flagged_messages = current_user.messages
    @sent_messages = Message.where(:user_id => current_user.id).order("created_at DESC")

    # cache the set of asset ids in case we need them later
    cache_list(@all_messages, "#{ALL_MESSAGE_FILTER}_#{INDEX_KEY_LIST_VAR}")
    cache_list(@new_messages, "#{NEW_MESSAGE_FILTER}_#{INDEX_KEY_LIST_VAR}")
    cache_list(@flagged_messages, "#{FLAGGED_MESSAGE_FILTER}_#{INDEX_KEY_LIST_VAR}")
    cache_list(@sent_messages, "#{SENT_MESSAGE_FILTER}_#{INDEX_KEY_LIST_VAR}")

    respond_to do |format|
      format.html # index.html.erb
      format.json { render :json => @messages }
    end
  end

  # Tags the message for the user or removes it if the message
  # is already tagged. called by ajax so no response is rendered
  def tag

    if @message.tagged? current_user
      @message.users.delete current_user
    else
      @message.tag current_user
    end

    # No response needed
    render :nothing => true

  end

  def show

    add_breadcrumb "My Messages", user_messages_path(current_user)
    add_breadcrumb @message.subject, user_message_path(current_user, @message)

    @response = Message.new
    @response.organization = @organization
    @response.user = current_user
    @response.priority_type = @message.priority_type

    # Mark this message as opened if not opened previously
    if @message.opened_at.nil?
      @message.opened_at = Time.current<commit_msg>Ensure that messages are only marked opened by the recipient [fixes #108489504]<commit_after>class MessagesController < OrganizationAwareController

  add_breadcrumb "Home", :root_path

  before_action :set_message, :only => [:show, :edit, :update, :destroy, :tag, :reply]
  before_action :check_for_cancel, :only => [:create]

  # Session Variables
  INDEX_KEY_LIST_VAR          = "messages_list_cache_var"

  NEW_MESSAGE_FILTER          = '1'
  FLAGGED_MESSAGE_FILTER      = '2'
  ALL_MESSAGE_FILTER          = '3'
  SENT_MESSAGE_FILTER         = '4'

  def index

    add_breadcrumb "My Messages", user_messages_path(current_user)

    # Start to set up the query
    conditions  = []
    values      = []

    # New messages must be for the current user
    conditions << 'to_user_id = ?'
    values << current_user.id

    # Get the messages
    @messages = Message.where(conditions.join(' AND '), *values).order("created_at DESC")
    @all_messages = @messages.where('opened_at IS NOT NULL')
    @new_messages = @messages.where('opened_at IS NULL')
    @flagged_messages = current_user.messages
    @sent_messages = Message.where(:user_id => current_user.id).order("created_at DESC")

    # cache the set of asset ids in case we need them later
    cache_list(@all_messages, "#{ALL_MESSAGE_FILTER}_#{INDEX_KEY_LIST_VAR}")
    cache_list(@new_messages, "#{NEW_MESSAGE_FILTER}_#{INDEX_KEY_LIST_VAR}")
    cache_list(@flagged_messages, "#{FLAGGED_MESSAGE_FILTER}_#{INDEX_KEY_LIST_VAR}")
    cache_list(@sent_messages, "#{SENT_MESSAGE_FILTER}_#{INDEX_KEY_LIST_VAR}")

    respond_to do |format|
      format.html # index.html.erb
      format.json { render :json => @messages }
    end
  end

  # Tags the message for the user or removes it if the message
  # is already tagged. called by ajax so no response is rendered
  def tag

    if @message.tagged? current_user
      @message.users.delete current_user
    else
      @message.tag current_user
    end

    # No response needed
    render :nothing => true

  end

  def show

    add_breadcrumb "My Messages", user_messages_path(current_user)
    add_breadcrumb @message.subject, user_message_path(current_user, @message)

    @response = Message.new
    @response.organization = @organization
    @response.user = current_user
    @response.priority_type = @message.priority_type

    # Mark this message as opened if not opened previously as long as the current_user
    # is the message recipient
    if @message.opened_at.nil? and current_user == @message.to_user
      @message.opened_at = Time.current
{'fraction_non_alphanumeric': 0.096579476861167, 'fraction_numerical': 0.003420523138832998, 'mean_word_length': 3.7118483412322276, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '5815965', 'n_tokens_mistral': 1583, 'n_tokens_neox': 1527, 'n_words': 388}