Skip to content

Commit

Permalink
feat: Refactor Sidebar to introduce sidebar settings from admin UI - M…
Browse files Browse the repository at this point in the history
…eeds-io/MIPs#159 (#4201)

Refactor Sidebar to introduce sidebar settings from admin UI
  • Loading branch information
boubaker committed Nov 25, 2024
1 parent 0e3246d commit d170151
Show file tree
Hide file tree
Showing 35 changed files with 809 additions and 257 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ protected SidebarItem resolvePageItemProperties(SidebarItem item, Locale locale)
long siteId = getSiteId(siteKey);

item.setName(getNodeLabel(Long.parseLong(nodeId), locale));
item.setIcon(node.getState().getIcon());
if (item.getIcon() == null) {
item.setIcon(node.getState().getIcon());
}

item.setProperties(new HashMap<>(item.getProperties()));
item.getProperties().put(SITE_ID_PROP_NAME, String.valueOf(siteId));
Expand All @@ -124,12 +126,14 @@ protected SidebarItem resolvePageItemProperties(SidebarItem item, Locale locale)
item.getProperties().put(SITE_ICON_PROP_NAME, getSiteIcon(siteKey));
item.getProperties().put(SITE_DISPLAY_NAME_PROP_NAME, getSiteLabel(siteKey, locale));

Page page = layoutService.getPage(node.getState().getPageRef());
if (PageType.LINK.name().equals(page.getType())) {
item.setUrl(page.getLink());
item.setTarget(node.getState().getTarget());
} else {
item.setUrl(getNodeUri(node));
if (node.getState().getPageRef() != null) {
Page page = layoutService.getPage(node.getState().getPageRef());
if (PageType.LINK.name().equals(page.getType())) {
item.setUrl(page.getLink());
item.setTarget(node.getState().getTarget());
} else {
item.setUrl(getNodeUri(node));
}
}
return item;
}
Expand Down Expand Up @@ -207,8 +211,7 @@ protected boolean isEligiblePage(long nodeId, String username) {
PortalConfig site = layoutService.getPortalConfig(node.getSiteKey());
if (!userAcl.hasAccessPermission(site, userAcl.getUserIdentity(username))) {
return false;
} else if (node.getState() == null
|| !isVisibilityEligible(node.getState())) {
} else if (node.getState() == null || !isVisibilityEligible(node.getState())) {
return false;
} else if (node.getState().getPageRef() == null) {
return node.iterator(false).hasNext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
package io.meeds.social.navigation.plugin;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -65,15 +66,18 @@ protected List<SidebarItem> getSpaces(SidebarItem item, String username) {
.toList();
}

protected SidebarItem toSidebarItem(Space space) {
private SidebarItem toSidebarItem(Space space) {
Map<String, String> properties = new HashMap<>();
properties.put("id", space.getId());
properties.put("groupId", space.getGroupId());
return new SidebarItem(space.getDisplayName(),
"/portal/s/" + space.getId(),
null,
space.getAvatarUrl(),
null,
SidebarItemType.SPACE,
null,
Collections.singletonMap("id", space.getId()));
properties);
}

private SortBy getSortField(SidebarItem item, SidebarSpaceSortBy sortBy) { // NOSONAR
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import org.exoplatform.portal.config.UserPortalConfigService;
import org.exoplatform.services.resources.LocaleConfigService;
import org.exoplatform.social.core.space.SpaceFilter;

Expand All @@ -40,10 +41,13 @@
@Order(40)
public class SpaceListSidebarPlugin extends AbstractSpaceSidebarPlugin {

public static final String SPACES_NAMES = "names";
public static final String SPACES_NAMES = "names";

@Autowired
private LocaleConfigService localeConfigService;
private LocaleConfigService localeConfigService;

@Autowired
private UserPortalConfigService portalConfigService;

@Override
public SidebarItemType getType() {
Expand All @@ -65,6 +69,7 @@ public SidebarItem resolveProperties(SidebarItem item, String username, Locale l
SPACES_NAMES,
locale));
item.setItems(getSpaces(item, username));
item.setUrl(String.format("/portal/%s/spaces", portalConfigService.getMetaPortal()));
return item;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@

import io.meeds.social.navigation.constant.SidebarItemType;
import io.meeds.social.navigation.model.NavigationConfiguration;
import io.meeds.social.navigation.model.SidebarConfiguration;
import io.meeds.social.navigation.model.SidebarItem;
import io.meeds.social.navigation.model.TopbarApplication;
import io.meeds.social.navigation.model.TopbarConfiguration;
import io.meeds.social.navigation.plugin.DefaultSidebarPlugin;
import io.meeds.social.navigation.plugin.SidebarPlugin;
import io.meeds.social.navigation.storage.NavigationConfigurationStorage;
Expand Down Expand Up @@ -81,6 +83,26 @@ public NavigationConfiguration getConfiguration(String username, Locale locale,
}
}

/**
* @param username User name
* @param locale {@link Locale} to compute Menu item names
* @return {@link TopbarConfiguration} switch user role and customized
* settings
*/
public TopbarConfiguration getTopbarConfiguration(String username, Locale locale) {
return getConfiguration(username, locale, true).getTopbar();
}

/**
* @param username User name
* @param locale {@link Locale} to compute Menu item names
* @return {@link SidebarConfiguration} switch user role and customized
* settings
*/
public SidebarConfiguration getSidebarConfiguration(String username, Locale locale) {
return getConfiguration(username, locale, true).getSidebar();
}

/**
* Updates the Navigation configuration
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,18 +342,6 @@ public ListAccess<Space> getLastAccessedSpaceByFilter(String username, SpaceFilt
SpaceListAccessType.LASTEST_ACCESSED);
}

@Override
public ListAccess<Space> getVisitedSpaces(String username) {
if (StringUtils.isBlank(username) || IdentityConstants.ANONIM.equals(username)) {
return new ListAccessImpl<>(Space.class, Collections.emptyList());
}
return new SpaceListAccess(spaceStorage,
spaceSearchConnector,
getSpaceTemplateService(),
username,
SpaceListAccessType.VISITED);
}

@Override
public ListAccess<Space> getCommonSpaces(String username, String otherUserId) {
if (StringUtils.isBlank(username) || IdentityConstants.ANONIM.equals(username)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,13 +389,6 @@ public int getVisibleSpacesCount(String userId, SpaceFilter spaceFilter) throws
return getSpacesCount(userId, null, xFilter);
}

public List<Space> getVisitedSpaces(SpaceFilter spaceFilter, int offset, int limit) throws SpaceStorageException {
XSpaceFilter xFilter = new XSpaceFilter();
xFilter.setSpaceFilter(spaceFilter);
xFilter.setVisited(true);
return getSpaces(spaceFilter.getRemoteId(), SpaceMembershipStatus.MEMBER, xFilter, offset, limit);
}

public Instant getSpaceMembershipDate(long spaceId, String username) {
return spaceMemberDAO.getSpaceMembershipDate(spaceId, username);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,6 @@ public Space[] load(int offset, int limit) { // NOSONAR
case LASTEST_ACCESSED:
listSpaces = spaceStorage.getLastAccessedSpace(this.getSpaceFilter(), offset, limit);
break;
case VISITED:
listSpaces = spaceStorage.getVisitedSpaces(this.getSpaceFilter(), offset, limit);
break;
case COMMON:
listSpaces = spaceStorage.getCommonSpaces(this.userId, this.otherUserId, offset, limit);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ public enum SpaceListAccessType {
LASTEST_ACCESSED,
/** Provides Relationship of Users requesting to join a Space */
PENDING_REQUESTS,
/** Gets the spaces which are visited at least once */
VISITED,
/** Gets the common spaces between two users */
COMMON
}
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ public void updateSpaceAccessed(String remoteId, Space space) throws SpaceStorag
spacesCache.select(selector);
} catch (Exception e) {
LOG.error("Error while removing cache entries for remoteId=" + remoteId + ", space=" + space.getDisplayName() +
" and type=" + SpaceType.LATEST_ACCESSED.name() + " or type=" + SpaceType.VISITED, e);
" and type=" + SpaceType.LATEST_ACCESSED.name(), e);
}
}

