From 574da21aec74b1da48aac4f2680482abfcfcfb63 Mon Sep 17 00:00:00 2001 From: Syphax Bouazzouni Date: Mon, 4 Apr 2022 14:30:15 +0200 Subject: [PATCH 01/15] auto lint refactor --- .../utils/notifications.rb | 75 ++++++++++--------- 1 file changed, 39 insertions(+), 36 deletions(-) diff --git a/lib/ontologies_linked_data/utils/notifications.rb b/lib/ontologies_linked_data/utils/notifications.rb index 340c0d5d..5ef8572a 100644 --- a/lib/ontologies_linked_data/utils/notifications.rb +++ b/lib/ontologies_linked_data/utils/notifications.rb @@ -7,10 +7,10 @@ class Notifications def self.notify(options = {}) return unless LinkedData.settings.enable_notifications - headers = { 'Content-Type' => 'text/html' } - sender = options[:sender] || LinkedData.settings.email_sender + headers = { 'Content-Type' => 'text/html' } + sender = options[:sender] || LinkedData.settings.email_sender recipients = options[:recipients] - raise ArgumentError, "Recipient needs to be provided in options[:recipients]" if !recipients || recipients.empty? + raise ArgumentError, 'Recipient needs to be provided in options[:recipients]' if !recipients || recipients.empty? # By default we override all recipients to avoid # sending emails from testing environments. @@ -22,20 +22,22 @@ def self.notify(options = {}) end Pony.mail({ - to: recipients, - from: sender, - subject: options[:subject], - body: options[:body], - headers: headers, - via: :smtp, - enable_starttls_auto: LinkedData.settings.enable_starttls_auto, - via_options: mail_options - }) + to: recipients, + from: sender, + subject: options[:subject], + body: options[:body], + headers: headers, + via: :smtp, + enable_starttls_auto: LinkedData.settings.enable_starttls_auto, + via_options: mail_options + }) end def self.new_note(note) note.bring_remaining note.creator.bring(:username) if note.creator.bring?(:username) + ontologies = note.relatedOntology.map {|o| o.name}.join(", ") + note.relatedOntology.each {|o| o.bring(:name) if o.bring?(:name); o.bring(:subscriptions) if o.bring?(:subscriptions)} ontologies = note.relatedOntology.map {|o| o.name}.join(", ") subject = "[BioPortal Notes] [#{ontologies}] #{note.subject}" @@ -44,6 +46,7 @@ def self.new_note(note) .gsub("%note_url%", LinkedData::Hypermedia.generate_links(note)["ui"]) .gsub("%note_subject%", note.subject || "") .gsub("%note_body%", note.body || "") + options = { ontologies: note.relatedOntology, @@ -58,15 +61,15 @@ def self.submission_processed(submission) submission.bring_remaining ontology = submission.ontology ontology.bring(:name, :acronym) - result = submission.ready? ? "Success" : "Failure" + result = submission.ready? ? 'Success' : 'Failure' status = LinkedData::Models::SubmissionStatus.readable_statuses(submission.submissionStatus) subject = "[BioPortal] #{ontology.name} Parsing #{result}" - body = SUBMISSION_PROCESSED.gsub("%ontology_name%", ontology.name) - .gsub("%ontology_acronym%", ontology.acronym) - .gsub("%statuses%", status.join("
")) - .gsub("%admin_email%", LinkedData.settings.email_sender) - .gsub("%ontology_location%", LinkedData::Hypermedia.generate_links(ontology)["ui"]) + body = SUBMISSION_PROCESSED.gsub('%ontology_name%', ontology.name) + .gsub('%ontology_acronym%', ontology.acronym) + .gsub('%statuses%', status.join('
')) + .gsub('%admin_email%', LinkedData.settings.email_sender) + .gsub('%ontology_location%', LinkedData::Hypermedia.generate_links(ontology)['ui']) options = { ontologies: ontology, @@ -83,10 +86,10 @@ def self.remote_ontology_pull(submission) ontology.bring(:name, :acronym, :administeredBy) subject = "[BioPortal] Load from URL failure for #{ontology.name}" - body = REMOTE_PULL_FAILURE.gsub("%ont_pull_location%", submission.pullLocation.to_s) - .gsub("%ont_name%", ontology.name) - .gsub("%ont_acronym%", ontology.acronym) - .gsub("%ontology_location%", LinkedData::Hypermedia.generate_links(ontology)["ui"]) + body = REMOTE_PULL_FAILURE.gsub('%ont_pull_location%', submission.pullLocation.to_s) + .gsub('%ont_name%', ontology.name) + .gsub('%ont_acronym%', ontology.acronym) + .gsub('%ontology_location%', LinkedData::Hypermedia.generate_links(ontology)['ui']) recipients = [] ontology.administeredBy.each do |user| user.bring(:email) if user.bring?(:email) @@ -94,9 +97,9 @@ def self.remote_ontology_pull(submission) end options = { - subject: subject, - body: body, - recipients: recipients + subject: subject, + body: body, + recipients: recipients } notify(options) end @@ -125,24 +128,24 @@ def self.reset_password(user, token) end def self.obofoundry_sync(missing_onts, obsolete_onts) - body = "" + body = '' if missing_onts.size > 0 - body << "The following OBO Library ontologies are missing from BioPortal:

" + body << 'The following OBO Library ontologies are missing from BioPortal:

' missing_onts.each do |ont| body << "#{ont['id']} (#{ont['title']})

" end end if obsolete_onts.size > 0 - body << "The following OBO Library ontologies have been deprecated:

" + body << 'The following OBO Library ontologies have been deprecated:

' obsolete_onts.each do |ont| body << "#{ont['id']} (#{ont['title']})

" end end if body.empty? - body << "BioPortal and the OBO Foundry are in sync.

" + body << 'BioPortal and the OBO Foundry are in sync.

' end options = { @@ -185,22 +188,22 @@ def self.send_ontology_notifications(options = {}) def self.mail_options options = { address: LinkedData.settings.smtp_host, - port: LinkedData.settings.smtp_port, - domain: LinkedData.settings.smtp_domain # the HELO domain provided by the client to the server + port: LinkedData.settings.smtp_port, + domain: LinkedData.settings.smtp_domain # the HELO domain provided by the client to the server } if LinkedData.settings.smtp_auth_type && LinkedData.settings.smtp_auth_type != :none options.merge({ - user_name: LinkedData.settings.smtp_user, - password: LinkedData.settings.smtp_password, - authentication: LinkedData.settings.smtp_auth_type - }) + user_name: LinkedData.settings.smtp_user, + password: LinkedData.settings.smtp_password, + authentication: LinkedData.settings.smtp_auth_type + }) end return options end -NEW_NOTE = <%username%.

