Implemented oauth session revocation notification.

Reworked webhooks notifications constructors
This commit is contained in:
ErickSkrauch
2020-10-01 01:40:28 +03:00
parent b904d5d314
commit 5fc97fdd7a
13 changed files with 283 additions and 137 deletions

View File

@@ -3,82 +3,44 @@ declare(strict_types=1);
namespace common\tasks;
use common\models\Account;
use common\models\WebHook;
use Yii;
use common\notifications\NotificationInterface;
use yii\db\Expression;
use yii\queue\RetryableJobInterface;
final class CreateWebHooksDeliveries implements RetryableJobInterface {
public string $type;
private NotificationInterface $notification;
public array $payloads;
public function __construct(string $type, array $payloads) {
$this->type = $type;
$this->payloads = $payloads;
public function __construct(NotificationInterface $notification) {
$this->notification = $notification;
}
public static function createAccountEdit(Account $account, array $changedAttributes): self {
return new static('account.edit', [
'id' => $account->id,
'uuid' => $account->uuid,
'username' => $account->username,
'email' => $account->email,
'lang' => $account->lang,
'isActive' => $account->status === Account::STATUS_ACTIVE,
'isDeleted' => $account->status === Account::STATUS_DELETED,
'registered' => date('c', (int)$account->created_at),
'changedAttributes' => $changedAttributes,
]);
}
public static function createAccountDeletion(Account $account): self {
return new static('account.deletion', [
'id' => $account->id,
'uuid' => $account->uuid,
'username' => $account->username,
'email' => $account->email,
'registered' => date('c', (int)$account->created_at),
'deleted' => date('c', (int)$account->deleted_at),
]);
}
/**
* @return int time to reserve in seconds
*/
public function getTtr(): int {
return 10;
}
/**
* @param int $attempt number
* @param \Exception|\Throwable $error from last execute of the job
*
* @return bool
*/
public function canRetry($attempt, $error): bool {
return true;
}
/**
* @param \yii\queue\Queue $queue which pushed and is handling the job
*/
public function execute($queue): void {
$type = $this->notification::getType();
$payloads = $this->notification->getPayloads();
/** @var WebHook[] $targets */
$targets = WebHook::find()
// It's very important to use exactly single quote to begin the string
// and double quote to specify the string value
->andWhere(new Expression("JSON_CONTAINS(`events`, '\"{$this->type}\"')"))
->andWhere(new Expression("JSON_CONTAINS(`events`, '\"{$type}\"')"))
->all();
foreach ($targets as $target) {
$job = new DeliveryWebHook();
$job->type = $this->type;
$job->type = $type;
$job->url = $target->url;
$job->secret = $target->secret;
$job->payloads = $this->payloads;
Yii::$app->queue->push($job);
$job->payloads = $payloads;
$queue->push($job);
}
}