From 92e52ac6355ceec4b8b2e5517279afda9a81b4ed Mon Sep 17 00:00:00 2001 From: Bryan W Clark Date: Thu, 14 Feb 2008 01:00:51 -0500 Subject: [PATCH] initial working version of attachments. Fixed up the Kwestie emailer during this process, still needs a bit more love Attachments from comments or new issues are now processed and stored on the file system. Beware that you'll need to work some permissions problems!! Updated the README to reflect the changes required in setup wrt what landed --- app/models/attachment.rb | 6 ++ app/models/comment.rb | 2 +- app/models/issue.rb | 2 + app/models/kwestie_mailer.rb | 135 +++++++++++++++++++------------------ app/models/user_observer.rb | 2 - app/views/comments/_show.rhtml | 7 ++ app/views/issues/show.rhtml | 8 +++ config/environments/development.rb | 4 +- doc/README_FOR_APP | 23 ++++--- public/attachments/.gitignore | 0 tmp/attachment_fu/.gitignore | 0 11 files changed, 113 insertions(+), 76 deletions(-) rewrite app/models/kwestie_mailer.rb (67%) create mode 100644 public/attachments/.gitignore create mode 100644 tmp/attachment_fu/.gitignore diff --git a/app/models/attachment.rb b/app/models/attachment.rb index 1b474b2..de07776 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -1,2 +1,8 @@ class Attachment < ActiveRecord::Base + usesguid + + has_attachment :storage => :file_system, :path_prefix => 'public/attachments', + :thumbnails => { :thumb => [50, 50], :geometry => 'x50' } + + belongs_to :created_by, :class_name => "User", :foreign_key => "created_by" end diff --git a/app/models/comment.rb b/app/models/comment.rb index f1e89f4..aeebd8d 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -4,7 +4,7 @@ class Comment < ActiveRecord::Base belongs_to :issue belongs_to :created_by, :class_name => "User", :foreign_key => "created_by" -# has_many :attachments, :class_name => "CommentAttachment", :foreign_key => "comment_id", :dependent => :destroy + has_many :attachments, :class_name => "Attachment", :foreign_key => "comment_id", :dependent => :destroy validates_presence_of :description diff --git a/app/models/issue.rb b/app/models/issue.rb index eee3fec..e3ebedd 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -9,6 +9,8 @@ class Issue < ActiveRecord::Base has_many :comments, :dependent => :destroy, :order => "comments.created_at ASC" has_many :all_permalnks, :class_name => "Permalink", :foreign_key => "issue_id", :dependent => :destroy + has_many :attachments, :class_name => "Attachment", :foreign_key => "issue_id", :dependent => :destroy + validates_presence_of :title, :description def year diff --git a/app/models/kwestie_mailer.rb b/app/models/kwestie_mailer.rb dissimilarity index 67% index 207b561..31c1d9c 100644 --- a/app/models/kwestie_mailer.rb +++ b/app/models/kwestie_mailer.rb @@ -1,64 +1,71 @@ -class KwestieMailer < ActionMailer::Base - # Large base method, might have made sense use a general mailer for receiving - def receive(email) - - user = find_user_from_email(email) - - return create_user_via_email(email) if ! user - - User.current_user = user - - issue = find_issue_from_email(email) - - if ! issue - create_issue_via_email(email) - else - create_comment_via_email(email, issue) - end - - end - - def create_comment_via_email(email, issue) - comment = Comment.create( { :description => email.body, :issue_id => issue.id } ) - - # FIXME: add attachments for comments -# if email.has_attachments? -# for attachment in email.attachments -# page.attachments.create({ -# :file => attachment, -# :description => email.subject -# }) -# end -# end - - end - - def create_issue_via_email(email) - issue = Issue.create( { :title => email.subject, :description => email.body} ) - - # FIXME: add attachments for issue - end - - def create_user_via_email(email) - #FIXME: require use of the UserMailer for creating a new user on the fly without a password - end - - private - - def find_user_from_email(email) - for mail in email.from - user = User.find_by_email(mail) - return user if ! user.nil? - end - return nil - end - - # FIXME: only handles one possible mail address. - def find_issue_from_email(email) - for mail in email.to - id = mail.split('@')[0].split('+')[1] - return Issue.find(id) if ! id.nil? - end - return nil - end -end +class KwestieMailer < ActionMailer::Base + + # Generic Receive Mail method + # The goal here is to + # 1. identify the reporter sending mail + # 1a. create a user for reporter if no account exists with that mail + # 2. identify and process the mail as either + # 2a. a new issue + # 2b. a comment on a current issue + def receive(email) + + # 1. identify the sender + User.current_user = find_user_from_email(email) + + # 2. if we find the issue the email is a comment response, otherwise we have a new issue + issue = Issue.find_or_create_by_id(issue_id_from_email(email)) + + if issue.new_record? + # 2a. process the email into the issue + process_issue_from_email(issue, email) + else + # 2b. process the email into a comment for this issue + process_comment_from_email(issue, email) + end + + end + + def process_comment_from_email(issue, email) + comment = issue.comments.create( { :description => email.body, :issue_id => issue.id } ) + + if email.has_attachments? + for attachment in email.attachments + comment.attachments.create({ :uploaded_data => attachment }) + end + end + + issue.save + end + + def process_issue_from_email(issue, email) + issue.update_attributes( { :description => email.body, :title => email.subject } ) + + if email.has_attachments? + for attachment in email.attachments + issue.attachments.create({ :uploaded_data => attachment }) + end + end + + issue.save + end + + private + + #FIXME: require use of the UserMailer for creating a new user on the fly without a password + def find_user_from_email(email) + for mail in email.from + user = User.find_by_email(mail) + return user if ! user.nil? + end + return nil + end + + # FIXME: doesn't handle the CC address + def issue_id_from_email(email) + for mail in email.to + id = mail.split('@')[0].split('+')[1] + return id if ! id.nil? + end + return nil + end +end diff --git a/app/models/user_observer.rb b/app/models/user_observer.rb index 03616f8..6a56c44 100644 --- a/app/models/user_observer.rb +++ b/app/models/user_observer.rb @@ -4,8 +4,6 @@ class UserObserver < ActiveRecord::Observer end def after_save(user) - UserMailer.deliver_activation(user) if user.recently_activated? - end end diff --git a/app/views/comments/_show.rhtml b/app/views/comments/_show.rhtml index 3c61911..3bfadeb 100644 --- a/app/views/comments/_show.rhtml +++ b/app/views/comments/_show.rhtml @@ -6,4 +6,11 @@
<%= distance_of_time_in_words_to_now(comment.created_at) %> ago (<%= comment.created_at.to_s %>)
By: <%= hCard(comment.created_by) %>
+ <% if ! comment.attachments.empty? %> +
+ <% for attachment in comment.attachments %> +

