Commit 754c3a2d authored by Marcelo Rivera's avatar Marcelo Rivera

(feat): auto-subscribe on login and signup when on pro standalone

1 merge request!308WIP: (feat): Minds Pro
Pipeline #82522613 failed with stages
in 10 minutes and 22 seconds
......@@ -16,6 +16,8 @@ use Minds\Entities;
use Minds\Interfaces;
use Minds\Api\Factory;
use Minds\Exceptions\TwoFactorRequired;
use Minds\Core\Queue;
use Minds\Core\Subscriptions;
class authenticate implements Interfaces\Api, Interfaces\ApiIgnorePam
{
......@@ -46,6 +48,9 @@ class authenticate implements Interfaces\Api, Interfaces\ApiIgnorePam
}
$user = new Entities\User(strtolower($_POST['username']));
$from = $_POST['from'] ?? null;
/** @var Core\Security\LoginAttempts $attempts */
$attempts = Core\Di\Di::_()->get('Security\LoginAttempts');
......@@ -106,6 +111,28 @@ class authenticate implements Interfaces\Api, Interfaces\ApiIgnorePam
Di::_()->get('Features\Manager')
->setCanaryCookie($user->isCanary());
// auto-subscribe to channel
if ($from) {
$targetChannel = new Entities\User($from);
$manager = (new Subscriptions\Manager())
->setSubscriber($user);
if (!$manager->isSubscribed($targetChannel)) {
$manager->subscribe($targetChannel);
}
//TODO: move Core/Subscriptions/Delegates
$event = new Core\Analytics\Metrics\Event();
$event->setType('action')
->setAction('subscribe')
->setProduct('platform')
->setUserGuid((string) Core\Session::getLoggedInUser()->guid)
->setUserPhoneNumberHash(Core\Session::getLoggedInUser()->getPhoneNumberHash())
->setEntityGuid((string) $from)
->push();
}
$response['status'] = 'success';
$response['user'] = $user->export();
......@@ -119,7 +146,7 @@ class authenticate implements Interfaces\Api, Interfaces\ApiIgnorePam
public function delete($pages)
{
$sessions = Di::_()->get('Sessions\Manager');
if (isset($pages[0]) && $pages[0] === 'all') {
$sessions->destroy(true);
} else {
......
......@@ -5,14 +5,13 @@
* @version 1
* @author Mark Harding
*/
namespace Minds\Controllers\api\v1;
use Minds\Api\Factory;
use Minds\Core;
use Minds\Core\Di\Di;
use Minds\Entities;
use Minds\Interfaces;
use Minds\Api\Factory;
use Minds\Helpers;
class register implements Interfaces\Api, Interfaces\ApiIgnorePam
{
......@@ -21,7 +20,7 @@ class register implements Interfaces\Api, Interfaces\ApiIgnorePam
*/
public function get($pages)
{
return Factory::response(['status'=>'error', 'message'=>'GET is not supported for this endpoint']);
return Factory::response(['status' => 'error', 'message' => 'GET is not supported for this endpoint']);
}
/**
......@@ -37,11 +36,11 @@ class register implements Interfaces\Api, Interfaces\ApiIgnorePam
public function post($pages)
{
if (!isset($_POST['username']) || !isset($_POST['password']) || !isset($_POST['username']) || !isset($_POST['email'])) {
return Factory::response(['status'=>'error']);
return Factory::response(['status' => 'error']);
}
if (!$_POST['username'] || !$_POST['password'] || !$_POST['username'] || !$_POST['email']) {
return Factory::response(['status'=>'error', 'message' => "Please fill out all the fields"]);
return Factory::response(['status' => 'error', 'message' => "Please fill out all the fields"]);
}
try {
......@@ -85,6 +84,10 @@ class register implements Interfaces\Api, Interfaces\ApiIgnorePam
'referrer' => isset($_COOKIE['referrer']) ? $_COOKIE['referrer'] : '',
];
if (isset($_POST['from'])) {
$params['from'] = (string) $_POST['from'];
}
// TODO: Move full reguster flow to the core
elgg_trigger_plugin_hook('register', 'user', $params, true);
Core\Events\Dispatcher::trigger('register', 'user', $params);
......@@ -96,11 +99,11 @@ class register implements Interfaces\Api, Interfaces\ApiIgnorePam
$sessions->save(); // Save to db and cookie
$response = [
'guid' => $guid,
'user' => $user->export()
'guid' => $guid,
'user' => $user->export(),
];
} catch (\Exception $e) {
$response = ['status'=>'error', 'message'=>$e->getMessage()];
$response = ['status' => 'error', 'message' => $e->getMessage()];
}
return Factory::response($response);
}
......
<?php
namespace Minds\Core\Queue\Runners;
use Minds\Core\Di\Di;
......@@ -24,11 +23,15 @@ class Registered implements QueueRunner
->receive(function ($data) use ($subscriptions, $repository) {
$data = $data->getData();
$user_guid = $data['user_guid'];
$from = $data['from'];
//subscribe to minds channel
$subscriber = new User($user_guid);
$subscriber->subscribe('100000000000000519');
if ($from) {
$subscriber->subscribe($from);
}
echo "[registered]: User registered $user_guid\n";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment