use private_class_method with returned symbol of def

This commit is contained in:
Benjamin Neff 2016-06-20 03:09:52 +02:00
parent bb0d7e46dc
commit 7bdf4921fd
3 changed files with 16 additions and 32 deletions

View file

@ -52,10 +52,9 @@ class Notification < ActiveRecord::Base
create(recipient_id: recipient_id, target: target, actors: [actor]) create(recipient_id: recipient_id, target: target, actors: [actor])
end end
def self.suppress_notification?(recipient, post) private_class_method def self.suppress_notification?(recipient, post)
post.is_a?(Post) && recipient.is_shareable_hidden?(post) post.is_a?(Post) && recipient.is_shareable_hidden?(post)
end end
private_class_method :suppress_notification?
def self.types def self.types
{ {

View file

@ -17,13 +17,12 @@ module Notifications
end end
end end
def self.notify_message(message) private_class_method def self.notify_message(message)
recipient_ids = message.conversation.participants.local.where.not(id: message.author_id).pluck(:owner_id) recipient_ids = message.conversation.participants.local.where.not(id: message.author_id).pluck(:owner_id)
User.where(id: recipient_ids).find_each do |recipient| User.where(id: recipient_ids).find_each do |recipient|
message.increase_unread(recipient) message.increase_unread(recipient)
new(recipient: recipient).email_the_user(message, message.author) new(recipient: recipient).email_the_user(message, message.author)
end end
end end
private_class_method :notify_message
end end
end end

View file

@ -161,21 +161,19 @@ module Diaspora
end end
end end
def self.author_of(entity) private_class_method def self.author_of(entity)
Person.find_by(diaspora_handle: entity.author) Person.find_by(diaspora_handle: entity.author)
end end
private_class_method :author_of
def self.build_location(entity) private_class_method def self.build_location(entity)
Location.new( Location.new(
address: entity.address, address: entity.address,
lat: entity.lat, lat: entity.lat,
lng: entity.lng lng: entity.lng
) )
end end
private_class_method :build_location
def self.build_message(entity) private_class_method def self.build_message(entity)
Message.new( Message.new(
author: author_of(entity), author: author_of(entity),
guid: entity.guid, guid: entity.guid,
@ -184,9 +182,8 @@ module Diaspora
conversation_guid: entity.conversation_guid conversation_guid: entity.conversation_guid
) )
end end
private_class_method :build_message
def self.build_poll(entity) private_class_method def self.build_poll(entity)
Poll.new( Poll.new(
guid: entity.guid, guid: entity.guid,
question: entity.question question: entity.question
@ -199,9 +196,8 @@ module Diaspora
end end
end end
end end
private_class_method :build_poll
def self.save_message(entity) private_class_method def self.save_message(entity)
ignore_existing_guid(Message, entity.guid, author_of(entity)) do ignore_existing_guid(Message, entity.guid, author_of(entity)) do
build_message(entity).tap do |message| build_message(entity).tap do |message|
message.author_signature = entity.author_signature if message.conversation.author.local? message.author_signature = entity.author_signature if message.conversation.author.local?
@ -209,9 +205,8 @@ module Diaspora
end end
end end
end end
private_class_method :save_message
def self.save_photo(entity) private_class_method def self.save_photo(entity)
Photo.create!( Photo.create!(
author: author_of(entity), author: author_of(entity),
guid: entity.guid, guid: entity.guid,
@ -225,14 +220,12 @@ module Diaspora
width: entity.width width: entity.width
) )
end end
private_class_method :save_photo
def self.receive_relayable(klass, entity) private_class_method def self.receive_relayable(klass, entity)
save_relayable(klass, entity) { yield }.tap {|relayable| relay_relayable(relayable) if relayable } save_relayable(klass, entity) { yield }.tap {|relayable| relay_relayable(relayable) if relayable }
end end
private_class_method :receive_relayable
def self.save_relayable(klass, entity) private_class_method def self.save_relayable(klass, entity)
ignore_existing_guid(klass, entity.guid, author_of(entity)) do ignore_existing_guid(klass, entity.guid, author_of(entity)) do
yield.tap do |relayable| yield.tap do |relayable|
retract_if_author_ignored(relayable) retract_if_author_ignored(relayable)
@ -242,9 +235,8 @@ module Diaspora
end end
end end
end end
private_class_method :save_relayable
def self.save_status_message(entity) private_class_method def self.save_status_message(entity)
try_load_existing_guid(StatusMessage, entity.guid, author_of(entity)) do try_load_existing_guid(StatusMessage, entity.guid, author_of(entity)) do
StatusMessage.new( StatusMessage.new(
author: author_of(entity), author: author_of(entity),
@ -261,9 +253,8 @@ module Diaspora
end end
end end
end end
private_class_method :save_status_message
def self.retract_if_author_ignored(relayable) private_class_method def self.retract_if_author_ignored(relayable)
parent_author = relayable.parent.author.owner parent_author = relayable.parent.author.owner
return unless parent_author && parent_author.ignored_people.include?(relayable.author) return unless parent_author && parent_author.ignored_people.include?(relayable.author)
@ -272,31 +263,28 @@ module Diaspora
raise Diaspora::Federation::AuthorIgnored raise Diaspora::Federation::AuthorIgnored
end end
private_class_method :retract_if_author_ignored
def self.relay_relayable(relayable) private_class_method def self.relay_relayable(relayable)
parent_author = relayable.parent.author.owner parent_author = relayable.parent.author.owner
Diaspora::Federation::Dispatcher.defer_dispatch(parent_author, relayable) if parent_author Diaspora::Federation::Dispatcher.defer_dispatch(parent_author, relayable) if parent_author
end end
private_class_method :relay_relayable
# check if the object already exists, otherwise save it. # check if the object already exists, otherwise save it.
# if save fails (probably because of a second object received parallel), # if save fails (probably because of a second object received parallel),
# check again if an object with the same guid already exists, but maybe has a different author. # check again if an object with the same guid already exists, but maybe has a different author.
# @raise [InvalidAuthor] if the author of the existing object doesn't match # @raise [InvalidAuthor] if the author of the existing object doesn't match
def self.ignore_existing_guid(klass, guid, author) private_class_method def self.ignore_existing_guid(klass, guid, author)
yield unless klass.where(guid: guid, author_id: author.id).exists? yield unless klass.where(guid: guid, author_id: author.id).exists?
rescue => e rescue => e
raise e unless load_from_database(klass, guid, author) raise e unless load_from_database(klass, guid, author)
logger.warn "ignoring error on receive #{klass}:#{guid}: #{e.class}: #{e.message}" logger.warn "ignoring error on receive #{klass}:#{guid}: #{e.class}: #{e.message}"
end end
private_class_method :ignore_existing_guid
# try to load the object first from the DB and if not available, save it. # try to load the object first from the DB and if not available, save it.
# if save fails (probably because of a second object received parallel), # if save fails (probably because of a second object received parallel),
# try again to load it, because it is possibly there now. # try again to load it, because it is possibly there now.
# @raise [InvalidAuthor] if the author of the existing object doesn't match # @raise [InvalidAuthor] if the author of the existing object doesn't match
def self.try_load_existing_guid(klass, guid, author) private_class_method def self.try_load_existing_guid(klass, guid, author)
load_from_database(klass, guid, author) || yield load_from_database(klass, guid, author) || yield
rescue Diaspora::Federation::InvalidAuthor => e rescue Diaspora::Federation::InvalidAuthor => e
raise e # don't try loading from db twice raise e # don't try loading from db twice
@ -306,17 +294,15 @@ module Diaspora
raise e unless object raise e unless object
end end
end end
private_class_method :try_load_existing_guid
# @raise [InvalidAuthor] if the author of the loaded object doesn't match # @raise [InvalidAuthor] if the author of the loaded object doesn't match
def self.load_from_database(klass, guid, author) private_class_method def self.load_from_database(klass, guid, author)
klass.find_by(guid: guid).tap do |object| klass.find_by(guid: guid).tap do |object|
if object && object.author_id != author.id if object && object.author_id != author.id
raise Diaspora::Federation::InvalidAuthor, "#{klass}:#{guid}: #{author.diaspora_handle}" raise Diaspora::Federation::InvalidAuthor, "#{klass}:#{guid}: #{author.diaspora_handle}"
end end
end end
end end
private_class_method :load_from_database
end end
end end
end end