diff --git a/app/jobs/send_template_message_job.rb b/app/jobs/send_template_message_job.rb index bec3f7b0..557d4d0f 100644 --- a/app/jobs/send_template_message_job.rb +++ b/app/jobs/send_template_message_job.rb @@ -8,13 +8,20 @@ class SendTemplateMessageJob < ApplicationJob receivers_id, template_id, props = args[0], args[1], args[2] template = MessageTemplate.find_by_id(template_id) return unless template.present? - receivers = User.where(id: receivers_id) + receivers = User.where(id: receivers_id).or(User.where(mail: receivers_id)) + not_exists_receivers = receivers_id - receivers.pluck(:id) - receivers.pluck(:mail) receivers_string, content, notification_url = MessageTemplate::CustomTip.get_message_content(receivers, template, props) Notice::Write::CreateService.call(receivers_string, content, notification_url, source, {receivers_id: receivers_id, template_id: template_id, props: props}) receivers.find_each do |receiver| receivers_email_string, email_title, email_content = MessageTemplate::CustomTip.get_email_message_content(receiver, template, props) Notice::Write::EmailCreateService.call(receivers_email_string, email_title, email_content) end + not_exists_receivers.each do |mail| + valid_email_regex = /^[a-zA-Z0-9]+([.\-_\\]*[a-zA-Z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/i + next unless (mail =~ valid_email_regex) + email_title, email_content = MessageTemplate::CustomTip.get_email_content(template, props) + Notice::Write::EmailCreateService.call(mail, email_title, email_content) + end when 'FollowTip' watcher_id = args[0] watcher = Watcher.find_by_id(watcher_id) diff --git a/app/models/message_template/custom_tip.rb b/app/models/message_template/custom_tip.rb index 6c5a3345..f1e3096f 100644 --- a/app/models/message_template/custom_tip.rb +++ b/app/models/message_template/custom_tip.rb @@ -48,4 +48,21 @@ class MessageTemplate::CustomTip < MessageTemplate Rails.logger.info("MessageTemplate::CustomTip.get_email_message_content [ERROR] #{e}") return '', '', '' end + + def self.get_email_content(template, props = {}) + return '', '', '' if template.blank? + title = template.email_title + content = template.email + props.each do |k, v| + title.gsub!("{#{k}}", v) + content.gsub!("{#{k}}", v) + end + title.gsub!('{platform}', PLATFORM) + content.gsub!('{platform}', PLATFORM) + + return title, content + rescue => e + Rails.logger.info("MessageTemplate::CustomTip.get_email_content [ERROR] #{e}") + return '', '' + end end \ No newline at end of file