Skip to content

Commit

Permalink
Update code to use email_enabled attribute instead of record presence
Browse files Browse the repository at this point in the history
  • Loading branch information
cmrd-senya committed Jul 22, 2022
1 parent 4be89fc commit 048b204
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 18 deletions.
7 changes: 3 additions & 4 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,9 @@ def change_settings(user_data, successful="users.update.settings_updated", error
end

def set_email_preferences
@email_prefs = Hash.new(true)

@user.user_preferences.each do |pref|
@email_prefs[pref.email_type] = false
@email_prefs = @user.user_preferences.to_h do |pref|
[pref.email_type, pref.email_enabled]
end
@email_prefs.default = true
end
end
3 changes: 2 additions & 1 deletion app/mailers/report_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ def new_report(type, id, reason, role)
person = Person.find(role.person_id)
return unless person.local?
user = User.find_by_id(person.owner_id)
return if user.user_preferences.exists?(email_type: :someone_reported)
return if NotificationSettingsService.new(user).email_disabled?(:someone_reported)

I18n.with_locale(user.language) do
resource[:email] = user.email
format(resource)
Expand Down
2 changes: 1 addition & 1 deletion app/models/notifications/mentioned_in_comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def self.filter_mentions(mentions, mentionable, _recipient_user_ids)
end

def mail_job
if !recipient.user_preferences.exists?(email_type: "mentioned_in_comment")
if NotificationSettingsService.new(recipient).email_enabled?(:mentioned_in_comment)
Workers::Mail::MentionedInComment
elsif shareable.author.owner_id == recipient_id
Workers::Mail::CommentOnPost
Expand Down
23 changes: 11 additions & 12 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,20 +180,18 @@ def send_reset_password_instructions

def update_user_preferences(pref_hash)
if self.disable_mail
UserPreference::VALID_EMAIL_TYPES.each{|x| self.user_preferences.find_or_create_by(email_type: x)}
UserPreference::VALID_EMAIL_TYPES.each do |type|
user_preferences.find_or_create_by(email_type: type).update(email_enabled: false)
end
self.disable_mail = false
self.save
end

pref_hash.keys.each do |key|
if pref_hash[key] == 'true'
self.user_preferences.find_or_create_by(email_type: key)
else
block = user_preferences.find_by(email_type: key)
if block
block.destroy
end
end
email_enabled = pref_hash[key] == "false"
user_preferences
.find_or_create_by(email_type: key)
.update(email_enabled: email_enabled)
end
end

Expand Down Expand Up @@ -354,9 +352,10 @@ def perform_export_photos!
def mail(job, *args)
return unless job.present?
pref = job.to_s.gsub('Workers::Mail::', '').underscore
if(self.disable_mail == false && !self.user_preferences.exists?(:email_type => pref))
job.perform_async(*args)
end
email_enabled = (disable_mail == false) &&
NotificationSettingsService.new(self).email_enabled?(pref)

job.perform_async(*args) if email_enabled
end

def send_confirm_email
Expand Down
32 changes: 32 additions & 0 deletions app/services/notification_settings_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

class NotificationSettingsService
def initialize(user)
@user = user
end

def email_disabled?(notification_type)
!email_enabled?(notification_type)
end

def email_enabled?(notification_type)
notification_enabled?(notification_type, :email_enabled?)
end

def in_app_enabled?(notification_type)
notification_enabled?(notification_type, :in_app_enabled?)
end

private

attr_reader :user

delegate :user_preferences, to: :user

def notification_enabled?(notification_type, pref_method)
pref = user_preferences.find_by(email_type: notification_type)
return true if pref.nil?

pref.public_send pref_method
end
end

0 comments on commit 048b204

Please sign in to comment.