Skip to content

Commit

Permalink
feat: Allows the creation of multiple targets with the same display n…
Browse files Browse the repository at this point in the history
…ame - EXO-74991 - Meeds-io/meeds#2541 (#290)
  • Loading branch information
sofyenne authored and hakermi committed Nov 28, 2024
1 parent ecfb638 commit 03c476c
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,81 @@
*/
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
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<String, String> 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<String, String> 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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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à.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,4 @@
</init-params>
</component-plugin>
</external-component-plugins>

<external-component-plugins>
<target-component>org.exoplatform.social.core.space.spi.SpaceService</target-component>
<component-plugin>
<name>content.listeners.social.space</name>
<set-method>addSpaceListener</set-method>
<type>io.meeds.news.listener.SpaceNewsTargetAutoCreationListener</type>
<description>Create a space news target when a new space is created</description>
</component-plugin>
</external-component-plugins>
</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
<v-btn
icon
text
@click="openDrawer(props.item.name, props.item.description, props.item.permissions)">
@click="openDrawer(props.item.name, props.item.label, props.item.description, props.item.permissions)">
<v-icon
dark
color="primary"
Expand Down Expand Up @@ -203,10 +203,11 @@ export default {
this.selectedTargetName = target;
this.$refs.deleteConfirmDialog.open();
},
openDrawer(targetName, targetDescription, targetPermissions) {
openDrawer(targetName, targetLabel, targetDescription, targetPermissions) {
let selectedTarget = null;
selectedTarget = {
targetName: targetName && targetName.trim(),
targetLabel: targetLabel,
targetDescription: targetDescription && targetDescription.trim(),
targetPermissions: targetPermissions,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,10 @@
<v-text-field
id="targetName"
ref="targetName"
v-model="targetName"
v-model="targetLabel"
type="string"
name="newsHeader"
:placeholder="$t('news.publishTargets.managementDrawer.placeholder.name')"
:error-messages="checkAlphanumeric"
maxlength="100"
class="targetName input-block-level ignore-vuetify-classes"
counter
Expand Down Expand Up @@ -166,7 +165,7 @@ export default {
saving: false,
targetDescriptionTextLength: 1000,
targetDescription: '',
targetName: '',
targetLabel: '',
sameTargetError: false,
selectedTarget: '',
originalTargetName: '',
Expand All @@ -184,11 +183,8 @@ export default {
noDataLabel: this.$t('news.publishTargets.managementDrawer.permissions.noData')
};
},
checkAlphanumeric() {
return this.targetName && !this.targetName.trim().match(/^[a-zA-Z\u00C0-\u00FF ]*$/) && this.targetName.length > 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');
Expand Down Expand Up @@ -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(); });
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -346,7 +337,7 @@ export default {
},
reset() {
this.targetDescription = '';
this.targetName = '';
this.targetLabel = '';
this.saveMode = 'creationMode';
this.permissions=[];
this.permissionsUpdated= false;
Expand Down

0 comments on commit 03c476c

Please sign in to comment.