From 03c476cd7fa1e490b26c21d6fa522a25bba1ce76 Mon Sep 17 00:00:00 2001 From: Sofien Haj Chedhli Date: Wed, 30 Oct 2024 19:33:00 +0100 Subject: [PATCH] feat: Allows the creation of multiple targets with the same display name - EXO-74991 - meeds-io/Meeds#2541 (#290) --- .../SpaceNewsTargetAutoCreationListener.java | 49 +++++++++++++++++-- .../io/meeds/news/rest/NewsTargetingRest.java | 6 +++ .../locale/portlet/news/News_en.properties | 4 +- .../locale/portlet/news/News_fr.properties | 4 +- .../WEB-INF/conf/news/news-configuration.xml | 10 ---- .../NewsPublishTargetsManagement.vue | 5 +- .../NewsPublishTargetsManagementDrawer.vue | 29 ++++------- 7 files changed, 68 insertions(+), 39 deletions(-) diff --git a/content-service/src/main/java/io/meeds/news/listener/SpaceNewsTargetAutoCreationListener.java b/content-service/src/main/java/io/meeds/news/listener/SpaceNewsTargetAutoCreationListener.java index 57891ac46..5444a4002 100644 --- a/content-service/src/main/java/io/meeds/news/listener/SpaceNewsTargetAutoCreationListener.java +++ b/content-service/src/main/java/io/meeds/news/listener/SpaceNewsTargetAutoCreationListener.java @@ -19,27 +19,44 @@ */ package io.meeds.news.listener; +import java.util.HashMap; import java.util.Map; +import jakarta.annotation.PostConstruct; import org.exoplatform.services.log.ExoLogger; import org.exoplatform.services.log.Log; import org.exoplatform.services.security.ConversationState; import org.exoplatform.services.security.Identity; import org.exoplatform.social.core.space.SpaceListenerPlugin; +import io.meeds.social.space.service.SpaceServiceImpl; import org.exoplatform.social.core.space.model.Space; import org.exoplatform.social.core.space.spi.SpaceLifeCycleEvent; import io.meeds.news.rest.NewsTargetingEntity; import io.meeds.news.service.NewsTargetingService; +import org.exoplatform.social.core.space.spi.SpaceService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +@Component public class SpaceNewsTargetAutoCreationListener extends SpaceListenerPlugin { private static final Log LOG = ExoLogger.getLogger(SpaceNewsTargetAutoCreationListener.class); + @Autowired private NewsTargetingService newsTargetingService; - public SpaceNewsTargetAutoCreationListener(NewsTargetingService newsTargetingService) { - this.newsTargetingService = newsTargetingService; + @Autowired + private SpaceService spaceService; + + private SpaceServiceImpl spaceServiceImpl; + + @PostConstruct + public void init() { + if (spaceService instanceof SpaceServiceImpl) { + this.spaceServiceImpl = (SpaceServiceImpl) spaceService; + } + spaceServiceImpl.addSpaceListener(this); } @Override @@ -47,12 +64,36 @@ public void spaceCreated(SpaceLifeCycleEvent event) { Identity currentIdentity = ConversationState.getCurrent().getIdentity(); Space space = event.getSpace(); NewsTargetingEntity spaceNewsTargetEntity = new NewsTargetingEntity(); - spaceNewsTargetEntity.setName(space.getDisplayName()); - spaceNewsTargetEntity.setProperties(Map.of("label", space.getDisplayName(), "permissions", "space:" + space.getId())); + String spaceGroupId = space.getGroupId(); + String spaceGroupName = spaceGroupId.substring(spaceGroupId.lastIndexOf("/") + 1); + spaceNewsTargetEntity.setName(spaceGroupName); + Map properties = new HashMap<>(); + properties.put("label", space.getDisplayName()); + properties.put("permissions", "space:" + space.getId()); + spaceNewsTargetEntity.setProperties(properties); try { newsTargetingService.createNewsTarget(spaceNewsTargetEntity, currentIdentity, false); } catch (Exception e) { LOG.warn("Can't create space {} news target", space.getPrettyName(), e); } } + + @Override + public void spaceRenamed(SpaceLifeCycleEvent event) { + Identity currentIdentity = ConversationState.getCurrent().getIdentity(); + Space space = event.getSpace(); + NewsTargetingEntity spaceNewsTargetEntity = new NewsTargetingEntity(); + Map properties = new HashMap<>(); + properties.put("label", space.getDisplayName()); + properties.put("permissions", "space:" + space.getId()); + spaceNewsTargetEntity.setProperties(properties); + String spaceGroupId = space.getGroupId(); + String spaceGroupName = spaceGroupId.substring(spaceGroupId.lastIndexOf("/") + 1); + spaceNewsTargetEntity.setName(spaceGroupName); + try { + newsTargetingService.updateNewsTargets(spaceGroupName, spaceNewsTargetEntity, currentIdentity); + } catch (Exception e) { + LOG.warn("Can't rename space {} news target", space.getPrettyName()); + } + } } diff --git a/content-service/src/main/java/io/meeds/news/rest/NewsTargetingRest.java b/content-service/src/main/java/io/meeds/news/rest/NewsTargetingRest.java index 14979acf7..04279bd0d 100644 --- a/content-service/src/main/java/io/meeds/news/rest/NewsTargetingRest.java +++ b/content-service/src/main/java/io/meeds/news/rest/NewsTargetingRest.java @@ -41,6 +41,7 @@ import org.exoplatform.services.log.ExoLogger; import org.exoplatform.services.log.Log; import org.exoplatform.services.security.ConversationState; +import org.exoplatform.social.common.Utils; import org.exoplatform.social.metadata.model.Metadata; import io.meeds.news.service.NewsTargetingService; @@ -215,6 +216,11 @@ public Response createNewsTarget(@RequestBody NewsTargetingEntity newsTargetingE } org.exoplatform.services.security.Identity currentIdentity = ConversationState.getCurrent().getIdentity(); try { + StringBuilder targetName = new StringBuilder(); + targetName.append(Utils.cleanString(newsTargetingEntity.getName())); + targetName.append('_'); + targetName.append(System.currentTimeMillis()); + newsTargetingEntity.setName(targetName.toString()); Metadata addedNewsTarget = newsTargetingService.createNewsTarget(newsTargetingEntity, currentIdentity); return Response.ok(addedNewsTarget).build(); } catch (IllegalAccessException e) { diff --git a/content-service/src/main/resources/locale/portlet/news/News_en.properties b/content-service/src/main/resources/locale/portlet/news/News_en.properties index 456208701..427d8f2f8 100644 --- a/content-service/src/main/resources/locale/portlet/news/News_en.properties +++ b/content-service/src/main/resources/locale/portlet/news/News_en.properties @@ -230,7 +230,7 @@ news.list.settings.translation.header=Translate header name newsTargets.settings.title=News Publication Targets newsTargets.settings.button.addTarget=Add a target -newsTargets.settings.name=Target name +newsTargets.settings.name=Target display name newsTargets.settings.description=Description newsTargets.settings.actions=Actions newsTargets.settings.noTargets=No targets @@ -254,7 +254,7 @@ news.publishTargets.managementDrawer.permissions.noData=No data news.publishTargets.managementDrawer.btn.confirm=Confirm news.publishTargets.managementDrawer.btn.update=Update news.publishTargets.managementDrawer.btn.cancel=Cancel -news.publishTargets.managementDrawer.placeholder.name=Enter the target name +news.publishTargets.managementDrawer.placeholder.name=Enter the target display name news.publishTargets.managementDrawer.placeholder.description=Enter a description here news.publishTargets.managementDrawer.placeholder.permissions=Enter a space name or group name news.publishTargets.managementDrawer.sameNewsTargetWarning=A target with the same name already exists. diff --git a/content-service/src/main/resources/locale/portlet/news/News_fr.properties b/content-service/src/main/resources/locale/portlet/news/News_fr.properties index 8369088f3..f66729a84 100644 --- a/content-service/src/main/resources/locale/portlet/news/News_fr.properties +++ b/content-service/src/main/resources/locale/portlet/news/News_fr.properties @@ -230,7 +230,7 @@ news.list.settings.translation.header=Traduire l'en-tête newsTargets.settings.title=Administrer les cibles d’articles newsTargets.settings.button.addTarget=Ajouter une cible -newsTargets.settings.name=Nom de la cible +newsTargets.settings.name=Nom affiché de la cible newsTargets.settings.description=Description newsTargets.settings.actions=Actions newsTargets.settings.noTargets=Pas de cibles @@ -254,7 +254,7 @@ news.publishTargets.managementDrawer.permissions.noData=Pas de donnée news.publishTargets.managementDrawer.btn.confirm=Confirmer news.publishTargets.managementDrawer.btn.update=Mettre à jour news.publishTargets.managementDrawer.btn.cancel=Annuler -news.publishTargets.managementDrawer.placeholder.name=Indiquer le nom de la cible +news.publishTargets.managementDrawer.placeholder.name=Indiquer le nom affiché de la cible news.publishTargets.managementDrawer.placeholder.description=Entrez une description ici news.publishTargets.managementDrawer.placeholder.permissions=Entrez un nom d'espace ou de groupe news.publishTargets.managementDrawer.sameNewsTargetWarning=Une cible avec le même nom existe déjà. diff --git a/content-webapp/src/main/webapp/WEB-INF/conf/news/news-configuration.xml b/content-webapp/src/main/webapp/WEB-INF/conf/news/news-configuration.xml index e65a72572..e16eb920f 100644 --- a/content-webapp/src/main/webapp/WEB-INF/conf/news/news-configuration.xml +++ b/content-webapp/src/main/webapp/WEB-INF/conf/news/news-configuration.xml @@ -59,14 +59,4 @@ - - - org.exoplatform.social.core.space.spi.SpaceService - - content.listeners.social.space - addSpaceListener - io.meeds.news.listener.SpaceNewsTargetAutoCreationListener - Create a space news target when a new space is created - - diff --git a/content-webapp/src/main/webapp/vue-app/news-publish-targets-management/components/NewsPublishTargetsManagement.vue b/content-webapp/src/main/webapp/vue-app/news-publish-targets-management/components/NewsPublishTargetsManagement.vue index fce7e121e..ced3bd9f9 100644 --- a/content-webapp/src/main/webapp/vue-app/news-publish-targets-management/components/NewsPublishTargetsManagement.vue +++ b/content-webapp/src/main/webapp/vue-app/news-publish-targets-management/components/NewsPublishTargetsManagement.vue @@ -87,7 +87,7 @@ + @click="openDrawer(props.item.name, props.item.label, props.item.description, props.item.permissions)"> 0 ? this.$t('news.list.settings.name.errorMessage') : ''; - }, disabled() { - return (this.selectedTarget.targetName === this.targetName && this.selectedTarget.targetDescription === this.targetDescription && !this.permissionsUpdated) || this.checkAlphanumeric !== '' || this.targetName.length === 0 || this.permissions.length === 0 || this.sameTargetError || (typeof this.targetDescription !== 'undefined' && this.targetDescription.length > this.targetDescriptionTextLength); + return (this.selectedTarget.targetLabel === this.targetLabel && this.selectedTarget.targetDescription === this.targetDescription && !this.permissionsUpdated) || this.permissions.length === 0 || (typeof this.targetDescription !== 'undefined' && this.targetDescription.length > this.targetDescriptionTextLength); }, saveButtonLabel() { return this.saveMode === 'edit' ? this.$t('news.publishTargets.managementDrawer.btn.update') : this.$t('news.publishTargets.managementDrawer.btn.confirm'); @@ -221,20 +217,15 @@ export default { this.$refs.newsPublishTargetsManagementDrawer.endLoading(); } }, - targetName(newVal, oldVal) { - this.sameTargetError = newVal && newVal.length > 0 && oldVal.length > 0 && newVal === oldVal; - }, }, created() { this.$root.$on('selected-target', (selectedTarget) => { this.selectedTarget = selectedTarget; + console.log(this.selectedTarget); this.originalTargetName = selectedTarget.targetName; - this.targetName = selectedTarget.targetName; + this.targetLabel = selectedTarget.targetLabel; this.targetDescription = selectedTarget.targetDescription; this.permissions = JSON.parse(JSON.stringify(selectedTarget.targetPermissions)); - if ( this.targetName === selectedTarget.targetName && this.targetDescription === selectedTarget.targetDescription) { - this.sameTargetError = true; - } this.saveMode = 'edit'; }); this.$root.$on('open-news-publish-targets-management-drawer', () => { this.open(); }); @@ -292,10 +283,10 @@ export default { permissions = `${permissions + permission.id},`; }); } - target.name = this.targetName; + target.name = this.targetLabel; target.properties = { description: this.targetDescription, - label: this.targetName, + label: this.targetLabel, permissions: permissions, }; this.sameTargetError = false; @@ -328,10 +319,10 @@ export default { permissions = `${permissions + permission.id},`; }); } - target.name = this.targetName; + target.name = this.selectedTarget?.targetName; target.properties = { description: this.targetDescription, - label: this.targetName, + label: this.targetLabel, permissions: permissions, }; this.$newsTargetingService.updateTarget(target, this.originalTargetName) @@ -346,7 +337,7 @@ export default { }, reset() { this.targetDescription = ''; - this.targetName = ''; + this.targetLabel = ''; this.saveMode = 'creationMode'; this.permissions=[]; this.permissionsUpdated= false;