Commit 847b2a09e62f4353f6bda08dedccdc8018cb6261

Authored by Braulio Bhavamitra
1 parent 100608aa

Move jobs from lib to app

app/jobs/download_reported_images_job.rb 0 → 100644
... ... @@ -0,0 +1,27 @@
  1 +class DownloadReportedImagesJob < Struct.new(:abuse_report, :article)
  2 + def perform
  3 + images_paths = article.image? ? [File.join(article.profile.environment.top_url, article.public_filename(:display))] : article.body_images_paths
  4 + images_paths.each do |image_path|
  5 + image = get_image(image_path)
  6 + reported_image = ReportedImage.create!( :abuse_report => abuse_report,
  7 + :uploaded_data => image,
  8 + :filename => File.basename(image_path),
  9 + :size => image.size )
  10 + abuse_report.content = parse_content(abuse_report, image_path, reported_image)
  11 + end
  12 + abuse_report.save!
  13 + end
  14 +
  15 + def get_image(image_path)
  16 + image = ActionController::UploadedTempfile.new('reported_image')
  17 + image.write(Net::HTTP.get(URI.parse(image_path)))
  18 + image.original_path = 'tmp/' + File.basename(image_path)
  19 + image.content_type = 'image/' + File.extname(image_path).gsub('.','')
  20 + image
  21 + end
  22 +
  23 + def parse_content(report, old_path, image)
  24 + old_path = old_path.gsub(report.reporter.environment.top_url, '')
  25 + report.content.gsub(/#{old_path}/, image.public_filename)
  26 + end
  27 +end
... ...
app/jobs/get_email_contacts_job.rb 0 → 100644
... ... @@ -0,0 +1,11 @@
  1 +class GetEmailContactsJob < Struct.new(:import_from, :login, :password, :contact_list_id)
  2 + def perform
  3 + begin
  4 + Invitation.get_contacts(import_from, login, password, contact_list_id)
  5 + rescue Contacts::AuthenticationError => ex
  6 + ContactList.exists?(contact_list_id) && ContactList.find(contact_list_id).register_auth_error
  7 + rescue Exception => ex
  8 + ContactList.exists?(contact_list_id) && ContactList.find(contact_list_id).register_error
  9 + end
  10 + end
  11 +end
... ...
app/jobs/invitation_job.rb 0 → 100644
... ... @@ -0,0 +1,10 @@
  1 +class InvitationJob < Struct.new(:person_id, :contacts_to_invite, :message, :profile_id, :contact_list_id, :locale)
  2 + def perform
  3 + Noosfero.with_locale(locale) do
  4 + person = Person.find(person_id)
  5 + profile = Profile.find(profile_id)
  6 + Invitation.invite(person, contacts_to_invite, message, profile)
  7 + ContactList.exists?(contact_list_id) && ContactList.find(contact_list_id).destroy
  8 + end
  9 + end
  10 +end
... ...
app/jobs/log_memory_consumption_job.rb 0 → 100644
... ... @@ -0,0 +1,22 @@
  1 +class LogMemoryConsumptionJob < Struct.new(:last_stat)
  2 + # Number of entries do display
  3 + N = 20
  4 +
  5 + def perform
  6 + logpath = File.join(Rails.root, 'log', "#{ENV['RAILS_ENV']}_memory_consumption.log")
  7 + logger = Logger.new(logpath)
  8 + stats = Hash.new(0)
  9 + ObjectSpace.each_object {|o| stats[o.class.to_s] += 1}
  10 + i = 1
  11 +
  12 + logger << "[#{Time.now.strftime('%F %T %z')}]\n"
  13 + stats.sort {|(k1,v1),(k2,v2)| v2 <=> v1}.each do |k,v|
  14 + logger << (sprintf "%-60s %10d", k, v)
  15 + logger << (sprintf " | delta %10d", (v - last_stat[k])) if last_stat && last_stat[k]
  16 + logger << "\n"
  17 + break if i > N
  18 + i += 1
  19 + end
  20 + logger << "\n"
  21 + end
  22 +end
... ...
app/jobs/mailing_job.rb 0 → 100644
... ... @@ -0,0 +1,8 @@
  1 +class MailingJob < Struct.new(:mailing_id)
  2 + def perform
  3 + mailing = Mailing.find(mailing_id)
  4 + Noosfero.with_locale(mailing.locale) do
  5 + mailing.deliver
  6 + end
  7 + end
  8 +end
... ...
app/jobs/notify_activity_to_profiles_job.rb 0 → 100644
... ... @@ -0,0 +1,44 @@
  1 +class NotifyActivityToProfilesJob < Struct.new(:tracked_action_id)
  2 + NOTIFY_ONLY_COMMUNITY = [
  3 + 'add_member_in_community'
  4 + ]
  5 +
  6 + NOT_NOTIFY_COMMUNITY = [
  7 + 'join_community'
  8 + ]
  9 + def perform
  10 + return unless ActionTracker::Record.exists?(tracked_action_id)
  11 + tracked_action = ActionTracker::Record.find(tracked_action_id)
  12 + return unless tracked_action.user.present?
  13 + target = tracked_action.target
  14 + if target.is_a?(Community) && ( NOTIFY_ONLY_COMMUNITY.include?(tracked_action.verb) || ! target.public_profile )
  15 + ActionTrackerNotification.create(:profile_id => target.id, :action_tracker_id => tracked_action.id)
  16 + return
  17 + end
  18 +
  19 + # Notify the user
  20 + ActionTrackerNotification.create(:profile_id => tracked_action.user.id, :action_tracker_id => tracked_action.id)
  21 +
  22 + # Notify all friends
  23 + 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})")
  24 +
  25 + if tracked_action.user.is_a? Organization
  26 + ActionTrackerNotification.connection.execute "insert into action_tracker_notifications(profile_id, action_tracker_id) " +
  27 + "select distinct accessor_id, #{tracked_action.id} from role_assignments where resource_id = #{tracked_action.user.id} and resource_type='Profile' " +
  28 + 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
  29 + end
  30 +
  31 + if target.is_a?(Community)
  32 + ActionTrackerNotification.create(:profile_id => target.id, :action_tracker_id => tracked_action.id) unless NOT_NOTIFY_COMMUNITY.include?(tracked_action.verb)
  33 +
  34 + 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}")
  35 + end
  36 +
  37 + if target.is_a?(Article) && target.profile.is_a?(Community)
  38 + ActionTrackerNotification.create(:profile_id => target.profile.id, :action_tracker_id => tracked_action.id) unless NOT_NOTIFY_COMMUNITY.include?(tracked_action.verb)
  39 +
  40 + 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}")
  41 + end
  42 +
  43 + end
  44 +end
