Skip to content

Commit

Permalink
feat: Implement edit post and publishing service layer - EXO-70713 - M…
Browse files Browse the repository at this point in the history
  • Loading branch information
sofyenne committed Apr 23, 2024
1 parent 30766ba commit 82c3abd
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ public Response updateNews(@Parameter(description = "News id", required = true)
@Parameter(description = "News object type to be updated", required = false)
@QueryParam("type")
String newsObjectType,
@Parameter(description = "News update action type to be done", required = false)
@Schema(defaultValue = "content")
@QueryParam("newsUpdateType")
String newsUpdateType,
@RequestBody(description = "News object to be updated", required = true)
News updatedNews) {

Expand All @@ -263,7 +267,7 @@ public Response updateNews(@Parameter(description = "News id", required = true)
news.setTargets(updatedNews.getTargets());
news.setAudience(updatedNews.getAudience());

news = newsService.updateNews(news, currentIdentity.getUserId(), post, updatedNews.isPublished(), newsObjectType);
news = newsService.updateNews(news, currentIdentity.getUserId(), post, updatedNews.isPublished(), newsObjectType, newsUpdateType);

return Response.ok(news).build();
} catch (IllegalAccessException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public interface NewsService {
* @return updated News
* @throws Exception
*/
News updateNews(News news, String updater, Boolean post, boolean publish, String newsObjectType) throws Exception;
News updateNews(News news, String updater, Boolean post, boolean publish, String newsObjectType, String newsUpdateType) throws Exception;

/**
* Delete news
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ public enum NewsObjectType {
DRAFT, LATEST_DRAFT, ARTICLE;
}

public enum NewsUpdateType {
CONTENT, SCHEDULE, POSTING_AND_PUBLISHING
}


public static void broadcastEvent(String eventName, Object source, Object data) {
try {
ListenerService listenerService = CommonsUtils.getService(ListenerService.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package io.meeds.news.rest;

import static io.meeds.news.utils.NewsUtils.NewsObjectType.ARTICLE;
import static io.meeds.news.utils.NewsUtils.NewsUpdateType.CONTENT;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
Expand Down Expand Up @@ -365,10 +366,10 @@ public void shouldGetOKWhenUpdatingNewsAndNewsExistsAndUserIsAuthorized() throws
updatedNews.setSummary("Updated Summary");
updatedNews.setBody("Updated Body");
updatedNews.setPublicationState("published");
lenient().when(newsService.updateNews(existingNews, JOHN, false, updatedNews.isPublished(), null)).then(returnsFirstArg());
lenient().when(newsService.updateNews(existingNews, JOHN, false, updatedNews.isPublished(), null, CONTENT.name().toLowerCase())).then(returnsFirstArg());

// When
Response response = newsRestResourcesV1.updateNews("1", false, null, updatedNews);
Response response = newsRestResourcesV1.updateNews("1", false, null, CONTENT.name().toLowerCase(), updatedNews);

// Then
assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
Expand All @@ -379,10 +380,10 @@ public void shouldGetOKWhenUpdatingNewsAndNewsExistsAndUserIsAuthorized() throws
assertEquals("Updated Body", returnedNews.getBody());
assertEquals("published", returnedNews.getPublicationState());

when(newsRestResourcesV1.updateNews("1", false, null, updatedNews)).thenThrow(IllegalAccessException.class);
when(newsRestResourcesV1.updateNews("1", false, null, CONTENT.name().toLowerCase(), updatedNews)).thenThrow(IllegalAccessException.class);

// When
response = newsRestResourcesV1.updateNews("1", false, null, updatedNews);
response = newsRestResourcesV1.updateNews("1", false, null, CONTENT.name().toLowerCase(), updatedNews);

// Then
assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), response.getStatus());
Expand All @@ -396,7 +397,7 @@ public void shouldGetNotFoundWhenUpdatingNewsAndNewsNotExists() throws Exception
lenient().when(newsService.getNewsById(anyString(), any(), anyBoolean(), nullable(String.class))).thenReturn(null);

// When
Response response = newsRestResourcesV1.updateNews("1", false, null, new News());
Response response = newsRestResourcesV1.updateNews("1", false, null, CONTENT.name().toLowerCase(), new News());

// Then
assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus());
Expand Down Expand Up @@ -597,10 +598,10 @@ public void shouldGetOKWhenUpdatingAndPinNewsAndNewsExistsAndAndUserIsAuthorized
currentIdentity.setMemberships(memberships);

lenient().when(newsService.getNewsById("id123", currentIdentity, false, null)).thenReturn(existingNews);
lenient().when(newsService.updateNews(existingNews, JOHN, false, updatedNews.isPublished(), null)).then(returnsFirstArg());
lenient().when(newsService.updateNews(existingNews, JOHN, false, updatedNews.isPublished(), null, CONTENT.name().toLowerCase())).then(returnsFirstArg());

// When
Response response = newsRestResourcesV1.updateNews("id123", false, null, updatedNews);
Response response = newsRestResourcesV1.updateNews("id123", false, null, CONTENT.name().toLowerCase(), updatedNews);

// Then
assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
Expand Down Expand Up @@ -643,7 +644,7 @@ public void shouldGetOKWhenUpdatingAndUnpinNewsAndNewsExistsAndAndUserIsAuthoriz
lenient().when(newsService.getNewsById("id123", currentIdentity, false, null)).thenReturn(oldnews);

// When
Response response = newsRestResourcesV1.updateNews("id123", false, null, updatedNews);
Response response = newsRestResourcesV1.updateNews("id123", false, null, CONTENT.name().toLowerCase(), updatedNews);

// Then
assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
Expand Down Expand Up @@ -758,7 +759,7 @@ public void shouldGetBadRequestWhenUpdatingNewsAndUpdatedNewsIsNull() throws Exc
ConversationState.setCurrent(new ConversationState(currentIdentity));

// When
Response response = newsRestResourcesV1.updateNews("1", false, null, null);
Response response = newsRestResourcesV1.updateNews("1", false, null, CONTENT.name().toLowerCase(), null);

// Then
assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static io.meeds.news.service.impl.NewsServiceImpl.NEWS_ACTIVITIES;
import static io.meeds.news.service.impl.NewsServiceImpl.NEWS_ARTICLES_ROOT_NOTE_PAGE_NAME;
import static io.meeds.news.service.impl.NewsServiceImpl.NEWS_SUMMARY;
import static io.meeds.news.utils.NewsUtils.NewsUpdateType.CONTENT;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
Expand Down Expand Up @@ -320,7 +321,7 @@ public void testUpdateDraftArticle() throws Exception {
expecteddraftPage.setWikiOwner("/space/groupId");

// When, Then
assertThrows(IllegalAccessException.class, () -> newsService.updateNews(news, "john", false, false, "draft"));
assertThrows(IllegalAccessException.class, () -> newsService.updateNews(news, "john", false, false, "draft", CONTENT.name().toLowerCase()));

// Given
when(spaceService.canRedactOnSpace(space, identity)).thenReturn(true);
Expand All @@ -331,7 +332,7 @@ public void testUpdateDraftArticle() throws Exception {
when(noteService.updateDraftForNewPage(any(DraftPage.class), anyLong())).thenReturn(expecteddraftPage);

// When
newsService.updateNews(news, "john", false, false, "draft");
newsService.updateNews(news, "john", false, false, "draft", CONTENT.name().toLowerCase());

// Then
verify(noteService, times(1)).updateDraftForNewPage(eq(expecteddraftPage), anyLong());
Expand Down Expand Up @@ -567,7 +568,7 @@ public void testCreateDraftArticleForExistingPage() throws Exception {
news.setOriginalBody("body");

// When, Then
assertThrows(IllegalAccessException.class, () -> newsService.updateNews(news, "john", false, false, "draft"));
assertThrows(IllegalAccessException.class, () -> newsService.updateNews(news, "john", false, false, "draft", CONTENT.name().toLowerCase()));

// Given
when(spaceService.canRedactOnSpace(space, identity)).thenReturn(true);
Expand All @@ -589,7 +590,7 @@ public void testCreateDraftArticleForExistingPage() throws Exception {
anyString())).thenReturn(draftPage);

// When
newsService.updateNews(news, "john", false, false, "latest_draft");
newsService.updateNews(news, "john", false, false, "latest_draft", CONTENT.name().toLowerCase());

// Then
verify(noteService, times(1)).createDraftForExistPage(any(DraftPage.class),
Expand Down Expand Up @@ -659,7 +660,7 @@ public void testUpdateDraftArticleForExistingPage() throws Exception {
news.setOriginalBody("body");

// When, Then
assertThrows(IllegalAccessException.class, () -> newsService.updateNews(news, "john", false, false, "draft"));
assertThrows(IllegalAccessException.class, () -> newsService.updateNews(news, "john", false, false, "draft", CONTENT.name().toLowerCase()));

// Given
when(spaceService.canRedactOnSpace(space, identity)).thenReturn(true);
Expand All @@ -676,7 +677,7 @@ public void testUpdateDraftArticleForExistingPage() throws Exception {
anyString())).thenReturn(draftPage);

// When
newsService.updateNews(news, "john", false, false, "latest_draft");
newsService.updateNews(news, "john", false, false, "latest_draft", CONTENT.name().toLowerCase());

// Then
verify(noteService, times(1)).updateDraftForExistPage(any(DraftPage.class),
Expand Down Expand Up @@ -746,7 +747,7 @@ public void testUpdateNewsArticle() throws Exception {
news.setOriginalBody("body");

// When, Then
assertThrows(IllegalAccessException.class, () -> newsService.updateNews(news, "john", false, false, "draft"));
assertThrows(IllegalAccessException.class, () -> newsService.updateNews(news, "john", false, false, "draft", CONTENT.name().toLowerCase()));

// Given
when(spaceService.canRedactOnSpace(space, identity)).thenReturn(true);
Expand All @@ -759,7 +760,7 @@ public void testUpdateNewsArticle() throws Exception {
when(noteService.updateNote(any(Page.class))).thenReturn(existingPage);

// When
newsService.updateNews(news, "john", false, false, "article");
newsService.updateNews(news, "john", false, false, "article", CONTENT.name().toLowerCase());

// Then
verify(noteService, times(1)).updateNote(any(Page.class));
Expand Down
14 changes: 13 additions & 1 deletion content-webapp/src/main/webapp/js/newsUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,16 @@ export function convertDate(date) {

export function pad(n) {
return n < 10 && `0${n}` || n;
}
}

export const NewsUpdateType = {
CONTENT: 'content',
POSTING_AND_PUBLISHING: 'postingAndPublishing',
SCHEDULE: 'schedule'
};

export const NewsObjectType = {
DRAFT: 'draft',
ARTICLE: 'article',
LATEST_DRAFT: 'latest_draft'
};
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
</template>
<script>
import {NewsObjectType} from '../../js/newsUtils';
const USER_TIMEZONE_ID = new window.Intl.DateTimeFormat().resolvedOptions().timeZone;
export default {
props: {
Expand Down Expand Up @@ -168,7 +170,7 @@ export default {
});
},
editLink() {
const newsType = this.activityId && this.activityId !== '' ? 'latest_draft' : this.newsType;
const newsType = this.activityId && this.activityId !== '' ? NewsObjectType.LATEST_DRAFT : this.newsType;
const editUrl = `${eXo.env.portal.context}/${eXo.env.portal.metaPortalName}/news/editor?spaceId=${this.spaceId}&newsId=${this.newsId}&activityId=${this.activityId}&type=${newsType}`;
window.open(editUrl, '_target');
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@
</template>

<script>
import {NewsObjectType, NewsUpdateType} from '../../js/newsUtils';
export default {
props: {
news: {
Expand Down Expand Up @@ -162,7 +164,7 @@ export default {
return this.news && this.news.published;
},
isHiddenActivity() {
return this.news && this.news.activityPosted;
return this.news && !this.news.activityPosted;
},
disableTargetOption() {
return this.selectedTargets && this.selectedTargets.length === 0;
Expand Down Expand Up @@ -202,7 +204,7 @@ export default {
openDrawer() {
if (this.news) {
this.publish = this.news.published;
this.isActivityPosted = !this.news.activityPosted;
this.isActivityPosted = this.news.activityPosted;
}
if (this.$refs.postNewsDrawer) {
this.disabled = true;
Expand All @@ -219,15 +221,15 @@ export default {
updateNews() {
this.editingNews = true;
this.news.published = this.publish;
this.news.activityPosted = !this.isActivityPosted;
this.news.activityPosted = this.isActivityPosted;
this.news.targets = this.selectedTargets;
if (this.publish) {
this.news.audience = this.selectedAudience === this.$t('news.composer.stepper.audienceSection.allUsers') ? 'all' : 'space';
}
else {
this.news.audience = null;
}
return this.$newsServices.updateNews(this.news, false).then(() => {
return this.$newsServices.updateNews(this.news, false, NewsObjectType.ARTICLE, NewsUpdateType.POSTING_AND_PUBLISHING).then(() => {
this.editingNews = false;
this.$root.$emit('alert-message', this.$t('news.composer.alert.success.UpdateTargets'), 'success');
this.$emit('refresh-news', this.news.newsId);
Expand Down
5 changes: 3 additions & 2 deletions content-webapp/src/main/webapp/services/newsServices.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/

import {newsConstants} from '../js/newsConstants.js';
import {NewsUpdateType} from '../js/newsUtils';

export function getNewsById(id, editMode, type) {
return fetch(`${newsConstants.NEWS_API}/${id}?editMode=${editMode || ''}&type=${type || ''}`, {
Expand Down Expand Up @@ -142,8 +143,8 @@ export function importFileFromUrl(url) {
});
}

export function updateNews(news, post, type) {
return fetch(`${newsConstants.NEWS_API}/${news.id}?post=${post}&type=${type || ''}`, {
export function updateNews(news, post, type, newsUpdateType) {
return fetch(`${newsConstants.NEWS_API}/${news.id}?post=${post}&type=${type || ''}&newsUpdateType=${newsUpdateType || NewsUpdateType.CONTENT}`, {
headers: {
'Content-Type': 'application/json'
},
Expand Down

0 comments on commit 82c3abd

Please sign in to comment.