Skip to content

Commit

Permalink
Incoming mail monitoring/logging (publiclab#9056)
Browse files Browse the repository at this point in the history
* Incoming mail monitoring/logging

* Update script/mailman_server

* Update script/mailman_server

* Update comment.rb

* Update mailman_server

* Update mailman_server

* Update outlook_parsing_test.rb

* Update gmail_parsing_test.rb

* Update token_comment_test.rb

* Update yahoo_parsing_test.rb
  • Loading branch information
jywarren authored Jan 28, 2021
1 parent b300979 commit 7e663aa
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 10 deletions.
9 changes: 5 additions & 4 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -232,24 +232,25 @@ def user_reactions_map
user_like_map
end

def self.receive_mail(mail)
def self.new_comment_from_email(mail)
user = User.where(email: mail.from.first).first
if user
node_id = mail.subject[/#([\d]+)/, 1] # This tooks out the node ID from the subject line
comment_id = mail.subject[/#c([\d]+)/, 1] # This tooks out the comment ID from the subject line if it exists
unless Comment.where(message_id: mail.message_id).any?
if node_id.present? && !comment_id.present?
add_comment(mail, node_id, user)
parse_comment_from_email(mail, node_id, user)
elsif comment_id.present?
comment = Comment.find comment_id
add_comment(mail, comment.nid, user, [true, comment.id])
parse_comment_from_email(mail, comment.nid, user, [true, comment.id])
end
end
return node_id
end
end

# parse mail and add comments based on emailed replies
def self.add_comment(mail, node_id, user, reply_to = [false, nil])
def self.parse_comment_from_email(mail, node_id, user, reply_to = [false, nil])
node = Node.where(nid: node_id).first
if node && mail&.html_part
mail_doc = Nokogiri::HTML(mail&.html_part&.body&.decoded) # To parse the mail to extract comment content and reply content
Expand Down
10 changes: 9 additions & 1 deletion script/mailman_server
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ Mailman::Application.run do
# retries ||= 0
# do something with the database that does not fetch mails yet (let's try to ensure we have a MySQL connection first)
puts Comment.last.inspect
Comment.receive_mail(message)
Mailman.logger.info "Mail received from #{message.from.first}: '#{message.subject}' - '#{message&.html_part}'"
if message.subject == "Incoming mail parsing test"
Mailman.logger.info "Received incoming mail parsing test from #{message.from.first} at #{Time.now.to_s}"
end
last_comment = Comment.last
comment_node_id = Comment.new_comment_from_email(message)
if Comment.last.id != last_comment.id
Mailman.logger.info "Comment created from #{message.from.first}: '#{message.subject}' - https://publiclab.org/n/#{comment_node_id}"
end
# rescue Exception => e
# Mailman.logger.error "Exception occurred while receiving message:\n#{message}"
# Mailman.logger.error [e, *e.backtrace].join("\n")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class GmailParsingTest < ActionDispatch::IntegrationTest
mail = Mail.read('test/fixtures/incoming_test_emails/gmail/incoming_gmail_email.eml')
node = Node.find(21) # this is the nid used in the .eml fixture
mail.subject = "Re: (##{node.id})"
Comment.receive_mail(mail)
Comment.new_comment_from_email(mail)
f = File.open('test/fixtures/incoming_test_emails/gmail/final_parsed_comment.txt', 'r')
reply = Comment.last # this should be the just-created comment
user_email = mail.from.first
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class OutlookParsingTest < ActionDispatch::IntegrationTest
mail = Mail.read('test/fixtures/incoming_test_emails/outlook/incoming_outlook_email.eml')
comment = comments(:first)
mail.subject = "Re: (##{comment.nid}) - #c#{comment.id}"
Comment.receive_mail(mail)
Comment.new_comment_from_email(mail)
f = File.open('test/fixtures/incoming_test_emails/outlook/final_parsed_comment.txt', 'r')
reply = Comment.last
user_email = mail.from.first
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class YahooParsingTest < ActionDispatch::IntegrationTest
# Mail contain ["[email protected]"] in from field.
node = Node.find(21) # this is the nid used in the .eml fixture
mail.subject = "Re: (##{node.id})"
Comment.receive_mail(mail)
Comment.new_comment_from_email(mail)
f = File.open('test/fixtures/incoming_test_emails/yahoo/final_parsed_comment.txt', 'r')
comment = Comment.last # this should be the just-created comment
user_email = mail.from.first
Expand Down
4 changes: 2 additions & 2 deletions test/integration/token_comment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class TokenCommentTest < ActionDispatch::IntegrationTest
node = Node.last
mail.subject = "Re: #{node.title} (##{node.nid})"
mail.from = ["[email protected]"]
Comment.receive_mail(mail)
Comment.new_comment_from_email(mail)
f = File.open('test/fixtures/incoming_test_emails/gmail/final_parsed_comment.txt', 'r')
comment = Comment.last
assert_equal comment.comment, f.read
Expand All @@ -40,7 +40,7 @@ class TokenCommentTest < ActionDispatch::IntegrationTest
mail = Mail.read('test/fixtures/incoming_test_emails/yahoo/incoming_yahoo_email.eml')
node = Node.last
mail.subject = "Re: #{node.title} (##{node.nid})"
Comment.receive_mail(mail)
Comment.new_comment_from_email(mail)
f = File.open('test/fixtures/incoming_test_emails/yahoo/final_parsed_comment.txt', 'r')
comment = Comment.last
user_email = mail.from.first
Expand Down

0 comments on commit 7e663aa

Please sign in to comment.