Module: Diaspora::Federation::Receive

Extended by:
Logging
Defined in:
lib/diaspora/federation/receive.rb

Class Method Summary collapse

Class Method Details

.account_deletion(entity) ⇒ Object


12
13
14
15
16
17
18
# File 'lib/diaspora/federation/receive.rb', line 12

def self.(entity)
  person = author_of(entity)
  AccountDeletion.create!(person: person) unless AccountDeletion.where(person: person).exists?
rescue => e # rubocop:disable Lint/RescueWithoutErrorClass
  raise e unless AccountDeletion.where(person: person).exists?
  logger.warn "ignoring error on receive AccountDeletion:#{entity.author}: #{e.class}: #{e.message}"
end

.account_migration(entity, opts) ⇒ Object


20
21
22
23
24
25
26
27
28
29
# File 'lib/diaspora/federation/receive.rb', line 20

def self.(entity, opts)
  old_person = author_of(entity)
  profile = profile(entity.profile, opts)
  return if AccountMigration.where(old_person: old_person, new_person: profile.person).exists?
  AccountMigration.create!(old_person: old_person, new_person: profile.person)
rescue => e # rubocop:disable Lint/RescueWithoutErrorClass
  raise e unless AccountMigration.where(old_person: old_person, new_person: profile.person).exists?
  logger.warn "ignoring error on receive #{entity}: #{e.class}: #{e.message}"
  nil
end

.comment(entity, opts) ⇒ Object


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

def self.comment(entity, opts)
  receive_relayable(Comment, entity, opts) do
    Comment.new(
      author:      author_of(entity),
      guid:        entity.guid,
      created_at:  entity.created_at,
      text:        entity.text,
      commentable: Post.find_by(guid: entity.parent_guid)
    )
  end
end

.contact(entity, _opts) ⇒ Object


43
44
45
46
47
48
49
50
51
# File 'lib/diaspora/federation/receive.rb', line 43

def self.contact(entity, _opts)
  recipient = Person.find_by(diaspora_handle: entity.recipient).owner
  if entity.sharing
    Contact.create_or_update_sharing_contact(recipient, author_of(entity))
  else
    recipient.disconnected_by(author_of(entity))
    nil
  end
end

.conversation(entity, _opts) ⇒ Object


53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/diaspora/federation/receive.rb', line 53

def self.conversation(entity, _opts)
  author = author_of(entity)
  ignore_existing_guid(Conversation, entity.guid, author) do
    Conversation.create!(
      author:              author,
      guid:                entity.guid,
      subject:             entity.subject,
      created_at:          entity.created_at,
      participant_handles: entity.participants,
      messages:            entity.messages.map {|message| build_message(message) }
    )
  end
end

.like(entity, opts) ⇒ Object


67
68
69
70
71
72
73
74
75
76
# File 'lib/diaspora/federation/receive.rb', line 67

def self.like(entity, opts)
  receive_relayable(Like, entity, opts) do
    Like.new(
      author:   author_of(entity),
      guid:     entity.guid,
      positive: entity.positive,
      target:   Mappings.model_class_for(entity.parent_type).find_by(guid: entity.parent_guid)
    )
  end
end

.message(entity, _opts) ⇒ Object


78
79
80
81
82
# File 'lib/diaspora/federation/receive.rb', line 78

def self.message(entity, _opts)
  ignore_existing_guid(Message, entity.guid, author_of(entity)) do
    build_message(entity).tap(&:save!)
  end
end

.participation(entity, _opts) ⇒ Object


84
85
86
87
88
89
90
91
92
93
# File 'lib/diaspora/federation/receive.rb', line 84

def self.participation(entity, _opts)
  author = author_of(entity)
  ignore_existing_guid(Participation, entity.guid, author) do
    Participation.create!(
      author: author,
      guid:   entity.guid,
      target: Mappings.model_class_for(entity.parent_type).find_by(guid: entity.parent_guid)
    )
  end
end

.perform(entity, opts = {}) ⇒ Object


