Skip to content
Extraits de code Groupes Projets
message.rb 1,98 ko
Newer Older
  • Learn to ignore specific revisions
  • class NotVisibleError < RuntimeError; end
    
    class Message < ActiveRecord::Base
      include ROXML
    
    danielvincent's avatar
    danielvincent a validé
    
    
      include Diaspora::Guid
      include Diaspora::Webhooks
    
    danielvincent's avatar
    danielvincent a validé
      include Diaspora::Relayable
    
    
      xml_attr :text
      xml_attr :created_at
    
    zhitomirskiyi's avatar
    zhitomirskiyi a validé
      xml_reader :diaspora_handle
      xml_reader :conversation_guid
    
    
      belongs_to :author, :class_name => 'Person'
    
    danielgrippi's avatar
    danielgrippi a validé
      belongs_to :conversation, :touch => true
    
    danielvincent's avatar
    danielvincent a validé
        #sign comment as commenter
        self.author_signature = self.sign_with_key(self.author.owner.encryption_key) if self.author.owner
    
    
        if !self.parent.blank? &&  self.author.owns?(self.parent)
    
    danielvincent's avatar
    danielvincent a validé
          #sign comment as post owner
          self.parent_author_signature = self.sign_with_key( self.parent.author.owner.encryption_key) if self.parent.author.owner
        end
    
    danielvincent's avatar
    danielvincent a validé
      end
    
    
      validate :participant_of_parent_conversation
    
    
    zhitomirskiyi's avatar
    zhitomirskiyi a validé
      def diaspora_handle
        self.author.diaspora_handle
      end
    
      def diaspora_handle= nh
        self.author = Webfinger.new(nh).fetch
      end
    
      def conversation_guid
        self.conversation.guid
      end
    
      def conversation_guid= guid
        if cnv = Conversation.find_by_guid(guid)
          self.conversation_id = cnv.id
        end
      end
    
    
    danielvincent's avatar
    danielvincent a validé
      def parent_class
        Conversation
    
    danielvincent's avatar
    danielvincent a validé
      def parent
        self.conversation
      end
    
      def parent= parent
        self.conversation = parent
    
    zhitomirskiyi's avatar
    zhitomirskiyi a validé
      end
    
      def after_receive(user, person)
        if vis = ConversationVisibility.where(:conversation_id => self.conversation_id, :person_id => user.person.id).first
          vis.unread += 1
          vis.save
          self
        else
    
          raise NotVisibleError.new("User #{user.id} with person #{user.person.id} is not allowed to see conversation #{conversation.id}!")
    
      def notification_type(user, person)
    
        Notifications::PrivateMessage unless user.person == person
    
      private
      def participant_of_parent_conversation
        if self.parent && !self.parent.participants.include?(self.author)
          errors[:base] << "Author is not participating in the conversation"
        else
          true
        end
      end