relayable.rb 1.43 KB
Newer Older
1 2
# frozen_string_literal: true

danielgrippi's avatar
danielgrippi committed
3
#   Copyright (c) 2010-2011, Diaspora Inc.  This file is
4 5 6 7 8 9 10
#   licensed under the Affero General Public License version 3 or later.  See
#   the COPYRIGHT file.

module Diaspora
  module Relayable
    def self.included(model)
      model.class_eval do
11
        validates :parent, presence: true
12
        validates_associated :parent
13
        validate :author_is_not_ignored
14

15 16
        delegate :public?, to: :parent
        delegate :author, :diaspora_handle, to: :parent, prefix: true
17 18
      end
    end
19

Benjamin Neff's avatar
Benjamin Neff committed
20 21 22 23 24 25
    def root
      @root ||= parent
      @root = @root.parent while @root.is_a?(Relayable)
      @root
    end

26
    def author_is_not_ignored
Benjamin Neff's avatar
Benjamin Neff committed
27 28
      unless new_record? && root.present? && root.author.local? &&
        root.author.owner.ignored_people.include?(author)
29
        return
30
      end
31 32

      errors.add(:author_id, "This relayable author is ignored by the post author")
33 34
    end

35
    # @return [Array<Person>]
36
    def subscribers
Benjamin Neff's avatar
Benjamin Neff committed
37
      if root.author.local?
38
        if author.local?
Benjamin Neff's avatar
Benjamin Neff committed
39
          root.subscribers
40
        else
Benjamin Neff's avatar
Benjamin Neff committed
41
          root.subscribers.select(&:remote?).reject {|person| person.pod_id == author.pod_id }
42
        end
Maxwell Salzberg's avatar
wip  
Maxwell Salzberg committed
43
      else
Benjamin Neff's avatar
Benjamin Neff committed
44
        [root.author, author]
45 46 47
      end
    end

48
    def sender_for_dispatch
Benjamin Neff's avatar
Benjamin Neff committed
49
      root.author.owner if root.author.local?
50 51
    end

Raphael Sofaer's avatar
Raphael Sofaer committed
52
    # @abstract
53 54 55 56 57
    def parent
      raise NotImplementedError.new('you must override parent in order to enable relayable on this model')
    end
  end
end