Expand Down Expand Up @@ -648,25 +648,6 @@ public List<Space> getLastSpaces(final int limit) {
return buildSpaces(keys);
}

@Override
public List<Space> getVisitedSpaces(final SpaceFilter filter, final int offset, final int limit) throws SpaceStorageException {
//
SpaceFilterKey key = new SpaceFilterKey(filter.getRemoteId(), filter, SpaceType.VISITED);
ListSpacesKey listKey = new ListSpacesKey(key, offset, limit);

//
ListSpacesData keys = spacesFutureCache.get(() -> {
if (limit == 0) {
return buildIds(Collections.emptyList());
}
List<Space> got = CachedSpaceStorage.super.getVisitedSpaces(filter, offset, limit);
return buildIds(got);
}, listKey);

//
return buildSpaces(keys);
}

@Override
public List<String> getMemberRoleSpaceIdentityIds(String identityId, int offset, int limit) throws SpaceStorageException {
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ public class SpaceFilterKey implements CacheKey {

private final String userId;

private long templateId;

private final int hash;

public SpaceFilterKey(String userId, SpaceFilter filter, SpaceType type) {
this.hash = Objects.hash(filter);
this.templateId = filter == null ? 0 : filter.getTemplateId();
this.type = type;
this.userId = userId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public enum SpaceType {
VISIBLE,
ALL,
LATEST_ACCESSED,
VISITED,
MEMBER_IDENTITY_IDS,
MEMBER_IDS
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
import org.exoplatform.social.core.storage.cache.model.key.SpaceType;

/**
* Cache selector for last accessed spaces.
* It select all cache entries for the given userId and for space type LATEST_ACCESSED or VISITED.
* Cache selector for last accessed spaces. It select all cache entries for the
* given userId and for space type LATEST_ACCESSED or VISITED.
*/
public class LastAccessedSpacesCacheSelector extends CacheSelector<ListSpacesKey, ListSpacesData> {

private String remoteId;

private long templateId;

private Space space;

private SocialStorageCacheService cacheService;
Expand All @@ -35,18 +37,13 @@ public LastAccessedSpacesCacheSelector(String remoteId, Space space, SocialStora

@Override
public boolean select(ListSpacesKey listSpacesKey, ObjectCacheInfo<? extends ListSpacesData> objectCacheInfo) {
if(listSpacesKey == null) {
if (listSpacesKey == null || listSpacesKey.getKey() == null) {
return false;
} else {
SpaceFilterKey spaceFilterKey = listSpacesKey.getKey();
return remoteId.equals(spaceFilterKey.getUserId())
&& SpaceType.LATEST_ACCESSED.equals(spaceFilterKey.getType());
}

SpaceFilterKey spaceFilterKey = listSpacesKey.getKey();
if(spaceFilterKey == null) {
return false;
}

return remoteId.equals(spaceFilterKey.getUserId())
&& (SpaceType.LATEST_ACCESSED.equals(spaceFilterKey.getType())
|| SpaceType.VISITED.equals(spaceFilterKey.getType()));
}

@SuppressWarnings("unchecked")
Expand All @@ -62,7 +59,9 @@ public void onSelect(ExoCache<? extends ListSpacesKey, ? extends ListSpacesData>
updateStore = false;
return;
} else if (listSpacesKey.getOffset() == 0
&& SpaceType.LATEST_ACCESSED.equals(listSpacesKey.getKey().getType())) {
&& SpaceType.LATEST_ACCESSED.equals(listSpacesKey.getKey().getType())
&& (listSpacesKey.getKey().getTemplateId() == 0
|| templateId == listSpacesKey.getKey().getTemplateId())) {
SpaceKey spaceKey = new SpaceKey(space.getSpaceId());
ids = new ArrayList<>(ids);
if (ids.contains(spaceKey)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1545,29 +1545,6 @@ public void testGetVisibleSpacesInvitedMember() throws Exception {
}
}

public void testVisited() throws Exception {
Space space0 = getSpaceInstance(5);
spaceStorage.saveSpace(space0, true);
Space space1 = getSpaceInstance(6);
spaceStorage.saveSpace(space1, true);

SpaceFilter filter = new SpaceFilter();
filter.setRemoteId("ghost");

List<Space> result = spaceStorage.getVisitedSpaces(filter, 0, -1);
assertEquals(2, result.size());
assertEquals(space0.getId(), result.get(0).getId());

restartTransaction();
spaceStorage.updateSpaceAccessed("ghost", space1);

// getVisitedSpaces return a list of spaces that
// order by visited space then others
result = spaceStorage.getVisitedSpaces(filter, 0, -1);
assertEquals(2, result.size());
assertEquals(space1.getId(), result.get(0).getId());
}

public void testLastAccess() throws Exception {
Space space2 = getSpaceInstance(7);
spaceStorage.saveSpace(space2, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.springframework.web.bind.annotation.RestController;

import io.meeds.social.navigation.model.NavigationConfiguration;
import io.meeds.social.navigation.model.SidebarConfiguration;
import io.meeds.social.navigation.model.TopbarConfiguration;
import io.meeds.social.navigation.service.NavigationConfigurationService;

import io.swagger.v3.oas.annotations.Operation;
Expand Down Expand Up @@ -56,6 +58,30 @@ public NavigationConfiguration getNavigationConfiguration(HttpServletRequest req
return navigationConfigurationService.getConfiguration(request.getRemoteUser(), request.getLocale(), true);
}

@GetMapping("/topbar")
@Secured("users")
@Operation(summary = "Retrieve Topbar settings",
method = "GET",
description = "This retrieves the configuration of Topbar switch user roles")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Request fulfilled"),
})
public TopbarConfiguration getTopbarConfiguration(HttpServletRequest request) {
return navigationConfigurationService.getTopbarConfiguration(request.getRemoteUser(), request.getLocale());
}

@GetMapping("/sidebar")
@Secured("users")
@Operation(summary = "Retrieve Sidebar settings",
method = "GET",
description = "This retrieves the configuration of Sidebar switch user roles")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Request fulfilled"),
})
public SidebarConfiguration getSidebarConfiguration(HttpServletRequest request) {
return navigationConfigurationService.getSidebarConfiguration(request.getRemoteUser(), request.getLocale());
}

@PutMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
@Secured("administrators")
@Operation(summary = "Updates Topbar and Sidebar settings",
Expand Down
Loading

0 comments on commit d170151

Please sign in to comment.