Skip to content

Commit

Permalink
feat: Implement share news service layer - EXO-70812 - Meeds-io/MIPs#119
Browse files Browse the repository at this point in the history
  (#26)
  • Loading branch information
sofyenne authored and azayati committed Apr 24, 2024
1 parent acfcb04 commit 4bd6f5c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import io.meeds.news.service.NewsService;
import io.meeds.news.utils.NewsUtils;

import static io.meeds.news.utils.NewsUtils.NewsObjectType.ARTICLE;

/**
* A triggered listener class about activity lifecyles. This class is used to
* propagate sharing activity in News elements to let targeted space members to
Expand Down Expand Up @@ -76,8 +78,8 @@ public void shareActivity(ActivityLifeCycleEvent event) {
String newsId = originalActivity.getTemplateParams().get(NEWS_ID);
org.exoplatform.services.security.Identity currentIdentity = ConversationState.getCurrent().getIdentity();
try {
News news = newsService.getNewsById(newsId, currentIdentity, false);
if (news != null) {
News news = newsService.getNewsById(newsId, currentIdentity, false, ARTICLE.name().toLowerCase());
if (news != null && !news.isDeleted()) {
Identity posterIdentity = getIdentity(sharedActivity);
Space space = getSpace(sharedActivity);
newsService.shareNews(news, space, posterIdentity, sharedActivity.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -708,8 +708,8 @@ public boolean canViewNews(News news, String authenticatedUser) {
|| isMemberOfsharedInSpaces(news, authenticatedUser))) {
return false;
}
if (news.isPublished() && news.getAudience().equals(NewsUtils.SPACE_NEWS_AUDIENCE)
&& !spaceService.isMember(space, authenticatedUser)) {
if (news.isPublished() && StringUtils.equals(news.getPublicationState(), PUBLISHED) && news.getAudience().equals(NewsUtils.SPACE_NEWS_AUDIENCE)
&& !(spaceService.isMember(space, authenticatedUser) || isMemberOfsharedInSpaces(news, authenticatedUser))) {
return false;
}
if (StringUtils.equals(news.getPublicationState(), STAGED)
Expand All @@ -732,6 +732,35 @@ public void shareNews(News news,
org.exoplatform.social.core.identity.model.Identity userIdentity,
String sharedActivityId) throws Exception {

if (!canViewNews(news, userIdentity.getRemoteId())) {
throw new IllegalAccessException("User with id " + userIdentity.getRemoteId() + "doesn't have access to news");
}
if (sharedActivityId != null) {
// update article metadata activities
NewsPageObject newsPageObject = new NewsPageObject(NEWS_METADATA_PAGE_OBJECT_TYPE, news.getId(), null);
MetadataItem metadataItem = metadataService.getMetadataItemsByMetadataAndObject(NEWS_METADATA_KEY, newsPageObject).stream().findFirst().orElse(null);
if (metadataItem == null) {
throw new ObjectNotFoundException("News metadata object with id " + news.getId() + " wasn't found");
}

Map<String, String> properties = metadataItem.getProperties();
if (properties == null) {
properties = new HashMap<>();
}
if (properties.containsKey(NEWS_ACTIVITIES)) {
String newsActivities = properties.get(NEWS_ACTIVITIES);
newsActivities = newsActivities.concat(";").concat(space.getId()).concat(":").concat(sharedActivityId);
properties.put(NEWS_ACTIVITIES, newsActivities);
} else {
properties.put(NEWS_ACTIVITIES, space.getId().concat(":").concat(sharedActivityId));
}

metadataItem.setProperties(properties);
metadataService.updateMetadataItem(metadataItem, Long.parseLong(userIdentity.getId()));

NewsUtils.broadcastEvent(NewsUtils.SHARE_NEWS, userIdentity.getRemoteId(), news);
}

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package io.meeds.news.listener;

import static io.meeds.news.utils.NewsUtils.NewsObjectType.ARTICLE;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -188,7 +189,7 @@ public void testNotShareWhenSharedActivityWhenNewsNotFound() throws Exception {

newsActivityListener.shareActivity(event);

verify(newsService, times(1)).getNewsById(newsId, currentIdentity, false);
verify(newsService, times(1)).getNewsById(newsId, currentIdentity, false, ARTICLE.name().toLowerCase());
verify(newsService, never()).shareNews(nullable(News.class),
nullable(Space.class),
nullable(Identity.class),
Expand Down Expand Up @@ -232,11 +233,11 @@ public void testShareWhenNewsFound() throws Exception {
ConversationState.setCurrent(new ConversationState(currentIdentity));

News news = new News();
when(newsService.getNewsById(newsId, currentIdentity, false)).thenReturn(news);
when(newsService.getNewsById(newsId, currentIdentity, false, ARTICLE.name().toLowerCase())).thenReturn(news);

newsActivityListener.shareActivity(event);

verify(newsService, times(1)).getNewsById(newsId, currentIdentity, false);
verify(newsService, times(1)).getNewsById(newsId, currentIdentity, false, ARTICLE.name().toLowerCase());
verify(newsService, times(1)).shareNews(eq(news), nullable(Space.class), nullable(Identity.class), nullable(String.class));
}
}

0 comments on commit 4bd6f5c

Please sign in to comment.