Skip to content

Commit

Permalink
PROD-31207 - Implement EDA event for when a user's settings and local…
Browse files Browse the repository at this point in the history
…e information are updated
  • Loading branch information
nkoporec authored and ribel committed Nov 18, 2024
1 parent a9f5eca commit 973061f
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 1 deletion.
85 changes: 85 additions & 0 deletions modules/social_features/social_user/asyncapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,87 @@ channels:
type: string
format: uri
description: Canonical URL of the actor user.
userLocaleUpdate:
address: com.getopensocial.cms.user.settings.locale
messages:
userLocaleUpdate:
payload:
allOf:
- $ref: '#/components/schemas/cloudEventsSchema'
- type: object
properties:
data:
type: object
properties:
user:
type: object
properties:
created:
type: string
format: date-time
description: Creation time of the user
updated:
type: string
format: date-time
description: Last update time of the user
status:
type: string
description: Status of the user
displayName:
type: string
description: Display name of the user
email:
type: string
format: email
description: Email of the user
roles:
type: array
items:
type: string
description: List of user roles
timezone:
type: string
description: Timezone of the user
language:
type: string
description: Language preference of the user
href:
type: object
properties:
canonical:
type: string
format: uri
description: Canonical URL for the user profile
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 Down Expand Up @@ -780,3 +861,7 @@ operations:
action: 'receive'
channel:
$ref: '#/channels/userEmailUpdate'
onUserLocaleUpdate:
action: 'receive'
channel:
$ref: '#/channels/userLocaleUpdate'
15 changes: 15 additions & 0 deletions modules/social_features/social_user/social_user.module
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,21 @@ function social_user_user_update(EntityInterface $entity): void {
if ($original->getEmail() != $entity->getEmail()) {
\Drupal::service('social_user.eda_handler')->userEmailUpdate($entity);
}

// Timezone change.
if ($original->getTimeZone() != $entity->getTimeZone()) {
\Drupal::service('social_user.eda_handler')->userLocaleInformationUpdate(
$entity
);
}

// Langcode change.
if ($original->getPreferredLangcode() != $entity->getPreferredLangcode()) {
\Drupal::service('social_user.eda_handler')->userLocaleInformationUpdate(
$entity
);
}

}

}
Expand Down
11 changes: 10 additions & 1 deletion modules/social_features/social_user/src/EdaHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,15 @@ public function userDelete(UserInterface $user): void {
);
}

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

/**
* Transforms a NodeInterface into a CloudEvent.
*/
Expand Down Expand Up @@ -227,7 +236,7 @@ public function fromEntity(UserInterface $user, string $event_type): CloudEvent
href: Href::fromEntity($user),
);
}
elseif (preg_match('/\.cms\.user\.settings\.email$/', $event_type)) {
elseif (preg_match('/\.cms\.user\.settings\.(email|locale)$/', $event_type)) {
$user_data = new UserEventEmailData(
created: DateTime::fromTimestamp($user->getCreatedTime())->toString(),
updated: DateTime::fromTimestamp($user->getChangedTime())->toString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,33 @@ public function testUserEmailUpdate(): void {
$this->assertEquals('com.getopensocial.cms.user.settings.email', $event->getType());
}

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

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

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

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

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

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

0 comments on commit 973061f

Please sign in to comment.