Commit c5213f1

mo khan <mo@mokhan.ca>
2016-05-14 14:34:33
search by to address token rather then from address.
1 parent c7dacd8
app/models/email_processor.rb
@@ -6,10 +6,9 @@ class EmailProcessor
   end
 
   def process
-    user = User.find_by!(email: email.from[:email])
-
-    email.attachments.each do |attachment|
-      BackupFile.new(user, attachment).process_later(Program.stronglifts)
+    tokens = email.to.map { |x| x[:token] }.uniq
+    User.find(tokens).each do |user|
+      user.add_to_inbox(email)
     end
   end
 end
app/models/user.rb
@@ -27,6 +27,12 @@ class User < ActiveRecord::Base
     username
   end
 
+  def add_to_inbox(email)
+    email.attachments.each do |attachment|
+      BackupFile.new(self, attachment).process_later(Program.stronglifts)
+    end
+  end
+
   def personal_record_for(exercise)
     exercise_sessions.
       joins(:exercise).
spec/models/email_processor_spec.rb
@@ -9,6 +9,7 @@ describe EmailProcessor do
     let(:user) { create(:user) }
 
     before :each do
+      email.to.first[:token] = user.id
       email.from[:email] = user.email
     end
 
spec/factories.rb
@@ -24,10 +24,10 @@ FactoryGirl.define do
   end
   factory :email, class: OpenStruct do
     to [{
-      full: "to_user@email.com",
-      email: "to_user@email.com",
-      token: "to_user",
-      host: "email.com",
+      full: "cf9b756e-789d-4bbb-aee7-2c8298bb69a7@stronglifters.com",
+      email: "cf9b756e-789d-4bbb-aee7-2c8298bb69a7@stronglifters.com",
+      token: "cf9b756e-789d-4bbb-aee7-2c8298bb69a7",
+      host: "stronglifters.com",
       name: nil
     }]
     from({