Skip to content

Commit

Permalink
minor optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
alexskr committed Feb 3, 2024
1 parent 12daf06 commit d0b6ad8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 31 deletions.
18 changes: 3 additions & 15 deletions lib/ontologies_linked_data/utils/notifications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,9 @@ def self.remote_ontology_pull(submission)
.gsub('%ontology_location%', LinkedData::Hypermedia.generate_links(ontology)['ui'])
.gsub('%support_contact%', LinkedData.settings.support_contact_email)
.gsub('%ui_name%', LinkedData.settings.ui_name)
recipients = []
ontology.administeredBy.each do |user|
user.bring(:email) if user.bring?(:email)
recipients << user.email
end
if !LinkedData.settings.ontoportal_admin_emails.nil? && LinkedData.settings.ontoportal_admin_emails.kind_of?(Array)
LinkedData.settings.ontoportal_admin_emails.each do |admin_email|
recipients << admin_email
end
end

Notifier.notify_mails_grouped subject, body, [Notifier.ontology_admin_emails(ontology) + Notifier.ontoportal_admin_emails]
Notifier.notify_ontoportal_admins_grouped subject, body
Notifier.notify_administrators_grouped subject, body, ontology
end

def self.new_user(user)
Expand All @@ -83,7 +74,6 @@ def self.new_user(user)
.gsub('%email%', user.email.to_s)
.gsub('%site_url%', LinkedData.settings.ui_host)
.gsub('%ui_name%', LinkedData.settings.ui_name)
recipients = LinkedData.settings.ontoportal_admin_emails

Notifier.notify_ontoportal_admins_grouped subject, body

Check warning on line 78 in lib/ontologies_linked_data/utils/notifications.rb

View check run for this annotation

Codecov / codecov/patch

lib/ontologies_linked_data/utils/notifications.rb#L78

Added line #L78 was not covered by tests
end
Expand All @@ -98,7 +88,6 @@ def self.new_ontology(ont)
.gsub('%site_url%', LinkedData.settings.ui_host)
.gsub('%ont_url%', LinkedData::Hypermedia.generate_links(ont)['ui'])
.gsub('%ui_name%', LinkedData.settings.ui_name)
recipients = LinkedData.settings.ontoportal_admin_emails

Notifier.notify_ontoportal_admins_grouped subject, body

Check warning on line 92 in lib/ontologies_linked_data/utils/notifications.rb

View check run for this annotation

Codecov / codecov/patch

lib/ontologies_linked_data/utils/notifications.rb#L92

Added line #L92 was not covered by tests
end
Expand All @@ -108,7 +97,7 @@ def self.reset_password(user, token)
subject = "[#{ui_name}] User #{user.username} password reset"
password_url = "https://#{LinkedData.settings.ui_host}/reset_password?tk=#{token}&em=#{CGI.escape(user.email)}&un=#{CGI.escape(user.username)}"

Check warning on line 98 in lib/ontologies_linked_data/utils/notifications.rb

View check run for this annotation

Codecov / codecov/patch

lib/ontologies_linked_data/utils/notifications.rb#L96-L98

Added lines #L96 - L98 were not covered by tests

body = REST_PASSWORD.gsub('%ui_name%', ui_name)
body = REST_PASSWORD.gsub('%ui_name%', ui_name)

Check warning on line 100 in lib/ontologies_linked_data/utils/notifications.rb

View check run for this annotation

Codecov / codecov/patch

lib/ontologies_linked_data/utils/notifications.rb#L100

Added line #L100 was not covered by tests
.gsub('%username%', user.username.to_s)
.gsub('%password_url%', password_url.to_s)

Expand Down Expand Up @@ -212,7 +201,6 @@ def self.obofoundry_sync(missing_onts, obsolete_onts)
Thanks,<br/>
%ui_name% Team
HTML

end
end
end
14 changes: 7 additions & 7 deletions lib/ontologies_linked_data/utils/notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ def self.notify(options = {})
})
end

def self.administrative_notifications_enabled?
LinkedData.settings.enable_administrative_notifications
end

def self.notify_ontoportal_admins_grouped(subject, body)
notify_mails_grouped subject, body, ontoportal_admin_emails if LinkedData.settings.enable_administrative_notifications
notify_mails_grouped subject, body, ontoportal_admin_emails if administrative_notifications_enabled?
end

