Class: MessageGateway

Inherits:
ActionMailer::Base
  • Object
show all
Defined in:
app/models/message_gateway.rb

Instance Method Summary collapse

Instance Method Details

#check_sender_is_in_mailmap(user, email) ⇒ Object (private)


66
67
68
69
70
71
72
# File 'app/models/message_gateway.rb', line 66

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

#get_address(email) ⇒ Object (private)


55
56
57
# File 'app/models/message_gateway.rb', line 55

def get_address(email)
  return SITE_CONFIG['email_dispatch'] == 'to' ?  email.to[0] :  email.from[0]
end

#get_all_parts(parts) ⇒ Object (private)


103
104
105
106
107
108
109
110
111
112
113
# File 'app/models/message_gateway.rb', line 103

def get_all_parts(parts)
  # return a flattened array of parts. If a multipart attachment is found, recurse over its parts
  all_parts = parts.inject([]) do |set, elem|
    if elem.content_type.start_with?("multipart/alternative")
      # recurse to handle multiparts in this multipart
      set += get_all_parts(elem.parts)
    else
      set << elem
    end
  end
end

#get_decoded_text_or_nil(text) ⇒ Object (private)


89
90
91
# File 'app/models/message_gateway.rb', line 89

def get_decoded_text_or_nil(text)
  return text ? text.decoded.strip : nil
end

#get_first_text_plain_part(email) ⇒ Object (private)


93
94
95
96
97
98
99
100
101
# File 'app/models/message_gateway.rb', line 93

def get_first_text_plain_part(email)
  # get all parts from multipart/alternative attachments
  parts = get_all_parts(email.parts)

  # remove all parts that are not text/plain
  parts.reject{|part| !part.content_type.start_with?("text/plain") }

  return parts.count > 0 ? parts[0].decoded.strip : ""
end

#get_receiving_user_from_email_address(email) ⇒ Object (private)


38
39
40
# File 'app/models/message_gateway.rb', line 38

def get_receiving_user_from_email_address(email)
  SITE_CONFIG['email_dispatch'] == 'single_user' ? get_receiving_user_from_env_setting : get_receiving_user_from_mail_header(email)
end

#get_receiving_user_from_env_settingObject (private)


42
43
44
45
46
47
# File 'app/models/message_gateway.rb', line 42

def get_receiving_user_from_env_setting
  Rails.logger.info "All received email goes to #{ENV['TRACKS_MAIL_RECEIVER']}"
  user = User.where(:login => ENV['TRACKS_MAIL_RECEIVER']).first
  Rails.logger.info "WARNING: Unknown user set for TRACKS_MAIL_RECEIVER (#{ENV['TRACKS_MAIL_RECEIVER']})" if user.nil?
  return user
end

#get_receiving_user_from_mail_header(email) ⇒ Object (private)


49
50
51
52
53
# File 'app/models/message_gateway.rb', line 49

def get_receiving_user_from_mail_header(email)
  user = get_receiving_user_from_sms_email( get_address(email) )
  Rails.logger.info(user.nil? ? "User unknown": "Email belongs to #{user.}")
  return user
end

#get_receiving_user_from_sms_email(address) ⇒ Object (private)


59
60
61
62
63
64
# File 'app/models/message_gateway.rb', line 59

def get_receiving_user_from_sms_email(address)
  Rails.logger.info "Looking for user with email #{address}"
  user = User.where("preferences.sms_email" => address.strip).includes(:preference).first
  user = User.where("preferences.sms_email" => address.strip[1.100]).includes(:preference).first if user.nil?
  return user
end

#get_text_or_nil(text) ⇒ Object (private)


85
86
87
# File 'app/models/message_gateway.rb', line 85

def get_text_or_nil(text)
  return text ? text.strip : nil
end

#get_todo_params(email) ⇒ Object (private)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/message_gateway.rb', line 20

def get_todo_params(email)
  params = {}

  if email.multipart?
    params[:description] = get_text_or_nil(email.subject)
    params[:notes]       = get_first_text_plain_part(email)
  else
    if email.subject.blank?
      params[:description] = get_decoded_text_or_nil(email.body)
      params[:notes]       = nil
    else
      params[:description] = get_text_or_nil(email.subject)
      params[:notes]       = get_decoded_text_or_nil(email.body)
    end
  end
  params
end

#receive(email) ⇒ Object


3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'app/models/message_gateway.rb', line 3

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)

  context = user.prefs.sms_context
  todo_params = get_todo_params(email)

  todo_builder = TodoFromRichMessage.new(user, context.id, todo_params[:description], todo_params[:notes])
  todo = todo_builder.construct
  todo.save!
  Rails.logger.info "Saved email as todo for user #{user.} in context #{context.name}"
  todo
end

#sender_is_in_mailmap?(user, email) ⇒ Boolean (private)

Returns:

  • (Boolean)

74
75
76
77
78
79
80
81
82
83
# File 'app/models/message_gateway.rb', line 74

def sender_is_in_mailmap?(user,email)
  if SITE_CONFIG['mailmap'].is_a? Hash and SITE_CONFIG['email_dispatch'] == 'to'
    # Look for the sender in the map of allowed senders
    SITE_CONFIG['mailmap'][user.preference.sms_email].include? email.from[0]
  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
  end
end