<commit_before>#   Copyright (c) 2010, Diaspora Inc.  This file is
#   licensed under the Affero General Public License version 3 or later.  See
#   the COPYRIGHT file.

class StatusMessage < Post
  include Diaspora::Socketable
  include YoutubeTitles
  require File.join(Rails.root, 'lib/youtube_titles')
  include ActionView::Helpers::TextHelper

  validates_length_of :message, :maximum => 1000, :message => "please make your status messages less than 1000 characters"
  xml_name :status_message
  xml_attr :raw_message

  has_many :photos, :dependent => :destroy
  validate :message_or_photos_present?

  attr_accessible :message

  serialize :youtube_titles, Hash
  before_save do
    get_youtube_title message
  end

  def message(opts = {})
    self.formatted_message(opts)
  end

  def raw_message
    read_attribute(:message)
  end
  def raw_message=(text)
    write_attribute(:message, text)
  end

  def formatted_message(opts = {})
    return self.raw_message unless self.raw_message
    people = self.mentioned_people
    regex = /@\{([^;]+); ([^\}]+)\}/
    escaped_message = ERB::Util.h(raw_message)
    form_message = escaped_message.gsub(regex) do |matched_string|
      inner_captures = matched_string.match(regex).captures
      person = people.detect{ |p|
        p.diaspora_handle == inner_captures.last
      }

      if opts[:plain_text]
        person ? ERB::Util.h(person.name) : ERB::Util.h(inner_captures.first)
      else
        person ? "@<a href=\"/people/#{person.id}\" class=\"mention\">#{ERB::Util.h(person.name)}</a>" : ERB::Util.h(inner_captures.first)
      end
    end
    form_message
  end

  def mentioned_people
    if self.persisted?
      create_mentions if self.mentions.empty?
      self.mentions.includes(:person => :profile).map{ |mention| mention.person }
    else
      mentioned_people_from_string
    end
  end

  def create_mentions
    mentioned_people_from_string.each do |person|
      self.mentions.create(:person => person)
    end
  end

  def mentioned_people_from_string
    regex = /@\{([^;]+); ([^\}]+)\}/
    identifiers = self.raw_message.scan(regex).map do |match|<commit_msg>Move the @ inside the mention links<commit_after>#   Copyright (c) 2010, Diaspora Inc.  This file is
#   licensed under the Affero General Public License version 3 or later.  See
#   the COPYRIGHT file.

class StatusMessage < Post
  include Diaspora::Socketable
  include YoutubeTitles
  require File.join(Rails.root, 'lib/youtube_titles')
  include ActionView::Helpers::TextHelper

  validates_length_of :message, :maximum => 1000, :message => "please make your status messages less than 1000 characters"
  xml_name :status_message
  xml_attr :raw_message

  has_many :photos, :dependent => :destroy
  validate :message_or_photos_present?

  attr_accessible :message

  serialize :youtube_titles, Hash
  before_save do
    get_youtube_title message
  end

  def message(opts = {})
    self.formatted_message(opts)
  end

  def raw_message
    read_attribute(:message)
  end
  def raw_message=(text)
    write_attribute(:message, text)
  end

  def formatted_message(opts = {})
    return self.raw_message unless self.raw_message
    people = self.mentioned_people
    regex = /@\{([^;]+); ([^\}]+)\}/
    escaped_message = ERB::Util.h(raw_message)
    form_message = escaped_message.gsub(regex) do |matched_string|
      inner_captures = matched_string.match(regex).captures
      person = people.detect{ |p|
        p.diaspora_handle == inner_captures.last
      }

      if opts[:plain_text]
        person ? ERB::Util.h(person.name) : ERB::Util.h(inner_captures.first)
      else
        person ? "<a href=\"/people/#{person.id}\" class=\"mention\">@#{ERB::Util.h(person.name)}</a>" : ERB::Util.h(inner_captures.first)
      end
    end
    form_message
  end

  def mentioned_people
    if self.persisted?
      create_mentions if self.mentions.empty?
      self.mentions.includes(:person => :profile).map{ |mention| mention.person }
    else
      mentioned_people_from_string
    end
  end

  def create_mentions
    mentioned_people_from_string.each do |person|
      self.mentions.create(:person => person)
    end
  end

  def mentioned_people_from_string
    regex = /@\{([^;]+); ([^\}]+)\}/
    identifiers = self.raw_message.scan(regex).map do |match|
{'fraction_non_alphanumeric': 0.11542034370645611, 'fraction_numerical': 0.006038086391082211, 'mean_word_length': 3.5965848452508005, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6786973', 'n_tokens_mistral': 1445, 'n_tokens_neox': 1369, 'n_words': 306}