def self.notify_subscribed_separately(subject, body, ontology, notification_type)
Expand Down Expand Up @@ -59,7 +63,7 @@ def self.notify_mails_grouped(subject, body, mail)
end

def self.notify_ontoportal_admins(subject, body)
notify_mails_grouped subject, body, ontoportal_admin_emails if LinkedData.settings.enable_administrative_notifications
notify_mails_grouped subject, body, ontoportal_admin_emails if administrative_notifications_enabled?

Check warning on line 66 in lib/ontologies_linked_data/utils/notifier.rb

View check run for this annotation

Codecov / codecov/patch

lib/ontologies_linked_data/utils/notifier.rb#L66

Added line #L66 was not covered by tests
end

def self.ontology_admin_emails(ontology)
Expand All @@ -73,19 +77,15 @@ def self.ontology_admin_emails(ontology)
end

def self.ontoportal_admin_emails
return LinkedData.settings.ontoportal_admin_emails if LinkedData.settings.ontoportal_admin_emails.is_a?(Array)

if !LinkedData.settings.ontoportal_admin_emails.nil? &&
LinkedData.settings.ontoportal_admin_emails.kind_of?(Array)
return LinkedData.settings.ontoportal_admin_emails
end
[]

Check warning on line 82 in lib/ontologies_linked_data/utils/notifier.rb

View check run for this annotation

Codecov / codecov/patch

lib/ontologies_linked_data/utils/notifier.rb#L82

Added line #L82 was not covered by tests
end

def self.subscribed_users_mails(ontology, notification_type)
emails = []
ontology.bring(:subscriptions) if ontology.bring?(:subscriptions)
ontology.subscriptions.each do |subscription|

subscription.bring(:notification_type) if subscription.bring?(:notification_type)
subscription.notification_type.bring(:type) if subscription.notification_type.bring?(:notification_type)

Expand Down
14 changes: 5 additions & 9 deletions test/util/test_notifications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def self.after_suite

def setup
LinkedData.settings.email_disable_override = true
reset_mailer
end

def _subscription(ont)
Expand Down Expand Up @@ -82,13 +83,11 @@ def test_new_note_notification
note.save
assert_match "[#{@@ui_name} Notes]", last_email_sent.subject
assert_equal [@@user.email], last_email_sent.to
reset_mailer
ensure
note.delete if note
end

def test_processing_complete_notification
reset_mailer
options = { ont_count: 1, submission_count: 1, acronym: "NOTIFY" }
ont = LinkedData::SampleData::Ontology.create_ontologies_and_submissions(options)[2].first
subscription = _subscription(ont)
Expand All @@ -114,7 +113,6 @@ def test_processing_complete_notification
end

def test_disable_administrative_notifications
reset_mailer
LinkedData.settings.enable_administrative_notifications = false
options = { ont_count: 1, submission_count: 1, acronym: "DONTNOTIFY" }
ont = LinkedData::SampleData::Ontology.create_ontologies_and_submissions(options)[2].first
Expand All @@ -130,7 +128,6 @@ def test_disable_administrative_notifications
ont.delete if ont
end


def test_remote_ontology_pull_notification
recipients = ["[email protected]"]
ont_count, acronyms, ontologies = LinkedData::SampleData::Ontology.create_ontologies_and_submissions(ont_count: 1, submission_count: 1, process_submission: false)
Expand All @@ -153,12 +150,11 @@ def test_remote_ontology_pull_notification
assert sub.valid?, sub.errors
LinkedData::Utils::Notifications.remote_ontology_pull(sub)

admin_mails = LinkedData::Utils::Notifier.ontology_admin_emails(ont)

assert_includes "[#{@@ui_name}] Load from URL failure for #{ont.name}", last_email_sent.subject
recipients = @@support_mails
ont_admins.each do |user|
recipients << user.email
end
assert_equal recipients.sort, last_email_sent.to.sort
assert_equal @@support_mails, all_emails.first.to
assert_equal admin_mails, last_email_sent.to.sort
ensure
ont_admins.each do |user|
user.delete if user
Expand Down

0 comments on commit d0b6ad8

Please sign in to comment.