Refactor federation mappings

The hashes don't work properly with rails 5, there are different classes
loaded when called from the rails engine with missing inheritance.

Also the hashes never worked well with spring.
This commit is contained in:
Benjamin Neff 2017-08-06 02:19:21 +02:00
parent 2b911b0131
commit b8ebb778bf
No known key found for this signature in database
GPG key ID: 971464C3F1A90194
3 changed files with 63 additions and 65 deletions

View file

@ -2,7 +2,7 @@ module Diaspora
module Federation
module Entities
def self.build(entity)
public_send(Mappings.builder_for(entity.class), entity)
public_send(Mappings.builder_for(entity), entity)
end
def self.post(post)

View file

@ -1,82 +1,80 @@
module Diaspora
module Federation
module Mappings
# rubocop:disable Metrics/CyclomaticComplexity
# used in Diaspora::Federation::Receive
def self.receiver_for(federation_class)
fetch_from(ENTITY_RECEIVERS, federation_class)
def self.receiver_for(federation_entity)
case federation_entity
when DiasporaFederation::Entities::Comment then :comment
when DiasporaFederation::Entities::Contact then :contact
when DiasporaFederation::Entities::Conversation then :conversation
when DiasporaFederation::Entities::Like then :like
when DiasporaFederation::Entities::Message then :message
when DiasporaFederation::Entities::Participation then :participation
when DiasporaFederation::Entities::Photo then :photo
when DiasporaFederation::Entities::PollParticipation then :poll_participation
when DiasporaFederation::Entities::Profile then :profile
when DiasporaFederation::Entities::Reshare then :reshare
when DiasporaFederation::Entities::StatusMessage then :status_message
else not_found(federation_entity.class)
end
end
# used in Diaspora::Federation::Entities
def self.builder_for(diaspora_class)
fetch_from(ENTITY_BUILDERS, diaspora_class)
def self.builder_for(diaspora_entity)
case diaspora_entity
when AccountDeletion then :account_deletion
when Comment then :comment
when Contact then :contact
when Conversation then :conversation
when Like then :like
when Message then :message
when Participation then :participation
when Photo then :photo
when PollParticipation then :poll_participation
when Profile then :profile
when Reshare then :reshare
when Retraction then :retraction
when ContactRetraction then :retraction
when StatusMessage then :status_message
else not_found(diaspora_entity.class)
end
end
def self.model_class_for(entity_name)
fetch_from(ENTITY_MODELS, entity_name)
case entity_name
when "Comment" then Comment
when "Conversation" then Conversation
when "Like" then Like
when "Participation" then Participation
when "PollParticipation" then PollParticipation
when "Photo" then Photo
when "Poll" then Poll
when "Post" then Post
when "Person" then Person # TODO: deprecated
when "Reshare" then Post
when "StatusMessage" then Post
else not_found(entity_name)
end
end
def self.entity_name_for(model)
fetch_from(ENTITY_NAMES, model.class.base_class)
case model
when Comment then "Comment"
when Like then "Like"
when Participation then "Participation"
when PollParticipation then "PollParticipation"
when Photo then "Photo"
when Post then "Post"
else not_found(model.class)
end
end
# rubocop:enable Metrics/CyclomaticComplexity
private_class_method def self.fetch_from(mapping, key)
mapping.fetch(key) { raise DiasporaFederation::Entity::UnknownEntity, "unknown entity: #{key}" }
private_class_method def self.not_found(key)
raise DiasporaFederation::Entity::UnknownEntity, "unknown entity: #{key}"
end
ENTITY_RECEIVERS = {
DiasporaFederation::Entities::Comment => :comment,
DiasporaFederation::Entities::Contact => :contact,
DiasporaFederation::Entities::Conversation => :conversation,
DiasporaFederation::Entities::Like => :like,
DiasporaFederation::Entities::Message => :message,
DiasporaFederation::Entities::Participation => :participation,
DiasporaFederation::Entities::Photo => :photo,
DiasporaFederation::Entities::PollParticipation => :poll_participation,
DiasporaFederation::Entities::Profile => :profile,
DiasporaFederation::Entities::Reshare => :reshare,
DiasporaFederation::Entities::StatusMessage => :status_message
}.freeze
ENTITY_BUILDERS = {
AccountDeletion => :account_deletion,
Comment => :comment,
Contact => :contact,
Conversation => :conversation,
Like => :like,
Message => :message,
Participation => :participation,
Photo => :photo,
PollParticipation => :poll_participation,
Profile => :profile,
Reshare => :reshare,
Retraction => :retraction,
ContactRetraction => :retraction,
StatusMessage => :status_message
}.freeze
ENTITY_MODELS = {
"Comment" => Comment,
"Conversation" => Conversation,
"Like" => Like,
"Participation" => Participation,
"PollParticipation" => PollParticipation,
"Photo" => Photo,
"Poll" => Poll,
"Post" => Post,
# TODO: deprecated
"Person" => Person,
"Reshare" => Post,
"StatusMessage" => Post
}.freeze
ENTITY_NAMES = {
Comment => "Comment",
Like => "Like",
Participation => "Participation",
PollParticipation => "PollParticipation",
Photo => "Photo",
Post => "Post"
}.freeze
end
end
end

View file

@ -4,7 +4,7 @@ module Diaspora
extend Diaspora::Logging
def self.perform(entity)
public_send(Mappings.receiver_for(entity.class), entity)
public_send(Mappings.receiver_for(entity), entity)
end
def self.account_deletion(entity)