Skip to content

Commit

Permalink
feat: Implement edit schedule of article created from exiting note pa…
Browse files Browse the repository at this point in the history
…ge - EXO-75389 - Meeds-io/MIPs#161 (#311)

Implement edit schedule of article created from exiting note page

fix: fix publish article from empty note default home page - EXO-73041 - Meeds-io/MIPs#161 (#313)

prior to this change, when try to publish newly created default note
home page, an exception is thrown as the note is auto created without
specifying the author.
This PR ensures to set the poster as author during the publish process.
  • Loading branch information
hakermi committed Nov 26, 2024
1 parent 015f03c commit 0e4a11e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 20 deletions.
3 changes: 3 additions & 0 deletions content-service/src/main/java/io/meeds/news/model/News.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,7 @@ public class News {
private NotePageProperties properties;

private boolean fromDraft;

private boolean fromExternalPage;

}
Original file line number Diff line number Diff line change
Expand Up @@ -265,13 +265,13 @@ News scheduleNews(News news,
/**
* Un-schedule publishing a News
*
* @param news
* @param pageOwnerId
* @param newsArticleCreator
* @param news news article
* @param space owner space
* @param newsArticleCreator article creator
* @return unscheduled News
* @throws Exception when error occurs
*/
News unScheduleNews(News news, String pageOwnerId, String newsArticleCreator) throws Exception;
News unScheduleNews(News news, Space space, String newsArticleCreator) throws Exception;

/**
* Search news by term
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public class NewsServiceImpl implements NewsService {
/** The Constant NEWS_METADATA_LATEST_DRAFT_OBJECT_TYPE. */
public static final String NEWS_METADATA_LATEST_DRAFT_OBJECT_TYPE = "newsLatestDraftPage";

public static final String NEWS_ATTACHMENTS_IDS = "attachmentsIds";
public static final String EXTERNAL_PAGE = "externalPage";

public static final String ARTICLE_CONTENT = "content";

Expand Down Expand Up @@ -221,7 +221,7 @@ public News createNews(News news, Identity currentIdentity) throws Exception {
if (POSTED.equals(news.getPublicationState())) {
createdNews = postNews(news, currentIdentity.getUserId());
} else if (news.getSchedulePostDate() != null) {
createdNews = unScheduleNews(news, space.getGroupId(), currentIdentity.getUserId());
createdNews = unScheduleNews(news, space, currentIdentity.getUserId());
} else {
createdNews = createDraftArticleForNewPage(news,
space.getGroupId(),
Expand All @@ -237,20 +237,21 @@ public News createNews(News news, Identity currentIdentity) throws Exception {

@Override
public News postNews(News news, String poster) throws Exception {
if (news.getPublicationState().equals(STAGED) || news.getSchedulePostDate() != null) {
if (news == null || poster == null || poster.isBlank()) {
throw new IllegalArgumentException("News and poster cannot be null or empty.");
}

if (STAGED.equals(news.getPublicationState()) || news.getSchedulePostDate() != null) {
news = postScheduledArticle(news);
} else if (!news.isFromDraft() && noteService.getNoteById(news.getId()) != null) {
news = createArticleFromExistingPage(news, poster);
} else {
news = createNewsArticlePage(news, poster);
}
postNewsActivity(news);
sendNotification(poster, news, NotificationConstants.NOTIFICATION_CONTEXT.POST_NEWS);
if (news.isPublished()) {
publishNews(news, poster);

if (news != null) {
postProcessing(news, poster);
}
NewsUtils.broadcastEvent(NewsUtils.POST_NEWS_ARTICLE, news.getId(), news);// Gamification
NewsUtils.broadcastEvent(NewsUtils.POST_NEWS, news.getAuthor(), news);// Analytics
return news;
}

Expand Down Expand Up @@ -776,12 +777,25 @@ public News scheduleNews(News news, Identity currentIdentity, String newsObjectT
* {@inheritDoc}
*/
@Override
public News unScheduleNews(News news, String pageOwnerId, String articleCreator) throws Exception {
public News unScheduleNews(News news, Space space, String articleCreator) throws Exception {
News existingNews = getNewsArticleById(news.getId());
if (existingNews != null) {
news = createDraftArticleForNewPage(news, pageOwnerId, articleCreator, System.currentTimeMillis());
if (existingNews != null && !existingNews.isFromExternalPage()) {
news = createDraftArticleForNewPage(news, space.getGroupId(), articleCreator, System.currentTimeMillis());
deleteArticle(existingNews, articleCreator);
return buildDraftArticle(news.getId(), articleCreator);
} else if (existingNews != null) {
PageVersion pageVersion = noteService.getPublishedVersionByPageIdAndLang(Long.parseLong(existingNews.getId()), null);
NewsPageVersionObject articleVersionMetaDataObject = new NewsPageVersionObject(NEWS_METADATA_PAGE_VERSION_OBJECT_TYPE,
pageVersion.getId(),
null,
Long.parseLong(space.getId()));
NewsPageObject newsPageObject = new NewsPageObject(NEWS_METADATA_PAGE_OBJECT_TYPE,
existingNews.getId(),
null,
Long.parseLong(space.getId()));
metadataService.deleteMetadataItemsByObject(articleVersionMetaDataObject);
metadataService.deleteMetadataItemsByObject(newsPageObject);
return existingNews;
}
return null;
}
Expand Down Expand Up @@ -971,7 +985,7 @@ public News createArticleFromExistingPage(News article, String creator) throws E
if (articlePage != null && space != null) {
PageVersion pageVersion = noteService.getPublishedVersionByPageIdAndLang(Long.parseLong(articlePage.getId()), null);
article.setIllustrationURL(NewsUtils.buildIllustrationUrl(articlePage.getProperties(), articlePage.getLang()));
buildNewArticleProperties(article, articlePage, creator, space.getId(), pageVersion.getId());
buildNewArticleProperties(article, articlePage, creator, space.getId(), pageVersion.getId(), true);
}
return article;
}
Expand Down Expand Up @@ -1030,7 +1044,7 @@ public News createNewsArticlePage(News newsArticle, String newsArticleCreator) t
newsArticle.setLatestVersionId(pageVersion.getId());
newsArticle.setIllustrationURL(NewsUtils.buildIllustrationUrl(newsArticlePage.getProperties(), newsArticle.getLang()));

buildNewArticleProperties(newsArticle, newsArticlePage, newsArticleCreator, space.getId(), pageVersion.getId());
buildNewArticleProperties(newsArticle, newsArticlePage, newsArticleCreator, space.getId(), pageVersion.getId(), false);
// delete the draft
deleteDraftArticle(draftNewsId, poster.getUserId());
return newsArticle;
Expand Down Expand Up @@ -1183,7 +1197,8 @@ private void buildNewArticleProperties(News article,
Page articlePage,
String creator,
String spaceId,
String versionId) throws Exception {
String versionId,
boolean externalPage) throws Exception {
NewsPageVersionObject articleVersionMetaDataObject = new NewsPageVersionObject(NEWS_METADATA_PAGE_VERSION_OBJECT_TYPE,
versionId,
null,
Expand Down Expand Up @@ -1211,6 +1226,7 @@ private void buildNewArticleProperties(News article,
if (StringUtils.isNotEmpty(article.getPublicationState())) {
newsPageProperties.put(NEWS_PUBLICATION_STATE, article.getPublicationState());
}
newsPageProperties.put(EXTERNAL_PAGE, String.valueOf(externalPage));
newsPageProperties.put(NEWS_ACTIVITY_POSTED, String.valueOf(article.isActivityPosted()));
newsPageProperties.put(PUBLISHED, String.valueOf(article.isPublished()));
newsPageProperties.put(NEWS_DELETED, String.valueOf(articlePage.isDeleted()));
Expand Down Expand Up @@ -1348,6 +1364,9 @@ private void buildArticleProperties(News article, String currentUsername, Metada
if (properties.containsKey(PUBLISHED) && StringUtils.isNotEmpty(properties.get(PUBLISHED))) {
article.setPublished(Boolean.parseBoolean(properties.get(PUBLISHED)));
}
if (properties.containsKey(EXTERNAL_PAGE) && StringUtils.isNotEmpty(properties.get(EXTERNAL_PAGE))) {
article.setFromExternalPage(Boolean.parseBoolean(properties.get(EXTERNAL_PAGE)));
}
if (properties.containsKey(NEWS_VIEWS) && StringUtils.isNotEmpty(properties.get(NEWS_VIEWS))) {
article.setViewsCount(Long.parseLong(properties.get(NEWS_VIEWS)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,11 @@ public void testPostNews() throws Exception {
anyMap(),
anyLong(),
anyBoolean());

clearInvocations(activityManager);
newsArticlePage.setAuthor(null);
newsService.createNews(newsArticle, identity);
verify(activityManager, times(1)).saveActivityNoReturn(any(), any());
}

@Test
Expand Down Expand Up @@ -950,6 +955,7 @@ public void testUnScheduleNews() throws Exception {

mockBuildArticle(metadataItems);
Space space = mockSpace();
when(space.getGroupId()).thenReturn("/spaces/test");
Wiki wiki = mock(Wiki.class);
when(wikiService.getWikiByTypeAndOwner(anyString(), anyString())).thenReturn(wiki);
org.exoplatform.wiki.model.Page rootPage = mock(org.exoplatform.wiki.model.Page.class);
Expand All @@ -976,10 +982,17 @@ public void testUnScheduleNews() throws Exception {
when(identityManager.getOrCreateUserIdentity(anyString())).thenReturn(identity1);
when(identity1.getId()).thenReturn("1");

newsService.unScheduleNews(newsArticle, space.getGroupId(), "john");
newsService.unScheduleNews(newsArticle, space, "john");

verify(noteService, times(1)).createDraftForNewPage(any(DraftPage.class), anyLong(), anyLong());
verify(noteService, times(1)).deleteNote(anyString(), anyString(), anyString());

clearInvocations(noteService);
properties.put(EXTERNAL_PAGE, "true");
newsService.unScheduleNews(newsArticle, space, "john");
verify(noteService, times(0)).createDraftForNewPage(any(DraftPage.class), anyLong(), anyLong());
verify(noteService, times(0)).deleteNote(anyString(), anyString(), anyString());
verify(metadataService, times(2)).deleteMetadataItemsByObject(any());
}

@Test
Expand Down

0 comments on commit 0e4a11e

Please sign in to comment.