Getting usernames with role from social service

This commit is contained in:
Luca Frosini 2021-11-25 17:50:14 +01:00
parent 04ae243f1d
commit 69a22d2461
2 changed files with 48 additions and 9 deletions

View File

@ -0,0 +1,44 @@
package org.gcube.gcat.social;
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode;
import org.gcube.common.gxhttp.request.GXHTTPStringRequest;
import org.gcube.gcat.utils.HTTPUtility;
public class SocialUsers {
protected static final String GET_USERNAMES_BY_ROLE = "2/users/get-usernames-by-role";
protected static final String GET_USERNAMES_BY_ROLE_ROLE_NAME_PARAMETER = "role-name";
protected static final String GET_USERNAMES_BY_ROLE_RESULT_KEY = "result";
public static Set<String> getUsernamesByRole(String roleName) throws Exception {
String socialServiceBasePath = SocialService.getSocialService().getServiceBasePath();
GXHTTPStringRequest gxhttpStringRequest = HTTPUtility.createGXHTTPStringRequest(socialServiceBasePath,
GET_USERNAMES_BY_ROLE, false);
Map<String,String> parameters = new HashMap<>();
parameters.put(GET_USERNAMES_BY_ROLE_ROLE_NAME_PARAMETER, roleName);
gxhttpStringRequest.queryParams(parameters);
HttpURLConnection httpURLConnection = gxhttpStringRequest.get();
String ret = HTTPUtility.getResultAsString(httpURLConnection);
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(ret);
ArrayNode arrayNode = (ArrayNode) jsonNode.get(GET_USERNAMES_BY_ROLE_RESULT_KEY);
Set<String> usernames = new HashSet<>();
for(JsonNode node : arrayNode) {
usernames.add(node.asText());
}
return usernames;
}
}

View File

@ -1,6 +1,5 @@
package org.gcube.gcat.zulip;
import java.util.HashSet;
import java.util.Set;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
@ -8,7 +7,9 @@ import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode;
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
import org.gcube.gcat.api.CMItemStatus;
import org.gcube.gcat.api.GCatConstants;
import org.gcube.gcat.persistence.ckan.CKANUser;
import org.gcube.gcat.social.SocialUsers;
import org.gcube.gcat.utils.Constants;
import org.gcube.storagehub.ApplicationMode;
import org.slf4j.Logger;
@ -129,8 +130,9 @@ public class ZulipStream {
getGCatZulipRestExecutor();
principalsArrayNode.add(gCatZulipRestExecutor.httpController.getUserName());
// Going to add the catalogue moderators
Set<String> moderators = getCatalogueModerators();
Set<String> moderators = SocialUsers.getUsernamesByRole(GCatConstants.CATALOGUE_MODERATOR);
for(String moderator : moderators) {
principalsArrayNode.add(moderator);
}
@ -145,13 +147,6 @@ public class ZulipStream {
postItemCreated();
}
private Set<String> getCatalogueModerators() {
Set<String> moderators = new HashSet<>();
// moderators.add("pasquale.pagano@isti.cnr.it");
// moderators.add("leonardo.candela@isti.cnr.it");
return moderators;
}
protected void postMessageToStream(ZulipRestExecutor zulipRestExecutor, CMItemStatus cmItemStatus, String message) throws Exception {
PostMessage postMessage = new PostMessage(getStreamName(), cmItemStatus.getFancyValue(), message);
logger.debug("Going to send the following message: {}", message);