<commit_before># -*- coding: utf-8 -*-
class Message
  include Mongoid::Document
  include Mongoid::Timestamps
  field :created_at
  field :body
  belongs_to :room
  belongs_to :user
  embeds_many :attachments
  index :created_at

  def encode_json(_)
    self.to_hash.to_json
  end

  def to_hash
    {
      'id'   => self.id,
      'body' => self.body,
      'html_body' => self.html_body(self.room),
      'name' => (self.user ? self.user.name : 'Anonymous User'),
      'screen_name' => (self.user ? self.user.screen_name : 'Anonymous User'),
      'profile_image_url' => (self.user ? self.user.profile_image_url : ''),
      'created_at' => I18n.l(self.created_at),
      'room'       => self.room.to_json,
      'attachment' => self.attachments && self.attachments.map {|a| a.to_hash}
    }
  end

  def html_body(room)
    AsakusaSatellite::Filter.process self, room
  end

  def prev(offset)
    Message.where(:room_id => self.room.id, :_id.lt => self._id).order_by(:_id.desc).limit(offset).to_a.reverse
  end

  def next(offset)
    Message.where(:room_id => self.room.id, :_id.gt => self._id).order_by(:_id.asc).limit(offset).to_a
  end

  def self.find_by_text(params)
    query = params[:text]
    rooms = params[:rooms] || Room.all_live
    rooms.map do |room|
      messages = Message.where(:room_id => room.id, 'room.deleted' => false, :body => /#{query}/i)
      { :room => room, :messages => messages }
    end
  end
end
<commit_msg>bugfix: search function<commit_after># -*- coding: utf-8 -*-
class Message
  include Mongoid::Document
  include Mongoid::Timestamps
  field :created_at
  field :body
  belongs_to :room
  belongs_to :user
  embeds_many :attachments
  index :created_at

  def encode_json(_)
    self.to_hash.to_json
  end

  def to_hash
    {
      'id'   => self.id,
      'body' => self.body,
      'html_body' => self.html_body(self.room),
      'name' => (self.user ? self.user.name : 'Anonymous User'),
      'screen_name' => (self.user ? self.user.screen_name : 'Anonymous User'),
      'profile_image_url' => (self.user ? self.user.profile_image_url : ''),
      'created_at' => I18n.l(self.created_at),
      'room'       => self.room.to_json,
      'attachment' => self.attachments && self.attachments.map {|a| a.to_hash}
    }
  end

  def html_body(room)
    AsakusaSatellite::Filter.process self, room
  end

  def prev(offset)
    Message.where(:room_id => self.room_id, :_id.lt => self._id).order_by(:_id.desc).limit(offset).to_a.reverse
  end

  def next(offset)
    Message.where(:room_id => self.room_id, :_id.gt => self._id).order_by(:_id.asc).limit(offset).to_a
  end

  def self.find_by_text(params)
    query = params[:text]
    rooms = (params[:rooms] || Room.all_live).select {|room| not room.deleted}
    rooms.map do |room|
      messages = Message.where(:room_id => room._id, :body => /#{query}/i)
      { :room => room, :messages => messages }
    end
  end
end

{'fraction_non_alphanumeric': 0.15566199110502907, 'fraction_numerical': 0.0020526855969893944, 'mean_word_length': 3.141643059490085, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 38, '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': '7447319', 'n_tokens_mistral': 1069, 'n_tokens_neox': 1054, 'n_words': 203}