Skip to content

Commit

Permalink
fix: fix publish article from empty note default home page - EXO-7304…
Browse files Browse the repository at this point in the history
…1 - Meeds-io/MIPs#161

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 19, 2024
1 parent e6e4e96 commit ae663a4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -238,20 +238,22 @@ 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 (isScheduled(news)) {
news = postScheduledArticle(news);
} else if (!news.isFromDraft() && noteService.getNoteById(news.getId()) != null) {
} else if (isExistingNote(news)) {
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) {
ensureAuthor(news, poster);
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 @@ -1179,6 +1181,33 @@ public void deleteDraftArticle(String draftArticleId, String draftArticleCreator
public List<String> getArticleLanguages(String articleId, boolean withDrafts) throws WikiException {
return noteService.getPageAvailableTranslationLanguages(Long.parseLong(articleId), withDrafts);
}

private boolean isScheduled(News news) {
return STAGED.equals(news.getPublicationState()) || news.getSchedulePostDate() != null;
}

private boolean isExistingNote(News news) throws Exception {
return !news.isFromDraft() && noteService.getNoteById(news.getId()) != null;
}

private void ensureAuthor(News news, String poster) {
if (news.getAuthor() == null) {
news.setAuthor(poster);
}
}

private void postProcessing(News news, String poster) throws Exception {
postNewsActivity(news);
sendNotification(poster, news, NotificationConstants.NOTIFICATION_CONTEXT.POST_NEWS);

if (news.isPublished()) {
publishNews(news, poster);
}

// Broadcast events for gamification and analytics
NewsUtils.broadcastEvent(NewsUtils.POST_NEWS_ARTICLE, news.getId(), news);
NewsUtils.broadcastEvent(NewsUtils.POST_NEWS, news.getAuthor(), news);
}

private void buildNewArticleProperties(News article,
Page articlePage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,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

0 comments on commit ae663a4

Please sign in to comment.