diff --git a/app/jobs/download_reported_images_job.rb b/app/jobs/download_reported_images_job.rb new file mode 100644 index 0000000..fdd601b --- /dev/null +++ b/app/jobs/download_reported_images_job.rb @@ -0,0 +1,27 @@ +class DownloadReportedImagesJob < Struct.new(:abuse_report, :article) + def perform + images_paths = article.image? ? [File.join(article.profile.environment.top_url, article.public_filename(:display))] : article.body_images_paths + images_paths.each do |image_path| + image = get_image(image_path) + reported_image = ReportedImage.create!( :abuse_report => abuse_report, + :uploaded_data => image, + :filename => File.basename(image_path), + :size => image.size ) + abuse_report.content = parse_content(abuse_report, image_path, reported_image) + end + abuse_report.save! + end + + def get_image(image_path) + image = ActionController::UploadedTempfile.new('reported_image') + image.write(Net::HTTP.get(URI.parse(image_path))) + image.original_path = 'tmp/' + File.basename(image_path) + image.content_type = 'image/' + File.extname(image_path).gsub('.','') + image + end + + def parse_content(report, old_path, image) + old_path = old_path.gsub(report.reporter.environment.top_url, '') + report.content.gsub(/#{old_path}/, image.public_filename) + end +end diff --git a/app/jobs/get_email_contacts_job.rb b/app/jobs/get_email_contacts_job.rb new file mode 100644 index 0000000..f911fb4 --- /dev/null +++ b/app/jobs/get_email_contacts_job.rb @@ -0,0 +1,11 @@ +class GetEmailContactsJob < Struct.new(:import_from, :login, :password, :contact_list_id) + def perform + begin + Invitation.get_contacts(import_from, login, password, contact_list_id) + rescue Contacts::AuthenticationError => ex + ContactList.exists?(contact_list_id) && ContactList.find(contact_list_id).register_auth_error + rescue Exception => ex + ContactList.exists?(contact_list_id) && ContactList.find(contact_list_id).register_error + end + end +end diff --git a/app/jobs/invitation_job.rb b/app/jobs/invitation_job.rb new file mode 100644 index 0000000..c42fbb8 --- /dev/null +++ b/app/jobs/invitation_job.rb @@ -0,0 +1,10 @@ +class InvitationJob < Struct.new(:person_id, :contacts_to_invite, :message, :profile_id, :contact_list_id, :locale) + def perform + Noosfero.with_locale(locale) do + person = Person.find(person_id) + profile = Profile.find(profile_id) + Invitation.invite(person, contacts_to_invite, message, profile) + ContactList.exists?(contact_list_id) && ContactList.find(contact_list_id).destroy + end + end +end diff --git a/app/jobs/log_memory_consumption_job.rb b/app/jobs/log_memory_consumption_job.rb new file mode 100644 index 0000000..eb7278b --- /dev/null +++ b/app/jobs/log_memory_consumption_job.rb @@ -0,0 +1,22 @@ +class LogMemoryConsumptionJob < Struct.new(:last_stat) + # Number of entries do display + N = 20 + + def perform + logpath = File.join(Rails.root, 'log', "#{ENV['RAILS_ENV']}_memory_consumption.log") + logger = Logger.new(logpath) + stats = Hash.new(0) + ObjectSpace.each_object {|o| stats[o.class.to_s] += 1} + i = 1 + + logger << "[#{Time.now.strftime('%F %T %z')}]\n" + stats.sort {|(k1,v1),(k2,v2)| v2 <=> v1}.each do |k,v| + logger << (sprintf "%-60s %10d", k, v) + logger << (sprintf " | delta %10d", (v - last_stat[k])) if last_stat && last_stat[k] + logger << "\n" + break if i > N + i += 1 + end + logger << "\n" + end +end diff --git a/app/jobs/mailing_job.rb b/app/jobs/mailing_job.rb new file mode 100644 index 0000000..04d6b32 --- /dev/null +++ b/app/jobs/mailing_job.rb @@ -0,0 +1,8 @@ +class MailingJob < Struct.new(:mailing_id) + def perform + mailing = Mailing.find(mailing_id) + Noosfero.with_locale(mailing.locale) do + mailing.deliver + end + end +end diff --git a/app/jobs/notify_activity_to_profiles_job.rb b/app/jobs/notify_activity_to_profiles_job.rb new file mode 100644 index 0000000..b9a5a80 --- /dev/null +++ b/app/jobs/notify_activity_to_profiles_job.rb @@ -0,0 +1,44 @@ +class NotifyActivityToProfilesJob < Struct.new(:tracked_action_id) + NOTIFY_ONLY_COMMUNITY = [ + 'add_member_in_community' + ] + + NOT_NOTIFY_COMMUNITY = [ + 'join_community' + ] + def perform + return unless ActionTracker::Record.exists?(tracked_action_id) + tracked_action = ActionTracker::Record.find(tracked_action_id) + return unless tracked_action.user.present? + target = tracked_action.target + if target.is_a?(Community) && ( NOTIFY_ONLY_COMMUNITY.include?(tracked_action.verb) || ! target.public_profile ) + ActionTrackerNotification.create(:profile_id => target.id, :action_tracker_id => tracked_action.id) + return + end + + # Notify the user + ActionTrackerNotification.create(:profile_id => tracked_action.user.id, :action_tracker_id => tracked_action.id) + + # Notify all friends + ActionTrackerNotification.connection.execute("insert into action_tracker_notifications(profile_id, action_tracker_id) select f.friend_id, #{tracked_action.id} from friendships as f where person_id=#{tracked_action.user.id} and f.friend_id not in (select atn.profile_id from action_tracker_notifications as atn where atn.action_tracker_id = #{tracked_action.id})") + + if tracked_action.user.is_a? Organization + ActionTrackerNotification.connection.execute "insert into action_tracker_notifications(profile_id, action_tracker_id) " + + "select distinct accessor_id, #{tracked_action.id} from role_assignments where resource_id = #{tracked_action.user.id} and resource_type='Profile' " + + if tracked_action.user.is_a? Enterprise then "union select distinct person_id, #{tracked_action.id} from favorite_enterprise_people where enterprise_id = #{tracked_action.user.id}" else "" end + end + + if target.is_a?(Community) + ActionTrackerNotification.create(:profile_id => target.id, :action_tracker_id => tracked_action.id) unless NOT_NOTIFY_COMMUNITY.include?(tracked_action.verb) + + ActionTrackerNotification.connection.execute("insert into action_tracker_notifications(profile_id, action_tracker_id) select distinct profiles.id, #{tracked_action.id} from role_assignments, profiles where profiles.type = 'Person' and profiles.id = role_assignments.accessor_id and profiles.id != #{tracked_action.user.id} and profiles.id not in (select atn.profile_id from action_tracker_notifications as atn where atn.action_tracker_id = #{tracked_action.id}) and role_assignments.resource_type = 'Profile' and role_assignments.resource_id = #{target.id}") + end + + if target.is_a?(Article) && target.profile.is_a?(Community) + ActionTrackerNotification.create(:profile_id => target.profile.id, :action_tracker_id => tracked_action.id) unless NOT_NOTIFY_COMMUNITY.include?(tracked_action.verb) + + ActionTrackerNotification.connection.execute("insert into action_tracker_notifications(profile_id, action_tracker_id) select distinct profiles.id, #{tracked_action.id} from role_assignments, profiles where profiles.type = 'Person' and profiles.id = role_assignments.accessor_id and profiles.id != #{tracked_action.user.id} and profiles.id not in (select atn.profile_id from action_tracker_notifications as atn where atn.action_tracker_id = #{tracked_action.id}) and role_assignments.resource_type = 'Profile' and role_assignments.resource_id = #{target.profile.id}") + end + + end +end diff --git a/app/jobs/profile_suggestions_job.rb b/app/jobs/profile_suggestions_job.rb new file mode 100644 index 0000000..f627986 --- /dev/null +++ b/app/jobs/profile_suggestions_job.rb @@ -0,0 +1,22 @@ +class ProfileSuggestionsJob < Struct.new(:person_id) + + def self.exists?(person_id) + !find(person_id).empty? + end + + def self.find(person_id) + Delayed::Job.by_handler("--- !ruby/struct:ProfileSuggestionsJob\nperson_id: #{person_id}\n") + end + + def perform + logger = Delayed::Worker.logger + begin + person = Person.find(person_id) + ProfileSuggestion.calculate_suggestions(person) + UserMailer.profiles_suggestions_email(person).deliver if person.email_suggestions + rescue Exception => exception + logger.error("Error with suggestions for person ID %d: %s" % [person_id, exception.to_s]) + end + end + +end diff --git a/app/jobs/user_activation_job.rb b/app/jobs/user_activation_job.rb new file mode 100644 index 0000000..19b1a48 --- /dev/null +++ b/app/jobs/user_activation_job.rb @@ -0,0 +1,6 @@ +class UserActivationJob < Struct.new(:user_id) + def perform + user = User.find(user_id) + user.destroy unless user.activated? || user.person.is_template? || user.moderate_registration_pending? + end +end diff --git a/lib/download_reported_images_job.rb b/lib/download_reported_images_job.rb deleted file mode 100644 index fdd601b..0000000 --- a/lib/download_reported_images_job.rb +++ /dev/null @@ -1,27 +0,0 @@ -class DownloadReportedImagesJob < Struct.new(:abuse_report, :article) - def perform - images_paths = article.image? ? [File.join(article.profile.environment.top_url, article.public_filename(:display))] : article.body_images_paths - images_paths.each do |image_path| - image = get_image(image_path) - reported_image = ReportedImage.create!( :abuse_report => abuse_report, - :uploaded_data => image, - :filename => File.basename(image_path), - :size => image.size ) - abuse_report.content = parse_content(abuse_report, image_path, reported_image) - end - abuse_report.save! - end - - def get_image(image_path) - image = ActionController::UploadedTempfile.new('reported_image') - image.write(Net::HTTP.get(URI.parse(image_path))) - image.original_path = 'tmp/' + File.basename(image_path) - image.content_type = 'image/' + File.extname(image_path).gsub('.','') - image - end - - def parse_content(report, old_path, image) - old_path = old_path.gsub(report.reporter.environment.top_url, '') - report.content.gsub(/#{old_path}/, image.public_filename) - end -end diff --git a/lib/get_email_contacts_job.rb b/lib/get_email_contacts_job.rb deleted file mode 100644 index f911fb4..0000000 --- a/lib/get_email_contacts_job.rb +++ /dev/null @@ -1,11 +0,0 @@ -class GetEmailContactsJob < Struct.new(:import_from, :login, :password, :contact_list_id) - def perform - begin - Invitation.get_contacts(import_from, login, password, contact_list_id) - rescue Contacts::AuthenticationError => ex - ContactList.exists?(contact_list_id) && ContactList.find(contact_list_id).register_auth_error - rescue Exception => ex - ContactList.exists?(contact_list_id) && ContactList.find(contact_list_id).register_error - end - end -end diff --git a/lib/invitation_job.rb b/lib/invitation_job.rb deleted file mode 100644 index c42fbb8..0000000 --- a/lib/invitation_job.rb +++ /dev/null @@ -1,10 +0,0 @@ -class InvitationJob < Struct.new(:person_id, :contacts_to_invite, :message, :profile_id, :contact_list_id, :locale) - def perform - Noosfero.with_locale(locale) do - person = Person.find(person_id) - profile = Profile.find(profile_id) - Invitation.invite(person, contacts_to_invite, message, profile) - ContactList.exists?(contact_list_id) && ContactList.find(contact_list_id).destroy - end - end -end diff --git a/lib/log_memory_consumption_job.rb b/lib/log_memory_consumption_job.rb deleted file mode 100644 index eb7278b..0000000 --- a/lib/log_memory_consumption_job.rb +++ /dev/null @@ -1,22 +0,0 @@ -class LogMemoryConsumptionJob < Struct.new(:last_stat) - # Number of entries do display - N = 20 - - def perform - logpath = File.join(Rails.root, 'log', "#{ENV['RAILS_ENV']}_memory_consumption.log") - logger = Logger.new(logpath) - stats = Hash.new(0) - ObjectSpace.each_object {|o| stats[o.class.to_s] += 1} - i = 1 - - logger << "[#{Time.now.strftime('%F %T %z')}]\n" - stats.sort {|(k1,v1),(k2,v2)| v2 <=> v1}.each do |k,v| - logger << (sprintf "%-60s %10d", k, v) - logger << (sprintf " | delta %10d", (v - last_stat[k])) if last_stat && last_stat[k] - logger << "\n" - break if i > N - i += 1 - end - logger << "\n" - end -end diff --git a/lib/mailing_job.rb b/lib/mailing_job.rb deleted file mode 100644 index 04d6b32..0000000 --- a/lib/mailing_job.rb +++ /dev/null @@ -1,8 +0,0 @@ -class MailingJob < Struct.new(:mailing_id) - def perform - mailing = Mailing.find(mailing_id) - Noosfero.with_locale(mailing.locale) do - mailing.deliver - end - end -end diff --git a/lib/notify_activity_to_profiles_job.rb b/lib/notify_activity_to_profiles_job.rb deleted file mode 100644 index b9a5a80..0000000 --- a/lib/notify_activity_to_profiles_job.rb +++ /dev/null @@ -1,44 +0,0 @@ -class NotifyActivityToProfilesJob < Struct.new(:tracked_action_id) - NOTIFY_ONLY_COMMUNITY = [ - 'add_member_in_community' - ] - - NOT_NOTIFY_COMMUNITY = [ - 'join_community' - ] - def perform - return unless ActionTracker::Record.exists?(tracked_action_id) - tracked_action = ActionTracker::Record.find(tracked_action_id) - return unless tracked_action.user.present? - target = tracked_action.target - if target.is_a?(Community) && ( NOTIFY_ONLY_COMMUNITY.include?(tracked_action.verb) || ! target.public_profile ) - ActionTrackerNotification.create(:profile_id => target.id, :action_tracker_id => tracked_action.id) - return - end - - # Notify the user - ActionTrackerNotification.create(:profile_id => tracked_action.user.id, :action_tracker_id => tracked_action.id) - - # Notify all friends - ActionTrackerNotification.connection.execute("insert into action_tracker_notifications(profile_id, action_tracker_id) select f.friend_id, #{tracked_action.id} from friendships as f where person_id=#{tracked_action.user.id} and f.friend_id not in (select atn.profile_id from action_tracker_notifications as atn where atn.action_tracker_id = #{tracked_action.id})") - - if tracked_action.user.is_a? Organization - ActionTrackerNotification.connection.execute "insert into action_tracker_notifications(profile_id, action_tracker_id) " + - "select distinct accessor_id, #{tracked_action.id} from role_assignments where resource_id = #{tracked_action.user.id} and resource_type='Profile' " + - if tracked_action.user.is_a? Enterprise then "union select distinct person_id, #{tracked_action.id} from favorite_enterprise_people where enterprise_id = #{tracked_action.user.id}" else "" end - end - - if target.is_a?(Community) - ActionTrackerNotification.create(:profile_id => target.id, :action_tracker_id => tracked_action.id) unless NOT_NOTIFY_COMMUNITY.include?(tracked_action.verb) - - ActionTrackerNotification.connection.execute("insert into action_tracker_notifications(profile_id, action_tracker_id) select distinct profiles.id, #{tracked_action.id} from role_assignments, profiles where profiles.type = 'Person' and profiles.id = role_assignments.accessor_id and profiles.id != #{tracked_action.user.id} and profiles.id not in (select atn.profile_id from action_tracker_notifications as atn where atn.action_tracker_id = #{tracked_action.id}) and role_assignments.resource_type = 'Profile' and role_assignments.resource_id = #{target.id}") - end - - if target.is_a?(Article) && target.profile.is_a?(Community) - ActionTrackerNotification.create(:profile_id => target.profile.id, :action_tracker_id => tracked_action.id) unless NOT_NOTIFY_COMMUNITY.include?(tracked_action.verb) - - ActionTrackerNotification.connection.execute("insert into action_tracker_notifications(profile_id, action_tracker_id) select distinct profiles.id, #{tracked_action.id} from role_assignments, profiles where profiles.type = 'Person' and profiles.id = role_assignments.accessor_id and profiles.id != #{tracked_action.user.id} and profiles.id not in (select atn.profile_id from action_tracker_notifications as atn where atn.action_tracker_id = #{tracked_action.id}) and role_assignments.resource_type = 'Profile' and role_assignments.resource_id = #{target.profile.id}") - end - - end -end diff --git a/lib/profile_suggestions_job.rb b/lib/profile_suggestions_job.rb deleted file mode 100644 index f627986..0000000 --- a/lib/profile_suggestions_job.rb +++ /dev/null @@ -1,22 +0,0 @@ -class ProfileSuggestionsJob < Struct.new(:person_id) - - def self.exists?(person_id) - !find(person_id).empty? - end - - def self.find(person_id) - Delayed::Job.by_handler("--- !ruby/struct:ProfileSuggestionsJob\nperson_id: #{person_id}\n") - end - - def perform - logger = Delayed::Worker.logger - begin - person = Person.find(person_id) - ProfileSuggestion.calculate_suggestions(person) - UserMailer.profiles_suggestions_email(person).deliver if person.email_suggestions - rescue Exception => exception - logger.error("Error with suggestions for person ID %d: %s" % [person_id, exception.to_s]) - end - end - -end diff --git a/lib/user_activation_job.rb b/lib/user_activation_job.rb deleted file mode 100644 index 19b1a48..0000000 --- a/lib/user_activation_job.rb +++ /dev/null @@ -1,6 +0,0 @@ -class UserActivationJob < Struct.new(:user_id) - def perform - user = User.find(user_id) - user.destroy unless user.activated? || user.person.is_template? || user.moderate_registration_pending? - end -end -- libgit2 0.21.2