... ...
app/jobs/profile_suggestions_job.rb 0 → 100644
... ... @@ -0,0 +1,22 @@
  1 +class ProfileSuggestionsJob < Struct.new(:person_id)
  2 +
  3 + def self.exists?(person_id)
  4 + !find(person_id).empty?
  5 + end
  6 +
  7 + def self.find(person_id)
  8 + Delayed::Job.by_handler("--- !ruby/struct:ProfileSuggestionsJob\nperson_id: #{person_id}\n")
  9 + end
  10 +
  11 + def perform
  12 + logger = Delayed::Worker.logger
  13 + begin
  14 + person = Person.find(person_id)
  15 + ProfileSuggestion.calculate_suggestions(person)
  16 + UserMailer.profiles_suggestions_email(person).deliver if person.email_suggestions
  17 + rescue Exception => exception
  18 + logger.error("Error with suggestions for person ID %d: %s" % [person_id, exception.to_s])
  19 + end
  20 + end
  21 +
  22 +end
... ...
app/jobs/user_activation_job.rb 0 → 100644
... ... @@ -0,0 +1,6 @@
  1 +class UserActivationJob < Struct.new(:user_id)
  2 + def perform
  3 + user = User.find(user_id)
  4 + user.destroy unless user.activated? || user.person.is_template? || user.moderate_registration_pending?
  5 + end
  6 +end
