Skip to content
This repository has been archived by the owner on Nov 19, 2021. It is now read-only.

Commit

Permalink
Merge pull request #609 from duckduckgo/cmpn-mail
Browse files Browse the repository at this point in the history
Allow non-account email for /wear updates
  • Loading branch information
jbarrett committed Apr 1, 2015
2 parents e3ba37f + ba00e54 commit 5cc5722
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 6 deletions.
1 change: 1 addition & 0 deletions lib/DDGC/Config.pm
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ sub campaigns {
question1 => "How did you hear about DuckDuckGo?",
question2 => "How long have you been a DuckDuckGo user?",
question3 => "Is this your first time spreading DuckDuckGo to others?",
question4 => "Share your email so that we can send updates on the T-shirt campaign.",
},
share_followup => {
id => 2,
Expand Down
33 changes: 32 additions & 1 deletion lib/DDGC/DB/Result/User/CampaignNotice.pm
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,40 @@ column bad_response => {
default_value => 0,
};

column campaign_email_is_account_email => {
data_type => 'bigint',
is_nullable => 0,
default_value => 0,
};

column campaign_email => {
data_type => 'text',
is_nullable => 1,
};

column campaign_email_verified => {
data_type => 'tinyint',
is_nullable => 0,
default_value => 0,
};

sub get_verified_campaign_email {
my ( $self ) = @_;

return '' if $self->bad_response;

if ( $self->campaign_email_is_account_email ) {
return $self->user->email if $self->user->email_verified;
return '';
}

return $self->campaign_email if $self->campaign_email_verified;
return '';
}

primary_key ( qw/users_id campaign_id campaign_source/ );

belongs_to 'notice_user', 'DDGC::DB::Result::User', 'users_id', {
belongs_to 'user', 'DDGC::DB::Result::User', 'users_id', {
on_delete => 'no action',
};

Expand Down
60 changes: 55 additions & 5 deletions lib/DDGC/Web/Controller/Campaign/SubmitResponse.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ use DDGC::Config;
use Try::Tiny;
use DateTime;
use DateTime::Duration;
use Email::Valid;
use Lingua::Identify qw(:language_identification);

sub base :Chained('/') :PathPart('campaign') :CaptureArgs(0) {
sub base :Chained('/base') :PathPart('campaign') :CaptureArgs(0) {
my ( $self, $c ) = @_;
if (!$c->user) {
$c->response->status(403);
Expand All @@ -19,7 +20,7 @@ sub base :Chained('/') :PathPart('campaign') :CaptureArgs(0) {
return $c->detach;
}
elsif (!$c->req->param('campaign_name')) {
$c->response->status(500);
$c->response->status(403);
$c->stash->{x} = {
ok => 0, no_campaign => 1,
errstr => "No campaign info supplied!"
Expand Down Expand Up @@ -49,7 +50,7 @@ sub base :Chained('/') :PathPart('campaign') :CaptureArgs(0) {

sub respond : Chained('base') : PathPart('respond') : Args(0) {
my ( $self, $c ) = @_;
#$c->require_action_token;
$c->require_action_token;

my $to = $c->d->config->share_email // '[email protected]';
my $from = '[email protected]';
Expand Down Expand Up @@ -80,7 +81,7 @@ sub respond : Chained('base') : PathPart('respond') : Args(0) {
}

if ($short_response) {
$c->response->status(500);
$c->response->status(403);
$c->stash->{x} = {
ok => 0, fields_empty => 1,
errstr => "Your responses are too short. Please add more explanation.",
Expand All @@ -89,6 +90,51 @@ sub respond : Chained('base') : PathPart('respond') : Args(0) {
return $c->detach;
}

if ($c->req->param('question4')) {
$c->stash->{campaign_email} = Email::Valid->address($c->req->param('question4'));
if (!$c->stash->{campaign_email}) {
$c->response->status(403);
$c->stash->{x} = {
ok => 0, invalid_email => 1,
errstr => "This email address appears to be invalid.",
};
$c->forward( $c->view('JSON') );
return $c->detach;
}

if (lc($c->stash->{campaign_email}) eq lc($c->user->email)) {
$c->stash->{campaign_email_is_account_email} = 1;
}
else {
my $data = $c->user->data || {};
$data->{wear_email_verify_token} = $c->d->uid;
$c->user->update({ data => $data });
$c->stash->{wear_email_verify_link} =
$c->chained_uri('My','wear_email_verify',$c->user->lowercase_username,$data->{wear_email_verify_token});
my $error = 0;
try {
$c->d->postman->template_mail(
1, $c->stash->{campaign_email}, $from,
'[DuckDuckGo Community] Thank you for participating in Share it + Wear it',
'wearemail',$c->stash);
}
catch {
$error = 1;
};

if ($error) {
$c->response->status(500);
$c->stash->{x} = {
ok => 0, mailer_error => 1,
errstr => "Sorry, there was a problem submitting your response. Please try again later."
};
$c->forward( $c->view('JSON') );
return $c->detach;
}

}
}

my $subject_extra = '';
if ($campaign_name eq 'share') {
($language ne "en" || $confidence < 0.57) && ($subject_extra = '** POSSIBLE SPAM ** ');
Expand Down Expand Up @@ -123,7 +169,11 @@ BAD_RESPONSE_LINK
return $c->detach;
}

$c->user->set_responded_campaign($campaign_name);
my $response = $c->user->set_responded_campaign($campaign_name);
$response->update( ($c->stash->{campaign_email_is_account_email}) ?
{ campaign_email_is_account_email => 1 } :
{ campaign_email => $c->stash->{campaign_email} }
);

my $return_on;
my $coupon;
Expand Down
39 changes: 39 additions & 0 deletions lib/DDGC/Web/Controller/My.pm
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,46 @@ sub email_verify :Chained('base') :Args(2) {
$user->email_verified(1);
$user->update;
$c->stash->{success} = 1;
}

sub wear_email_verify :Chained('base') :Args(2) {
my ( $self, $c, $username, $token ) = @_;

$c->stash->{title} = 'Share + Wear email confirmation token check';
$c->add_bc($c->stash->{title}, '');

my $user = $c->d->find_user($username);

if (!$user || !$token) {
$c->stash->{invalid_token} = 1;
return $c->detach;
}

my $response = $c->d->rs('User::CampaignNotice')->find({
campaign_id => $c->d->config->id_for_campaign('share'),
users_id => $user->id,
responded => { '!=' => undef },
});

if (!$response) {
$c->stash->{invalid_token} = 1;
return $c->detach;
}


unless ($user->data && $user->data->{wear_email_verify_token} &&
($token eq $user->data->{wear_email_verify_token}) ) {
$c->stash->{invalid_token} = 1;
return $c->detach;
}

my $data = $user->data;
delete $data->{wear_email_verify_token};
$user->data($data);
$user->update;
$response->campaign_email_verified(1);
$response->update;
$c->stash->{success} = 1;
}

sub forgotpw_tokencheck :Chained('logged_out') :Args(2) {
Expand Down
13 changes: 13 additions & 0 deletions sql/1.027/cmpn-mail.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- Convert schema 'DDGC::DBOld v1.x' to 'DDGC::DB v1.x':;

BEGIN;

ALTER TABLE user_campaign_notice ADD COLUMN campaign_email_is_account_email bigint DEFAULT 0 NOT NULL;

ALTER TABLE user_campaign_notice ADD COLUMN campaign_email text;

ALTER TABLE user_campaign_notice ADD COLUMN campaign_email_verified smallint DEFAULT 0 NOT NULL;

COMMIT;


17 changes: 17 additions & 0 deletions templates/email/wearemail.tx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

<: include "email/heading.tx" { title => "Verify your email address" } :>
<tr>
<td width="10"></td>
<td align="center" colspan="2">
<table cellpadding="0" cellspacing="5" border="0" bgcolor="#fff" <: style("msg_content") :>>
<tr>
<td colspan="2" valign="middle">
<h4 <: style("default") :>>Please click on the following link to verify your email address</h4>
<p <: style("default") :>><a href="<: $wear_email_verify_link :>">Yes, I signed up for Share it + Wear it with this address</a></p>
<p <: style("sub_text") :>>Regards, <br/><i <: style("signoff") :>>The DuckDuckGo Community team.</i></p>
</td>
</tr>
</table>
</td>
<td width="10"></td>
</tr>
11 changes: 11 additions & 0 deletions templates/my/wear_email_verify.tx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<: if $invalid_token { :>
<div class="notice error">
<i class="icn icon-warning-sign"></i>
This token is invalid.
</div>
<: } else { :>
<div class="notice success">
<i class="icn icon-ok"></i>
You have successfully confirmed your email. Thank you.
</div>
<: } :>
4 changes: 4 additions & 0 deletions templates/wear.tx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@
<div class="input-wrap">
<textarea name="question3"></textarea>
</div>
<label class="shareit-form__lbl" for="question4"><: $campaign_config.question4 :></label>
<div class="input-wrap">
<input name="question4" type="text" value="<: $user.email :>" />
</div>
<input type="hidden" name="campaign_id" value="1">
<input type="hidden" name="campaign_name" value="share">
<input type="hidden" name="action_token" value="<: $action_token :>">
Expand Down

0 comments on commit 5cc5722

Please sign in to comment.