Skip to content

Incoming Email From Unknown User

byakugie edited this page Dec 21, 2010 · 10 revisions

This feature has now been implemented.

Current problem

We can’t create work logs from incoming emails if email author not registered in app.

Solution

Add email_address association to WorkLog and use it in Mailman.

Implementation

Link work_logs to an email_addresses and user at the same time in Mailman

  • Create migration to add email_address_id in work_logs table.
  • Change model relation in Worklog .rb, User.rb, EmailAddress.rb.
    class WorkLog < ActiveRecord::Base
      belongs_to :email_address
      belongs_to :_user_, :class_name => "User", :foreign_key => "user_id"
    end
    
    class EmailAddress < ActiveRecord::Base
      has_many :work_logs
    end
    
    class User < ActiveRecord::Base
      has_many :work_logs
    end
  • In WorkLog model, Create “user” accessor to rewrite user association.
    def user
      if _user_.nil?
        User.new(:name=>"Unknown User", :email=>email_address)
      else
        _user_
      end
    end
    def user=(u)
      self._user_ = u
    end

    If we don’t have user, “worklog.user” return new User object with given email address and name = ‘Unknown User’ (we’ll never store it to db). This solution is good, because it encapsulate changes. We don’t need to change any code outside of WorkLog and Mailman since we keep using User Association across all codes, as it is now. Also it avoid work_log → email_address → user join.

When we get an incoming email from someone not in our system :

  • Create new email_address and link work_log to new email address. but we don’t yet know who they are. (user_id will be null)
  • In Task History view, we will display “Unknown User (user@domain.com)”
  • If email has attachment, set user_id = null in project_files table

Modify some codes in work_log.rb :

  • Rename “build_work_added_or_comment” to a better name and modify the codes

Modify Mailman. rb

  • Since we can get an incoming email from someone not in our system, We have to make sure Notifications.unknown_from_address is only used for task creation

Further enhancements

  • Remove the existing field in tasks which allows for one or more custom email addresses to be added as notifications for a task. That can now go into the new relationship.
  • we need a UI to assign orphan email addresses to a user

Database Schemas

create_table "email_addresses", :force => true do |t|
   t.string "email"
   t.boolean "default"
   t.datetime "created_at"
   t.datetime "updated_at"
end

create_table "work_logs", :force => true do |t|
   t.integer "user_id"
   t.integer 'email_address_id'    
   t.integer "task_id"
   t.integer "project_id", :default => 0, :null => false
   t.integer "company_id", :default => 0, :null => false
   t.integer "customer_id", :default => 0, :null => false
   t.datetime "started_at", :null => false
   t.integer "duration", :default => 0, :null => false
   t.text "body"
   t.integer "log_type", :default => 0
   t.integer "paused_duration", :default => 0
   t.boolean "comment", :default => false
   t.datetime "exported"
   t.boolean "approved"
   t.integer "access_level_id", :default => 1
end