Skip to content

Commit

Permalink
PROD-30976: Implement "user is blocked" event for the EDA
Browse files Browse the repository at this point in the history
  • Loading branch information
tregismoreira authored and ribel committed Nov 6, 2024
1 parent aa07c91 commit 2c4f831
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 0 deletions.
84 changes: 84 additions & 0 deletions modules/social_features/social_user/asyncapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,86 @@ channels:
type: string
format: uri
description: Canonical URL of the actor user.
userBlock:
address: com.getopensocial.cms.user.block
messages:
userBlock:
payload:
allOf:
- $ref: '#/components/schemas/cloudEventsSchema'
- type: object
properties:
data:
type: object
properties:
id:
type: string
description: The UUID of the user.
created:
type: string
format: date-time
description: The creation time of the user.
updated:
type: string
format: date-time
description: The last updated time of the user.
status:
type: string
description: The status of the user.
enum:
- active
- blocked
displayName:
type: string
description: The display name of the user.
roles:
type: array
items:
type: string
description: Roles assigned to the user.
timezone:
type: string
description: The timezone of the user.
language:
type: string
description: The preferred language of the user.
href:
type: object
properties:
canonical:
type: string
format: uri
description: Canonical URL of the user.
actor:
type: object
properties:
application:
type: object
nullable: true
properties:
id:
type: string
description: The UUID of the application.
name:
type: string
description: The name of the application.
user:
type: object
nullable: true
properties:
id:
type: string
description: The UUID of the actor user.
displayName:
type: string
description: The display name of the actor user.
href:
type: object
properties:
canonical:
type: string
format: uri
description: Canonical URL of the actor user.

operations:
onUserCreate:
Expand All @@ -447,3 +527,7 @@ operations:
action: 'receive'
channel:
$ref: '#/channels/userLogout'
onUserBlock:
action: 'receive'
channel:
$ref: '#/channels/userBlock'
4 changes: 4 additions & 0 deletions modules/social_features/social_user/social_user.module
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,10 @@ function social_user_user_update(EntityInterface $entity): void {
\Drupal::service('social_user.eda_handler')->userCreate($entity);
}
}
// Status changed to blocked.
elseif ($entity->isBlocked() && $original instanceof UserInterface && $original->isActive()) {
\Drupal::service('social_user.eda_handler')->userBlock($entity);
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions modules/social_features/social_user/src/EdaHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ public function userLogout(UserInterface $user): void {
$this->dispatch($topic_name, $event_type, $user);
}

/**
* User block handler.
*/
public function userBlock(UserInterface $user): void {
$event_type = 'com.getopensocial.cms.user.block';
$topic_name = 'com.getopensocial.cms.user.block';
$this->dispatch($topic_name, $event_type, $user);
}

/**
* Transforms a NodeInterface into a CloudEvent.
*/
Expand Down Expand Up @@ -141,6 +150,7 @@ public function fromEntity(UserInterface $user, string $event_type): CloudEvent
switch ($event_type) {
case 'com.getopensocial.cms.user.login':
case 'com.getopensocial.cms.user.logout':
case 'com.getopensocial.cms.user.block':
$user_data = new UserEventDataLite(
id: $user->get('uuid')->value,
created: DateTime::fromTimestamp($user->getCreatedTime())->toString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,33 @@ public function testUserLogout(): void {
$this->assertEquals('com.getopensocial.cms.user.logout', $event->getType());
}

/**
* Test the userBlock() method.
*
* @covers ::userBlock
*/
public function testUserBlock(): void {
// Create the handler instance.
$handler = $this->getMockedHandler();

// Create the event object.
$event = $handler->fromEntity($this->user, 'com.getopensocial.cms.user.block');

// Expect the dispatch method in the dispatcher to be called.
$this->dispatcher->expects($this->once())
->method('dispatch')
->with(
$this->equalTo('com.getopensocial.cms.user.block'),
$this->equalTo($event)
);

// Call the userBlock method.
$handler->userBlock($this->user);

// Assert that the correct event is dispatched.
$this->assertEquals('com.getopensocial.cms.user.block', $event->getType());
}

/**
* Returns a mocked handler with dependencies injected.
*
Expand Down

0 comments on commit 2c4f831

Please sign in to comment.