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 authored Nov 26, 2024
1 parent 8b21fe1 commit 86dfb69
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,15 @@
import static io.meeds.news.utils.NewsUtils.NewsObjectType.ARTICLE;

@Component
public class ArticlePageVersionAttachmentPlugin extends AttachmentPlugin {
public class ArticlePageAttachmentPlugin extends AttachmentPlugin {

@Autowired
private AttachmentService attachmentService;

@Autowired
private NewsService newsService;

@Autowired
private NoteService noteService;

public static final String OBJECT_TYPE = "articlePageVersion";
public static final String OBJECT_TYPE = "articlePage";

@PostConstruct
public void init() {
Expand All @@ -57,18 +54,16 @@ public String getObjectType() {
}

@Override
public boolean hasAccessPermission(Identity identity, String articleVersionId) throws ObjectNotFoundException {
PageVersion pageVersion = noteService.getPageVersionById(Long.parseLong(articleVersionId));
News news = newsService.getNewsArticleById(pageVersion.getParent().getId());
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 articleVersionId) throws ObjectNotFoundException {
public boolean hasEditPermission(Identity identity, String articleId) throws ObjectNotFoundException {
News news = null;
try {
PageVersion pageVersion = noteService.getPageVersionById(Long.parseLong(articleVersionId));
news = newsService.getNewsById(pageVersion.getParent().getId(), identity, false, ARTICLE.name());
news = newsService.getNewsById(articleId, identity, false, ARTICLE.name());
} catch (IllegalAccessException e) {
return false;
}
Expand All @@ -81,9 +76,8 @@ public long getAudienceId(String s) throws ObjectNotFoundException {
}

@Override
public long getSpaceId(String articleVersionId) throws ObjectNotFoundException {
PageVersion pageVersion = noteService.getPageVersionById(Long.parseLong(articleVersionId));
News news = newsService.getNewsArticleById(pageVersion.getParent().getId());
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 @@ -39,10 +39,11 @@
import java.util.regex.Matcher;
import java.util.stream.Stream;

import io.meeds.news.plugin.ArticlePageVersionAttachmentPlugin;
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 @@ -945,6 +946,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 Down Expand Up @@ -1025,6 +1027,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 @@ -1036,7 +1039,6 @@ public News createNewsArticlePage(News newsArticle, String newsArticleCreator) t
newsArticlePage.setProperties(new NotePageProperties(Long.parseLong(draftNewsId), null, null, false, false, true));
}
newsArticlePage = noteService.createNote(wiki, newsArticlesRootNotePage.getName(), newsArticlePage, poster);
noteService.createVersionOfNote(newsArticlePage, poster.getUserId(), ArticlePageVersionAttachmentPlugin.OBJECT_TYPE, draftNewsId);
if (newsArticlePage != null) {
PageVersion pageVersion = noteService.getPublishedVersionByPageIdAndLang(Long.parseLong(newsArticlePage.getId()), null);
// set properties
Expand Down Expand Up @@ -1067,6 +1069,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 Down Expand Up @@ -1240,6 +1243,7 @@ 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,
Expand Down Expand Up @@ -1851,6 +1855,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 @@ -1894,7 +1899,7 @@ 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(), ArticlePageVersionAttachmentPlugin.OBJECT_TYPE, null);
noteService.createVersionOfNote(existingPage, updater.getUserId());
PageVersion pageVersion = noteService.getPublishedVersionByPageIdAndLang(Long.valueOf(news.getId()), news.getLang());
news.setLatestVersionId(pageVersion.getId());
news.setBody(pageVersion.getContent());
Expand Down Expand Up @@ -2014,6 +2019,7 @@ 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.setId(draftPage.getId());
Expand Down Expand Up @@ -2157,7 +2163,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 @@ -2180,12 +2186,13 @@ 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(), ArticlePageVersionAttachmentPlugin.OBJECT_TYPE, null);
noteService.createVersionOfNote(existingPage, versionCreator.getUserId());
PageVersion pageVersion = noteService.getPublishedVersionByPageIdAndLang(Long.valueOf(newsId), news.getLang());
news.setLatestVersionId(pageVersion.getId());
news.setBody(pageVersion.getContent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,14 @@
@RunWith(MockitoJUnitRunner.class)
public class ArticleAttachmentPluginTest {

@Mock
private NoteService noteService;

@Mock
private NewsService newsService;

@Mock
private AttachmentService attachmentService;

@InjectMocks
private ArticlePageVersionAttachmentPlugin plugin;
private ArticlePageAttachmentPlugin plugin;

@Before
public void setUp() {
Expand All @@ -61,20 +58,14 @@ public void setUp() {

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

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

when(userIdentity.getUserId()).thenReturn("user123");
when(noteService.getPageVersionById(anyLong())).thenReturn(pageVersion);
when(pageVersion.getParent()).thenReturn(page);
when(page.getId()).thenReturn("1");
when(newsService.getNewsArticleById(anyString())).thenReturn(news);
when(newsService.canViewNews(news, userIdentity.getUserId())).thenReturn(true);

Expand All @@ -84,13 +75,8 @@ public void testHasAccessPermission() throws Exception {
@Test
public void testHasEditPermission() throws Exception {
org.exoplatform.services.security.Identity userIdentity = mock(org.exoplatform.services.security.Identity.class);
PageVersion pageVersion = mock(PageVersion.class);
Page page = mock(Page.class);
News news = mock(News.class);

when(noteService.getPageVersionById(anyLong())).thenReturn(pageVersion);
when(pageVersion.getParent()).thenReturn(page);
when(page.getId()).thenReturn("1");
when(newsService.getNewsById("1", userIdentity, false, ARTICLE.name())).thenReturn(news);
when(news.isCanEdit()).thenReturn(true);

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

import io.meeds.news.plugin.ArticlePageVersionAttachmentPlugin;
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 @@ -529,6 +529,7 @@ public void testPostNews() throws Exception {
newsArticlePage.setAuthor(newsArticle.getAuthor());
newsArticlePage.setProperties(new NotePageProperties(Long.parseLong(newsArticle.getId()), null, null, false, false, true));
newsArticlePage.setLang(null);
newsArticlePage.setAttachmentObjectType(ArticlePageAttachmentPlugin.OBJECT_TYPE);

Page createdPage = mock(Page.class);
when(createdPage.getId()).thenReturn("1");
Expand Down Expand Up @@ -791,7 +792,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(), ArticlePageVersionAttachmentPlugin.OBJECT_TYPE, null);
verify(noteService, times(1)).createVersionOfNote(existingPage, identity.getUserId());
verify(noteService, times(2)).getPublishedVersionByPageIdAndLang(1L, null);
}

Expand Down Expand Up @@ -1063,7 +1064,7 @@ public void testAddNewsArticleTranslation() throws Exception {

// Then
verify(noteService, times(1)).updateNote(any(Page.class), any(), any());
verify(noteService, times(1)).createVersionOfNote(existingPage, identity.getUserId(), ArticlePageVersionAttachmentPlugin.OBJECT_TYPE, null);
verify(noteService, times(1)).createVersionOfNote(existingPage, identity.getUserId());
verify(noteService, times(2)).getPublishedVersionByPageIdAndLang(1L, null);
NEWS_UTILS.verify(() -> NewsUtils.broadcastEvent(eq(NewsUtils.ADD_ARTICLE_TRANSLATION), anyObject(), anyObject()), times(1));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,6 @@ export default {
if (updatedArticle?.properties) {
updatedArticle.properties.draft = true;
}
updatedArticle.body = this.$noteUtils.sanitizeSrcImageTags(updatedArticle.body);
const setEditorDataMutely = this.$noteUtils.isHasImagesToBeProcessed(updatedArticle.body, this.draftObjectType);
updatedArticle.publicationState = 'draft';
return this.$newsServices.updateNews(updatedArticle, false, this.articleType).then((createdArticle) => {
this.spaceUrl = createdArticle.spaceUrl;
Expand All @@ -350,9 +348,9 @@ export default {
if (this.article.body !== createdArticle.body) {
this.imagesURLs = this.extractImagesURLsDiffs(this.article.body, createdArticle.body);
}
if (setEditorDataMutely) {
this.$refs?.editor?.setEditorDataMutely?.(createdArticle.body);
}
document.dispatchEvent(new CustomEvent('update-processed-image-url', {detail: {
content: createdArticle.body
}}));
}).then(() => this.$emit('draftUpdated'))
.then(() => this.draftSavingStatus = this.$t('news.composer.draft.savedDraftStatus'))
.finally(() => {
Expand Down Expand Up @@ -381,7 +379,9 @@ export default {
this.imagesURLs = this.extractImagesURLsDiffs(this.article.body, createdArticle.body);
}
this.fillArticle(createdArticle.id, false, createdArticle.lang);
this.$refs?.editor?.setEditorDataMutely?.(createdArticle.body);
document.dispatchEvent(new CustomEvent('update-processed-image-url', {detail: {
content: updatedArticle.body
}}));
this.displayAlert({
message: this.$t('news.save.success.message'),
type: 'success',
Expand Down Expand Up @@ -428,8 +428,6 @@ export default {
properties: properties,
draftPage: true
};
article.body = this.$noteUtils.sanitizeSrcImageTags(article.body);
const setEditorDataMutely = this.$noteUtils.isHasImagesToBeProcessed(article.body, this.draftObjectType);
if (this.article.id) {
if (this.article.title || this.article.body) {
article.id = this.article.id;
Expand All @@ -441,9 +439,9 @@ export default {
this.article.draftPage = true;
this.article.id = updatedArticle.id;
this.article.properties = updatedArticle?.properties;
if (setEditorDataMutely) {
this.$refs?.editor?.setEditorDataMutely?.(updatedArticle.body);
}
document.dispatchEvent(new CustomEvent('update-processed-image-url', {detail: {
content: updatedArticle.body
}}));
})
.then(() => this.$emit('draftUpdated'))
.then(() => {
Expand All @@ -470,9 +468,9 @@ export default {
if (!this.articleId) {
this.articleId = createdArticle.id;
}
if (setEditorDataMutely) {
this.$refs?.editor?.setEditorDataMutely?.(createdArticle.body);
}
document.dispatchEvent(new CustomEvent('update-processed-image-url', {detail: {
content: createdArticle.body
}}));
this.$emit('draftCreated');
this.savingDraft = false;
if (this.autosaveProcessedFromEditorExtension) {
Expand Down Expand Up @@ -539,7 +537,9 @@ export default {
if (article.publicationState ==='staged') {
this.$newsServices.scheduleNews(article, this.articleType).then((scheduleArticle) => {
this.articleType = 'latest_draft';
this.$refs?.editor?.setEditorDataMutely?.(scheduleArticle.body);
document.dispatchEvent(new CustomEvent('update-processed-image-url', {detail: {
content: scheduleArticle.body
}}));
this.fillArticle(scheduleArticle.id, false, null).then(() => {
this.updateUrl();
this.initDataPropertiesFromUrl();
Expand All @@ -555,7 +555,9 @@ export default {
} else {
this.$newsServices.saveNews(article).then((createdArticle) => {
this.articleType = 'latest_draft';
this.$refs?.editor?.setEditorDataMutely?.(createdArticle.body);
document.dispatchEvent(new CustomEvent('update-processed-image-url', {detail: {
content: createdArticle.body
}}));
this.fillArticle(createdArticle.id, false, createdArticle.lang || this.selectedLanguage).then(() => {
this.updateUrl();
this.initDataPropertiesFromUrl();
Expand Down

0 comments on commit 86dfb69

Please sign in to comment.