Skip to content

Commit

Permalink
Merge pull request #771 from bakaphp/refact-social-notifications
Browse files Browse the repository at this point in the history
refact: allow to send notification base on kanvas notification type
  • Loading branch information
kaioken authored Jan 1, 2024
2 parents 3e29c3e + eddfa94 commit 6ed11bf
Show file tree
Hide file tree
Showing 43 changed files with 1,256 additions and 480 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ jobs:
- name: Setup Kanvas Ecosystem
run: php artisan kanvas:setup-ecosystem

- name: create .env file
run: echo '' > .env

- name: Run Tests
if: success()
run: php artisan test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@
use Kanvas\AccessControlList\Enums\RolesEnums;
use Kanvas\Apps\Models\Apps;
use Kanvas\Notifications\Actions\EvaluateNotificationsLogicAction;
use Kanvas\Notifications\Actions\SendMessageNotificationsToAllFollowersAction;
use Kanvas\Notifications\Actions\SendMessageNotificationsToOneFollowerAction;
use Kanvas\Notifications\Jobs\SendMessageNotificationsToAllFollowersJob;
use Kanvas\Notifications\Jobs\SendMessageNotificationsToUsersJob;
use Kanvas\Notifications\Models\NotificationTypes;
use Kanvas\Notifications\Repositories\NotificationTypesMessageLogicRepository;
use Kanvas\Notifications\Templates\Blank;
use Kanvas\Social\MessagesTypes\Repositories\MessagesTypesRepository;
use Kanvas\Users\Models\Users;
use Kanvas\Social\Messages\DataTransferObject\MessagesNotificationMetadata;
use Kanvas\Users\Repositories\UsersRepository;
use Kanvas\Social\Messages\DataTransferObject\MessagesNotificationsPayloadDto;

class NotificationsManagementMutation
{
/**
* sendNotificationBaseOnTemplate
* @deprecated use sendNotificationByMessage
* @psalm-suppress MixedArgument
*/
public function sendNotificationBaseOnTemplate(mixed $root, array $request): bool
Expand Down Expand Up @@ -52,42 +52,71 @@ public function sendNotificationBaseOnTemplate(mixed $root, array $request): boo
* sendNotificationByMessage
* @psalm-suppress MixedArgument
*/
public function sendNotificationByMessage(mixed $root, array $request): bool
public function sendNotificationByMessage(mixed $root, array $request): array
{
$app = app(Apps::class);
$user = auth()->user();

$notificationMessagePayload = MessagesNotificationsPayloadDto::fromArray($request);
$notificationMessagePayload = MessagesNotificationMetadata::fromArray($request);
$notificationType = NotificationTypes::getById($notificationMessagePayload->notificationTypeId, $app);
$notificationTypeMessageLogic = NotificationTypesMessageLogicRepository::getByNotificationType($app, $notificationType);

// TODO Maybe get rid of the notification_type_id on notification_types_message_logic table, not doing anything there?
$messageType = MessagesTypesRepository::getByVerb($notificationMessagePayload->verb, $app);
$notificationTypeMessageLogic = NotificationTypesMessageLogicRepository::getByMessageType($app, $messageType->getId());
$evaluateNotificationsLogic = new EvaluateNotificationsLogicAction($notificationTypeMessageLogic, $notificationMessagePayload->message);
$results = $evaluateNotificationsLogic->execute();
if (! $notificationType) {
return [
'sent' => false,
'message' => 'Notification type not found',
];
}

$canSendNotification = true;

if ($results) {
if ($notificationMessagePayload->type == 'one' && $follower = Users::getById($notificationMessagePayload->follower_id)) {
$sendNotificationsToFollower = new SendMessageNotificationsToOneFollowerAction(
$user,
$follower,
$app,
$notificationMessagePayload
);
$sendNotificationsToFollower->execute();
if ($notificationTypeMessageLogic) {
$canSendNotification = (new EvaluateNotificationsLogicAction(
$app,
$user,
$notificationTypeMessageLogic,
$notificationMessagePayload->message
))->execute();
}

return true;
}
if (! $canSendNotification) {
return [
'sent' => false,
'message' => 'Notification logic not met',
];
}

$sendNotificationsToFollowers = new SendMessageNotificationsToAllFollowersAction(
if ($notificationMessagePayload->distributeToSpecificUsers()) {
SendMessageNotificationsToUsersJob::dispatch(
$user,
$app,
$notificationType,
$notificationMessagePayload
);
$sendNotificationsToFollowers->execute();

return true;
return [
'sent' => true,
'message' => 'Notification sent to users ' . implode(',', $notificationMessagePayload->usersId),
];
}

if (! $notificationMessagePayload->distributeToFollowers()) {
return [
'sent' => false,
'message' => 'Notification distribution type not found in request payload',
];
}

return false;
SendMessageNotificationsToAllFollowersJob::dispatch(
$user,
$app,
$notificationType,
$notificationMessagePayload
);

return [
'sent' => true,
'message' => 'Notification sent to all followers',
];
}
}
Loading

0 comments on commit 6ed11bf

Please sign in to comment.