... ...
lib/download_reported_images_job.rb
... ... @@ -1,27 +0,0 @@
1   -class DownloadReportedImagesJob < Struct.new(:abuse_report, :article)
2   - def perform
3   - images_paths = article.image? ? [File.join(article.profile.environment.top_url, article.public_filename(:display))] : article.body_images_paths
4   - images_paths.each do |image_path|
5   - image = get_image(image_path)
6   - reported_image = ReportedImage.create!( :abuse_report => abuse_report,
7   - :uploaded_data => image,
8   - :filename => File.basename(image_path),
9   - :size => image.size )
10   - abuse_report.content = parse_content(abuse_report, image_path, reported_image)
11   - end
12   - abuse_report.save!
13   - end
14   -
15   - def get_image(image_path)
16   - image = ActionController::UploadedTempfile.new('reported_image')
17   - image.write(Net::HTTP.get(URI.parse(image_path)))
18   - image.original_path = 'tmp/' + File.basename(image_path)
19   - image.content_type = 'image/' + File.extname(image_path).gsub('.','')
20   - image
21   - end
22   -
23   - def parse_content(report, old_path, image)
24   - old_path = old_path.gsub(report.reporter.environment.top_url, '')
25   - report.content.gsub(/#{old_path}/, image.public_filename)
26   - end
27   -end
lib/get_email_contacts_job.rb
... ... @@ -1,11 +0,0 @@
1   -class GetEmailContactsJob < Struct.new(:import_from, :login, :password, :contact_list_id)
2   - def perform
3   - begin
4   - Invitation.get_contacts(import_from, login, password, contact_list_id)
5   - rescue Contacts::AuthenticationError => ex
6   - ContactList.exists?(contact_list_id) && ContactList.find(contact_list_id).register_auth_error
7   - rescue Exception => ex
8   - ContactList.exists?(contact_list_id) && ContactList.find(contact_list_id).register_error
9   - end
10   - end
11   -end
lib/invitation_job.rb
... ... @@ -1,10 +0,0 @@
1   -class InvitationJob < Struct.new(:person_id, :contacts_to_invite, :message, :profile_id, :contact_list_id, :locale)
2   - def perform
3   - Noosfero.with_locale(locale) do
4   - person = Person.find(person_id)
5   - profile = Profile.find(profile_id)
6   - Invitation.invite(person, contacts_to_invite, message, profile)
7   - ContactList.exists?(contact_list_id) && ContactList.find(contact_list_id).destroy
8   - end
9   - end
10   -end
lib/log_memory_consumption_job.rb
... ... @@ -1,22 +0,0 @@
1   -class LogMemoryConsumptionJob < Struct.new(:last_stat)
2   - # Number of entries do display
3   - N = 20
4   -
5   - def perform
6   - logpath = File.join(Rails.root, 'log', "#{ENV['RAILS_ENV']}_memory_consumption.log")
7   - logger = Logger.new(logpath)
8   - stats = Hash.new(0)
9   - ObjectSpace.each_object {|o| stats[o.class.to_s] += 1}
10   - i = 1
11   -
12   - logger << "[#{Time.now.strftime('%F %T %z')}]\n"
13   - stats.sort {|(k1,v1),(k2,v2)| v2 <=> v1}.each do |k,v|
14   - logger << (sprintf "%-60s %10d", k, v)
15   - logger << (sprintf " | delta %10d", (v - last_stat[k])) if last_stat && last_stat[k]
16   - logger << "\n"
17   - break if i > N
18   - i += 1
19   - end
20   - logger << "\n"
21   - end
22   -end
lib/mailing_job.rb
... ... @@ -1,8 +0,0 @@
1   -class MailingJob < Struct.new(:mailing_id)
2   - def perform
3   - mailing = Mailing.find(mailing_id)
4   - Noosfero.with_locale(mailing.locale) do
5   - mailing.deliver
6   - end
7   - end
8   -end
lib/notify_activity_to_profiles_job.rb
... ... @@ -1,44 +0,0 @@
1   -class NotifyActivityToProfilesJob < Struct.new(:tracked_action_id)
2   - NOTIFY_ONLY_COMMUNITY = [
3   - 'add_member_in_community'
4   - ]
5   -
6   - NOT_NOTIFY_COMMUNITY = [
7   - 'join_community'
8   - ]
9   - def perform
10   - return unless ActionTracker::Record.exists?(tracked_action_id)
11   - tracked_action = ActionTracker::Record.find(tracked_action_id)
12   - return unless tracked_action.user.present?
13   - target = tracked_action.target
14   - if target.is_a?(Community) && ( NOTIFY_ONLY_COMMUNITY.include?(tracked_action.verb) || ! target.public_profile )
15   - ActionTrackerNotification.create(:profile_id => target.id, :action_tracker_id => tracked_action.id)
16   - return
17   - end
18   -
19   - # Notify the user
20   - ActionTrackerNotification.create(:profile_id => tracked_action.user.id, :action_tracker_id => tracked_action.id)
21   -
22   - # Notify all friends
23   - 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})")
24   -
25   - if tracked_action.user.is_a? Organization
26   - ActionTrackerNotification.connection.execute "insert into action_tracker_notifications(profile_id, action_tracker_id) " +
27   - "select distinct accessor_id, #{tracked_action.id} from role_assignments where resource_id = #{tracked_action.user.id} and resource_type='Profile' " +
28   - 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
29   - end
30   -
31   - if target.is_a?(Community)
32   - ActionTrackerNotification.create(:profile_id => target.id, :action_tracker_id => tracked_action.id) unless NOT_NOTIFY_COMMUNITY.include?(tracked_action.verb)
33   -
34   - 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}")
35   - end
36   -
37   - if target.is_a?(Article) && target.profile.is_a?(Community)
38   - ActionTrackerNotification.create(:profile_id => target.profile.id, :action_tracker_id => tracked_action.id) unless NOT_NOTIFY_COMMUNITY.include?(tracked_action.verb)
39   -
40   - 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}")
41   - end
42   -
43   - end
44   -end
lib/profile_suggestions_job.rb
... ... @@ -1,22 +0,0 @@
1   -class ProfileSuggestionsJob < Struct.new(:person_id)
2   -
3   - def self.exists?(person_id)
4   - !find(person_id).empty?
5   - end
6   -
7   - def self.find(person_id)
8   - Delayed::Job.by_handler("--- !ruby/struct:ProfileSuggestionsJob\nperson_id: #{person_id}\n")
9   - end
10   -
11   - def perform
12   - logger = Delayed::Worker.logger
13   - begin
14   - person = Person.find(person_id)
15   - ProfileSuggestion.calculate_suggestions(person)
16   - UserMailer.profiles_suggestions_email(person).deliver if person.email_suggestions
17   - rescue Exception => exception
18   - logger.error("Error with suggestions for person ID %d: %s" % [person_id, exception.to_s])
19   - end
20   - end
21   -
22   -end
lib/user_activation_job.rb
... ... @@ -1,6 +0,0 @@
1   -class UserActivationJob < Struct.new(:user_id)
2   - def perform
3   - user = User.find(user_id)
4   - user.destroy unless user.activated? || user.person.is_template? || user.moderate_registration_pending?
5   - end
6   -end