Skip to content

Commit

Permalink
feat: Implement Notes/Content insert image option - EXO-74754 - Meeds…
Browse files Browse the repository at this point in the history
  • Loading branch information
sofyenne committed Dec 13, 2024
1 parent e1da202 commit f517c63
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* This file is part of the Meeds project (https://meeds.io/).
*
* Copyright (C) 2020 - 2024 Meeds Association [email protected]
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package io.meeds.news.plugin;

import io.meeds.news.model.News;
import io.meeds.news.service.NewsService;
import jakarta.annotation.PostConstruct;
import org.exoplatform.commons.exception.ObjectNotFoundException;
import org.exoplatform.services.security.Identity;
import org.exoplatform.social.attachment.AttachmentPlugin;
import org.exoplatform.social.attachment.AttachmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Component
public class ArticlePageAttachmentPlugin extends AttachmentPlugin {

@Autowired
private AttachmentService attachmentService;

@Autowired
private NewsService newsService;

public static final String OBJECT_TYPE = "articlePage";

@PostConstruct
public void init() {
attachmentService.addPlugin(this);
}

@Override
public String getObjectType() {
return OBJECT_TYPE;
}

@Override
public boolean hasAccessPermission(Identity identity, String articleId) throws ObjectNotFoundException {
News news = newsService.getNewsArticleById(articleId);
return news != null && newsService.canViewNews(news, identity.getUserId());
}

@Override
public boolean hasEditPermission(Identity identity, String articleId) throws ObjectNotFoundException {
News news = null;
try {
news = newsService.getNewsById(articleId, identity, false, ARTICLE.name());
} catch (IllegalAccessException e) {
return false;
}
return news != null && news.isCanEdit();
}

@Override
public long getAudienceId(String s) throws ObjectNotFoundException {
return 0;
}

@Override
public long getSpaceId(String articleId) throws ObjectNotFoundException {
News news = newsService.getNewsArticleById(articleId);
return news != null ? Long.parseLong(news.getSpaceId()) : 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@
import java.util.regex.Matcher;
import java.util.stream.Stream;

import io.meeds.news.plugin.ArticlePageAttachmentPlugin;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.exoplatform.wiki.service.plugin.WikiDraftPageAttachmentPlugin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -925,6 +927,7 @@ public News createDraftArticleForNewPage(News draftArticle,
draftArticlePage.setParentPageId(newsArticlesRootNotePage.getId());
draftArticlePage.setAuthor(draftArticle.getAuthor());
draftArticlePage.setProperties(draftArticle.getProperties());
draftArticlePage.setAttachmentObjectType(WikiDraftPageAttachmentPlugin.OBJECT_TYPE);
draftArticlePage =
noteService.createDraftForNewPage(draftArticlePage,
creationDate,
Expand All @@ -936,6 +939,7 @@ public News createDraftArticleForNewPage(News draftArticle,
draftArticle.setId(draftArticlePage.getId());
draftArticle.setCreationDate(draftArticlePage.getCreatedDate());
draftArticle.setUpdateDate(draftArticlePage.getUpdatedDate());
draftArticle.setBody(draftArticlePage.getContent());
Space draftArticleSpace = spaceService.getSpaceByGroupId(pageOwnerId);
draftArticle.setSpaceId(draftArticleSpace.getId());
NewsDraftObject draftArticleMetaDataObject = new NewsDraftObject(NEWS_METADATA_DRAFT_OBJECT_TYPE,
Expand Down Expand Up @@ -989,6 +993,7 @@ public News createNewsArticlePage(News newsArticle, String newsArticleCreator) t

if (newsArticlesRootNotePage != null) {
Page newsArticlePage = new Page();
newsArticlePage.setAttachmentObjectType(ArticlePageAttachmentPlugin.OBJECT_TYPE);
newsArticlePage.setName(newsArticle.getName());
newsArticlePage.setTitle(newsArticle.getTitle());
newsArticlePage.setContent(newsArticle.getBody());
Expand All @@ -1003,6 +1008,7 @@ public News createNewsArticlePage(News newsArticle, String newsArticleCreator) t
PageVersion pageVersion = noteService.getPublishedVersionByPageIdAndLang(Long.parseLong(newsArticlePage.getId()), null);
// set properties
newsArticle.setId(newsArticlePage.getId());
newsArticle.setBody(pageVersion.getContent());
newsArticle.setLang(newsArticlePage.getLang());
newsArticle.setCreationDate(pageVersion.getCreatedDate());
newsArticle.setProperties(newsArticlePage.getProperties());
Expand Down Expand Up @@ -1066,6 +1072,7 @@ public News createDraftForExistingPage(News draftArticle,
long creationDate,
Space space) throws Exception {
DraftPage draftArticlePage = new DraftPage();
draftArticlePage.setAttachmentObjectType(ArticlePageAttachmentPlugin.OBJECT_TYPE);
draftArticlePage.setNewPage(false);
draftArticlePage.setTargetPageId(targetArticlePage.getId());
draftArticlePage.setTitle(draftArticle.getTitle());
Expand All @@ -1082,6 +1089,7 @@ public News createDraftForExistingPage(News draftArticle,
draftArticle.setTargetPageId(draftArticlePage.getTargetPageId());
draftArticle.setProperties(draftArticlePage.getProperties());
draftArticle.setId(draftArticlePage.getId());
draftArticle.setBody(draftArticlePage.getContent());
NewsLatestDraftObject latestDraftObject = new NewsLatestDraftObject(NEWS_METADATA_LATEST_DRAFT_OBJECT_TYPE,
draftArticlePage.getId(),
targetArticlePage.getId(),
Expand Down Expand Up @@ -1177,13 +1185,15 @@ private News updateDraftArticleForNewPage(News draftArticle, String draftArticle
draftArticlePage.setTitle(draftArticle.getTitle());
draftArticlePage.setContent(draftArticle.getBody());
draftArticlePage.setProperties(draftArticle.getProperties());
draftArticlePage.setAttachmentObjectType(WikiDraftPageAttachmentPlugin.OBJECT_TYPE);
// created and updated date set by default during the draft creation
DraftPage draftPage =
noteService.updateDraftForNewPage(draftArticlePage,
System.currentTimeMillis(),
Long.parseLong(identityManager.getOrCreateUserIdentity(draftArticleUpdater)
.getId()));
draftArticle.setProperties(draftPage.getProperties());
draftArticle.setBody(draftPage.getContent());
draftArticle.setIllustrationURL(NewsUtils.buildIllustrationUrl(draftPage.getProperties(), draftArticle.getLang()));

// Update content permissions
Expand Down Expand Up @@ -1787,6 +1797,7 @@ private News updateArticle(News news, Identity updater, String newsUpdateType) t
existingPage.setContent(news.getBody());
}
existingPage.setProperties(news.getProperties());
existingPage.setAttachmentObjectType(ArticlePageAttachmentPlugin.OBJECT_TYPE);
existingPage = noteService.updateNote(existingPage, PageUpdateType.EDIT_PAGE_CONTENT_AND_TITLE, updater);
news.setUpdateDate(existingPage.getUpdatedDate());
news.setUpdater(existingPage.getAuthor());
Expand Down Expand Up @@ -1833,6 +1844,8 @@ private News updateArticle(News news, Identity updater, String newsUpdateType) t
// create the version
if (newsUpdateType.equalsIgnoreCase(CONTENT_AND_TITLE.name())) {
noteService.createVersionOfNote(existingPage, updater.getUserId());
PageVersion pageVersion = noteService.getPublishedVersionByPageIdAndLang(Long.valueOf(news.getId()), news.getLang());
news.setBody(pageVersion.getContent());
// remove the draft
DraftPage draftPage = noteService.getLatestDraftPageByUserAndTargetPageAndLang(Long.parseLong(existingPage.getId()),
updater.getUserId(),
Expand Down Expand Up @@ -1957,13 +1970,15 @@ private News updateDraftArticleForExistingPage(News news, String updater, Page p
draftPage.setTargetPageId(page.getId());
draftPage.setLang(news.getLang());
draftPage.setProperties(news.getProperties());
draftPage.setAttachmentObjectType(ArticlePageAttachmentPlugin.OBJECT_TYPE);

draftPage = noteService.updateDraftForExistPage(draftPage, page, null, System.currentTimeMillis(), updater);

news.setDraftUpdateDate(draftPage.getUpdatedDate());
news.setDraftUpdater(draftPage.getAuthor());
news.setTargetPageId(draftPage.getTargetPageId());
news.setProperties(draftPage.getProperties());
news.setBody(draftPage.getContent());
news.setIllustrationURL(NewsUtils.buildIllustrationUrl(draftPage.getProperties(), news.getLang()));

NewsLatestDraftObject latestDraftObject = new NewsLatestDraftObject(NEWS_METADATA_LATEST_DRAFT_OBJECT_TYPE,
Expand Down Expand Up @@ -2090,7 +2105,7 @@ private News addNewArticleVersionWithLang(News news, Identity versionCreator, Sp
String newsId = news.getTargetPageId() != null ? news.getTargetPageId() : news.getId();
Page existingPage = noteService.getNoteById(newsId);
if (existingPage != null) {
existingPage.setLang(news.getLang());
existingPage.setAttachmentObjectType(ArticlePageAttachmentPlugin.OBJECT_TYPE);
existingPage = noteService.updateNote(existingPage, PageUpdateType.EDIT_PAGE_CONTENT_AND_TITLE, versionCreator);
news.setPublicationState(POSTED);
// update the metadata item
Expand All @@ -2113,12 +2128,15 @@ private News addNewArticleVersionWithLang(News news, Identity versionCreator, Sp
}
existingPage.setTitle(news.getTitle());
existingPage.setContent(news.getBody());
existingPage.setLang(news.getLang());
NotePageProperties properties = news.getProperties();
if (properties != null) {
properties.setDraft(false);
}
existingPage.setProperties(properties);
noteService.createVersionOfNote(existingPage, versionCreator.getUserId());
PageVersion pageVersion = noteService.getPublishedVersionByPageIdAndLang(Long.valueOf(newsId), news.getLang());
news.setBody(pageVersion.getContent());
news.setIllustrationURL(NewsUtils.buildIllustrationUrl(news.getProperties(), news.getLang()));
DraftPage draftPage = noteService.getLatestDraftPageByTargetPageAndLang(Long.parseLong(newsId), news.getLang());
if (draftPage != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* This file is part of the Meeds project (https://meeds.io/).
*
* Copyright (C) 2020 - 2024 Meeds Association [email protected]
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package io.meeds.news.plugin;

import io.meeds.news.model.News;
import io.meeds.news.service.NewsService;
import org.exoplatform.social.attachment.AttachmentService;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import static io.meeds.news.utils.NewsUtils.NewsObjectType.ARTICLE;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class ArticleAttachmentPluginTest {

@Mock
private NewsService newsService;

@Mock
private AttachmentService attachmentService;

@InjectMocks
private ArticlePageAttachmentPlugin plugin;

@Before
public void setUp() {
plugin.init();
}

@Test
public void testGetObjectType() {
Assert.assertEquals("articlePage", plugin.getObjectType());
}

@Test
public void testHasAccessPermission() throws Exception {
org.exoplatform.services.security.Identity userIdentity = mock(org.exoplatform.services.security.Identity.class);
News news = mock(News.class);

when(newsService.getNewsArticleById(anyString())).thenReturn(news);
when(newsService.canViewNews(news, userIdentity.getUserId())).thenReturn(true);

assertTrue(plugin.hasAccessPermission(userIdentity, "1"));
}

@Test
public void testHasEditPermission() throws Exception {
org.exoplatform.services.security.Identity userIdentity = mock(org.exoplatform.services.security.Identity.class);
News news = mock(News.class);

when(newsService.getNewsById("1", userIdentity, false, ARTICLE.name())).thenReturn(news);
when(news.isCanEdit()).thenReturn(true);

assertTrue(plugin.hasEditPermission(userIdentity, "1"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.util.List;
import java.util.Map;

import io.meeds.news.plugin.ArticlePageAttachmentPlugin;
import io.meeds.news.search.NewsSearchConnector;
import io.meeds.news.search.NewsESSearchResult;
import io.meeds.notes.model.NoteFeaturedImage;
Expand Down Expand Up @@ -527,6 +528,7 @@ public void testPostNews() throws Exception {
newsArticlePage.setParentPageId(rootPage.getId());
newsArticlePage.setAuthor(newsArticle.getAuthor());
newsArticlePage.setLang(null);
newsArticlePage.setAttachmentObjectType(ArticlePageAttachmentPlugin.OBJECT_TYPE);

Page createdPage = mock(Page.class);
when(createdPage.getId()).thenReturn("1");
Expand Down Expand Up @@ -609,6 +611,7 @@ public void testCreateDraftArticleForExistingPage() throws Exception {
when(draftPage.getCreatedDate()).thenReturn(new Date());
when(draftPage.getAuthor()).thenReturn("john");
when(draftPage.getId()).thenReturn("1");
when(draftPage.getContent()).thenReturn("content");
when(noteService.createDraftForExistPage(any(DraftPage.class),
any(Page.class),
nullable(String.class),
Expand Down Expand Up @@ -769,7 +772,7 @@ public void testUpdateNewsArticle() throws Exception {
// Then
verify(noteService, times(1)).updateNote(any(Page.class), any(), any());
verify(noteService, times(1)).createVersionOfNote(existingPage, identity.getUserId());
verify(noteService, times(1)).getPublishedVersionByPageIdAndLang(1L, null);
verify(noteService, times(2)).getPublishedVersionByPageIdAndLang(1L, null);
}

@Test
Expand Down Expand Up @@ -997,6 +1000,7 @@ public void testAddNewsArticleTranslation() throws Exception {
when(pageVersion.getAuthor()).thenReturn("john");
when(pageVersion.getUpdatedDate()).thenReturn(new Date());
when(pageVersion.getAuthorFullName()).thenReturn("full name");
when(pageVersion.getContent()).thenReturn("content");

News news = new News();
news.setAuthor("john");
Expand Down
Loading

0 comments on commit f517c63

Please sign in to comment.