Allow +1 via email

This commit is contained in:
James Kiesel 2015-12-30 12:17:45 +01:00
parent e45caa7e2e
commit 86da47880a
5 changed files with 132 additions and 2 deletions

View file

@ -21,6 +21,7 @@ module Email
class ReplyUserNotFoundError < ProcessingError; end class ReplyUserNotFoundError < ProcessingError; end
class ReplyUserNotMatchingError < ProcessingError; end class ReplyUserNotMatchingError < ProcessingError; end
class InactiveUserError < ProcessingError; end class InactiveUserError < ProcessingError; end
class InvalidPostAction < ProcessingError; end
attr_reader :body, :email_log attr_reader :body, :email_log
@ -103,7 +104,11 @@ module Email
raise ReplyUserNotFoundError if user.blank? raise ReplyUserNotFoundError if user.blank?
raise ReplyUserNotMatchingError if @email_log.user_id != user.id raise ReplyUserNotMatchingError if @email_log.user_id != user.id
create_reply(@email_log) if post_action_type = post_action_for(@body)
create_post_action(@email_log, post_action_type)
else
create_reply(@email_log)
end
end end
rescue Encoding::UndefinedConversionError, Encoding::InvalidByteSequenceError => e rescue Encoding::UndefinedConversionError, Encoding::InvalidByteSequenceError => e
@ -239,6 +244,18 @@ module Email
@body = "[quote=\"#{user_email}\"]\n#{@body}\n[/quote]" @body = "[quote=\"#{user_email}\"]\n#{@body}\n[/quote]"
end end
def create_post_action(email_log, type)
PostAction.act(email_log.user, email_log.post, type)
rescue PostAction::AlreadyActed => e
raise InvalidPostAction.new(e)
end
def post_action_for(body)
if ['+1', I18n.t('post_action_types.like.title').downcase].include? body.downcase
PostActionType.types[:like]
end
end
def create_reply(email_log) def create_reply(email_log)
create_post_with_attachments(email_log.user, create_post_with_attachments(email_log.user,
raw: @body, raw: @body,

View file

@ -340,6 +340,45 @@ This is a link http://example.com"
expect(Upload.find_by(sha1: upload_sha)).not_to eq(nil) expect(Upload.find_by(sha1: upload_sha)).not_to eq(nil)
end end
describe 'Liking via email' do
let!(:reply_key) { '636ca428858779856c226bb145ef4fad' }
let(:replied_user_like_params) { { user: replying_user, post: post, post_action_type_id: PostActionType.types[:like] } }
let(:replied_user_like) { PostAction.find_by(replied_user_like_params) }
describe "plus_one.eml" do
let!(:email_raw) {
fixture_file("emails/plus_one.eml")
.gsub("TO", "reply+#{reply_key}@appmail.adventuretime.ooo")
.gsub("FROM", replying_user_email)
}
it "adds a user like to the post" do
expect { receiver.process }.to change { PostAction.count }.by(1)
expect(replied_user_like).to be_present
end
it "does not create a duplicate like" do
PostAction.create(replied_user_like_params)
before_count = PostAction.count
expect { receiver.process }.to raise_error(Email::Receiver::InvalidPostAction)
expect(PostAction.count).to eq before_count
expect(replied_user_like).to be_present
end
end
describe "like.eml" do
let!(:email_raw) {
fixture_file("emails/like.eml")
.gsub("TO", "reply+#{reply_key}@appmail.adventuretime.ooo")
.gsub("FROM", replying_user_email)
}
it 'adds a user like to the post' do
expect { receiver.process }.to change { PostAction.count }.by(1)
expect(replied_user_like).to be_present
end
end
end
end end
# === Failure Conditions === # === Failure Conditions ===

37
spec/fixtures/emails/like.eml vendored Normal file
View file

@ -0,0 +1,37 @@
Return-Path: <FROM>
Received: from iceking.adventuretime.ooo ([unix socket]) by iceking (Cyrus v2.2.13-Debian-2.2.13-19+squeeze3) with LMTPA; Thu, 13 Jun 2013 17:03:50 -0400
Received: from mail-ie0-x234.google.com (mail-ie0-x234.google.com [IPv6:2607:f8b0:4001:c03::234]) by iceking.adventuretime.ooo (8.14.3/8.14.3/Debian-9.4) with ESMTP id r5DL3nFJ016967 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=NOT) for <reply+59d8df8370b7e95c5a49fbf86aeb2c93@appmail.adventuretime.ooo>; Thu, 13 Jun 2013 17:03:50 -0400
Received: by mail-ie0-f180.google.com with SMTP id f4so21977375iea.25 for <reply+59d8df8370b7e95c5a49fbf86aeb2c93@appmail.adventuretime.ooo>; Thu, 13 Jun 2013 14:03:48 -0700
Received: by 10.0.0.1 with HTTP; Thu, 13 Jun 2013 14:03:48 -0700
Date: Thu, 13 Jun 2013 17:03:48 -0400
From: FROM
To: TO
Message-ID: <CADkmRc+rNGAGGbV2iE5p918UVy4UyJqVcXRO2=otppgzduJSg@mail.gmail.com>
Subject: re: [Discourse Meta] eviltrout posted in 'Adventure Time Sux'
Mime-Version: 1.0
Content-Type: text/plain;
charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
X-Sieve: CMU Sieve 2.2
X-Received: by 10.0.0.1 with SMTP id n7mr11234144ipb.85.1371157428600; Thu,
13 Jun 2013 14:03:48 -0700 (PDT)
X-Scanned-By: MIMEDefang 2.69 on IPv6:2001:470:1d:165::1
LIKE
On Sun, Jun 9, 2013 at 1:39 PM, eviltrout via Discourse Meta
<reply+59d8df8370b7e95c5a49fbf86aeb2c93@appmail.adventuretime.ooo> wrote:
>
>
>
> eviltrout posted in 'Adventure Time Sux' on Discourse Meta:
>
> ---
> hey guys everyone knows adventure time sucks!
>
> ---
> Please visit this link to respond: http://localhost:3000/t/adventure-time-sux/1234/3
>
> To unsubscribe from these emails, visit your [user preferences](http://localhost:3000/user_preferences).
>

37
spec/fixtures/emails/plus_one.eml vendored Normal file
View file

@ -0,0 +1,37 @@
Return-Path: <FROM>
Received: from iceking.adventuretime.ooo ([unix socket]) by iceking (Cyrus v2.2.13-Debian-2.2.13-19+squeeze3) with LMTPA; Thu, 13 Jun 2013 17:03:50 -0400
Received: from mail-ie0-x234.google.com (mail-ie0-x234.google.com [IPv6:2607:f8b0:4001:c03::234]) by iceking.adventuretime.ooo (8.14.3/8.14.3/Debian-9.4) with ESMTP id r5DL3nFJ016967 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=NOT) for <reply+59d8df8370b7e95c5a49fbf86aeb2c93@appmail.adventuretime.ooo>; Thu, 13 Jun 2013 17:03:50 -0400
Received: by mail-ie0-f180.google.com with SMTP id f4so21977375iea.25 for <reply+59d8df8370b7e95c5a49fbf86aeb2c93@appmail.adventuretime.ooo>; Thu, 13 Jun 2013 14:03:48 -0700
Received: by 10.0.0.1 with HTTP; Thu, 13 Jun 2013 14:03:48 -0700
Date: Thu, 13 Jun 2013 17:03:48 -0400
From: FROM
To: TO
Message-ID: <CADkmRc+rNGAGGbV2iE5p918UVy4UyJqVcXRO2=otppgzduJSg@mail.gmail.com>
Subject: re: [Discourse Meta] eviltrout posted in 'Adventure Time Sux'
Mime-Version: 1.0
Content-Type: text/plain;
charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
X-Sieve: CMU Sieve 2.2
X-Received: by 10.0.0.1 with SMTP id n7mr11234144ipb.85.1371157428600; Thu,
13 Jun 2013 14:03:48 -0700 (PDT)
X-Scanned-By: MIMEDefang 2.69 on IPv6:2001:470:1d:165::1
+1
On Sun, Jun 9, 2013 at 1:39 PM, eviltrout via Discourse Meta
<reply+59d8df8370b7e95c5a49fbf86aeb2c93@appmail.adventuretime.ooo> wrote:
>
>
>
> eviltrout posted in 'Adventure Time Sux' on Discourse Meta:
>
> ---
> hey guys everyone knows adventure time sucks!
>
> ---
> Please visit this link to respond: http://localhost:3000/t/adventure-time-sux/1234/3
>
> To unsubscribe from these emails, visit your [user preferences](http://localhost:3000/user_preferences).
>

View file

@ -18,4 +18,4 @@ X-Received: by 10.0.0.1 with SMTP id n7mr11234144ipb.85.1371157428600; Thu,
X-Scanned-By: MIMEDefang 2.69 on IPv6:2001:470:1d:165::1 X-Scanned-By: MIMEDefang 2.69 on IPv6:2001:470:1d:165::1
+1 ok