subscribe directly to lbry. fixes #215, #237

This commit is contained in:
Alex Grintsvayg 2017-10-16 15:57:08 -04:00
parent 81b0701fd5
commit 2141ec591f
6 changed files with 9 additions and 2644 deletions

View file

@ -100,7 +100,6 @@ class Controller
$router->any('/list/subscribe', 'MailActions::executeSubscribe'); $router->any('/list/subscribe', 'MailActions::executeSubscribe');
$router->any('/list/subscribed', 'MailActions::executeSubscribed'); $router->any('/list/subscribed', 'MailActions::executeSubscribed');
$router->get('/list/confirm/{hash}', 'MailActions::executeConfirm');
$router->get('/list/unsubscribe/{email}', 'MailActions::executeUnsubscribe'); $router->get('/list/unsubscribe/{email}', 'MailActions::executeUnsubscribe');
$router->any('/dmca', 'ReportActions::executeDmca'); $router->any('/dmca', 'ReportActions::executeDmca');

View file

@ -24,36 +24,15 @@ class MailActions extends Actions
return Controller::redirect(Request::getRelativeUri()); return Controller::redirect(Request::getRelativeUri());
} }
$sent = Mailgun::sendSubscriptionConfirmation($email); $response = LBRY::subscribe($email);
if (!$sent) if ($response['error'])
{ {
return ['mail/subscribe', ['error' => __('email.subscribe_send_failed')]]; return ['mail/subscribe', ['error' => $response['error']]];
} }
return ['mail/subscribe', ['subscribeSuccess' => true, 'nextUrl' => $nextUrl]]; return ['mail/subscribe', ['subscribeSuccess' => true, 'nextUrl' => $nextUrl]];
} }
public static function executeConfirm(string $hash)
{
$email = Mailgun::checkConfirmHashAndGetEmail($hash);
if ($email === null)
{
return ['mail/subscribe', ['error' => __('email.invalid_confirm_hash')]];
}
$sendy = new Sendy(Config::get('sendy_api_key'), Config::get('sendy_install_url'), Sendy::LIST_GENERAL);
try
{
$sendy->subscribe($email);
}
catch (SendyException $e)
{
return ['mail/subscribe', ['error' => $e->getMessage()]];
}
return ['mail/subscribe', ['confirmSuccess' => true, 'learnFooter' => true]];
}
public static function executeSubscribed() public static function executeSubscribed()
{ {
return ['mail/subscribe', ['confirmSuccess' => true, 'learnFooter' => true]]; return ['mail/subscribe', ['confirmSuccess' => true, 'learnFooter' => true]];

View file

@ -6,7 +6,6 @@ $config = [];
// $config['github_developer_credits_client_secret'] = ''; // $config['github_developer_credits_client_secret'] = '';
// $config['lbry_api_server'] = ''; // $config['lbry_api_server'] = '';
// $config['mailchimp_key'] = ''; // $config['mailchimp_key'] = '';
// $config['prefinery_key'] = '';
// $config['asana_key'] = ''; // $config['asana_key'] = '';
// $config['aws_log_access_key'] = ''; // $config['aws_log_access_key'] = '';
// $config['aws_log_secret_key'] = ''; // $config['aws_log_secret_key'] = '';

View file

@ -17,8 +17,13 @@ class LBRY
return $response['data']['lbc_usd'] ?? 0; return $response['data']['lbc_usd'] ?? 0;
} }
public static function subscribe($email)
{
return Curl::post(static::getApiUrl('/list/subscribe'), ['email' => $email], ['json_response' => true]);
}
public static function unsubscribe($email) public static function unsubscribe($email)
{ {
return Curl::post(static::getApiUrl('/user_unsubscribe/new'), ['email' => $email], ['json_response' => true]); return Curl::post(static::getApiUrl('/list/unsubscribe'), ['email' => $email], ['json_response' => true]);
} }
} }

View file

@ -1,132 +0,0 @@
<?php
class SendyException extends Exception
{
}
class Sendy
{
const STATUS_SUBSCRIBED = 'Subscribed';
const STATUS_UNSUBSCRIBED = 'Unsubscribed';
const STATUS_UNCONFIRMED = 'Unconfirmed';
const STATUS_BOUNCED = 'Bounced';
const STATUS_SOFT_BOUNCED = 'Soft bounced';
const STATUS_COMPLAINED = 'Complained';
const LIST_TEST = 'ko9LyWC4SQMI6763xTYCXucA';
const LIST_GENERAL = '5Fax4vQ1c9lku763BKUNb4aQ';
protected $installationUrl;
protected $apiKey;
protected $listId;
public function __construct($apiKey, $installationUrl, $listId)
{
$this->apiKey = $apiKey;
$this->installationUrl = rtrim($installationUrl, '/');
$this->listId = $listId;
}
public static function getAllSubscriptionStatuses()
{
$r = new ReflectionClass(get_called_class());
$constants = [];
foreach ($r->getConstants() as $constant => $value)
{
if (preg_match('/^STATUS_/', $constant))
{
$constants[] = $value;
}
}
return $constants;
}
public function subscribe($email, $name = null, array $extraVars = [])
{
$result = $this->call('subscribe', array_merge($extraVars, [
'email' => $email,
'name' => $name,
'list' => $this->listId,
'boolean' => 'true',
]));
switch ($result)
{
case '1':
return true;
case 'Already subscribed.':
return 'Already subscribed.';
default:
throw new SendyException($result);
}
}
public function unsubscribe($email)
{
$result = $this->call('subscribe', [
'email' => $email,
'list' => $this->listId,
'boolean' => 'true',
]);
if ($result == 1)
{
return true;
}
throw new SendyException($result);
}
public function getSubscriptionStatus($email)
{
$result = $this->callAuth('api/subscribers/subscription-status.php', ['email' => $email, 'list_id' => $this->listId]);
if (in_array($result, static::getAllSubscriptionStatuses()))
{
return $result;
}
throw new SendyException($result);
}
public function getActiveSubscriberCount()
{
$result = $this->callAuth('api/subscribers/active-subscriber-count.php', ['list_id' => $this->listId]);
if (!is_numeric($result))
{
throw new SendyException($result);
}
return (int)$result;
}
public function createCampaign(array $values)
{
$result = $this->callAuth('api/campaigns/create.php', $values);
switch ($result)
{
case 'Campaign created':
case 'Campaign created and now sending':
return $result;
default:
throw new SendyException($result);
}
}
protected function call($endpoint, array $values = [])
{
return Curl::post($this->installationUrl . '/' . $endpoint, $values);
}
protected function callAuth($endpoint, array $values = [])
{
return $this->call($endpoint, array_merge($values, ['api_key' => $this->apiKey]));
}
}

File diff suppressed because it is too large Load diff