From 0e4a11e8c232454d0dff362bb31928501dbabfa2 Mon Sep 17 00:00:00 2001 From: Helmi Akermi <70575401+hakermi@users.noreply.github.com> Date: Tue, 19 Nov 2024 10:29:20 +0100 Subject: [PATCH] feat: Implement edit schedule of article created from exiting note page - 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. --- .../main/java/io/meeds/news/model/News.java | 3 ++ .../io/meeds/news/service/NewsService.java | 8 +-- .../news/service/impl/NewsServiceImpl.java | 49 +++++++++++++------ .../service/impl/NewsServiceImplTest.java | 15 +++++- 4 files changed, 55 insertions(+), 20 deletions(-) diff --git a/content-service/src/main/java/io/meeds/news/model/News.java b/content-service/src/main/java/io/meeds/news/model/News.java index ae36779dd..8ede3a6db 100644 --- a/content-service/src/main/java/io/meeds/news/model/News.java +++ b/content-service/src/main/java/io/meeds/news/model/News.java @@ -144,4 +144,7 @@ public class News { private NotePageProperties properties; private boolean fromDraft; + + private boolean fromExternalPage; + } diff --git a/content-service/src/main/java/io/meeds/news/service/NewsService.java b/content-service/src/main/java/io/meeds/news/service/NewsService.java index adf9371c3..5d94816cc 100644 --- a/content-service/src/main/java/io/meeds/news/service/NewsService.java +++ b/content-service/src/main/java/io/meeds/news/service/NewsService.java @@ -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 diff --git a/content-service/src/main/java/io/meeds/news/service/impl/NewsServiceImpl.java b/content-service/src/main/java/io/meeds/news/service/impl/NewsServiceImpl.java index e5218f95d..f81c76436 100644 --- a/content-service/src/main/java/io/meeds/news/service/impl/NewsServiceImpl.java +++ b/content-service/src/main/java/io/meeds/news/service/impl/NewsServiceImpl.java @@ -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"; @@ -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(), @@ -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; } @@ -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; } @@ -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; } @@ -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; @@ -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, @@ -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())); @@ -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))); } diff --git a/content-service/src/test/java/io/meeds/news/service/impl/NewsServiceImplTest.java b/content-service/src/test/java/io/meeds/news/service/impl/NewsServiceImplTest.java index b73c90208..c8114461a 100644 --- a/content-service/src/test/java/io/meeds/news/service/impl/NewsServiceImplTest.java +++ b/content-service/src/test/java/io/meeds/news/service/impl/NewsServiceImplTest.java @@ -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 @@ -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); @@ -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