----------------------------------------------------------------------------------
From b7a48565e153fa7d81ddc52ea5853b316ac0a75d Mon Sep 17 00:00:00 2001 From: Syphax Bouazzouni Date: Mon, 4 Apr 2022 14:38:10 +0200 Subject: [PATCH 02/15] remove the Bioportal harder coded constant in notification subjects --- lib/ontologies_linked_data/utils/notifications.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/ontologies_linked_data/utils/notifications.rb b/lib/ontologies_linked_data/utils/notifications.rb index 5ef8572a..716e140e 100644 --- a/lib/ontologies_linked_data/utils/notifications.rb +++ b/lib/ontologies_linked_data/utils/notifications.rb @@ -40,7 +40,7 @@ def self.new_note(note) note.relatedOntology.each {|o| o.bring(:name) if o.bring?(:name); o.bring(:subscriptions) if o.bring?(:subscriptions)} ontologies = note.relatedOntology.map {|o| o.name}.join(", ") - subject = "[BioPortal Notes] [#{ontologies}] #{note.subject}" + subject = "[#{LinkedData.settings.ui_host} Notes] [#{ontologies}] #{note.subject}" body = NEW_NOTE.gsub("%username%", note.creator.username) .gsub("%ontologies%", ontologies) .gsub("%note_url%", LinkedData::Hypermedia.generate_links(note)["ui"]) @@ -64,7 +64,7 @@ def self.submission_processed(submission) result = submission.ready? ? 'Success' : 'Failure' status = LinkedData::Models::SubmissionStatus.readable_statuses(submission.submissionStatus) - subject = "[BioPortal] #{ontology.name} Parsing #{result}" + subject = "[#{LinkedData.settings.ui_host}] #{ontology.name} Parsing #{result}" body = SUBMISSION_PROCESSED.gsub('%ontology_name%', ontology.name) .gsub('%ontology_acronym%', ontology.acronym) .gsub('%statuses%', status.join('
')) @@ -85,7 +85,7 @@ def self.remote_ontology_pull(submission) ontology = submission.ontology ontology.bring(:name, :acronym, :administeredBy) - subject = "[BioPortal] Load from URL failure for #{ontology.name}" + subject = "[#{LinkedData.settings.ui_host}] Load from URL failure for #{ontology.name}" body = REMOTE_PULL_FAILURE.gsub('%ont_pull_location%', submission.pullLocation.to_s) .gsub('%ont_name%', ontology.name) .gsub('%ont_acronym%', ontology.acronym) From d837cea9f5101b2933fc471251b43acc4d84f481 Mon Sep 17 00:00:00 2001 From: Syphax Bouazzouni Date: Mon, 4 Apr 2022 14:43:43 +0200 Subject: [PATCH 03/15] send the ontology pull failure notification also to the portal admins --- lib/ontologies_linked_data/utils/notifications.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/ontologies_linked_data/utils/notifications.rb b/lib/ontologies_linked_data/utils/notifications.rb index 716e140e..da01bfba 100644 --- a/lib/ontologies_linked_data/utils/notifications.rb +++ b/lib/ontologies_linked_data/utils/notifications.rb @@ -95,11 +95,15 @@ def self.remote_ontology_pull(submission) user.bring(:email) if user.bring?(:email) recipients << user.email end + if !LinkedData.settings.admin_emails.nil? && LinkedData.settings.admin_emails.kind_of?(Array) + LinkedData.settings.admin_emails.each do |admin_email| + recipients << admin_email + end options = { - subject: subject, - body: body, - recipients: recipients + subject: subject, + body: body, + recipients: recipients } notify(options) end From 339a268f19d99e460e590a56ef96d249cefe6abf Mon Sep 17 00:00:00 2001 From: Syphax Bouazzouni Date: Mon, 4 Apr 2022 14:45:07 +0200 Subject: [PATCH 04/15] adding a notification for new users creation --- .../utils/notifications.rb | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lib/ontologies_linked_data/utils/notifications.rb b/lib/ontologies_linked_data/utils/notifications.rb index da01bfba..f97501e7 100644 --- a/lib/ontologies_linked_data/utils/notifications.rb +++ b/lib/ontologies_linked_data/utils/notifications.rb @@ -108,6 +108,23 @@ def self.remote_ontology_pull(submission) notify(options) end + def self.new_user(user) + user.bring_remaining + + subject = "[#{LinkedData.settings.ui_host}] New User: #{user.username}" + body = NEW_USER_CREATED.gsub('%username%', user.username.to_s) + .gsub('%email%', user.email.to_s) + .gsub('%site_url%', LinkedData.settings.ui_host) + recipients = LinkedData.settings.admin_emails + + options = { + subject: subject, + body: body, + recipients: recipients + } + notify(options) + end + def self.reset_password(user, token) subject = "[BioPortal] 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)}" @@ -245,6 +262,14 @@ def self.mail_options If you need further assistance, please contact us via the BioPortal support mailing list.

Thank you,
+ + NEW_USER_CREATED = < +Username: %username% +
+Email: %email% +

The BioPortal Team EOS From 402e361196f3ea8fb46a63a9ad436662c8621b5a Mon Sep 17 00:00:00 2001 From: Syphax Bouazzouni Date: Mon, 4 Apr 2022 14:45:54 +0200 Subject: [PATCH 05/15] add a notification for new ontology creation --- .../utils/notifications.rb | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/lib/ontologies_linked_data/utils/notifications.rb b/lib/ontologies_linked_data/utils/notifications.rb index f97501e7..a3fcaae2 100644 --- a/lib/ontologies_linked_data/utils/notifications.rb +++ b/lib/ontologies_linked_data/utils/notifications.rb @@ -125,6 +125,25 @@ def self.new_user(user) notify(options) end + def self.new_ontology(ont) + ont.bring_remaining + + subject = "[#{LinkedData.settings.ui_host}] New Ontology: #{ont.acronym}" + body = NEW_ONTOLOGY_CREATED.gsub('%acronym%', ont.acronym) + .gsub('%name%', ont.name.to_s) + .gsub('%addedby%', ont.administeredBy[0].to_s) + .gsub('%site_url%', LinkedData.settings.ui_host) + .gsub('%ont_url%', LinkedData::Hypermedia.generate_links(ont)['ui']) + recipients = LinkedData.settings.admin_emails + + options = { + subject: subject, + body: body, + recipients: recipients + } + notify(options) + end + def self.reset_password(user, token) subject = "[BioPortal] 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)}" @@ -271,6 +290,18 @@ def self.mail_options Email: %email%

The BioPortal Team +EOS + + NEW_ONTOLOGY_CREATED = < +Acronym: %acronym% +
+Name: %name% +
+At %ont_url% +

+The BioPortal Team EOS end From c66eaf02bcf6e3098bc732070d295e0d7c805119 Mon Sep 17 00:00:00 2001 From: Syphax Bouazzouni Date: Thu, 31 Mar 2022 16:25:46 +0200 Subject: [PATCH 06/15] removing duplicate if found in the notify recipients --- lib/ontologies_linked_data/utils/notifications.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ontologies_linked_data/utils/notifications.rb b/lib/ontologies_linked_data/utils/notifications.rb index a3fcaae2..e7080aaf 100644 --- a/lib/ontologies_linked_data/utils/notifications.rb +++ b/lib/ontologies_linked_data/utils/notifications.rb @@ -9,7 +9,7 @@ def self.notify(options = {}) headers = { 'Content-Type' => 'text/html' } sender = options[:sender] || LinkedData.settings.email_sender - recipients = options[:recipients] + recipients = Array(options[:recipients]).uniq raise ArgumentError, 'Recipient needs to be provided in options[:recipients]' if !recipients || recipients.empty? # By default we override all recipients to avoid From c18270dcb4d773b274b93db7611e4358e9ebf2ed Mon Sep 17 00:00:00 2001 From: Syphax Bouazzouni Date: Mon, 4 Apr 2022 14:54:21 +0200 Subject: [PATCH 07/15] configurize the hardcoded Bioportal name --- lib/ontologies_linked_data/config/config.rb | 5 +- .../utils/notifications.rb | 46 +++++++++++-------- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/lib/ontologies_linked_data/config/config.rb b/lib/ontologies_linked_data/config/config.rb index b5aaadf2..101da531 100644 --- a/lib/ontologies_linked_data/config/config.rb +++ b/lib/ontologies_linked_data/config/config.rb @@ -35,8 +35,9 @@ def config(&block) # @settings.redis_port ||= 6379 # ### - @settings.ui_host ||= "bioportal.bioontology.org" - @settings.replace_url_prefix ||= false + @settings.ui_name ||= 'Bioportal' + @settings.ui_host ||= 'bioportal.bioontology.org' + @settings.replace_url_prefix ||= false @settings.id_url_prefix ||= "http://data.bioontology.org/" @settings.queries_debug ||= false @settings.enable_monitoring ||= false diff --git a/lib/ontologies_linked_data/utils/notifications.rb b/lib/ontologies_linked_data/utils/notifications.rb index e7080aaf..a5e6b1ab 100644 --- a/lib/ontologies_linked_data/utils/notifications.rb +++ b/lib/ontologies_linked_data/utils/notifications.rb @@ -43,10 +43,11 @@ def self.new_note(note) subject = "[#{LinkedData.settings.ui_host} Notes] [#{ontologies}] #{note.subject}" body = NEW_NOTE.gsub("%username%", note.creator.username) .gsub("%ontologies%", ontologies) - .gsub("%note_url%", LinkedData::Hypermedia.generate_links(note)["ui"]) .gsub("%note_subject%", note.subject || "") .gsub("%note_body%", note.body || "") + .gsub("%note_url%", LinkedData::Hypermedia.generate_links(note)["ui"]) + .gsub('%ui_name%', LinkedData.settings.ui_name) options = { ontologies: note.relatedOntology, @@ -70,6 +71,7 @@ def self.submission_processed(submission) .gsub('%statuses%', status.join('
')) .gsub('%admin_email%', LinkedData.settings.email_sender) .gsub('%ontology_location%', LinkedData::Hypermedia.generate_links(ontology)['ui']) + .gsub('%ui_name%', LinkedData.settings.ui_name) options = { ontologies: ontology, @@ -90,6 +92,8 @@ def self.remote_ontology_pull(submission) .gsub('%ont_name%', ontology.name) .gsub('%ont_acronym%', ontology.acronym) .gsub('%ontology_location%', LinkedData::Hypermedia.generate_links(ontology)['ui']) + .gsub('%support_mail%', support_mails.first) + .gsub('%ui_name%', LinkedData.settings.ui_name) recipients = [] ontology.administeredBy.each do |user| user.bring(:email) if user.bring?(:email) @@ -115,6 +119,7 @@ def self.new_user(user) body = NEW_USER_CREATED.gsub('%username%', user.username.to_s) .gsub('%email%', user.email.to_s) .gsub('%site_url%', LinkedData.settings.ui_host) + .gsub('%ui_name%', LinkedData.settings.ui_name) recipients = LinkedData.settings.admin_emails options = { @@ -134,6 +139,7 @@ def self.new_ontology(ont) .gsub('%addedby%', ont.administeredBy[0].to_s) .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.admin_emails options = { @@ -145,9 +151,10 @@ def self.new_ontology(ont) end def self.reset_password(user, token) - subject = "[BioPortal] User #{user.username} password reset" + ui_name = LinkedData.settings.ui_name + 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)}" - + body = <<~HTML Someone has requested a password reset for user #{user.username}. If this was you, please click on the link below to reset your password. Otherwise, please @@ -157,6 +164,7 @@ def self.reset_password(user, token) Thanks,
BioPortal Team + #{ui_name} Team HTML options = { @@ -169,9 +177,9 @@ def self.reset_password(user, token) def self.obofoundry_sync(missing_onts, obsolete_onts) body = '' - + ui_name = LinkedData.settings.ui_name if missing_onts.size > 0 - body << 'The following OBO Library ontologies are missing from BioPortal:

' + body << "The following OBO Library ontologies are missing from #{ui_name}:

" missing_onts.each do |ont| body << "#{ont['id']} (#{ont['title']})

" end @@ -185,7 +193,7 @@ def self.obofoundry_sync(missing_onts, obsolete_onts) end if body.empty? - body << 'BioPortal and the OBO Foundry are in sync.

' + body << "#{ui_name} and the OBO Foundry are in sync.

" end options = { @@ -252,35 +260,37 @@ def self.mail_options %note_body%
----------------------------------------------------------------------------------

-You can respond by visiting: NCBO BioPortal.

+You can respond by visiting: %ui_name%.

EOS -SUBMISSION_PROCESSED = <
%statuses%

Please contact %admin_email% if you have questions.

-The ontology can be browsed in BioPortal. +The ontology can be browsed in %ui_name%.

Thank you,
-The BioPortal Team +The %ui_name% Team EOS -REMOTE_PULL_FAILURE = <
Please verify the URL you provided for daily loading of your ontology:
    -
  1. Make sure you are signed in to BioPortal.
  2. -
  3. Navigate to your ontology summary page: %ontology_location%.
  4. +
  5. Make sure you are signed in to %ui_name%.
  6. +
  7. Navigate to your ontology summary page: %ontology_location%.
  8. Click the "Edit submission information" link.
  9. In the Location row, verify that you entered a valid URL for daily loading of your ontology in the URL text area.
-If you need further assistance, please contact us via the BioPortal support mailing list. +If you need further assistance, please contact us via the %ui_name% support mailing list.

Thank you,
+The %ui_name% Team +EOS NEW_USER_CREATED = < Email: %email%

-The BioPortal Team +The %ui_name% Team EOS NEW_ONTOLOGY_CREATED = < At %ont_url%

-The BioPortal Team +The %ui_name% Team EOS end From fcd5bbde217563f939514453808183c83b922c10 Mon Sep 17 00:00:00 2001 From: Syphax Bouazzouni Date: Mon, 4 Apr 2022 14:55:35 +0200 Subject: [PATCH 08/15] fix empty admin_emails value test --- .../utils/notifications.rb | 2 +- test/util/test_notifications.rb | 35 ++++++++++++++----- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/lib/ontologies_linked_data/utils/notifications.rb b/lib/ontologies_linked_data/utils/notifications.rb index a5e6b1ab..3d7097b2 100644 --- a/lib/ontologies_linked_data/utils/notifications.rb +++ b/lib/ontologies_linked_data/utils/notifications.rb @@ -92,7 +92,7 @@ def self.remote_ontology_pull(submission) .gsub('%ont_name%', ontology.name) .gsub('%ont_acronym%', ontology.acronym) .gsub('%ontology_location%', LinkedData::Hypermedia.generate_links(ontology)['ui']) - .gsub('%support_mail%', support_mails.first) + .gsub('%support_mail%', support_mails.first || '') .gsub('%ui_name%', LinkedData.settings.ui_name) recipients = [] ontology.administeredBy.each do |user| diff --git a/test/util/test_notifications.rb b/test/util/test_notifications.rb index 8508dc1e..4d6c7430 100644 --- a/test/util/test_notifications.rb +++ b/test/util/test_notifications.rb @@ -8,8 +8,12 @@ class TestNotifications < LinkedData::TestCase def self.before_suite @@notifications_enabled = LinkedData.settings.enable_notifications @@disable_override = LinkedData.settings.email_disable_override + @@old_suppor_mails = LinkedData.settings.admin_emails + LinkedData.settings.admin_emails = ["ontoportal-support@mail.com"] if LinkedData.settings.admin_emails.empty? LinkedData.settings.email_disable_override = true LinkedData.settings.enable_notifications = true + + @@support_mails = LinkedData.settings.admin_emails @@ont = LinkedData::SampleData::Ontology.create_ontologies_and_submissions(ont_count: 1, submission_count: 1)[2].first @@ont.bring_remaining @@user = @@ont.administeredBy.first @@ -22,6 +26,7 @@ def self.before_suite def self.after_suite LinkedData.settings.enable_notifications = @@notifications_enabled LinkedData.settings.email_disable_override = @@disable_override + LinkedData.settings.admin_emails = @@old_suppor_mails @@ont.delete if defined?(@@ont) @@subscription.delete if defined?(@@subscription) @@user.delete if defined?(@@user) @@ -51,10 +56,10 @@ def test_send_notification # Disable override LinkedData.settings.email_disable_override = true LinkedData::Utils::Notifications.notify({ - recipients: recipients, - subject: subject, - body: body - }) + recipients: recipients, + subject: subject, + body: body + }) assert_equal recipients, last_email_sent.to assert_equal [LinkedData.settings.email_sender], last_email_sent.from assert_equal last_email_sent.body.raw_source, body @@ -81,14 +86,26 @@ def test_new_note_notification def test_processing_complete_notification begin - options = {ont_count: 1, submission_count: 1, acronym: "NOTIFY"} + options = { ont_count: 1, submission_count: 1, acronym: "NOTIFY" } ont = LinkedData::SampleData::Ontology.create_ontologies_and_submissions(options)[2].first subscription = _subscription(ont) @@user.subscription = @@user.subscription.dup << subscription @@user.save ont.latest_submission(status: :any).process_submission(Logger.new(TestLogFile.new)) - assert last_email_sent.subject.include?("Parsing Success") - assert_equal [@@user.email], last_email_sent.to + subscription.bring :user + mail_sent_count = subscription.user.size + 1 + mails = all_emails.last(mail_sent_count) + assert_equal mail_sent_count, mails.size # subscribed users + support mail + + first_user = subscription.user.first + first_user.bring :email + assert mails.first.subject.include?("Parsing Success") + assert_equal [first_user.email], mails.first.to + + + assert mails.last.subject.include?("Parsing Success") + assert_equal @@support_mails, mails.last.to + ensure ont.delete if ont subscription.delete if subscription @@ -117,8 +134,8 @@ def test_remote_ontology_pull_notification assert sub.valid?, sub.errors LinkedData::Utils::Notifications.remote_ontology_pull(sub) - assert last_email_sent.subject.include? "[BioPortal] Load from URL failure for #{ont.name}" - recipients = [] + assert last_email_sent.subject.include? "Load from URL failure for #{ont.name}" + recipients = @@support_mails ont_admins.each do |user| recipients << user.email end From a31727cb2aab20964430e36051aee9229fb29655 Mon Sep 17 00:00:00 2001 From: Syphax Bouazzouni Date: Mon, 4 Apr 2022 14:58:00 +0200 Subject: [PATCH 09/15] extract the notifier (sender) from the notifications (the content) --- lib/ontologies_linked_data.rb | 1 + .../utils/notifications.rb | 134 ++---------------- lib/ontologies_linked_data/utils/notifier.rb | 125 ++++++++++++++++ test/util/test_notifications.rb | 4 +- 4 files changed, 143 insertions(+), 121 deletions(-) create mode 100644 lib/ontologies_linked_data/utils/notifier.rb diff --git a/lib/ontologies_linked_data.rb b/lib/ontologies_linked_data.rb index 678e0e89..75b7c8bc 100644 --- a/lib/ontologies_linked_data.rb +++ b/lib/ontologies_linked_data.rb @@ -17,6 +17,7 @@ require "ontologies_linked_data/utils/file" require "ontologies_linked_data/utils/triples" require "ontologies_linked_data/utils/notifications" +require "ontologies_linked_data/utils/notifier" require "ontologies_linked_data/utils/ontology_csv_writer" require "ontologies_linked_data/utils/multi_logger" require "ontologies_linked_data/parser/parser" diff --git a/lib/ontologies_linked_data/utils/notifications.rb b/lib/ontologies_linked_data/utils/notifications.rb index 3d7097b2..d3b6b4dc 100644 --- a/lib/ontologies_linked_data/utils/notifications.rb +++ b/lib/ontologies_linked_data/utils/notifications.rb @@ -1,37 +1,11 @@ require 'cgi' require 'pony' -module LinkedData::Utils +module LinkedData + module Utils class Notifications - def self.notify(options = {}) - return unless LinkedData.settings.enable_notifications - - headers = { 'Content-Type' => 'text/html' } - sender = options[:sender] || LinkedData.settings.email_sender - recipients = Array(options[:recipients]).uniq - raise ArgumentError, 'Recipient needs to be provided in options[:recipients]' if !recipients || recipients.empty? - - # By default we override all recipients to avoid - # sending emails from testing environments. - # Set `email_disable_override` in production - # to send to the actual user. - unless LinkedData.settings.email_disable_override - headers['Overridden-Sender'] = recipients - recipients = LinkedData.settings.email_override - end - Pony.mail({ - to: recipients, - from: sender, - subject: options[:subject], - body: options[:body], - headers: headers, - via: :smtp, - enable_starttls_auto: LinkedData.settings.enable_starttls_auto, - via_options: mail_options - }) - end def self.new_note(note) note.bring_remaining @@ -49,13 +23,10 @@ def self.new_note(note) .gsub("%note_url%", LinkedData::Hypermedia.generate_links(note)["ui"]) .gsub('%ui_name%', LinkedData.settings.ui_name) - options = { - ontologies: note.relatedOntology, - notification_type: "NOTES", - subject: subject, - body: body - } - send_ontology_notifications(options) + + note.relatedOntology.each do |ont| + Notifier.notify_subscribed_separately subject, body, ont, 'NOTES' + end end def self.submission_processed(submission) @@ -73,13 +44,8 @@ def self.submission_processed(submission) .gsub('%ontology_location%', LinkedData::Hypermedia.generate_links(ontology)['ui']) .gsub('%ui_name%', LinkedData.settings.ui_name) - options = { - ontologies: ontology, - notification_type: "PROCESSING", - subject: subject, - body: body - } - send_ontology_notifications(options) + Notifier.notify_subscribed_separately subject, body, ontology, 'PROCESSING' + Notifier.notify_support_grouped subject, body end def self.remote_ontology_pull(submission) @@ -92,7 +58,7 @@ def self.remote_ontology_pull(submission) .gsub('%ont_name%', ontology.name) .gsub('%ont_acronym%', ontology.acronym) .gsub('%ontology_location%', LinkedData::Hypermedia.generate_links(ontology)['ui']) - .gsub('%support_mail%', support_mails.first || '') + .gsub('%support_mail%', Notifier.support_mails.first || '') .gsub('%ui_name%', LinkedData.settings.ui_name) recipients = [] ontology.administeredBy.each do |user| @@ -104,12 +70,7 @@ def self.remote_ontology_pull(submission) recipients << admin_email end - options = { - subject: subject, - body: body, - recipients: recipients - } - notify(options) + Notifier.notify_mails_grouped subject, body, [Notifier.admin_mails(ontology) + Notifier.support_mails] end def self.new_user(user) @@ -122,12 +83,7 @@ def self.new_user(user) .gsub('%ui_name%', LinkedData.settings.ui_name) recipients = LinkedData.settings.admin_emails - options = { - subject: subject, - body: body, - recipients: recipients - } - notify(options) + Notifier.notify_support_grouped subject, body end def self.new_ontology(ont) @@ -142,12 +98,7 @@ def self.new_ontology(ont) .gsub('%ui_name%', LinkedData.settings.ui_name) recipients = LinkedData.settings.admin_emails - options = { - subject: subject, - body: body, - recipients: recipients - } - notify(options) + Notifier.notify_support_grouped subject, body end def self.reset_password(user, token) @@ -166,13 +117,7 @@ def self.reset_password(user, token) BioPortal Team #{ui_name} Team HTML - - options = { - subject: subject, - body: body, - recipients: user.email - } - notify(options) + Notifier.notify_mails_separately subject, body, [user.mail] end def self.obofoundry_sync(missing_onts, obsolete_onts) @@ -196,60 +141,10 @@ def self.obofoundry_sync(missing_onts, obsolete_onts) body << "#{ui_name} and the OBO Foundry are in sync.

" end - options = { - subject: "[BioPortal] OBOFoundry synchronization report", - body: body, - recipients: LinkedData.settings.email_sender - } - notify(options) - end - - private - - ## - # This method takes a list of ontologies and a notification type, - # then looks up all the users who subscribe to that ontology/type pair - # and sends them an email with the given subject and body. - def self.send_ontology_notifications(options = {}) - ontologies = options[:ontologies] - ontologies = ontologies.is_a?(Array) ? ontologies : [ontologies] - notification_type = options[:notification_type] - subject = options[:subject] - body = options[:body] - emails = [] - ontologies.each {|o| o.bring(:subscriptions) if o.bring?(:subscriptions)} - ontologies.each do |ont| - ont.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) - next unless subscription.notification_type.type.eql?(notification_type.to_s.upcase) || subscription.notification_type.type.eql?("ALL") - subscription.bring(:user) if subscription.bring?(:user) - subscription.user.each do |user| - user.bring(:email) if user.bring?(:email) - emails << notify(recipients: user.email, subject: subject, body: body) - end - end - end - emails + Notifier.notify_mails_separately subject, body, [LinkedData.settings.email_sender] end - def self.mail_options - options = { - address: LinkedData.settings.smtp_host, - port: LinkedData.settings.smtp_port, - domain: LinkedData.settings.smtp_domain # the HELO domain provided by the client to the server - } - - if LinkedData.settings.smtp_auth_type && LinkedData.settings.smtp_auth_type != :none - options.merge({ - user_name: LinkedData.settings.smtp_user, - password: LinkedData.settings.smtp_password, - authentication: LinkedData.settings.smtp_auth_type - }) - end - return options - end NEW_NOTE = <%username%.

@@ -315,4 +210,5 @@ def self.mail_options EOS end + end end \ No newline at end of file diff --git a/lib/ontologies_linked_data/utils/notifier.rb b/lib/ontologies_linked_data/utils/notifier.rb new file mode 100644 index 00000000..c4d9ad71 --- /dev/null +++ b/lib/ontologies_linked_data/utils/notifier.rb @@ -0,0 +1,125 @@ +module LinkedData + module Utils + class Notifier + def self.notify(options = {}) + return unless LinkedData.settings.enable_notifications + + headers = { 'Content-Type' => 'text/html' } + sender = options[:sender] || LinkedData.settings.email_sender + recipients = Array(options[:recipients]).uniq + raise ArgumentError, 'Recipient needs to be provided in options[:recipients]' if !recipients || recipients.empty? + + # By default we override all recipients to avoid + # sending emails from testing environments. + # Set `email_disable_override` in production + # to send to the actual user. + unless LinkedData.settings.email_disable_override + headers['Overridden-Sender'] = recipients + recipients = LinkedData.settings.email_override + end + + Pony.mail({ + to: recipients, + from: sender, + subject: options[:subject], + body: options[:body], + headers: headers, + via: :smtp, + enable_starttls_auto: LinkedData.settings.enable_starttls_auto, + via_options: mail_options + }) + end + + def self.notify_support_grouped(subject, body) + notify_mails_grouped subject, body, support_mails + end + + def self.notify_subscribed_separately(subject, body, ontology, notification_type) + mails = subscribed_users_mails(ontology, notification_type) + notify_mails_separately subject, body, mails + end + + def self.notify_administrators_grouped(subject, body, ontology) + notify_support_grouped subject, body, admin_mails(ontology) + end + + def self.notify_mails_separately(subject, body, mails) + mails.each do |mail| + notify_mails_grouped subject, body, mail + end + end + + def self.notify_mails_grouped(subject, body, mail) + options = { + subject: subject, + body: body, + recipients: mail + } + notify(options) + end + + def self.notify_support(subject, body) + notify_mails_grouped subject, body, support_mails + end + + def self.admin_mails(ontology) + ontology.bring :administeredBy if ontology.bring? :administeredBy + recipients = [] + ontology.administeredBy.each do |user| + user.bring(:email) if user.bring?(:email) + recipients << user.email + end + recipients + end + + def self.support_mails + + if !LinkedData.settings.admin_emails.nil? && + LinkedData.settings.admin_emails.kind_of?(Array) + return LinkedData.settings.admin_emails + end + [] + 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) + + unless subscription.notification_type.type.eql?(notification_type.to_s.upcase) || + subscription.notification_type.type.eql?('ALL') + next + end + + subscription.bring(:user) if subscription.bring?(:user) + subscription.user.each do |user| + user.bring(:email) if user.bring?(:email) + emails << user.email + end + end + emails + end + + def self.mail_options + options = { + address: LinkedData.settings.smtp_host, + port: LinkedData.settings.smtp_port, + domain: LinkedData.settings.smtp_domain # the HELO domain provided by the client to the server + } + + if LinkedData.settings.smtp_auth_type && LinkedData.settings.smtp_auth_type != :none + options.merge({ + user_name: LinkedData.settings.smtp_user, + password: LinkedData.settings.smtp_password, + authentication: LinkedData.settings.smtp_auth_type + }) + end + + options + end + end + end +end diff --git a/test/util/test_notifications.rb b/test/util/test_notifications.rb index 4d6c7430..1187698f 100644 --- a/test/util/test_notifications.rb +++ b/test/util/test_notifications.rb @@ -50,12 +50,12 @@ def test_send_notification # Email recipient address will be overridden LinkedData.settings.email_disable_override = false - LinkedData::Utils::Notifications.notify(recipients: recipients) + LinkedData::Utils::Notifier.notify(recipients: recipients) assert_equal [LinkedData.settings.email_override], last_email_sent.to # Disable override LinkedData.settings.email_disable_override = true - LinkedData::Utils::Notifications.notify({ + LinkedData::Utils::Notifier.notify({ recipients: recipients, subject: subject, body: body From 90798afdf9878bfeb58f0e68f84ffbb52faf54b3 Mon Sep 17 00:00:00 2001 From: Syphax Bouazzouni Date: Fri, 1 Apr 2022 15:35:02 +0200 Subject: [PATCH 10/15] add the ontology administrators to the processing notification targets --- lib/ontologies_linked_data/utils/notifications.rb | 2 +- test/util/test_notifications.rb | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/ontologies_linked_data/utils/notifications.rb b/lib/ontologies_linked_data/utils/notifications.rb index d3b6b4dc..7f54cd6b 100644 --- a/lib/ontologies_linked_data/utils/notifications.rb +++ b/lib/ontologies_linked_data/utils/notifications.rb @@ -45,7 +45,7 @@ def self.submission_processed(submission) .gsub('%ui_name%', LinkedData.settings.ui_name) Notifier.notify_subscribed_separately subject, body, ontology, 'PROCESSING' - Notifier.notify_support_grouped subject, body + Notifier.notify_mails_grouped subject, body, Notifier.support_mails + Notifier.admin_mails(ontology) end def self.remote_ontology_pull(submission) diff --git a/test/util/test_notifications.rb b/test/util/test_notifications.rb index 1187698f..34038b0c 100644 --- a/test/util/test_notifications.rb +++ b/test/util/test_notifications.rb @@ -93,6 +93,7 @@ def test_processing_complete_notification @@user.save ont.latest_submission(status: :any).process_submission(Logger.new(TestLogFile.new)) subscription.bring :user + admin_mails = LinkedData::Utils::Notifier.admin_mails(ont) mail_sent_count = subscription.user.size + 1 mails = all_emails.last(mail_sent_count) assert_equal mail_sent_count, mails.size # subscribed users + support mail @@ -104,7 +105,7 @@ def test_processing_complete_notification assert mails.last.subject.include?("Parsing Success") - assert_equal @@support_mails, mails.last.to + assert_equal (@@support_mails + admin_mails).uniq.sort, mails.last.to.sort ensure ont.delete if ont From 31d146ea583fc575090891fab173ade974101a45 Mon Sep 17 00:00:00 2001 From: Syphax Bouazzouni Date: Mon, 4 Apr 2022 15:28:32 +0200 Subject: [PATCH 11/15] replace ui_host with ui_name --- .../utils/notifications.rb | 247 +++++++++--------- 1 file changed, 120 insertions(+), 127 deletions(-) diff --git a/lib/ontologies_linked_data/utils/notifications.rb b/lib/ontologies_linked_data/utils/notifications.rb index 7f54cd6b..ed8e42ad 100644 --- a/lib/ontologies_linked_data/utils/notifications.rb +++ b/lib/ontologies_linked_data/utils/notifications.rb @@ -3,150 +3,143 @@ module LinkedData module Utils - class Notifications - - - - def self.new_note(note) - note.bring_remaining - note.creator.bring(:username) if note.creator.bring?(:username) - ontologies = note.relatedOntology.map {|o| o.name}.join(", ") - - note.relatedOntology.each {|o| o.bring(:name) if o.bring?(:name); o.bring(:subscriptions) if o.bring?(:subscriptions)} - ontologies = note.relatedOntology.map {|o| o.name}.join(", ") - subject = "[#{LinkedData.settings.ui_host} Notes] [#{ontologies}] #{note.subject}" - body = NEW_NOTE.gsub("%username%", note.creator.username) - .gsub("%ontologies%", ontologies) - .gsub("%note_subject%", note.subject || "") - .gsub("%note_body%", note.body || "") - - .gsub("%note_url%", LinkedData::Hypermedia.generate_links(note)["ui"]) - .gsub('%ui_name%', LinkedData.settings.ui_name) - + class Notifications + def self.new_note(note) + note.bring_remaining + note.creator.bring(:username) if note.creator.bring?(:username) + note.relatedOntology.each { |o| o.bring(:name) if o.bring?(:name); o.bring(:subscriptions) if o.bring?(:subscriptions) } + ontologies = note.relatedOntology.map { |o| o.name }.join(", ") + + subject = "[#{LinkedData.settings.ui_name} Notes] [#{ontologies}] #{note.subject}" + body = NEW_NOTE.gsub("%username%", note.creator.username) + .gsub("%ontologies%", ontologies) + .gsub("%note_subject%", note.subject || "") + .gsub("%note_body%", note.body || "") + .gsub("%note_url%", LinkedData::Hypermedia.generate_links(note)["ui"]) + .gsub('%ui_name%', LinkedData.settings.ui_name) + + note.relatedOntology.each do |ont| + Notifier.notify_subscribed_separately subject, body, ont, 'NOTES' + end + end - note.relatedOntology.each do |ont| - Notifier.notify_subscribed_separately subject, body, ont, 'NOTES' + def self.submission_processed(submission) + submission.bring_remaining + ontology = submission.ontology + ontology.bring(:name, :acronym) + result = submission.ready? ? 'Success' : 'Failure' + status = LinkedData::Models::SubmissionStatus.readable_statuses(submission.submissionStatus) + + subject = "[#{LinkedData.settings.ui_name}] #{ontology.name} Parsing #{result}" + body = SUBMISSION_PROCESSED.gsub('%ontology_name%', ontology.name) + .gsub('%ontology_acronym%', ontology.acronym) + .gsub('%statuses%', status.join('
')) + .gsub('%admin_email%', LinkedData.settings.email_sender) + .gsub('%ontology_location%', LinkedData::Hypermedia.generate_links(ontology)['ui']) + .gsub('%ui_name%', LinkedData.settings.ui_name) + + Notifier.notify_subscribed_separately subject, body, ontology, 'PROCESSING' + Notifier.notify_mails_grouped subject, body, Notifier.support_mails + Notifier.admin_mails(ontology) end - end - def self.submission_processed(submission) - submission.bring_remaining - ontology = submission.ontology - ontology.bring(:name, :acronym) - result = submission.ready? ? 'Success' : 'Failure' - status = LinkedData::Models::SubmissionStatus.readable_statuses(submission.submissionStatus) - - subject = "[#{LinkedData.settings.ui_host}] #{ontology.name} Parsing #{result}" - body = SUBMISSION_PROCESSED.gsub('%ontology_name%', ontology.name) - .gsub('%ontology_acronym%', ontology.acronym) - .gsub('%statuses%', status.join('
')) - .gsub('%admin_email%', LinkedData.settings.email_sender) - .gsub('%ontology_location%', LinkedData::Hypermedia.generate_links(ontology)['ui']) - .gsub('%ui_name%', LinkedData.settings.ui_name) - - Notifier.notify_subscribed_separately subject, body, ontology, 'PROCESSING' - Notifier.notify_mails_grouped subject, body, Notifier.support_mails + Notifier.admin_mails(ontology) - end + def self.remote_ontology_pull(submission) + submission.bring_remaining + ontology = submission.ontology + ontology.bring(:name, :acronym, :administeredBy) + + subject = "[#{LinkedData.settings.ui_name}] Load from URL failure for #{ontology.name}" + body = REMOTE_PULL_FAILURE.gsub('%ont_pull_location%', submission.pullLocation.to_s) + .gsub('%ont_name%', ontology.name) + .gsub('%ont_acronym%', ontology.acronym) + .gsub('%ontology_location%', LinkedData::Hypermedia.generate_links(ontology)['ui']) + .gsub('%support_mail%', Notifier.support_mails.first || '') + .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.admin_emails.nil? && LinkedData.settings.admin_emails.kind_of?(Array) + LinkedData.settings.admin_emails.each do |admin_email| + recipients << admin_email + end + end - def self.remote_ontology_pull(submission) - submission.bring_remaining - ontology = submission.ontology - ontology.bring(:name, :acronym, :administeredBy) - - subject = "[#{LinkedData.settings.ui_host}] Load from URL failure for #{ontology.name}" - body = REMOTE_PULL_FAILURE.gsub('%ont_pull_location%', submission.pullLocation.to_s) - .gsub('%ont_name%', ontology.name) - .gsub('%ont_acronym%', ontology.acronym) - .gsub('%ontology_location%', LinkedData::Hypermedia.generate_links(ontology)['ui']) - .gsub('%support_mail%', Notifier.support_mails.first || '') - .gsub('%ui_name%', LinkedData.settings.ui_name) - recipients = [] - ontology.administeredBy.each do |user| - user.bring(:email) if user.bring?(:email) - recipients << user.email + Notifier.notify_mails_grouped subject, body, [Notifier.admin_mails(ontology) + Notifier.support_mails] end - if !LinkedData.settings.admin_emails.nil? && LinkedData.settings.admin_emails.kind_of?(Array) - LinkedData.settings.admin_emails.each do |admin_email| - recipients << admin_email - end - Notifier.notify_mails_grouped subject, body, [Notifier.admin_mails(ontology) + Notifier.support_mails] - end + def self.new_user(user) + user.bring_remaining - def self.new_user(user) - user.bring_remaining + subject = "[#{LinkedData.settings.ui_name}] New User: #{user.username}" + body = NEW_USER_CREATED.gsub('%username%', user.username.to_s) + .gsub('%email%', user.email.to_s) + .gsub('%site_url%', LinkedData.settings.ui_host) + .gsub('%ui_name%', LinkedData.settings.ui_name) + recipients = LinkedData.settings.admin_emails - subject = "[#{LinkedData.settings.ui_host}] New User: #{user.username}" - body = NEW_USER_CREATED.gsub('%username%', user.username.to_s) - .gsub('%email%', user.email.to_s) - .gsub('%site_url%', LinkedData.settings.ui_host) - .gsub('%ui_name%', LinkedData.settings.ui_name) - recipients = LinkedData.settings.admin_emails + Notifier.notify_support_grouped subject, body + end - Notifier.notify_support_grouped subject, body - end + def self.new_ontology(ont) + ont.bring_remaining - def self.new_ontology(ont) - ont.bring_remaining + subject = "[#{LinkedData.settings.ui_name}] New Ontology: #{ont.acronym}" + body = NEW_ONTOLOGY_CREATED.gsub('%acronym%', ont.acronym) + .gsub('%name%', ont.name.to_s) + .gsub('%addedby%', ont.administeredBy[0].to_s) + .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.admin_emails - subject = "[#{LinkedData.settings.ui_host}] New Ontology: #{ont.acronym}" - body = NEW_ONTOLOGY_CREATED.gsub('%acronym%', ont.acronym) - .gsub('%name%', ont.name.to_s) - .gsub('%addedby%', ont.administeredBy[0].to_s) - .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.admin_emails + Notifier.notify_support_grouped subject, body + end - Notifier.notify_support_grouped subject, body - end + def self.reset_password(user, token) + ui_name = LinkedData.settings.ui_name + 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)}" - def self.reset_password(user, token) - ui_name = LinkedData.settings.ui_name - 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)}" + body = <<~HTML + Someone has requested a password reset for user #{user.username}. If this was + you, please click on the link below to reset your password. Otherwise, please + ignore this email.

- body = <<~HTML - Someone has requested a password reset for user #{user.username}. If this was - you, please click on the link below to reset your password. Otherwise, please - ignore this email.

+ #{password_url}

- #{password_url}

+ Thanks,
+ BioPortal Team + #{ui_name} Team + HTML + Notifier.notify_mails_separately subject, body, [user.mail] + end - Thanks,
- BioPortal Team - #{ui_name} Team - HTML - Notifier.notify_mails_separately subject, body, [user.mail] - end + def self.obofoundry_sync(missing_onts, obsolete_onts) + body = '' + ui_name = LinkedData.settings.ui_name + if missing_onts.size > 0 + body << "The following OBO Library ontologies are missing from #{ui_name}:

" + missing_onts.each do |ont| + body << "#{ont['id']} (#{ont['title']})

" + end + end - def self.obofoundry_sync(missing_onts, obsolete_onts) - body = '' - ui_name = LinkedData.settings.ui_name - if missing_onts.size > 0 - body << "The following OBO Library ontologies are missing from #{ui_name}:

" - missing_onts.each do |ont| - body << "#{ont['id']} (#{ont['title']})

" + if obsolete_onts.size > 0 + body << 'The following OBO Library ontologies have been deprecated:

' + obsolete_onts.each do |ont| + body << "#{ont['id']} (#{ont['title']})

" + end end - end - if obsolete_onts.size > 0 - body << 'The following OBO Library ontologies have been deprecated:

' - obsolete_onts.each do |ont| - body << "#{ont['id']} (#{ont['title']})

" + if body.empty? + body << "#{ui_name} and the OBO Foundry are in sync.

" end - end - if body.empty? - body << "#{ui_name} and the OBO Foundry are in sync.

" + Notifier.notify_mails_separately subject, body, [LinkedData.settings.email_sender] end - Notifier.notify_mails_separately subject, body, [LinkedData.settings.email_sender] - end - - - - NEW_NOTE = <%username%.

----------------------------------------------------------------------------------
@@ -158,7 +151,7 @@ def self.obofoundry_sync(missing_onts, obsolete_onts) You can respond by visiting: %ui_name%.

EOS - SUBMISSION_PROCESSED = <
%statuses% @@ -171,7 +164,7 @@ def self.obofoundry_sync(missing_onts, obsolete_onts) The %ui_name% Team EOS - REMOTE_PULL_FAILURE = <
Please verify the URL you provided for daily loading of your ontology: @@ -187,7 +180,7 @@ def self.obofoundry_sync(missing_onts, obsolete_onts) The %ui_name% Team EOS - NEW_USER_CREATED = < Username: %username% @@ -197,7 +190,7 @@ def self.obofoundry_sync(missing_onts, obsolete_onts) The %ui_name% Team EOS - NEW_ONTOLOGY_CREATED = < Acronym: %acronym% @@ -209,6 +202,6 @@ def self.obofoundry_sync(missing_onts, obsolete_onts) The %ui_name% Team EOS + end end - end -end \ No newline at end of file +end From ee738188b43f54e0758411041994a7a6d3f0e950 Mon Sep 17 00:00:00 2001 From: Syphax Bouazzouni Date: Mon, 4 Apr 2022 15:29:00 +0200 Subject: [PATCH 12/15] use ui_name in the tests --- test/util/test_notifications.rb | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/test/util/test_notifications.rb b/test/util/test_notifications.rb index 34038b0c..f3d93a99 100644 --- a/test/util/test_notifications.rb +++ b/test/util/test_notifications.rb @@ -8,11 +8,13 @@ class TestNotifications < LinkedData::TestCase def self.before_suite @@notifications_enabled = LinkedData.settings.enable_notifications @@disable_override = LinkedData.settings.email_disable_override - @@old_suppor_mails = LinkedData.settings.admin_emails - LinkedData.settings.admin_emails = ["ontoportal-support@mail.com"] if LinkedData.settings.admin_emails.empty? + @@old_support_mails = LinkedData.settings.admin_emails + if @@old_support_mails.nil? || @@old_support_mails.empty? + LinkedData.settings.admin_emails = ["ontoportal-support@mail.com"] + end LinkedData.settings.email_disable_override = true LinkedData.settings.enable_notifications = true - + @@ui_name = LinkedData.settings.ui_name @@support_mails = LinkedData.settings.admin_emails @@ont = LinkedData::SampleData::Ontology.create_ontologies_and_submissions(ont_count: 1, submission_count: 1)[2].first @@ont.bring_remaining @@ -26,7 +28,7 @@ def self.before_suite def self.after_suite LinkedData.settings.enable_notifications = @@notifications_enabled LinkedData.settings.email_disable_override = @@disable_override - LinkedData.settings.admin_emails = @@old_suppor_mails + LinkedData.settings.admin_emails = @@old_support_mails @@ont.delete if defined?(@@ont) @@subscription.delete if defined?(@@subscription) @@user.delete if defined?(@@user) @@ -76,8 +78,7 @@ def test_new_note_notification note.body = body note.relatedOntology = [@@ont] note.save - - assert last_email_sent.subject.include?("[BioPortal Notes]") + assert last_email_sent.subject.include?("[#{@@ui_name} Notes]") assert_equal [@@user.email], last_email_sent.to ensure note.delete if note @@ -135,7 +136,7 @@ def test_remote_ontology_pull_notification assert sub.valid?, sub.errors LinkedData::Utils::Notifications.remote_ontology_pull(sub) - assert last_email_sent.subject.include? "Load from URL failure for #{ont.name}" + assert last_email_sent.subject.include? "[#{@@ui_name}] Load from URL failure for #{ont.name}" recipients = @@support_mails ont_admins.each do |user| recipients << user.email From 10d3248750162fb91ac6aef72cd1f51c5e2d19ed Mon Sep 17 00:00:00 2001 From: Syphax bouazzouni Date: Fri, 1 Dec 2023 08:02:38 +0000 Subject: [PATCH 13/15] fix mailer configuration when using login auth option --- lib/ontologies_linked_data/utils/notifier.rb | 2 +- test/util/test_notifications.rb | 26 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/ontologies_linked_data/utils/notifier.rb b/lib/ontologies_linked_data/utils/notifier.rb index c4d9ad71..70f109e0 100644 --- a/lib/ontologies_linked_data/utils/notifier.rb +++ b/lib/ontologies_linked_data/utils/notifier.rb @@ -111,7 +111,7 @@ def self.mail_options } if LinkedData.settings.smtp_auth_type && LinkedData.settings.smtp_auth_type != :none - options.merge({ + options.merge!({ user_name: LinkedData.settings.smtp_user, password: LinkedData.settings.smtp_password, authentication: LinkedData.settings.smtp_auth_type diff --git a/test/util/test_notifications.rb b/test/util/test_notifications.rb index f3d93a99..c8bc7017 100644 --- a/test/util/test_notifications.rb +++ b/test/util/test_notifications.rb @@ -148,4 +148,30 @@ def test_remote_ontology_pull_notification end end end + + def test_mail_options + options = LinkedData::Utils::Notifier.mail_options + expected_options = { + address: LinkedData.settings.smtp_host, + port: LinkedData.settings.smtp_port, + domain: LinkedData.settings.smtp_domain + } + assert_equal options, expected_options + + # testing SMTP authentification-based login + current_auth_type = LinkedData.settings.smtp_auth_type + LinkedData.settings.smtp_auth_type = :plain + options = LinkedData::Utils::Notifier.mail_options + expected_options = { + address: LinkedData.settings.smtp_host, + port: LinkedData.settings.smtp_port, + domain: LinkedData.settings.smtp_domain, + user_name: LinkedData.settings.smtp_user, + password: LinkedData.settings.smtp_password, + authentication: LinkedData.settings.smtp_auth_type + } + assert_equal options, expected_options + + LinkedData.settings.smtp_auth_type = current_auth_type + end end From f5b9478c7ec16ae3cf6df2a6c13ccf2435ad35f9 Mon Sep 17 00:00:00 2001 From: Syphax bouazzouni Date: Fri, 1 Dec 2023 08:03:50 +0000 Subject: [PATCH 14/15] refactor notifications of notes and reset password --- .../utils/notifications.rb | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/lib/ontologies_linked_data/utils/notifications.rb b/lib/ontologies_linked_data/utils/notifications.rb index ed8e42ad..e2664fc9 100644 --- a/lib/ontologies_linked_data/utils/notifications.rb +++ b/lib/ontologies_linked_data/utils/notifications.rb @@ -9,13 +9,18 @@ def self.new_note(note) note.creator.bring(:username) if note.creator.bring?(:username) note.relatedOntology.each { |o| o.bring(:name) if o.bring?(:name); o.bring(:subscriptions) if o.bring?(:subscriptions) } ontologies = note.relatedOntology.map { |o| o.name }.join(", ") - + # Fix the note URL when using replace_url_prefix (in another VM than NCBO) + if LinkedData.settings.replace_url_prefix == true + note_url = "http://#{LinkedData.settings.ui_host}/notes/#{CGI.escape(note.id.to_s.gsub("http://data.bioontology.org", LinkedData.settings.rest_url_prefix))}" + else + note_url = "http://#{LinkedData.settings.ui_host}/notes/#{CGI.escape(note.id.to_s)}" + end subject = "[#{LinkedData.settings.ui_name} Notes] [#{ontologies}] #{note.subject}" body = NEW_NOTE.gsub("%username%", note.creator.username) .gsub("%ontologies%", ontologies) .gsub("%note_subject%", note.subject || "") .gsub("%note_body%", note.body || "") - .gsub("%note_url%", LinkedData::Hypermedia.generate_links(note)["ui"]) + .gsub("%note_url%", note_url) .gsub('%ui_name%', LinkedData.settings.ui_name) note.relatedOntology.each do |ont| @@ -101,18 +106,11 @@ 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)}" - body = <<~HTML - Someone has requested a password reset for user #{user.username}. If this was - you, please click on the link below to reset your password. Otherwise, please - ignore this email.

- - #{password_url}

+ body = REST_PASSWORD.gsub('%ui_name%', ui_name) + .gsub('%username%', user.username.to_s) + .gsub('%password_url%', password_url.to_s) - Thanks,
- BioPortal Team - #{ui_name} Team - HTML - Notifier.notify_mails_separately subject, body, [user.mail] + Notifier.notify_mails_separately subject, body, [user.email] end def self.obofoundry_sync(missing_onts, obsolete_onts) @@ -202,6 +200,17 @@ def self.obofoundry_sync(missing_onts, obsolete_onts) The %ui_name% Team EOS + REST_PASSWORD = <<~HTML + Someone has requested a password reset for user %username% . If this was + you, please click on the link below to reset your password. Otherwise, please + ignore this email.

+ + %password_url%

+ + Thanks,
+ %ui_name% Team +HTML + end end end From e6f1dac07f28f9d3427a4660f44179f0ba97b3ed Mon Sep 17 00:00:00 2001 From: Syphax bouazzouni Date: Mon, 4 Dec 2023 11:44:48 +0100 Subject: [PATCH 15/15] use id_url_prefix in generating note id instead of of hardcoded value --- lib/ontologies_linked_data/utils/notifications.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/ontologies_linked_data/utils/notifications.rb b/lib/ontologies_linked_data/utils/notifications.rb index e2664fc9..beb6cf4b 100644 --- a/lib/ontologies_linked_data/utils/notifications.rb +++ b/lib/ontologies_linked_data/utils/notifications.rb @@ -10,11 +10,12 @@ def self.new_note(note) note.relatedOntology.each { |o| o.bring(:name) if o.bring?(:name); o.bring(:subscriptions) if o.bring?(:subscriptions) } ontologies = note.relatedOntology.map { |o| o.name }.join(", ") # Fix the note URL when using replace_url_prefix (in another VM than NCBO) - if LinkedData.settings.replace_url_prefix == true - note_url = "http://#{LinkedData.settings.ui_host}/notes/#{CGI.escape(note.id.to_s.gsub("http://data.bioontology.org", LinkedData.settings.rest_url_prefix))}" + if LinkedData.settings.replace_url_prefix + note_id = CGI.escape(note.id.to_s.gsub(LinkedData.settings.id_url_prefix, LinkedData.settings.rest_url_prefix)) else - note_url = "http://#{LinkedData.settings.ui_host}/notes/#{CGI.escape(note.id.to_s)}" + note_id = CGI.escape(note.id.to_s) end + note_url = "http://#{LinkedData.settings.ui_host}/notes/#{note_id}" subject = "[#{LinkedData.settings.ui_name} Notes] [#{ontologies}] #{note.subject}" body = NEW_NOTE.gsub("%username%", note.creator.username) .gsub("%ontologies%", ontologies)