Skip to content

Commit

Permalink
fix:[news] '#' glitches the filter - EXO-65188
Browse files Browse the repository at this point in the history
Prior to this change, when create a tag in article and go to news app make a search for that tag(# followed by tag name), there is no filter applied, the display remain the same. After this change, return results with the searched tag.
  • Loading branch information
akhanfir committed Aug 8, 2023
1 parent f2bcb17 commit d453ebd
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,17 @@ public StringBuilder buildQuery(NewsFilter filter) throws Exception {
String escapedQuoteFuzzyText = fuzzyText.replace("'", "''").replace("\"", "\"\"");
String escapedQuoteSearchText = filter.getSearchText().replace("'", "''").replace("\"", "\"\"");
sqlQuery.append("(CONTAINS(.,'").append(escapedQuoteFuzzyText).append("') OR (exo:body LIKE '%").append(escapedQuoteSearchText).append("%'))");
if (filter.getTagNames() != null && !filter.getTagNames().isEmpty()){
sqlQuery.append(" OR (");
sqlQuery.append("AND ");
} else {
if (filter.getTagNames() != null && !filter.getTagNames().isEmpty()) {
for (String tagName : filter.getTagNames()) {
sqlQuery.append(" exo:body LIKE '%#").append(tagName).append("%'");
if (filter.getTagNames().indexOf(tagName) != filter.getTagNames().size() -1) {
if (filter.getTagNames().indexOf(tagName) != filter.getTagNames().size() - 1) {
sqlQuery.append(" OR");
}
}
sqlQuery.append(" ) AND ");
} else sqlQuery.append("AND ");
sqlQuery.append(" AND ");
}
}
if (filter.isPublishedNews()) {
sqlQuery.append("exo:pinned = 'true' AND ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,12 @@ public Response getNews(@Context HttpServletRequest request,
String lang = request.getLocale().getLanguage();
TagService tagService = CommonsUtils.getService(TagService.class);
long userIdentityId = RestUtils.getCurrentUserIdentityId();
List<TagName> tagNames = tagService.findTags(new TagFilter(text, 0), userIdentityId);
if (tagNames != null && !tagNames.isEmpty()) newsFilter.setTagNames(tagNames.stream().map(e -> e.getName()).toList());
if (text.indexOf("#") == 0) {
String tagName = text.replace("#","");
List<TagName> tagNames = tagService.findTags(new TagFilter(tagName, 0), userIdentityId);
if (tagNames != null && !tagNames.isEmpty()) newsFilter.setTagNames(tagNames.stream().map(e -> e.getName()).toList());
}

news = newsService.searchNews(newsFilter, lang);
} else {
org.exoplatform.services.security.Identity currentIdentity = ConversationState.getCurrent().getIdentity();
Expand Down Expand Up @@ -1008,7 +1012,7 @@ private NewsFilter buildFilter(List<String> spaces, String filter, String text,
}
}
// Set text to search news with
if (StringUtils.isNotEmpty(text)) {
if (StringUtils.isNotEmpty(text) && text.indexOf("#") != 0) {
newsFilter.setSearchText(text);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void shouldCreateQueryWithPinnedStateAndSearchTextAndAuthorAndOneSpaceAnd

// then
assertNotNull(query);
assertEquals("SELECT * FROM exo:news WHERE ( exo:archived IS NULL OR exo:archived = 'false' OR ( exo:archived = 'true' AND exo:author = 'john')) AND (CONTAINS(.,'text~0.6') OR (exo:body LIKE '%text%')) OR ( exo:body LIKE '%#text%' OR exo:body LIKE '%#tex%' ) AND exo:pinned = 'true' AND ( exo:spaceId = '1') AND exo:author = 'john' AND (publication:currentState = 'published' OR (publication:currentState = 'draft' AND exo:activities <> '' )) AND jcr:path LIKE '/Groups/spaces/%' ORDER BY jcr:score DESC",
assertEquals("SELECT * FROM exo:news WHERE ( exo:archived IS NULL OR exo:archived = 'false' OR ( exo:archived = 'true' AND exo:author = 'john')) AND (CONTAINS(.,'text~0.6') OR (exo:body LIKE '%text%'))AND exo:pinned = 'true' AND ( exo:spaceId = '1') AND exo:author = 'john' AND (publication:currentState = 'published' OR (publication:currentState = 'draft' AND exo:activities <> '' )) AND jcr:path LIKE '/Groups/spaces/%' ORDER BY jcr:score DESC",
query.toString());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1576,7 +1576,6 @@ public void shouldGetAllNewsWhenSearchingWithTextInTheGivenSpaces() throws Excep
allNews.add(news3);
COMMONS_UTILS.when(()-> CommonsUtils.getService(TagService.class)).thenReturn(tagService);
REST_UTILS.when(()-> RestUtils.getCurrentUserIdentityId()).thenReturn(1L);
when(tagService.findTags(new TagFilter(text, 0), 1L)).thenReturn(new ArrayList<>());
lenient().when(newsService.searchNews(any(), any())).thenReturn(allNews);
lenient().when(spaceService.isMember(any(Space.class), any())).thenReturn(true);
lenient().when(spaceService.getSpaceById(anyString())).thenReturn(new Space());
Expand Down Expand Up @@ -1632,7 +1631,6 @@ public void shouldGetAllNewsWhenSearchingWithTextInTheGivenSpace() throws Except
allNews.add(news3);
COMMONS_UTILS.when(()-> CommonsUtils.getService(TagService.class)).thenReturn(tagService);
REST_UTILS.when(()-> RestUtils.getCurrentUserIdentityId()).thenReturn(1L);
when(tagService.findTags(new TagFilter(text, 0), 1L)).thenReturn(new ArrayList<>());
lenient().when(newsService.searchNews(any(), any())).thenReturn(allNews);
lenient().when(spaceService.isMember(any(Space.class), any())).thenReturn(true);
lenient().when(spaceService.getSpaceById(anyString())).thenReturn(new Space());
Expand Down Expand Up @@ -1681,7 +1679,6 @@ public void shouldGetAllNewsWhenSearchingWithTagTextInTheGivenSpace() throws Exc
allNews.add(news2);
COMMONS_UTILS.when(()-> CommonsUtils.getService(TagService.class)).thenReturn(tagService);
REST_UTILS.when(()-> RestUtils.getCurrentUserIdentityId()).thenReturn(1L);
when(tagService.findTags(new TagFilter(tagText, 0), 1L)).thenReturn(Arrays.asList(new TagName("tagText")));
lenient().when(newsService.searchNews(any(), any())).thenReturn(allNews);
lenient().when(spaceService.isMember(any(Space.class), any())).thenReturn(true);
lenient().when(spaceService.getSpaceById(anyString())).thenReturn(new Space());
Expand Down Expand Up @@ -1733,7 +1730,6 @@ public void shouldGetPinnedNewsWhenSearchingWithTextInTheGivenSpaces() throws Ex
allNews.add(news3);
COMMONS_UTILS.when(()-> CommonsUtils.getService(TagService.class)).thenReturn(tagService);
REST_UTILS.when(()-> RestUtils.getCurrentUserIdentityId()).thenReturn(1L);
when(tagService.findTags(new TagFilter(text, 0), 1L)).thenReturn(new ArrayList<>());
lenient().when(newsService.searchNews(any(), any())).thenReturn(allNews);
lenient().when(spaceService.isMember(any(Space.class), any())).thenReturn(true);
lenient().when(spaceService.getSpaceById(anyString())).thenReturn(new Space());
Expand Down Expand Up @@ -1918,7 +1914,6 @@ public void shouldGetMyPostedNewsWhenSearchingWithTheGivenSpaces() throws Except
allNews.add(news3);
COMMONS_UTILS.when(()-> CommonsUtils.getService(TagService.class)).thenReturn(tagService);
REST_UTILS.when(()-> RestUtils.getCurrentUserIdentityId()).thenReturn(1L);
when(tagService.findTags(new TagFilter(text, 0), 1L)).thenReturn(new ArrayList<>());
lenient().when(newsService.searchNews(any(), any())).thenReturn(allNews);
lenient().when(spaceService.isMember(any(Space.class), any())).thenReturn(true);
lenient().when(spaceService.getSpaceById(anyString())).thenReturn(new Space());
Expand Down
3 changes: 3 additions & 0 deletions webapp/src/main/webapp/services/newsServices.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ export function markNewsAsRead(newsId){
export function getNews(filter, spaces, searchText, offset, limit, returnSize) {
let url = `${newsConstants.NEWS_API}?author=${newsConstants.userName}&publicationState=published&filter=${filter}`;
if (searchText) {
if (searchText.indexOf('#') === 0) {
searchText = searchText.replace('#', '%23');
}
url += `&text=${searchText}`;
}
if (spaces) {
Expand Down

0 comments on commit d453ebd

Please sign in to comment.