add html titles to these pages
[kwestie.git] / app / models / kwestie_mailer.rb
blob207b561ac76644df21053b7da67e854060ca4e04
1 class KwestieMailer < ActionMailer::Base
2   # Large base method, might have made sense use a general mailer for receiving
3   def receive(email)
5     user = find_user_from_email(email)
7     return create_user_via_email(email) if ! user
9     User.current_user = user
11     issue = find_issue_from_email(email)
13     if ! issue
14       create_issue_via_email(email) 
15     else
16       create_comment_via_email(email, issue)
17     end
19   end
21   def create_comment_via_email(email, issue)
22     comment = Comment.create( { :description => email.body, :issue_id => issue.id } )
24     # FIXME: add attachments for comments
25 #    if email.has_attachments?
26 #      for attachment in email.attachments
27 #        page.attachments.create({ 
28 #          :file => attachment, 
29 #          :description => email.subject
30 #        })
31 #      end
32 #    end
34   end
36   def create_issue_via_email(email)
37     issue = Issue.create( { :title => email.subject, :description => email.body} )
39     # FIXME: add attachments for issue
40   end
42   def create_user_via_email(email)
43     #FIXME: require use of the UserMailer for creating a new user on the fly without a password
44   end
46   private
48     def find_user_from_email(email)
49       for mail in email.from
50         user = User.find_by_email(mail)
51         return user if ! user.nil?
52       end
53       return nil
54     end
56     # FIXME: only handles one possible mail address.
57     def find_issue_from_email(email) 
58       for mail in email.to
59         id = mail.split('@')[0].split('+')[1]
60         return Issue.find(id) if ! id.nil?
61       end
62       return nil
63     end
64 end