8
9
10
# File 'lib/diaspora/federation/receive.rb', line 8

def self.perform(entity, opts={})
  public_send(Mappings.receiver_for(entity), entity, opts)
end

.photo(entity, _opts) ⇒ Object


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/diaspora/federation/receive.rb', line 95

def self.photo(entity, _opts)
  author = author_of(entity)
  persisted_photo = load_from_database(Photo, entity.guid, author)

  if persisted_photo
    persisted_photo.tap do |photo|
      photo.update_attributes(
        text:                entity.text,
        public:              entity.public,
        created_at:          entity.created_at,
        remote_photo_path:   entity.remote_photo_path,
        remote_photo_name:   entity.remote_photo_name,
        status_message_guid: entity.status_message_guid,
        height:              entity.height,
        width:               entity.width
      )
    end
  else
    save_photo(entity)
  end
end

.poll_participation(entity, opts) ⇒ Object


117
118
119
120
121
122
123
124
125
126
# File 'lib/diaspora/federation/receive.rb', line 117

def self.poll_participation(entity, opts)
  receive_relayable(PollParticipation, entity, opts) do
    PollParticipation.new(
      author:           author_of(entity),
      guid:             entity.guid,
      poll:             Poll.find_by(guid: entity.parent_guid),
      poll_answer_guid: entity.poll_answer_guid
    )
  end
end

.profile(entity, _opts) ⇒ Object


128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/diaspora/federation/receive.rb', line 128

def self.profile(entity, _opts)
  author_of(entity).profile.tap do |profile|
    profile.update_attributes(
      first_name:       entity.first_name,
      last_name:        entity.last_name,
      image_url:        entity.image_url,
      image_url_medium: entity.image_url_medium,
      image_url_small:  entity.image_url_small,
      birthday:         entity.birthday,
      gender:           entity.gender,
      bio:              entity.bio,
      location:         entity.location,
      searchable:       entity.searchable,
      nsfw:             entity.nsfw,
      tag_string:       entity.tag_string,
      public_details:   entity.public
    )
  end
end

.reshare(entity, _opts) ⇒ Object


148
149
150
151
152
153
154
155
156
157
158
# File 'lib/diaspora/federation/receive.rb', line 148

def self.reshare(entity, _opts)
  author = author_of(entity)
  ignore_existing_guid(Reshare, entity.guid, author) do
    Reshare.create!(
      author:     author,
      guid:       entity.guid,
      created_at: entity.created_at,
      root_guid:  entity.root_guid
    ).tap {|reshare| send_participation_for(reshare) }
  end
end

.retraction(entity, recipient_id) ⇒ Object


160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/diaspora/federation/receive.rb', line 160

def self.retraction(entity, recipient_id)
  model_class = Diaspora::Federation::Mappings.model_class_for(entity.target_type)
  object = model_class.where(guid: entity.target_guid).take!

  case object
  when Person
    User.find(recipient_id).disconnected_by(object)
  when Diaspora::Relayable
    if object.root.author.local?
      root_author = object.root.author.owner
      retraction = Retraction.for(object)
      retraction.defer_dispatch(root_author, false)
      retraction.perform
    else
      object.destroy!
    end
  else
    object.destroy!
  end
end

.status_message(entity, _opts) ⇒ Object

rubocop:disable Metrics/AbcSize


181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/diaspora/federation/receive.rb', line 181

def self.status_message(entity, _opts) # rubocop:disable Metrics/AbcSize
  try_load_existing_guid(StatusMessage, entity.guid, author_of(entity)) do
    StatusMessage.new(
      author:                author_of(entity),
      guid:                  entity.guid,
      text:                  entity.text,
      public:                entity.public,
      created_at:            entity.created_at,
      provider_display_name: entity.provider_display_name
    ).tap do |status_message|
      status_message.location = build_location(entity.location) if entity.location
      status_message.poll = build_poll(entity.poll) if entity.poll
      status_message.photos = save_or_load_photos(entity.photos)

      status_message.save!

      send_participation_for(status_message)
    end
  end
end