Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
azayati authored Sep 18, 2024
2 parents b988d77 + 31be95b commit 2b6bccb
Show file tree
Hide file tree
Showing 89 changed files with 3,121 additions and 2,568 deletions.
8 changes: 3 additions & 5 deletions content-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<rest.api.doc.version>1.0</rest.api.doc.version>
<rest.api.doc.description>Content addon used Rest endpoints</rest.api.doc.description>

<exo.test.coverage.ratio>0.53</exo.test.coverage.ratio>
<exo.test.coverage.ratio>0.51</exo.test.coverage.ratio>
</properties>

<dependencies>
Expand Down Expand Up @@ -78,10 +78,8 @@
<groupId>io.openapitools.swagger</groupId>
<artifactId>swagger-maven-plugin</artifactId>
<configuration>
<useResourcePackagesChildren>true</useResourcePackagesChildren>
<resourcePackages>
<locations>io.meeds.news.rest</locations>
</resourcePackages>
<applicationClass>io.meeds.news.rest.NewsRest</applicationClass>
<applicationClass>io.meeds.news.rest.NewsTargetingRest</applicationClass>
<swaggerConfig>
<info>
<title>${rest.api.doc.title}</title>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@
package io.meeds.news.activity.processor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jakarta.annotation.PostConstruct;
import liquibase.util.CollectionUtil;
import org.apache.commons.lang3.math.NumberUtils;
import org.exoplatform.container.xml.InitParams;
import org.exoplatform.container.xml.ValueParam;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
import org.exoplatform.social.common.RealtimeListAccess;
Expand All @@ -33,21 +38,37 @@
import io.meeds.news.model.News;
import io.meeds.news.service.NewsService;
import io.meeds.news.utils.NewsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

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

@Component
public class ActivityNewsProcessor extends BaseActivityProcessorPlugin {

private static final Log LOG = ExoLogger.getLogger(ActivityNewsProcessor.class);
private static final Log LOG = ExoLogger.getLogger(ActivityNewsProcessor.class);

private static final String ACTIVITY_PROCESSOR_NAME = "ActivityNewsProcessor";

private static final int processorPriority = 30;

private NewsService newsService;
@Autowired
private NewsService newsService;

@Autowired
private ActivityManager activityManager;

public ActivityNewsProcessor() {
super(getInitParams());
}

private ActivityManager activityManager;
@PostConstruct
public void init() {
activityManager.addProcessorPlugin(this);
}

public ActivityNewsProcessor(ActivityManager activityManager, NewsService newsService, InitParams initParams) {
super(initParams);
this.newsService = newsService;
this.activityManager = activityManager;
@Override
public String getName() {
return ACTIVITY_PROCESSOR_NAME;
}

@Override
Expand All @@ -72,12 +93,35 @@ public void processActivity(ExoSocialActivity activity) {

activity.setMetadataObjectId(news.getId());
activity.setMetadataObjectType(NewsUtils.NEWS_METADATA_OBJECT_TYPE);
news.setIllustration(null);
} catch (Exception e) {
LOG.warn("Error retrieving news with id {}", activity.getTemplateParams().get("newsId"), e);
}
activity.getLinkedProcessedEntities().put("news", news);
try {
String newsId = news.getId();
List<String> articleLanguages = newsService.getArticleLanguages(newsId, false);
if (!CollectionUtils.isEmpty(articleLanguages)) {
Map<String, News> newsTranslations = new HashMap<>();
articleLanguages.stream().forEach(articleLanguage -> {
News articleTranslation = newsService.getNewsArticleByIdAndLang(newsId, articleLanguage);
if (articleTranslation != null) {
newsTranslations.put("news_" + articleLanguage, articleTranslation);
}
});
activity.getLinkedProcessedEntities().put("newsTranslations", newsTranslations);
}
} catch (Exception exception) {
LOG.error("Error when adding the news translation to the activity linked processed entities", exception);
}
}
}

