Skip to content

Commit

Permalink
fix: Select a space where notes event occurs - MEED-4273 - Meeds-io/m…
Browse files Browse the repository at this point in the history
…eeds#1781 (#1043)

This PR will ensure the ability to select a space or multiple ones when
creating note event
  • Loading branch information
AzmiTouil authored Jul 17, 2024
1 parent c1d3e4b commit fbcb1ed
Show file tree
Hide file tree
Showing 14 changed files with 469 additions and 12 deletions.
5 changes: 5 additions & 0 deletions notes-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@
<artifactId>layout-service</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.meeds.gamification</groupId>
<artifactId>gamification-services</artifactId>
<scope>provided</scope>
</dependency>

<!-- Tests -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* This file is part of the Meeds project (https://meeds.io/).
*
* Copyright (C) 2020 - 2024 Meeds Association [email protected]
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
package io.meeds.notes.plugin;

import java.util.*;

import io.meeds.gamification.plugin.EventPlugin;
import org.apache.commons.collections.CollectionUtils;

public class NoteEventPlugin extends EventPlugin {

public static final String EVENT_TYPE = "note";

public static final String GAMIFICATION_WIKI_ADD_PAGE = "addWikiPage";

public static final String GAMIFICATION_WIKI_UPDATE_PAGE = "updateWikiPage";

@Override
public String getEventType() {
return EVENT_TYPE;
}

public List<String> getTriggers() {
return List.of(GAMIFICATION_WIKI_ADD_PAGE, GAMIFICATION_WIKI_UPDATE_PAGE);
}

@Override
public boolean isValidEvent(Map<String, String> eventProperties, String triggerDetails) {
List<String> desiredSpaceIds = eventProperties.get("spaceIds") != null
? Arrays.asList(eventProperties.get("spaceIds")
.split(","))
: Collections.emptyList();
Map<String, String> triggerDetailsMop = stringToMap(triggerDetails);
return (CollectionUtils.isNotEmpty(desiredSpaceIds) && desiredSpaceIds.contains(triggerDetailsMop.get("spaceId")));
}

private static Map<String, String> stringToMap(String mapAsString) {
Map<String, String> map = new HashMap<>();
mapAsString = mapAsString.substring(1, mapAsString.length() - 1);
String[] pairs = mapAsString.split(", ");
for (String pair : pairs) {
String[] keyValue = pair.split(": ");
String key = keyValue[0].trim();
String value = keyValue[1].trim();
map.put(key, value);
}
return map;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@
*/
package org.exoplatform.wiki.integration.gamification;

import static io.meeds.gamification.constant.GamificationConstant.EVENT_NAME;
import static io.meeds.gamification.constant.GamificationConstant.OBJECT_ID_PARAM;
import static io.meeds.gamification.constant.GamificationConstant.OBJECT_TYPE_PARAM;
import static io.meeds.gamification.constant.GamificationConstant.RECEIVER_ID;
import static io.meeds.gamification.constant.GamificationConstant.SENDER_ID;
import static io.meeds.gamification.constant.GamificationConstant.*;
import static io.meeds.gamification.listener.GamificationGenericListener.GENERIC_EVENT_NAME;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.exoplatform.services.listener.Asynchronous;
import org.exoplatform.services.listener.ListenerService;
import org.exoplatform.services.security.ConversationState;
import org.exoplatform.social.core.manager.IdentityManager;
import org.exoplatform.social.core.space.model.Space;
import org.exoplatform.social.core.space.spi.SpaceService;
import org.exoplatform.wiki.WikiException;
import org.exoplatform.wiki.model.Page;
import org.exoplatform.wiki.model.WikiType;
import org.exoplatform.wiki.service.PageUpdateType;
import org.exoplatform.wiki.service.listener.PageWikiListener;
import org.exoplatform.wiki.utils.NoteConstants;
Expand All @@ -51,10 +51,14 @@ public class GamificationWikiListener extends PageWikiListener {

protected ListenerService listenerService;

protected SpaceService spaceService;

public GamificationWikiListener(IdentityManager identityManager,
ListenerService listenerService) {
ListenerService listenerService,
SpaceService spaceService) {
this.identityManager = identityManager;
this.listenerService = listenerService;
this.spaceService = spaceService;
}

@Override
Expand All @@ -70,7 +74,7 @@ public void postAddPage(String wikiType, String wikiOwner, String pageId, Page p
String actorUsername = ConversationState.getCurrent().getIdentity().getUserId();
// Compute user id
String actorId = identityManager.getOrCreateUserIdentity(actorUsername).getId();
createGamificationRealization(actorId, actorId, GAMIFICATION_WIKI_ADD_PAGE, page.getId());
createGamificationRealization(actorId, actorId, GAMIFICATION_WIKI_ADD_PAGE, page.getId(), wikiOwner, wikiType);
}
}

Expand Down Expand Up @@ -106,26 +110,34 @@ public void postUpdatePage(String wikiType,

// Compute user id
String actorId = identityManager.getOrCreateUserIdentity(actorUsername).getId();

createGamificationRealization(actorId, actorId, GAMIFICATION_WIKI_UPDATE_PAGE, page.getId());
createGamificationRealization(actorId, actorId, GAMIFICATION_WIKI_UPDATE_PAGE, page.getId(), wikiOwner, wikiType);
}
}

private void createGamificationRealization(String earnerIdentityId,
String receiverId,
String gamificationEventName,
String pageId) {
String pageId,
String wikiOwner,
String wikiType) {
String eventDetails = "";
if (StringUtils.isNotBlank(wikiOwner) && StringUtils.equalsIgnoreCase(WikiType.GROUP.name(), wikiType)) {
Space space = spaceService.getSpaceByGroupId(wikiOwner);
eventDetails = "{spaceId: " + space.getId() + "}";
}
Map<String, String> gam = new HashMap<>();
try {
gam.put(EVENT_NAME, gamificationEventName);
gam.put(OBJECT_ID_PARAM, pageId);
gam.put(OBJECT_TYPE_PARAM, NOTES_OBJECT_TYPE);
gam.put(SENDER_ID, earnerIdentityId);
gam.put(RECEIVER_ID, receiverId);
gam.put(EVENT_DETAILS_PARAM, eventDetails);
listenerService.broadcast(GENERIC_EVENT_NAME, gam, null);
} catch (Exception e) {
throw new IllegalStateException("Error triggering Gamification Listener Event: " + gam, e);
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,13 @@
</component-plugin>
</external-component-plugins>

<external-component-plugins profiles="gamification">
<target-component>io.meeds.gamification.service.EventService</target-component>
<component-plugin>
<name>note</name>
<set-method>addPlugin</set-method>
<type>io.meeds.notes.plugin.NoteEventPlugin</type>
</component-plugin>
</external-component-plugins>

</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.Map;

import org.apache.commons.codec.binary.StringUtils;
import org.exoplatform.social.core.space.spi.SpaceService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
Expand Down Expand Up @@ -67,6 +68,9 @@ public class GamificationWikiListenerTest {
@Mock
IdentityManager identityManager;

@Mock
SpaceService spaceService;

@Mock
Page page;

Expand All @@ -75,7 +79,7 @@ public class GamificationWikiListenerTest {

@Test
public void testCreatePageEvent() throws Exception {
GamificationWikiListener gamificationListener = new GamificationWikiListener(identityManager, listenerService);
GamificationWikiListener gamificationListener = new GamificationWikiListener(identityManager, listenerService, spaceService);

when(identityManager.getOrCreateUserIdentity(USERNAME)).thenReturn(userIdentity);
when(userIdentity.getId()).thenReturn(USER_IDENTITY_ID);
Expand All @@ -98,7 +102,7 @@ public void testCreatePageEvent() throws Exception {

@Test
public void testUpdatePageEvent() throws Exception {
GamificationWikiListener gamificationListener = new GamificationWikiListener(identityManager, listenerService);
GamificationWikiListener gamificationListener = new GamificationWikiListener(identityManager, listenerService, spaceService);

when(identityManager.getOrCreateUserIdentity(USERNAME)).thenReturn(userIdentity);
when(userIdentity.getId()).thenReturn(USER_IDENTITY_ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ gamification.event.title.addWikiPage=Notes : Create page
gamification.event.description.addWikiPage=You created a note
gamification.event.title.updateWikiPage=Notes : Edit page
gamification.event.description.updateWikiPage=You updated a note

gamification.event.detail.notes.label=Note
gamification.event.detail.anyNote.label=Any note
gamification.event.detail.noteInSpace.label=Any note in a space

23 changes: 23 additions & 0 deletions notes-webapp/src/main/webapp/WEB-INF/gatein-resources.xml
Original file line number Diff line number Diff line change
Expand Up @@ -255,5 +255,28 @@
<module>extensionRegistry</module>
</depends>
</module>

<module>
<name>engagementCenterConnectorEventsNotesExtensions</name>
<load-group>engagement-center-connector-event-extensions</load-group>
<script>
<path>/javascript/connectorEventExtensions.bundle.js</path>
</script>
<depends>
<module>vue</module>
</depends>
<depends>
<module>vuetify</module>
</depends>
<depends>
<module>eXoVueI18n</module>
</depends>
<depends>
<module>extensionRegistry</module>
</depends>
<depends>
<module>commonVueComponents</module>
</depends>
</module>

</gatein-resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<!--
This file is part of the Meeds project (https://meeds.io/).
Copyright (C) 2020 - 2024 Meeds Association [email protected]
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-->
<template>
<div>
<notes-connector-event-form
v-if="isEditing"
:trigger="trigger"
:properties="properties" />
<notes-connector-event-display
v-else
:trigger="trigger"
:properties="properties" />
</div>
</template>
<script>
export default {
props: {
properties: {
type: Object,
default: null
},
trigger: {
type: String,
default: null
},
isEditing: {
type: Boolean,
default: false
}
},
};
</script>
Loading

0 comments on commit fbcb1ed

Please sign in to comment.