mirror of
https://github.com/ENSL/ensl.org.git
synced 2024-12-27 04:51:14 +00:00
103 lines
3 KiB
Ruby
103 lines
3 KiB
Ruby
# == Schema Information
|
|
#
|
|
# Table name: messages
|
|
#
|
|
# id :integer not null, primary key
|
|
# recipient_type :string(255)
|
|
# sender_type :string(255)
|
|
# text :text(65535)
|
|
# text_parsed :text(65535)
|
|
# title :string(255)
|
|
# created_at :datetime
|
|
# updated_at :datetime
|
|
# recipient_id :integer
|
|
# sender_id :integer
|
|
#
|
|
# Indexes
|
|
#
|
|
# index_messages_on_recipient_id_and_recipient_type (recipient_id,recipient_type)
|
|
# index_messages_on_sender_id_and_sender_type (sender_id,sender_type)
|
|
#
|
|
|
|
class Message < ActiveRecord::Base
|
|
include Extra
|
|
|
|
#attr_protected :id, :created_at, :updated_at
|
|
attr_accessor :sender_raw
|
|
|
|
validates_length_of :title, :in => 1..100
|
|
validates_length_of :text, :in => 1..65000
|
|
|
|
scope :ordered, -> { order("created_at DESC") }
|
|
|
|
# FIXME: check before removing, provided by unread
|
|
#scope :read_by,
|
|
# lambda { |user| {:include => :readings, :conditions => ["readings.user_id = ?", user.id]} }
|
|
#scope :unread_by,
|
|
# lambda { |user| {
|
|
# :joins => "LEFT JOIN readings ON readable_type = 'Message' AND readable_id = messages.id AND readings.user_id = #{user.id}",
|
|
# :conditions => "readings.user_id IS NULL"} }
|
|
|
|
belongs_to :sender, :polymorphic => true, :optional => true
|
|
belongs_to :recipient, :polymorphic => true, :optional => true
|
|
|
|
before_save :parse_text
|
|
after_create :send_notifications
|
|
|
|
acts_as_readable
|
|
|
|
def to_s
|
|
title
|
|
end
|
|
|
|
def thread
|
|
if sender_type == 'System'
|
|
Message.where(recipient_id: recipient.id, sender_type: 'System')
|
|
else
|
|
Message.find_by_sql ["
|
|
(SELECT `messages`.* FROM `messages` WHERE `messages`.`sender_id` = ? AND `messages`.`sender_type` = 'User' AND `messages`.`recipient_id` = ?)
|
|
UNION
|
|
(SELECT `messages`.* FROM `messages` WHERE `messages`.`sender_id` = ? AND `messages`.`sender_type` = 'User' AND `messages`.`recipient_id` = ?)
|
|
ORDER BY id", sender.id, recipient.id, recipient.id, sender.id]
|
|
end
|
|
end
|
|
|
|
def parse_text
|
|
if self.text
|
|
self.text_parsed = bbcode_to_html(self.text)
|
|
end
|
|
end
|
|
|
|
def send_notifications
|
|
if recipient.instance_of?(User)
|
|
if recipient.profile.notify_pms
|
|
Notifications.pm recipient, self
|
|
end
|
|
elsif recipient.instance_of?(Group)
|
|
recipient.users.each do |u|
|
|
if u.profile.notify_pms
|
|
Notifications.pm u, self
|
|
end
|
|
end
|
|
elsif recipient.instance_of?(Team)
|
|
recipient.teamers.active.each do |teamer|
|
|
if teamer.user.profile.notify_pms
|
|
Notifications.pm teamer.user, self
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
def can_show? cuser
|
|
cuser and (cuser.received_messages.include?(self) or cuser.sent_messages.include?(self))
|
|
end
|
|
|
|
def can_create? cuser
|
|
cuser and !cuser.banned?(Ban::TYPE_MUTE)
|
|
end
|
|
|
|
def self.params(params, cuser)
|
|
# FIXME: check this
|
|
params.require(:message).permit(:recipient_type, :sender_type, :title, :text, :recipient_id, :sender_id, :sender_raw)
|
|
end
|
|
end
|