Skip to content
This repository was archived by the owner on Jan 15, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,14 @@
import javax.ws.rs.core.SecurityContext;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.ext.RuntimeDelegate;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.Map.Entry;

@Path("/homepage/intranet/people/")
@Produces("application/json")
public class PeopleRestServices implements ResourceContainer {

private static final int NUMBER_OF_SUGGESTIONS = 10;
private static Log log = ExoLogger.getLogger(PeopleRestServices.class);

private static final CacheControl cacheControl;
Expand Down Expand Up @@ -232,27 +230,27 @@ public Response getSuggestions(@Context SecurityContext sc, @Context UriInfo uri

ListAccess<Identity> connectionList = relationshipManager.getConnections(identity);
int size = connectionList.getSize();
Map<Identity, Integer> suggestions;
Map<Identity, Integer> connectionsSuggestions;
if (size > 0) {
suggestions = relationshipManager.getSuggestions(identity, 20, 50, 10);
if (suggestions.size() == 1 && suggestions.keySet().iterator().next().getRemoteId().equals(userACL.getSuperUser())) {
connectionsSuggestions = relationshipManager.getSuggestions(identity, 20, 50, 10);
if (connectionsSuggestions.size() == 1 && connectionsSuggestions.keySet().iterator().next().getRemoteId().equals(userACL.getSuperUser())) {
// The only suggestion is the super user so we clear the suggestion list
suggestions = Collections.emptyMap();
connectionsSuggestions = Collections.emptyMap();
}
} else {
suggestions = Collections.emptyMap();
connectionsSuggestions = Collections.emptyMap();
}

JSONObject jsonGlobal = new JSONObject();
JSONArray jsonArray = new JSONArray();
if (suggestions.isEmpty()) {
Map<Identity, Integer> suggestions = new HashMap<>(connectionsSuggestions);
if (connectionsSuggestions.size() < NUMBER_OF_SUGGESTIONS) {
// Returns the last users
List<Identity> identities = identityManager.getLastIdentities(10);
suggestions = new LinkedHashMap<Identity, Integer>();
List<Identity> identities = identityManager.getLastIdentities(NUMBER_OF_SUGGESTIONS - suggestions.size());
for (Identity id : identities) {
if (identity.equals(id) || relationshipManager.get(identity, id) != null)
continue;
suggestions.put(id, new Integer(0));
suggestions.put(id, 0);
}
}
for (Entry<Identity, Integer> suggestion : suggestions.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public void testSuggestions() throws Exception {
idFoo.setId("foo");
Identity idBar = new Identity(OrganizationIdentityProvider.NAME, "bar");
idBar.setId("bar");
Identity idBaz = new Identity(OrganizationIdentityProvider.NAME, "baz");
idBaz.setId("baz");
Identity idQux = new Identity(OrganizationIdentityProvider.NAME, "qux");
idBaz.setId("qux");
envctx.put(SecurityContext.class, new MockSecurityContext(idFoo.getRemoteId()));

Map<String, Object> imResults = new HashMap<String, Object>();
Expand Down Expand Up @@ -84,7 +88,7 @@ public void testSuggestions() throws Exception {
assertTrue(json.has("items"));
assertTrue(json.getJSONArray("items").length() == 0);

// The only suggestion is demo
// The only suggestion is bar
rmResults.put("getConnections", new MockListAccess<Identity>(new Identity[]{idBar}));
rmResults.put("getSuggestions", Collections.singletonMap(idRoot, 1));
rmResults.remove("get");
Expand All @@ -94,9 +98,25 @@ public void testSuggestions() throws Exception {
assertEquals("application/json", resp.getContentType().toString());
json = new JSONObject(resp.getEntity().toString());
assertTrue(json.has("items"));
assertTrue(json.getJSONArray("items").length() == 1);
assertEquals(1, json.getJSONArray("items").length());
assertEquals(json.getJSONArray("items").getJSONObject(0).getString("username"), idBar.getRemoteId());

// There is one suggestion and one connection to a contact
// bar is the suggestion since he is a connection
// baz is a new suggestion as he is a new registered user
Map<Identity, Integer> suggestions = new HashMap<>();
suggestions.put(idRoot, 1);
suggestions.put(idBar, 2);
imResults.put("getLastIdentities", Arrays.asList(idRoot, idFoo, idBaz));
rmResults.put("getConnections", new MockListAccess<Identity>(new Identity[]{idQux}));
rmResults.put("getSuggestions", suggestions);
resp = launcher.service("GET", path, "", null, null, envctx);
assertEquals(200, resp.getStatus());
assertEquals("application/json", resp.getContentType().toString());
json = new JSONObject(resp.getEntity().toString());
assertTrue(json.has("items"));
assertEquals(2, json.getJSONArray("items").length());

getContainer().unregisterComponent("UserACL");
getContainer().unregisterComponent("RelationshipManager");
getContainer().unregisterComponent("IdentityManager");
Expand Down