From 1a569a7aec85b6df5e4c1dff855c52bd9882deed Mon Sep 17 00:00:00 2001 From: Ayoub Zayati Date: Mon, 11 Sep 2023 12:07:14 +0200 Subject: [PATCH] feat: Update getSites rest service and builder with additional params - EXO-66028 - Meeds-io/MIPs#72 (#2805) --- .../social/rest/api/EntityBuilder.java | 85 ++++++++++++------- .../social/rest/impl/site/SiteRest.java | 47 +++++++--- .../webapp/vue-apps/common/js/SiteService.js | 12 ++- .../components/HamburgerMenuNavigation.vue | 2 +- 4 files changed, 96 insertions(+), 50 deletions(-) diff --git a/component/service/src/main/java/org/exoplatform/social/rest/api/EntityBuilder.java b/component/service/src/main/java/org/exoplatform/social/rest/api/EntityBuilder.java index 04e746ed83a..65df98f3009 100644 --- a/component/service/src/main/java/org/exoplatform/social/rest/api/EntityBuilder.java +++ b/component/service/src/main/java/org/exoplatform/social/rest/api/EntityBuilder.java @@ -26,6 +26,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.Comparator; import java.util.Date; import java.util.HashMap; import java.util.Iterator; @@ -62,6 +63,7 @@ import org.exoplatform.portal.mop.SiteKey; import org.exoplatform.portal.mop.SiteType; import org.exoplatform.portal.mop.navigation.Scope; +import org.exoplatform.portal.mop.rest.model.UserNodeRestEntity; import org.exoplatform.portal.mop.service.LayoutService; import org.exoplatform.portal.mop.user.HttpUserPortalContext; import org.exoplatform.portal.mop.user.UserNavigation; @@ -1753,14 +1755,29 @@ public static RelationshipManager getRelationshipManager() { public static List buildSiteEntities(List sites, HttpServletRequest request, - boolean expandNavigations) { - return sites.stream().map(site -> buildSiteEntity(site, request, expandNavigations)).filter(Objects::nonNull).toList(); - } - - public static SiteEntity buildSiteEntity(PortalConfig site, HttpServletRequest request, boolean expandNavigations) { - if (site == null) { - return null; - } + boolean expandNavigations, + boolean excludeEmptyNavigationSites, + boolean filterByPermission, + boolean sortByDisplayOrder) { + List filteredByPermissionSites = sites; + if (filterByPermission) { + filteredByPermissionSites = sites.stream().filter(getUserACL()::hasPermission).toList(); + } + List siteEntities = filteredByPermissionSites.stream() + .map(site -> buildSiteEntity(site, + request, + expandNavigations, + excludeEmptyNavigationSites)) + .filter(Objects::nonNull) + .toList(); + return sortByDisplayOrder ? siteEntities + : siteEntities.stream().sorted(Comparator.comparing(SiteEntity::getDisplayName, String.CASE_INSENSITIVE_ORDER)).toList(); + } + + public static SiteEntity buildSiteEntity(PortalConfig site, + HttpServletRequest request, + boolean expandNavigations, + boolean excludeEmptyNavigationSites) { SiteType siteType = SiteType.valueOf(site.getType().toUpperCase()); String displayName = site.getLabel(); org.exoplatform.services.security.Identity userIdentity = ConversationState.getCurrent().getIdentity(); @@ -1780,24 +1797,34 @@ public static SiteEntity buildSiteEntity(PortalConfig site, HttpServletRequest r Map editPermission = computePermission(site.getEditPermission()); UserNode rootNode = null; - if (expandNavigations) { - String currentUser = userIdentity.getUserId(); - try { - HttpUserPortalContext userPortalContext = new HttpUserPortalContext(request); - UserPortalConfig userPortalConfig = - getUserPortalConfigService().getUserPortalConfig(siteType != SiteType.PORTAL ? getUserPortalConfigService().getDefaultPortal() - : site.getName(), - currentUser, - userPortalContext); - - UserPortal userPortal = userPortalConfig.getUserPortal(); - UserNavigation navigation = userPortal.getNavigation(new SiteKey(siteType.getName(), site.getName())); - if (navigation != null) { - rootNode = userPortal.getNode(navigation, Scope.ALL, UserNodeFilterConfig.builder().withReadWriteCheck().build(), null); - } - } catch (Exception e) { - LOG.error("Error while getting site {} navigations for user {}", site.getName(), currentUser, e); + String currentUser = userIdentity.getUserId(); + try { + HttpUserPortalContext userPortalContext = new HttpUserPortalContext(request); + UserPortalConfig userPortalConfig = + getUserPortalConfigService().getUserPortalConfig(siteType != SiteType.PORTAL ? getUserPortalConfigService().getDefaultPortal() + : site.getName(), + currentUser, + userPortalContext); + + UserPortal userPortal = userPortalConfig.getUserPortal(); + UserNavigation navigation = userPortal.getNavigation(new SiteKey(siteType.getName(), site.getName())); + if (navigation != null) { + rootNode = userPortal.getNode(navigation, Scope.ALL, UserNodeFilterConfig.builder().withReadWriteCheck().build(), null); } + } catch (Exception e) { + LOG.error("Error while getting site {} navigations for user {}", site.getName(), currentUser, e); + } + + if (excludeEmptyNavigationSites && (rootNode == null || rootNode.getChildren().isEmpty())) { + return null; + } + List siteNavigations = null; + if (expandNavigations && rootNode != null) { + siteNavigations = toUserNodeRestEntity(rootNode.getChildren(), + true, + getOrganizationService(), + getLayoutService(), + getUserACL()); } return new SiteEntity(siteType, site.getName(), @@ -1808,14 +1835,8 @@ public static SiteEntity buildSiteEntity(PortalConfig site, HttpServletRequest r site.isDisplayed(), site.getDisplayOrder(), isMetaSite(site.getName()), - rootNode == null ? null - : toUserNodeRestEntity(rootNode.getChildren(), - true, - getOrganizationService(), - getLayoutService(), - getUserACL()), + siteNavigations, getUserACL().hasPermission(site.getEditPermission())); - } private static List> computePermissions(String[] permissions) { diff --git a/component/service/src/main/java/org/exoplatform/social/rest/impl/site/SiteRest.java b/component/service/src/main/java/org/exoplatform/social/rest/impl/site/SiteRest.java index 7580dce3ea8..c0070e54229 100644 --- a/component/service/src/main/java/org/exoplatform/social/rest/impl/site/SiteRest.java +++ b/component/service/src/main/java/org/exoplatform/social/rest/impl/site/SiteRest.java @@ -65,24 +65,39 @@ public SiteRest(LayoutService layoutService) { @ApiResponse(responseCode = "500", description = "Internal server error"), }) public Response getSites(@Context HttpServletRequest request, - @Parameter(description = "Portal site type, possible values: PORTAL, GROUP or USER", required = true) + @Parameter(description = "Portal site types, possible values: PORTAL, GROUP or USER", required = true) @QueryParam("siteType") - String siteTypeName, + String siteType, + @Parameter(description = "Site type to be excluded") + @QueryParam("excludedSiteType") + String excludedSiteType, @Parameter(description = "Site name to be excluded") @QueryParam("excludedSiteName") String excludedSiteName, + @Parameter(description = "to exclude space sites") + @DefaultValue("false") + @QueryParam("excludeSpaceSites") + boolean excludeSpaceSites, + @Parameter(description = "to exclude sites with empty navigation") + @DefaultValue("false") + @QueryParam("excludeEmptyNavigationSites") + boolean excludeEmptyNavigationSites, @Parameter(description = "to expand site navigations nodes") @DefaultValue("false") @QueryParam("expandNavigations") boolean expandNavigations, - @Parameter(description = "to retrieve sites with its displayed status") + @Parameter(description = "to sort with display order") + @DefaultValue("false") + @QueryParam("sortByDisplayOrder") + boolean sortByDisplayOrder, + @Parameter(description = "to filter sites by displayed status") + @DefaultValue("false") + @QueryParam("filterByDisplayed") + boolean filterByDisplayed, + @Parameter(description = "to retrieve sites with displayed status") @DefaultValue("false") @QueryParam("displayed") - Boolean displayed, - @Parameter(description = "to retrieve all sites") - @DefaultValue("true") - @QueryParam("allSites") - boolean allSites, + boolean displayed, @Parameter(description = "to filter sites by view/edit permissions") @DefaultValue("false") @QueryParam("filterByPermissions") @@ -97,21 +112,25 @@ public Response getSites(@Context int limit) { try { SiteFilter siteFilter = new SiteFilter(); - if (siteTypeName != null) { - siteFilter.setSiteType(SiteType.valueOf(siteTypeName.toUpperCase())); + if (siteType != null) { + siteFilter.setSiteType(SiteType.valueOf(siteType.toUpperCase())); + } + if (excludedSiteType != null) { + siteFilter.setExcludedSiteType(SiteType.valueOf(excludedSiteType.toUpperCase())); } if (excludedSiteName != null) { siteFilter.setExcludedSiteName(excludedSiteName); } - if (displayed != null) { + siteFilter.setExcludeSpaceSites(excludeSpaceSites); + siteFilter.setSortByDisplayOrder(sortByDisplayOrder); + if (filterByDisplayed) { + siteFilter.setFilterByDisplayed(filterByDisplayed); siteFilter.setDisplayed(displayed); } - siteFilter.setAllSites(allSites); - siteFilter.setFilterByPermission(filterByPermission); siteFilter.setLimit(limit); siteFilter.setOffset(offset); List sites = layoutService.getSites(siteFilter); - return Response.ok(EntityBuilder.buildSiteEntities(sites, request, expandNavigations)).build(); + return Response.ok(EntityBuilder.buildSiteEntities(sites, request, expandNavigations, excludeEmptyNavigationSites, filterByPermission, sortByDisplayOrder)).build(); } catch (Exception e) { LOG.warn("Error while retrieving sites", e); return Response.serverError().build(); diff --git a/webapp/portlet/src/main/webapp/vue-apps/common/js/SiteService.js b/webapp/portlet/src/main/webapp/vue-apps/common/js/SiteService.js index 80c5cd54294..dee1f0b40e8 100644 --- a/webapp/portlet/src/main/webapp/vue-apps/common/js/SiteService.js +++ b/webapp/portlet/src/main/webapp/vue-apps/common/js/SiteService.js @@ -1,16 +1,22 @@ -export function getSites(siteType, excludedSiteName, expandNavigations, displayed, allSites, filterByPermissions) { +export function getSites(siteType, excludedSiteType, excludedSiteName, excludeEmptyNavigationSites, excludeSpaceSites, expandNavigations, filterByDisplayed, sortByDisplayOrder, displayed, filterByPermissions) { const formData = new FormData(); if (siteType) { formData.append('siteType', siteType); } + if (excludedSiteType) { + formData.append('excludedSiteType', excludedSiteType); + } if (excludedSiteName) { formData.append('excludedSiteName', excludedSiteName); } + formData.append('excludeEmptyNavigationSites', excludeEmptyNavigationSites); + formData.append('excludeSpaceSites', excludeSpaceSites); formData.append('expandNavigations', expandNavigations); - if (displayed) { + formData.append('filterByDisplayed', filterByDisplayed); + formData.append('sortByDisplayOrder', sortByDisplayOrder); + if (filterByDisplayed) { formData.append('displayed', displayed); } - formData.append('allSites', allSites); formData.append('filterByPermissions', filterByPermissions); const params = new URLSearchParams(formData).toString(); diff --git a/webapp/portlet/src/main/webapp/vue-apps/hamburger-menu/components/HamburgerMenuNavigation.vue b/webapp/portlet/src/main/webapp/vue-apps/hamburger-menu/components/HamburgerMenuNavigation.vue index 6a3682546a1..0b84565b332 100644 --- a/webapp/portlet/src/main/webapp/vue-apps/hamburger-menu/components/HamburgerMenuNavigation.vue +++ b/webapp/portlet/src/main/webapp/vue-apps/hamburger-menu/components/HamburgerMenuNavigation.vue @@ -312,7 +312,7 @@ export default { }, retrieveSites(){ if (eXo.env.portal.newLeftNavigationDrawer) { - return this.$siteService.getSites('PORTAL', 'global', true, true, false, true) + return this.$siteService.getSites('PORTAL', null, 'global', true, true, true, true, true, true, true) .then(data => this.sites = data || []); } },