From b8ebb778bf03152ad27ec7bbe561e38ab318cafd Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Sun, 6 Aug 2017 02:19:21 +0200 Subject: [PATCH] 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. --- lib/diaspora/federation/entities.rb | 2 +- lib/diaspora/federation/mappings.rb | 124 ++++++++++++++-------------- lib/diaspora/federation/receive.rb | 2 +- 3 files changed, 63 insertions(+), 65 deletions(-) diff --git a/lib/diaspora/federation/entities.rb b/lib/diaspora/federation/entities.rb index 48b07c43b..0165724fe 100644 --- a/lib/diaspora/federation/entities.rb +++ b/lib/diaspora/federation/entities.rb @@ -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) diff --git a/lib/diaspora/federation/mappings.rb b/lib/diaspora/federation/mappings.rb index 0d19af545..086df4685 100644 --- a/lib/diaspora/federation/mappings.rb +++ b/lib/diaspora/federation/mappings.rb @@ -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 diff --git a/lib/diaspora/federation/receive.rb b/lib/diaspora/federation/receive.rb index d4dee7648..a3e8cadd4 100644 --- a/lib/diaspora/federation/receive.rb +++ b/lib/diaspora/federation/receive.rb @@ -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)