<%= attachment.filename %>

+ <% end %> +
+ <% end %> diff --git a/app/views/issues/show.rhtml b/app/views/issues/show.rhtml index 2d8e42a..a943439 100644 --- a/app/views/issues/show.rhtml +++ b/app/views/issues/show.rhtml @@ -2,6 +2,14 @@ <%= render :partial => 'show' %> + <% if ! @issue.attachments.empty? %> +
+ <% for attachment in @issue.attachments %> +

<%= attachment.filename %>

+ <% end %> +
+ <% end %> +

Comments

<% for c in @issue.comments %> diff --git a/config/environments/development.rb b/config/environments/development.rb index 09a451f..701fb3d 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -15,4 +15,6 @@ config.action_controller.perform_caching = false config.action_view.cache_template_extensions = false # Don't care if the mailer can't send -config.action_mailer.raise_delivery_errors = false \ No newline at end of file +config.action_mailer.raise_delivery_errors = false + +#ATTACHMENT_FU_TEMPFILE_PATH = "/tmp/attachment_fu" diff --git a/doc/README_FOR_APP b/doc/README_FOR_APP index 7a6e7cb..5e0ba98 100644 --- a/doc/README_FOR_APP +++ b/doc/README_FOR_APP @@ -54,15 +54,22 @@ issue@kwestie.com issue # make -C /etc/mail # /sbin/service sendmail restart -{ In Mutt or some other mailer where you can change your from address, compose the following email } +== Attachments == -to: issue -from: clarkbw@gmail.com -subject: test mail issue -body: test mail body +In fedora, with the default sendmail you'll need to give the sendmail group permissions on two directories to be able to save files coming in from email. If these directories don't exist then go ahead and create them. + +chgrp mail tmp/attachment_fu +chmod g+w tmp/attachment_fu -==== +chgrp mail public/attachments +chmod g+w public/attachments -Use this README file to introduce your application and point to useful places in the API for learning more. -Run "rake appdoc" to generate API documentation for your models and controllers. +== Sending Mail == +In Mutt or some other mailer where you can change your from address, compose the following email + +to: issue +from: clarkbw@localhost.localdomain +subject: test mail issue +body: test mail body +attachment: <> diff --git a/public/attachments/.gitignore b/public/attachments/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/tmp/attachment_fu/.gitignore b/tmp/attachment_fu/.gitignore new file mode 100644 index 0000000..e69de29 -- 2.11.4.GIT