Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement Notes/Content insert image option - EXO-74754 - Meeds-io/MIPs#145 #1211

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1591,6 +1591,22 @@ public PageVersion getPageVersionById(long versionId) {
return EntityConverter.convertPageVersionEntityToPageVersion(pageVersionDAO.find(versionId));
}

@Override
public PageVersion updatePageVersionContent(long versionId, String content) {
PageVersionEntity pageVersionEntity = pageVersionDAO.find(versionId);
pageVersionEntity.setContent(content);
pageVersionEntity.setUpdatedDate(new Date(System.currentTimeMillis()));
return EntityConverter.convertPageVersionEntityToPageVersion(pageVersionDAO.update(pageVersionEntity));
}

@Override
public DraftPage updateDraftContent(long draftId, String content) {
DraftPageEntity draftPageEntity = draftPageDAO.find(draftId);
draftPageEntity.setContent(content);
draftPageEntity.setUpdatedDate(new Date(System.currentTimeMillis()));
return EntityConverter.convertDraftPageEntityToDraftPage(draftPageDAO.update(draftPageEntity));
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,23 @@ public default List<Attachment> getAttachmentsOfPage(Page page, boolean loadCont
*/
PageVersion getPageVersionById(long versionId);

/**
* Updates page version content by its given id
*
* @param versionId page version id
* @param content page version content
* @return {@link PageVersion}
*/
PageVersion updatePageVersionContent(long versionId, String content) throws WikiException;

/**
* Updates draft page content by its given id
*
* @param draftId draft page id
* @param content draft page content
* @return {@link PageVersion}
*/
DraftPage updateDraftContent(long draftId, String content) throws WikiException;

/**
* Gets draft pages of a given wiki
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,18 @@ List<BreadcrumbData> getBreadCrumb(String noteType,
*/
void createVersionOfNote(Page note, String userName) throws WikiException;

/**
* Creates a version of a note. This method only tag the current note data as
* a new version, it does not update the note data
*
* @param note The note
* @param objectType The object type to link to the page version's content images
* @param draftId The ID of the latest draft, used as the source ID to move content images to the new version
* @param userName The author name
* @throws WikiException if an error occured
*/
void createVersionOfNote(Page note, String userName, String objectType, String draftId) throws WikiException;

/**
* Restores a version of a note
*
Expand Down Expand Up @@ -591,7 +603,6 @@ DraftPage createDraftForExistPage(DraftPage draftNoteToSave,
String revision,
long currentTimeMillis,
String username) throws WikiException;

/**
* Creates a draft for a new page
*
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* 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 org.exoplatform.wiki.service.plugin;

import org.exoplatform.commons.exception.ObjectNotFoundException;
import org.exoplatform.services.security.Identity;
import org.exoplatform.social.attachment.AttachmentPlugin;
import org.exoplatform.social.core.manager.IdentityManager;
import org.exoplatform.social.core.space.spi.SpaceService;
import org.exoplatform.wiki.model.DraftPage;
import org.exoplatform.wiki.service.NoteService;
import org.exoplatform.wiki.utils.Utils;

public class WikiDraftPageAttachmentPlugin extends AttachmentPlugin {

private final NoteService noteService;

private final SpaceService spaceService;

public static final String OBJECT_TYPE = "wikiDraft";

public WikiDraftPageAttachmentPlugin(NoteService noteService, SpaceService spaceService, IdentityManager identityManager) {
this.noteService = noteService;
this.spaceService = spaceService;
}

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

@Override
public boolean hasAccessPermission(Identity identity, String draftId) {
try {
DraftPage draftPage = noteService.getDraftNoteById(draftId, identity.getUserId());
return draftPage != null && draftPage.isCanView();
} catch (Exception e) {
return false;
}
}

@Override
public boolean hasEditPermission(Identity identity, String draftId) throws ObjectNotFoundException {
try {
DraftPage draftPage = noteService.getDraftNoteById(draftId, identity.getUserId());
return draftPage != null && draftPage.isCanManage();
} catch (Exception e) {
return false;
}
}

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

@Override
public long getSpaceId(String draftId) throws ObjectNotFoundException {
try {
String username = Utils.getCurrentUser();
DraftPage draftPage = noteService.getDraftNoteById(draftId, username);
return Long.parseLong(spaceService.getSpaceByGroupId(draftPage.getWikiOwner()).getId());
} catch (Exception exception) {
return 0;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* 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 org.exoplatform.wiki.service.plugin;

import org.exoplatform.commons.exception.ObjectNotFoundException;
import org.exoplatform.services.security.Identity;
import org.exoplatform.social.attachment.AttachmentPlugin;
import org.exoplatform.social.core.space.spi.SpaceService;
import org.exoplatform.wiki.model.Page;
import org.exoplatform.wiki.model.PageVersion;
import org.exoplatform.wiki.service.NoteService;

public class WikiPageVersionAttachmentPlugin extends AttachmentPlugin {

private final NoteService noteService;

private final SpaceService spaceService;

public static final String OBJECT_TYPE = "wikiPageVersion";

public WikiPageVersionAttachmentPlugin(NoteService noteService, SpaceService spaceService) {
this.noteService = noteService;
this.spaceService = spaceService;
}

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

@Override
public boolean hasAccessPermission(Identity identity, String versionId) {
try {
PageVersion pageVersion = noteService.getPageVersionById(Long.parseLong(versionId));
Page note = noteService.getNoteById(pageVersion.getParent().getId(),identity);
return pageVersion != null && note != null && note.isCanView();
} catch (Exception e) {
return false;
}
}

@Override
public boolean hasEditPermission(Identity identity, String versionId) throws ObjectNotFoundException {
try {
PageVersion pageVersion = noteService.getPageVersionById(Long.parseLong(versionId));
Page note = noteService.getNoteById(pageVersion.getParent().getId(),identity);
return pageVersion != null && note != null && note.isCanManage();
} catch (Exception e) {
return false;
}
}

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

@Override
public long getSpaceId(String versionId) throws ObjectNotFoundException {
try {
PageVersion pageVersion = noteService.getPageVersionById(Long.parseLong(versionId));
return Long.parseLong(spaceService.getSpaceByGroupId(pageVersion.getWikiOwner()).getId());
} catch (Exception exception) {
return 0;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.exoplatform.wiki.service.plugin.WikiPageVersionAttachmentPlugin;
import org.gatein.api.EntityNotFoundException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
Expand Down Expand Up @@ -508,6 +509,7 @@ public Response createNote(@Parameter(description = "note object to be created",
}
String noteBookType = note.getWikiType();
String noteBookOwner = note.getWikiOwner();
String draftId = note.getProperties().isDraft() ? String.valueOf(note.getProperties().getNoteId()) : null;
try {
Identity identity = ConversationState.getCurrent().getIdentity();
if (StringUtils.isNotEmpty(note.getParentPageId())) {
Expand Down Expand Up @@ -542,6 +544,7 @@ public Response createNote(@Parameter(description = "note object to be created",
note.getParentPageName(),
io.meeds.notes.rest.utils.EntityBuilder.toPage(note),
identity);
noteService.createVersionOfNote(createdNote, currentUser, WikiPageVersionAttachmentPlugin.OBJECT_TYPE, draftId);
return Response.ok(createdNote, MediaType.APPLICATION_JSON).cacheControl(cc).build();
} catch (IllegalAccessException e) {
log.error("User does not have view permissions on the note {}", note.getName(), e);
Expand All @@ -568,7 +571,7 @@ public Response saveDraft(@RequestBody(description = "Note draft page object to
log.warn("Draft Note's title should not be number");
return Response.status(Response.Status.BAD_REQUEST).entity("{ message: Draft Note's title should not be number}").build();
}

draftNoteToSave.setContent(Utils.sanitizeSrcImageTags(draftNoteToSave.getContent()));
String noteBookType = draftNoteToSave.getWikiType();
String noteBookOwner = draftNoteToSave.getWikiOwner();
Page parentNote = null;
Expand Down Expand Up @@ -662,6 +665,7 @@ public Response updateNote(@Parameter(description = "NoteBook Type", required =
log.warn("Note's title should not be number");
return Response.status(Response.Status.BAD_REQUEST).entity("{ message: Note's title should not be number}").build();
}
note.setContent(Utils.sanitizeSrcImageTags(note.getContent()));
try {
if (noteBookType.toUpperCase().equals(WikiType.GROUP.name())) {
noteBookOwner = formatWikiOwnerToGroupId(noteBookOwner);
Expand Down Expand Up @@ -690,7 +694,7 @@ public Response updateNote(@Parameter(description = "NoteBook Type", required =
note_.setName(newNoteName);
}
note_ = noteService.updateNote(note_, PageUpdateType.EDIT_PAGE_CONTENT_AND_TITLE, identity);
noteService.createVersionOfNote(note_, identity.getUserId());
noteService.createVersionOfNote(note_, identity.getUserId(), WikiPageVersionAttachmentPlugin.OBJECT_TYPE, null);
} else if (!note_.getTitle().equals(note.getTitle())) {
String newNoteName = TitleResolver.getId(note.getTitle(), false);
if (!NoteConstants.NOTE_HOME_NAME.equals(note.getName()) && !note.getName().equals(newNoteName)) {
Expand All @@ -703,7 +707,7 @@ public Response updateNote(@Parameter(description = "NoteBook Type", required =
} else if (!note_.getContent().equals(note.getContent())) {
note_.setContent(note.getContent());
note_ = noteService.updateNote(note_, PageUpdateType.EDIT_PAGE_CONTENT, identity);
noteService.createVersionOfNote(note_, identity.getUserId());
noteService.createVersionOfNote(note_, identity.getUserId(), WikiPageVersionAttachmentPlugin.OBJECT_TYPE, null);
}
return Response.ok(note_, MediaType.APPLICATION_JSON).cacheControl(cc).build();
} catch (IllegalAccessException e) {
Expand Down Expand Up @@ -732,6 +736,7 @@ public Response updateNoteById(@Parameter(description = "Note id", required = tr
log.warn("Note's title should not be number");
return Response.status(Response.Status.BAD_REQUEST).entity("{ message: Note's title should not be number}").build();
}
note.setContent(Utils.sanitizeSrcImageTags(note.getContent()));
try {
Identity identity = ConversationState.getCurrent().getIdentity();
Page note_ = noteService.getNoteById(noteId, identity);
Expand Down Expand Up @@ -770,7 +775,7 @@ public Response updateNoteById(@Parameter(description = "Note id", required = tr
note_.setContent(note.getContent());
note_.setProperties(notePageProperties);
}
noteService.createVersionOfNote(note_, identity.getUserId());
noteService.createVersionOfNote(note_, identity.getUserId(), WikiPageVersionAttachmentPlugin.OBJECT_TYPE, null);
if (!Utils.ANONYM_IDENTITY.equals(identity.getUserId())) {
WikiPageParams noteParams = new WikiPageParams(note_.getWikiType(), note_.getWikiOwner(), newNoteName);
noteService.removeDraftOfNote(noteParams, note.getLang());
Expand All @@ -791,7 +796,7 @@ public Response updateNoteById(@Parameter(description = "Note id", required = tr
note_.setTitle(note.getTitle());
note_.setProperties(notePageProperties);
}
noteService.createVersionOfNote(note_, identity.getUserId());
noteService.createVersionOfNote(note_, identity.getUserId(), WikiPageVersionAttachmentPlugin.OBJECT_TYPE, null);
if (!Utils.ANONYM_IDENTITY.equals(identity.getUserId())) {
WikiPageParams noteParams = new WikiPageParams(note_.getWikiType(), note_.getWikiOwner(), newNoteName);
noteService.removeDraftOfNote(noteParams, note.getLang());
Expand All @@ -807,7 +812,7 @@ public Response updateNoteById(@Parameter(description = "Note id", required = tr
note_.setContent(note.getContent());
note_.setProperties(notePageProperties);
}
noteService.createVersionOfNote(note_, identity.getUserId());
noteService.createVersionOfNote(note_, identity.getUserId(), WikiPageVersionAttachmentPlugin.OBJECT_TYPE, null);
if (!Utils.ANONYM_IDENTITY.equals(identity.getUserId())) {
WikiPageParams noteParams = new WikiPageParams(note_.getWikiType(), note_.getWikiOwner(), newNoteName);
noteService.removeDraftOfNote(noteParams, note.getLang());
Expand All @@ -831,7 +836,7 @@ public Response updateNoteById(@Parameter(description = "Note id", required = tr
note_ = noteService.updateNote(note_, PageUpdateType.PUBLISH, identity);
} else if (note.isExtensionDataUpdated()) {
note_ = noteService.updateNote(note_, PageUpdateType.EDIT_PAGE_CONTENT_AND_TITLE, identity);
noteService.createVersionOfNote(note_, identity.getUserId());
noteService.createVersionOfNote(note_, identity.getUserId(), WikiPageVersionAttachmentPlugin.OBJECT_TYPE, null);
if (!Utils.ANONYM_IDENTITY.equals(identity.getUserId())) {
WikiPageParams noteParams = new WikiPageParams(note_.getWikiType(), note_.getWikiOwner(), newNoteName);
noteService.removeDraftOfNote(noteParams, note.getLang());
Expand Down
Loading
Loading