mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-27 09:36:19 -05:00
Merge pull request #3943 from gdpelican/plus-one-via-email
Allow +1 via email
This commit is contained in:
commit
f0694d491a
7 changed files with 182 additions and 2 deletions
|
@ -1,5 +1,6 @@
|
|||
require_dependency 'new_post_manager'
|
||||
require_dependency 'email/html_cleaner'
|
||||
require_dependency 'post_action_creator'
|
||||
|
||||
module Email
|
||||
|
||||
|
@ -21,6 +22,7 @@ module Email
|
|||
class ReplyUserNotFoundError < ProcessingError; end
|
||||
class ReplyUserNotMatchingError < ProcessingError; end
|
||||
class InactiveUserError < ProcessingError; end
|
||||
class InvalidPostAction < ProcessingError; end
|
||||
|
||||
attr_reader :body, :email_log
|
||||
|
||||
|
@ -103,7 +105,11 @@ module Email
|
|||
raise ReplyUserNotFoundError if user.blank?
|
||||
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
|
||||
|
||||
rescue Encoding::UndefinedConversionError, Encoding::InvalidByteSequenceError => e
|
||||
|
@ -239,6 +245,18 @@ module Email
|
|||
@body = "[quote=\"#{user_email}\"]\n#{@body}\n[/quote]"
|
||||
end
|
||||
|
||||
def create_post_action(email_log, type)
|
||||
PostActionCreator.new(email_log.user, email_log.post).perform(type)
|
||||
rescue Discourse::InvalidAccess, 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)
|
||||
create_post_with_attachments(email_log.user,
|
||||
raw: @body,
|
||||
|
|
20
lib/post_action_creator.rb
Normal file
20
lib/post_action_creator.rb
Normal file
|
@ -0,0 +1,20 @@
|
|||
# creates post actions based on a post and a user
|
||||
class PostActionCreator
|
||||
|
||||
def initialize(user, post)
|
||||
@user = user
|
||||
@post = post
|
||||
end
|
||||
|
||||
def perform(action)
|
||||
guardian.ensure_post_can_act!(@post, action, taken_actions: PostAction.counts_for([@post].compact, @user)[@post.try(:id)])
|
||||
PostAction.act(@user, @post, action)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def guardian
|
||||
@guardian ||= Guardian.new(@user)
|
||||
end
|
||||
|
||||
end
|
|
@ -340,6 +340,53 @@ This is a link http://example.com"
|
|||
expect(Upload.find_by(sha1: upload_sha)).not_to eq(nil)
|
||||
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
|
||||
|
||||
it "does not allow unauthorized happiness" do
|
||||
post.trash!
|
||||
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_not 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
|
||||
|
||||
# === Failure Conditions ===
|
||||
|
|
21
spec/components/post_action_creator_spec.rb
Normal file
21
spec/components/post_action_creator_spec.rb
Normal file
|
@ -0,0 +1,21 @@
|
|||
require 'rails_helper'
|
||||
require 'post_action_creator'
|
||||
|
||||
describe PostActionCreator do
|
||||
let(:user) { Fabricate(:user) }
|
||||
let(:post) { Fabricate(:post) }
|
||||
let(:like_type_id) { PostActionType.types[:like] }
|
||||
|
||||
|
||||
describe 'perform' do
|
||||
it 'creates a post action' do
|
||||
expect { PostActionCreator.new(user, post).perform(like_type_id) }.to change { PostAction.count }.by(1)
|
||||
expect(PostAction.find_by(user: user, post: post, post_action_type_id: like_type_id)).to be_present
|
||||
end
|
||||
|
||||
it 'does not create an invalid post action' do
|
||||
expect { PostActionCreator.new(user, nil).perform(like_type_id) }.to raise_error(Discourse::InvalidAccess)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
37
spec/fixtures/emails/like.eml
vendored
Normal file
37
spec/fixtures/emails/like.eml
vendored
Normal 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
37
spec/fixtures/emails/plus_one.eml
vendored
Normal 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).
|
||||
>
|
2
spec/fixtures/emails/too_short.eml
vendored
2
spec/fixtures/emails/too_short.eml
vendored
|
@ -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
|
||||
|
||||
|
||||
+1
|
||||
ok
|
||||
|
|
Loading…
Reference in a new issue