Module: Diaspora::Relayable

Includes:
Encryptable
Included in:
Comment, Federated::Relayable, Message, PollParticipation
Defined in:
lib/diaspora/relayable.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Encryptable

#sign_with_key, #signable_accessors, #signable_string, #verify_signature

Class Method Details

.included(model) ⇒ Object


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/diaspora/relayable.rb', line 9

def self.included(model)
  model.class_eval do
    #these fields must be in the schema for a relayable model
    xml_attr :parent_guid
    xml_attr :parent_author_signature
    xml_attr :author_signature

    validates_associated :parent
    validates :author, :presence => true
    validate :author_is_not_ignored

    delegate :public?, to: :parent
    delegate :author, :diaspora_handle, to: :parent, prefix: true

    after_commit :on => :create do
      parent.touch(:interacted_at) if parent.respond_to?(:interacted_at)
    end

  end
end

Instance Method Details

#after_receive(user, person) ⇒ Object


96
97
98
# File 'lib/diaspora/relayable.rb', line 96

def after_receive(user, person)
  self
end

#author_is_not_ignoredObject


30
31
32
33
34
35
36
37
38
39
40
# File 'lib/diaspora/relayable.rb', line 30

def author_is_not_ignored
  if self.new_record? && self.parent.present?
     = self.parent.author
    relayable_author = self.author

    if .local? && .owner.ignored_people.include?(relayable_author)
      self.errors.add(:author_id, 'This person is ignored by the post author')
      #post_author.owner.retract(self)
    end
  end
end

#initialize_signaturesObject


100
101
102
103
104
105
106
107
108
# File 'lib/diaspora/relayable.rb', line 100

def initialize_signatures
  #sign relayable as model creator
  self.author_signature = self.sign_with_key(author.owner.encryption_key)

  if !self.parent.blank? && self.author.owns?(self.parent)
    #sign relayable as parent object owner
    self.parent_author_signature = sign_with_key(author.owner.encryption_key)
  end
end

#parentObject

This method is abstract.

Returns An instance of Relayable#parent_class

Raises:

  • (NotImplementedError)

128
129
130
# File 'lib/diaspora/relayable.rb', line 128

def parent
  raise NotImplementedError.new('you must override parent in order to enable relayable on this model')
end

#parent=(parent) ⇒ Object

This method is abstract.

Raises:

  • (NotImplementedError)

134
135
136
# File 'lib/diaspora/relayable.rb', line 134

def parent= parent
  raise NotImplementedError.new('you must override parent= in order to enable relayable on this model')
end

#parent_classClass

This method is abstract.

Raises:

  • (NotImplementedError)

122
123
124
# File 'lib/diaspora/relayable.rb', line 122

def parent_class
  raise NotImplementedError.new('you must override parent_class in order to enable relayable on this model')
end

#parent_guidString


48
49
50
51
# File 'lib/diaspora/relayable.rb', line 48

def parent_guid
  return nil unless parent.present?
  self.parent.guid
end

#parent_guid=(new_parent_guid) ⇒ Object


53
54
55
# File 'lib/diaspora/relayable.rb', line 53

def parent_guid= new_parent_guid
  self.parent = parent_class.where(:guid => new_parent_guid).first
end

#receive(user, person = nil) ⇒ Object


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/diaspora/relayable.rb', line 68

def receive(user, person=nil)
  comment_or_like = self.class.where(:guid => self.guid).first || self

  # Check to make sure the signature of the comment or like comes from the person claiming to author it
  unless comment_or_like.parent_author == user.person || comment_or_like.verify_parent_author_signature
    Rails.logger.info("event=receive status=abort reason='object signature not valid' recipient=#{user.diaspora_handle} sender=#{self.parent.author.diaspora_handle} payload_type=#{self.class} parent_id=#{self.parent.id}")
    return
  end

  # As the owner of the post being liked or commented on, you need to add your own signature in order to
  # pass it to the people who received your original post
  if user.owns? comment_or_like.parent
    comment_or_like.parent_author_signature = comment_or_like.sign_with_key(user.encryption_key)
    comment_or_like.save!
  end

  # Dispatch object DOWNSTREAM, received it via UPSTREAM
  unless user.owns?(comment_or_like)
    comment_or_like.save!
    Postzord::Dispatcher.build(user, comment_or_like).post
  end

  if comment_or_like.after_receive(user, person)
    comment_or_like
  end
end

#relayable?Boolean


43
44
45
# File 'lib/diaspora/relayable.rb', line 43

def relayable?
  true
end

#signature_valid?Boolean


116
117
118
# File 'lib/diaspora/relayable.rb', line 116

def signature_valid?
  verify_signature(self.author_signature, self.author)
end

#subscribers(user) ⇒ Array<Person>


58
59
60
61
62
63
64
65
66
# File 'lib/diaspora/relayable.rb', line 58

def subscribers(user)
  if user.owns?(self.parent)
    self.parent.subscribers(user)
  elsif user.owns?(self)
    [self.parent.author]
  else
    []
  end
end

#verify_parent_author_signatureBoolean


111
112
113
# File 'lib/diaspora/relayable.rb', line 111

def verify_parent_author_signature
  verify_signature(self.parent_author_signature, self.parent.author)
end