diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index 6c90441afb..29c2f4c939 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -32,6 +32,8 @@ use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\UID; use Utopia\Locale\Locale; use Appwrite\Extend\Exception; +use Appwrite\Messaging\Adapter\Realtime; +use Appwrite\Utopia\Response\Model\User; use Utopia\Validator\ArrayList; use Utopia\Validator\Assoc; use Utopia\Validator\Range; @@ -494,6 +496,63 @@ App::get('/v1/account/sessions/oauth2/:provider/redirect') 'memberships' => null, 'search' => implode(' ', [$userId, $email, $name]) ]))); + + /** Trigger Webhook */ + $userModel = new User(); + + $userCreate = new Event(Event::WEBHOOK_QUEUE_NAME, Event::WEBHOOK_CLASS_NAME); + $userCreate + ->setProject($project) + ->setUser($user) + ->setEvent('users.[userId].create') + ->setParam('userId', $userId) + ->setPayload($user->getArrayCopy(array_keys(($userModel->getRules())))) + ->trigger(); + + /** Trigger Functions */ + $userCreate + ->setClass(Event::FUNCTIONS_CLASS_NAME) + ->setQueue(Event::FUNCTIONS_QUEUE_NAME) + ->trigger(); + + /** Trigger realtime event */ + $allEvents = Event::generateEvents('users.[userId].create', [ + 'userId' => $userId + ]); + + $target = Realtime::fromPayload( + event: $allEvents[0], + payload: $user + ); + + Realtime::send( + projectId: 'console', + payload: $user->getArrayCopy(), + events: $allEvents, + channels: $target['channels'], + roles: $target['roles'] + ); + + Realtime::send( + projectId: $project->getId(), + payload: $user->getArrayCopy(), + events: $allEvents, + channels: $target['channels'], + roles: $target['roles'] + ); + + /** Update usage stats */ + global $register; + if (App::getEnv('_APP_USAGE_STATS', 'enabled') === 'enabled') { + $statsd = $register->get('statsd'); + $usage = new Stats($statsd); + $usage + ->setParam('projectId', $project->getId()) + ->setParam('userId', $user->getId()) + ->setParam('users.create', 1) + ->submit(); + } + } catch (Duplicate $th) { throw new Exception('Account already exists', 409, Exception::USER_ALREADY_EXISTS); }