Newer
Older
# licensed under the Affero General Public License version 3 or later. See
Daniel Vincent Grippi
a validé
class StatusMessage < Post
include YoutubeTitles
require File.join(Rails.root, 'lib/youtube_titles')
maxwell
a validé
include ActionView::Helpers::TextHelper
Maxwell Salzberg
a validé
include PeopleHelper
validates_length_of :text, :maximum => 10000, :message => I18n.t('status_messages.too_long', :count => 10000)
has_many :photos, :dependent => :destroy, :foreign_key => :status_message_guid, :primary_key => :guid
danielvincent
a validé
validate :message_or_photos_present?
attr_accessible :text
danielgrippi
a validé
after_create :create_mentions
read_attribute(:text)
end
def raw_message=(text)
write_attribute(:text, text)
def formatted_message(opts={})
return self.raw_message unless self.raw_message
escaped_message = opts[:plain_text] ? self.raw_message: ERB::Util.h(self.raw_message)
mentioned_message = self.format_mentions(escaped_message, opts)
Diaspora::Taggable.format_tags(mentioned_message, opts.merge(:no_escape => true))
end
def format_mentions(text, opts = {})
form_message = text.to_str.gsub(regex) do |matched_string|
danielgrippi
a validé
people = self.mentioned_people
person = people.detect{ |p|
person ? ERB::Util.h(person.name) : ERB::Util.h($~[1])
Maxwell Salzberg
a validé
person ? person_link(person, :class => 'mention hovercardable') : ERB::Util.h($~[1])
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 mentions?(person)
mentioned_people.include? person
end
def notify_person(person)
self.mentions.where(:person_id => person.id).first.try(:notify_recipient)
end
identifiers = self.raw_message.scan(regex).map do |match|
identifiers.empty? ? [] : Person.where(:diaspora_handle => identifiers)
def to_activity(opts={})
author = opts[:author] || self.author #Use an already loaded author if passed in.
maxwell
a validé
<<-XML
<entry>
<title>#{x(self.formatted_message(:plain_text => true))}</title>
<content>#{x(self.formatted_message(:plain_text => true))}</content>
<link rel="alternate" type="text/html" href="#{author.url}p/#{self.id}"/>
<id>#{author.url}p/#{self.id}</id>
<published>#{self.created_at.xmlschema}</published>
<updated>#{self.updated_at.xmlschema}</updated>
<activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
<activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
</entry>
maxwell
a validé
end
def socket_to_user(user_or_id, opts={})
unless opts[:aspect_ids]
user_id = user_or_id.instance_of?(Fixnum) ? user_or_id : user_or_id.id
Raphael Sofaer
a validé
aspect_ids = AspectMembership.connection.select_values(
AspectMembership.joins(:contact).where(:contacts => {:user_id => user_id, :person_id => self.author_id}).select('aspect_memberships.aspect_id').to_sql
Raphael Sofaer
a validé
)
opts.merge!(:aspect_ids => aspect_ids)
end
super(user_or_id, opts)
end
Raphael Sofaer
a validé
def after_dispatch sender
unless self.photos.empty?
self.photos.update_all(:pending => false, :public => self.public)
for photo in self.photos
if photo.pending
sender.add_to_streams(photo, self.aspects)
sender.dispatch_post(photo)
end
end
end
end
def comment_email_subject
formatted_message(:plain_text => true)
The Lambda Calculus
a validé
end
def empty?
self.text.blank? && self.photos == []
danielvincent
a validé
protected
def message_or_photos_present?
The Lambda Calculus
a validé
if self.blank?
danielvincent
a validé
errors[:base] << 'Status message requires a message or at least one photo'
end
end
The Lambda Calculus
a validé