From c528280256682cbd64da76ad8bd246102dec7386 Mon Sep 17 00:00:00 2001 From: nkoporec Date: Wed, 27 Nov 2024 16:10:20 +0100 Subject: [PATCH] PROD-31340 - Implement group publish event for the EDA --- .../social_group_flexible_group/asyncapi.yml | 146 ++++++++++++++++++ .../social_group_flexible_group.module | 9 ++ .../src/EdaHandler.php | 15 +- .../src/Event/GroupEntityData.php | 2 +- .../tests/src/Unit/EdaHandlerTest.php | 27 ++++ 5 files changed, 197 insertions(+), 2 deletions(-) diff --git a/modules/social_features/social_group/modules/social_group_flexible_group/asyncapi.yml b/modules/social_features/social_group/modules/social_group_flexible_group/asyncapi.yml index 439448ce9b7..55833fee549 100644 --- a/modules/social_features/social_group/modules/social_group_flexible_group/asyncapi.yml +++ b/modules/social_features/social_group/modules/social_group_flexible_group/asyncapi.yml @@ -281,6 +281,148 @@ channels: type: string format: uri description: Canonical URL for the actor profile + groupPublish: + address: com.getopensocial.cms.group.unpublish + messages: + groupPublish: + payload: + allOf: + - $ref: '#/components/schemas/cloudEventsSchema' + - type: object + properties: + data: + type: object + properties: + id: + type: string + description: Unique group ID (UUIDv4) + created: + type: string + format: date-time + description: Creation time of the group + updated: + type: string + format: date-time + description: Last update time of the group + status: + type: string + description: Status of the group (e.g., published or unpublished) + enum: + - published + - unpublished + label: + type: string + description: Label of the group + visibility: + type: object + properties: + type: + type: string + description: The visibility type. + roles: + type: array + description: The list of roles ids. + contentVisibility: + type: object + properties: + method: + type: string + description: Visibility of content + enum: + - public + - community + - group + membership: + type: object + properties: + method: + type: string + description: Method of membership + enum: + - open + - invite + - request + type: + type: string + nullable: true + description: Type of the group (optional) + address: + type: object + properties: + label: + type: string + nullable: true + description: Label for the address (optional) + countryCode: + type: string + nullable: true + description: Country code (optional) + administrativeArea: + type: string + nullable: true + description: Administrative area (optional) + locality: + type: string + nullable: true + description: Locality (optional) + dependentLocality: + type: string + nullable: true + description: Dependent locality (optional) + postalCode: + type: string + nullable: true + description: Postal code (optional) + sortingCode: + type: string + nullable: true + description: Sorting code (optional) + addressLine1: + type: string + nullable: true + description: Address line 1 (optional) + addressLine2: + type: string + nullable: true + description: Address line 2 (optional) + href: + type: object + properties: + canonical: + type: string + format: uri + description: Canonical URL for the group + actor: + type: object + properties: + application: + type: object + nullable: true + properties: + id: + type: string + description: Application ID (UUIDv4) + name: + type: string + description: Name of the application + user: + type: object + nullable: true + properties: + id: + type: string + description: Actor user ID (UUIDv4) + displayName: + type: string + description: Display name of the actor user + href: + type: object + properties: + canonical: + type: string + format: uri + description: Canonical URL for the actor profile + operations: onGroupCreate: action: 'receive' @@ -290,3 +432,7 @@ operations: action: 'receive' channel: $ref: '#/channels/groupUnpublish' + onGroupPublish: + action: 'receive' + channel: + $ref: '#/channels/groupPublish' diff --git a/modules/social_features/social_group/modules/social_group_flexible_group/social_group_flexible_group.module b/modules/social_features/social_group/modules/social_group_flexible_group/social_group_flexible_group.module index 09d83d80342..4b7989bfbf0 100644 --- a/modules/social_features/social_group/modules/social_group_flexible_group/social_group_flexible_group.module +++ b/modules/social_features/social_group/modules/social_group_flexible_group/social_group_flexible_group.module @@ -355,6 +355,15 @@ function social_group_flexible_group_social_core_group_unpublished(EntityInterfa } } +/** + * Implements hook_social_core_ENTITY_TYPE_published(). + */ +function social_group_flexible_group_social_core_group_published(EntityInterface $entity): void { + if ($entity instanceof GroupInterface && $entity->bundle() == 'flexible_group') { + \Drupal::service('social_group_flexible_group.eda_handler')->groupPublish($entity); + } +} + /** * Implements template_preprocess_form_element(). */ diff --git a/modules/social_features/social_group/modules/social_group_flexible_group/src/EdaHandler.php b/modules/social_features/social_group/modules/social_group_flexible_group/src/EdaHandler.php index fa6f4f63edf..dba7168e417 100644 --- a/modules/social_features/social_group/modules/social_group_flexible_group/src/EdaHandler.php +++ b/modules/social_features/social_group/modules/social_group_flexible_group/src/EdaHandler.php @@ -109,6 +109,17 @@ public function groupCreate(GroupInterface $group): void { ); } + /** + * Publish event handler. + */ + public function groupPublish(GroupInterface $group): void { + $this->dispatch( + topic_name: $this->topicName, + event_type: "{$this->namespace}.cms.group.publish", + group: $group + ); + } + /** * Unpublish event handler. */ @@ -150,7 +161,9 @@ public function fromEntity(GroupInterface $group, string $event_type, string $op status: $status, label: (string) $group->label(), visibility: GroupVisibility::fromEntity($group), - contentVisibility: $group->get('field_group_allowed_visibility')->value, + contentVisibility: [ + 'type' => $group->get('field_group_allowed_visibility')->value, + ], membership: GroupMembershipMethod::fromEntity($group), type: $group->getGroupType()->get('uuid'), author: User::fromEntity($group->get('uid')->entity), diff --git a/modules/social_features/social_group/modules/social_group_flexible_group/src/Event/GroupEntityData.php b/modules/social_features/social_group/modules/social_group_flexible_group/src/Event/GroupEntityData.php index bd339295042..997d9ea3983 100644 --- a/modules/social_features/social_group/modules/social_group_flexible_group/src/Event/GroupEntityData.php +++ b/modules/social_features/social_group/modules/social_group_flexible_group/src/Event/GroupEntityData.php @@ -23,7 +23,7 @@ public function __construct( public readonly string $status, public readonly string $label, public readonly GroupVisibility|null $visibility, - public readonly string $contentVisibility, + public readonly array $contentVisibility, public readonly GroupMembershipMethod|null $membership, public readonly ?string $type, public readonly User $author, diff --git a/modules/social_features/social_group/modules/social_group_flexible_group/tests/src/Unit/EdaHandlerTest.php b/modules/social_features/social_group/modules/social_group_flexible_group/tests/src/Unit/EdaHandlerTest.php index 18666d5efbe..2d3406636ce 100644 --- a/modules/social_features/social_group/modules/social_group_flexible_group/tests/src/Unit/EdaHandlerTest.php +++ b/modules/social_features/social_group/modules/social_group_flexible_group/tests/src/Unit/EdaHandlerTest.php @@ -338,6 +338,33 @@ public function testGroupUnpublish(): void { $this->assertEquals('com.getopensocial.cms.group.unpublish', $group->getType()); } + /** + * Test the groupPublish() method. + * + * @covers ::groupPublish + */ + public function testGroupPublish(): void { + // Create the handler instance. + $handler = $this->getMockedHandler(); + + // Create the group object. + $group = $handler->fromEntity($this->group, 'com.getopensocial.cms.group.publish'); + + // Expect the dispatch method in the dispatcher to be called. + $this->dispatcher->expects($this->once()) + ->method('dispatch') + ->with( + $this->equalTo('com.getopensocial.cms.group.v1'), + $this->equalTo($group) + ); + + // Call the groupPublish method. + $handler->groupPublish($this->group); + + // Assert that the correct group is dispatched. + $this->assertEquals('com.getopensocial.cms.group.publish', $group->getType()); + } + /** * Returns a mocked handler with dependencies injected. *