Skip to content

Commit

Permalink
Fix code style issues and the sender map logic to handle the empty pr…
Browse files Browse the repository at this point in the history
…ef case correctly.

Also fixed the rails script to use the correct directory always.
  • Loading branch information
ZeiP committed Feb 22, 2022
1 parent aedc9f5 commit f395ee1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
31 changes: 13 additions & 18 deletions app/models/message_gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class MessageGateway < ActionMailer::Base
def receive(email)
user = get_receiving_user_from_email_address(email)
return false if user.nil?
return false unless check_sender_is_in_mailmap(user, email)
return false unless check_sender_is_in_mailmap(user, email.from[0])

context = user.prefs.sms_context
todo_params = get_todo_params(email)
Expand Down Expand Up @@ -90,29 +90,24 @@ def get_receiving_user_from_sms_email(address)
return user
end

def check_sender_is_in_mailmap(user, email)
if user.present? && !sender_is_in_mailmap?(user, email)
Rails.logger.warn "#{email.from[0]} not found in mailmap for #{user.login}"
def check_sender_is_in_mailmap(user, from)
if user.present? && SITE_CONFIG['email_dispatch'] == 'to' && !sender_is_in_mailmap?(user, from)
Rails.logger.warn "#{from} not found in mailmap for #{user.login}"
return false
end
return true
end

def sender_is_in_mailmap?(user, email)
if SITE_CONFIG['email_dispatch'] == 'to'
if SITE_CONFIG['mailmap'].is_a? Hash
# Look for the sender in the map of allowed senders
SITE_CONFIG['mailmap'][user.preference.sms_email].include? email.from[0]
else
# If the config mailmap isn't defined, use the values provided by the users.
pref_senders = user.prefs.sms_permitted_senders.split(',').collect(&:strip)
pref_senders.include? email.from[0]
end
else
# We can't check the map if it's not defined, or if the lookup is the
# wrong way round, so just allow it
true
def sender_is_in_mailmap?(user, from)
if SITE_CONFIG['mailmap'].is_a? Hash
# Look for the sender in the map of allowed senders
SITE_CONFIG['mailmap'][user.preference.sms_email].include? from
elsif !(pref_senders = user.prefs.sms_permitted_senders).empty?
# If the config mailmap isn't defined, use the values provided by the users.
pref_senders.split(',').collect(&:strip).include? from
end
# We can't check the map if it's not defined so just allow it
true
end

def get_text_or_nil(text)
Expand Down
2 changes: 1 addition & 1 deletion app/models/preference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Preference < ApplicationRecord
belongs_to :user
belongs_to :sms_context, :class_name => 'Context'

validates_uniqueness_of :sms_email, :case_sensitive => false
validates :sms_email, uniqueness: { case_sensitive: false }

def self.themes
{ :black => 'black', :light_blue => 'light_blue' }
Expand Down
2 changes: 1 addition & 1 deletion bin/rails
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ SCRIPT=$(readlink -f "$0")
# Absolute path this script is in, thus /home/user/bin
SCRIPTPATH=$(dirname "$SCRIPT")

if [ -e ../.use-docker -a ! -e /etc/app-env ];
if [ -e $SCRIPTPATH/../.use-docker -a ! -e /etc/app-env ];
then
$SCRIPTPATH/../script/docker-environment $0 "$@"
else
Expand Down

0 comments on commit f395ee1

Please sign in to comment.