private static InitParams getInitParams() {
InitParams initParams = new InitParams();
ValueParam param = new ValueParam();
param.setName("priority");
param.setValue(String.valueOf(processorPriority));
initParams.addParameter(param);
return initParams;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,70 +20,77 @@
package io.meeds.news.listener;

import static io.meeds.analytics.utils.AnalyticsUtils.addSpaceStatistics;
import static io.meeds.news.utils.NewsUtils.COMMENT_NEWS;
import static io.meeds.news.utils.NewsUtils.DELETE_NEWS;
import static io.meeds.news.utils.NewsUtils.LIKE_NEWS;
import static io.meeds.news.utils.NewsUtils.POST_NEWS;
import static io.meeds.news.utils.NewsUtils.SHARE_NEWS;
import static io.meeds.news.utils.NewsUtils.UPDATE_NEWS;
import static io.meeds.news.utils.NewsUtils.VIEW_NEWS;

import jakarta.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import io.meeds.analytics.model.StatisticData;
import io.meeds.analytics.utils.AnalyticsUtils;
import org.exoplatform.container.ExoContainerContext;
import org.exoplatform.services.listener.Asynchronous;
import org.exoplatform.services.listener.Event;
import org.exoplatform.services.listener.Listener;
import org.exoplatform.services.listener.ListenerService;
import org.exoplatform.social.core.identity.model.Identity;
import org.exoplatform.social.core.identity.provider.OrganizationIdentityProvider;
import org.exoplatform.social.core.manager.IdentityManager;
import org.exoplatform.social.core.space.model.Space;
import org.exoplatform.social.core.space.spi.SpaceService;

import io.meeds.analytics.model.StatisticData;
import io.meeds.analytics.utils.AnalyticsUtils;
import io.meeds.news.model.News;
import io.meeds.news.utils.NewsUtils;

@Asynchronous
@Component
@Profile("analytics")
public class AnalyticsNewsListener extends Listener<String, News> {

private static final String CREATE_CONTENT_OPERATION_NAME = "createContent";
private static final String CREATE_CONTENT_OPERATION_NAME = "createContent";

private static final String UPDATE_CONTENT_OPERATION_NAME = "updateContent";

private static final String DELETE_CONTENT_OPERATION_NAME = "deleteContent";

private static final String UPDATE_CONTENT_OPERATION_NAME = "updateContent";
private static final String VIEW_CONTENT_OPERATION_NAME = "viewContent";

private static final String DELETE_CONTENT_OPERATION_NAME = "deleteContent";
private static final String SHARE_CONTENT_OPERATION_NAME = "shareContent";

private static final String VIEW_CONTENT_OPERATION_NAME = "viewContent";
private static final String LIKE_CONTENT_OPERATION_NAME = "likeContent";

private static final String SHARE_CONTENT_OPERATION_NAME = "shareContent";
private static final String COMMENT_CONTENT_OPERATION_NAME = "commentContent";

private static final String LIKE_CONTENT_OPERATION_NAME = "likeContent";
private static final String[] LISTENER_EVENTS = { POST_NEWS, UPDATE_NEWS, DELETE_NEWS, VIEW_NEWS, SHARE_NEWS, COMMENT_NEWS, LIKE_NEWS };

private static final String COMMENT_CONTENT_OPERATION_NAME = "commentContent";
@Autowired
private IdentityManager identityManager;

private IdentityManager identityManager;
@Autowired
private SpaceService spaceService;

private SpaceService spaceService;
@Autowired
private ListenerService listenerService;

@PostConstruct
public void init() {
for (String listener : LISTENER_EVENTS) {
listenerService.addListener(listener, this);
}
}

@Override
public void onEvent(Event<String, News> event) throws Exception {
News news = event.getData();
String operation = "";
switch (event.getEventName()) {
case "exo.news.postArticle":
operation = CREATE_CONTENT_OPERATION_NAME;
break;
case "exo.news.updateArticle":
operation = UPDATE_CONTENT_OPERATION_NAME;
break;
case "exo.news.deleteArticle":
operation = DELETE_CONTENT_OPERATION_NAME;
break;
case "exo.news.viewArticle":
operation = VIEW_CONTENT_OPERATION_NAME;
break;
case "exo.news.shareArticle":
operation = SHARE_CONTENT_OPERATION_NAME;
break;
case "exo.news.commentArticle":
operation = COMMENT_CONTENT_OPERATION_NAME;
break;
case "exo.news.likeArticle":
operation = LIKE_CONTENT_OPERATION_NAME;
break;
}
String operation = mapEventNameToOperation(event.getEventName());
long userId = 0;
Identity identity = getIdentityManager().getOrCreateIdentity(OrganizationIdentityProvider.NAME, event.getSource());
if (identity != null) {
Expand Down Expand Up @@ -115,6 +122,27 @@ public void onEvent(Event<String, News> event) throws Exception {
AnalyticsUtils.addStatisticData(statisticData);
}

private String mapEventNameToOperation(String eventName) {
switch (eventName) {
case "exo.news.postArticle":
return CREATE_CONTENT_OPERATION_NAME;
case "exo.news.updateArticle":
return UPDATE_CONTENT_OPERATION_NAME;
case "exo.news.deleteArticle":
return DELETE_CONTENT_OPERATION_NAME;
case "exo.news.viewArticle":
return VIEW_CONTENT_OPERATION_NAME;
case "exo.news.shareArticle":
return SHARE_CONTENT_OPERATION_NAME;
case "exo.news.commentArticle":
return COMMENT_CONTENT_OPERATION_NAME;
case "exo.news.likeArticle":
return LIKE_CONTENT_OPERATION_NAME;
default:
throw new IllegalArgumentException("Unknown event: " + eventName);
}
}

public IdentityManager getIdentityManager() {
if (identityManager == null) {
identityManager = ExoContainerContext.getService(IdentityManager.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,51 @@

import org.exoplatform.services.listener.Event;
import org.exoplatform.services.listener.Listener;
import org.exoplatform.services.listener.ListenerService;
import org.exoplatform.social.core.activity.model.ExoSocialActivity;
import org.exoplatform.social.core.storage.api.ActivityStorage;
import org.exoplatform.social.core.storage.cache.CachedActivityStorage;

import io.meeds.news.model.News;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import jakarta.annotation.PostConstruct;

import static io.meeds.news.utils.NewsUtils.ADD_ARTICLE_TRANSLATION;
import static io.meeds.news.utils.NewsUtils.POST_NEWS;
import static io.meeds.news.utils.NewsUtils.REMOVE_ARTICLE_TRANSLATION;
import static io.meeds.news.utils.NewsUtils.SCHEDULE_NEWS;
import static io.meeds.news.utils.NewsUtils.SHARE_NEWS;
import static io.meeds.news.utils.NewsUtils.UNSCHEDULE_NEWS;
import static io.meeds.news.utils.NewsUtils.UPDATE_NEWS;

/**
* A listener to clear cached news inside
* {@link ExoSocialActivity#getLinkedProcessedEntities()} after any modification
* made on {@link News}
*/
@Component
public class AttachedActivityCacheUpdater extends Listener<String, News> {

@Autowired
private ActivityStorage activityStorage;

@Autowired
private ListenerService listenerService;

private CachedActivityStorage cachedActivityStorage;

public AttachedActivityCacheUpdater(ActivityStorage activityStorage) {
private String[] LISTENER_EVENTS = { POST_NEWS, UPDATE_NEWS, SHARE_NEWS, SCHEDULE_NEWS, UNSCHEDULE_NEWS, ADD_ARTICLE_TRANSLATION, REMOVE_ARTICLE_TRANSLATION };

@PostConstruct
public void init() {
if (activityStorage instanceof CachedActivityStorage) {
this.cachedActivityStorage = (CachedActivityStorage) activityStorage;
}
for (String listener : LISTENER_EVENTS) {
listenerService.addListener(listener, this);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.exoplatform.commons.search.index.IndexingService;
import org.exoplatform.services.listener.Event;
import org.exoplatform.services.listener.Listener;
import org.exoplatform.services.listener.ListenerService;
import org.exoplatform.social.core.storage.api.ActivityStorage;
import org.exoplatform.social.core.storage.cache.CachedActivityStorage;
import org.exoplatform.social.metadata.model.MetadataItem;
Expand All @@ -32,21 +33,38 @@
import io.meeds.news.search.NewsIndexingServiceConnector;
import io.meeds.news.service.NewsService;
import io.meeds.news.utils.NewsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import jakarta.annotation.PostConstruct;

@Component
public class MetadataItemModified extends Listener<Long, MetadataItem> {

@Autowired
private IndexingService indexingService;

@Autowired
private NewsService newsService;

@Autowired
private ActivityStorage activityStorage;

@Autowired
private ListenerService listenerService;

private CachedActivityStorage cachedActivityStorage;

public MetadataItemModified(NewsService newsService, IndexingService indexingService, ActivityStorage activityStorage) {
this.newsService = newsService;
this.indexingService = indexingService;
private String[] LISTENER_EVENTS = { "social.metadataItem.updated", "social.metadataItem.created", "social.metadataItem.deleted" };

@PostConstruct
public void init() {
if (activityStorage instanceof CachedActivityStorage) {
this.cachedActivityStorage = (CachedActivityStorage) activityStorage;
}
for (String listener : LISTENER_EVENTS) {
listenerService.addListener(listener, this);
}
}

@Override
Expand All @@ -57,7 +75,7 @@ public void onEvent(Event<Long, MetadataItem> event) throws Exception {
if (isNewsEvent(objectType)) {
// Ensure to re-execute all ActivityProcessors to compute & cache
// metadatas of the activity again
News news = newsService.getNewsById(objectId, false);
News news = newsService.getNewsArticleById(StringUtils.substringBefore(objectId, "-"));
if (news != null) {
if (StringUtils.isNotBlank(news.getActivityId())) {
clearCache(news.getActivityId());
Expand Down
Loading

0 comments on commit 2b6bccb

Please sign in to comment.