Skip to content

Commit

Permalink
RSS feed from search results (Angular) (#3227)
Browse files Browse the repository at this point in the history
* Port rss to 7.6 and upgrades to search functionality

* 116466: add missing imports

* 116466: fix tests and lint issues

* 116466: rss component use activated route data

* 116466: lint fixes

* 116466: More Lint fixes

---------

Co-authored-by: Nathan Buckingham <[email protected]>
  • Loading branch information
ConfusionOrb221 and Nathan Buckingham authored Dec 19, 2024
1 parent 0ade76a commit d13d886
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 28 deletions.
8 changes: 7 additions & 1 deletion src/app/app-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const APP_ROUTES: Route[] = [
path: 'home',
loadChildren: () => import('./home-page/home-page-routes')
.then((m) => m.ROUTES),
data: { showBreadcrumbs: false },
data: { showBreadcrumbs: false, enableRSS: true },
providers: [provideSuggestionNotificationsState()],
canActivate: [endUserAgreementCurrentUserGuard],
},
Expand Down Expand Up @@ -101,12 +101,14 @@ export const APP_ROUTES: Route[] = [
path: COMMUNITY_MODULE_PATH,
loadChildren: () => import('./community-page/community-page-routes')
.then((m) => m.ROUTES),
data: { enableRSS: true },
canActivate: [endUserAgreementCurrentUserGuard],
},
{
path: COLLECTION_MODULE_PATH,
loadChildren: () => import('./collection-page/collection-page-routes')
.then((m) => m.ROUTES),
data: { showBreadcrumbs: false, enableRSS: true },
canActivate: [endUserAgreementCurrentUserGuard],
},
{
Expand Down Expand Up @@ -137,13 +139,15 @@ export const APP_ROUTES: Route[] = [
path: 'mydspace',
loadChildren: () => import('./my-dspace-page/my-dspace-page-routes')
.then((m) => m.ROUTES),
data: { enableRSS: true },
providers: [provideSuggestionNotificationsState()],
canActivate: [authenticatedGuard, endUserAgreementCurrentUserGuard],
},
{
path: 'search',
loadChildren: () => import('./search-page/search-page-routes')
.then((m) => m.ROUTES),
data: { enableRSS: true },
canActivate: [endUserAgreementCurrentUserGuard],
},
{
Expand All @@ -156,6 +160,7 @@ export const APP_ROUTES: Route[] = [
path: ADMIN_MODULE_PATH,
loadChildren: () => import('./admin/admin-routes')
.then((m) => m.ROUTES),
data: { enableRSS: true },
canActivate: [siteAdministratorGuard, endUserAgreementCurrentUserGuard],
},
{
Expand Down Expand Up @@ -200,6 +205,7 @@ export const APP_ROUTES: Route[] = [
providers: [provideSubmissionState()],
loadChildren: () => import('./workflowitems-edit-page/workflowitems-edit-page-routes')
.then((m) => m.ROUTES),
data: { enableRSS: true },
canActivate: [endUserAgreementCurrentUserGuard],
},
{
Expand Down
10 changes: 7 additions & 3 deletions src/app/shared/rss-feed/rss.component.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<ng-container *ngIf="(isEnabled$ | async) && (hasRoute('home') || hasRoute('collections') || hasRoute('communities'))">
<ng-container
*ngIf="(isEnabled$ | async) && (isActivated$ | async)">
<div *ngIf="route$ | async as route" class="d-inline-block float-right margin-right">
<a [href]="route" class="btn btn-secondary" [title]="'feed.description' | translate" [attr.aria-label]="'feed.description' | translate"><i class="fas fa-rss-square"></i></a>
<a target="_blank" rel="noopener noreferrer" [href]="route" class="btn btn-secondary"
[title]="'feed.description' | translate" [attr.aria-label]="'feed.description' | translate">
<i class="fas fa-rss-square"></i>
</a>
</div>
</ng-container>
</ng-container>
53 changes: 43 additions & 10 deletions src/app/shared/rss-feed/rss.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@ import {
TestBed,
waitForAsync,
} from '@angular/core/testing';
import { Router } from '@angular/router';
import {
ActivatedRoute,
Router,
} from '@angular/router';
import { TranslateService } from '@ngx-translate/core';
import { of as observableOf } from 'rxjs';

import {
SortDirection,
SortOptions,
} from '../../core/cache/models/sort-options.model';
import { ConfigurationDataService } from '../../core/data/configuration-data.service';
import { RemoteData } from '../../core/data/remote-data';
import { GroupDataService } from '../../core/eperson/group-data.service';
Expand All @@ -14,22 +22,24 @@ import { LinkHeadService } from '../../core/services/link-head.service';
import { Collection } from '../../core/shared/collection.model';
import { ConfigurationProperty } from '../../core/shared/configuration-property.model';
import { SearchConfigurationService } from '../../core/shared/search/search-configuration.service';
import { MockActivatedRoute } from '../mocks/active-router.mock';
import { RouterMock } from '../mocks/router.mock';
import { getMockTranslateService } from '../mocks/translate.service.mock';
import { PaginationComponentOptions } from '../pagination/pagination-component-options.model';
import {
createSuccessfulRemoteDataObject,
createSuccessfulRemoteDataObject$,
} from '../remote-data.utils';
import { PaginatedSearchOptions } from '../search/models/paginated-search-options.model';
import { SearchFilter } from '../search/models/search-filter.model';
import { PaginationServiceStub } from '../testing/pagination-service.stub';
import { SearchConfigurationServiceStub } from '../testing/search-configuration-service.stub';
import { createPaginatedList } from '../testing/utils.test';
import { RSSComponent } from './rss.component';



describe('RssComponent', () => {
let comp: RSSComponent;
let options: SortOptions;
let fixture: ComponentFixture<RSSComponent>;
let uuid: string;
let query: string;
Expand Down Expand Up @@ -69,6 +79,7 @@ describe('RssComponent', () => {
pageSize: 10,
currentPage: 1,
}),
sort: new SortOptions('dc.title', SortDirection.ASC),
}));
groupDataService = jasmine.createSpyObj('groupsDataService', {
findListByHref: createSuccessfulRemoteDataObject$(createPaginatedList([])),
Expand All @@ -80,38 +91,60 @@ describe('RssComponent', () => {
paginatedSearchOptions: mockSearchOptions,
};
TestBed.configureTestingModule({
imports: [RSSComponent],
providers: [
{ provide: GroupDataService, useValue: groupDataService },
{ provide: LinkHeadService, useValue: linkHeadService },
{ provide: ConfigurationDataService, useValue: configurationDataService },
{ provide: SearchConfigurationService, useValue: new SearchConfigurationServiceStub() },
{ provide: PaginationService, useValue: paginationService },
{ provide: Router, useValue: new RouterMock() },
{ provide: ActivatedRoute, useValue: new MockActivatedRoute },
{ provide: TranslateService, useValue: getMockTranslateService() },
],
declarations: [],
}).compileComponents();
}));

beforeEach(() => {
options = new SortOptions('dc.title', SortDirection.DESC);
uuid = '2cfcf65e-0a51-4bcb-8592-b8db7b064790';
query = 'test';
fixture = TestBed.createComponent(RSSComponent);
comp = fixture.componentInstance;
});

it('should formulate the correct url given params in url', () => {
const route = comp.formulateRoute(uuid, 'opensearch/search', query);
expect(route).toBe('/opensearch/search?format=atom&scope=2cfcf65e-0a51-4bcb-8592-b8db7b064790&query=test');
const route = comp.formulateRoute(uuid, 'opensearch/search', options, query);
expect(route).toBe('/opensearch/search?format=atom&scope=2cfcf65e-0a51-4bcb-8592-b8db7b064790&sort=dc.title&sort_direction=DESC&query=test');
});

it('should skip uuid if its null', () => {
const route = comp.formulateRoute(null, 'opensearch/search', query);
expect(route).toBe('/opensearch/search?format=atom&query=test');
const route = comp.formulateRoute(null, 'opensearch/search', options, query);
expect(route).toBe('/opensearch/search?format=atom&sort=dc.title&sort_direction=DESC&query=test');
});

it('should default to query * if none provided', () => {
const route = comp.formulateRoute(null, 'opensearch/search', null);
expect(route).toBe('/opensearch/search?format=atom&query=*');
const route = comp.formulateRoute(null, 'opensearch/search', options, null);
expect(route).toBe('/opensearch/search?format=atom&sort=dc.title&sort_direction=DESC&query=*');
});

it('should include filters in opensearch url if provided', () => {
const filters = [
new SearchFilter('f.test', ['value','another value'], 'contains'), // should be split into two arguments, spaces should be URI-encoded
new SearchFilter('f.range', ['[1987 TO 1988]'], 'equals'), // value should be URI-encoded, ',equals' should not
];
const route = comp.formulateRoute(uuid, 'opensearch/search', options, query, filters);
expect(route).toBe('/opensearch/search?format=atom&scope=2cfcf65e-0a51-4bcb-8592-b8db7b064790&sort=dc.title&sort_direction=DESC&query=test&f.test=value,contains&f.test=another%20value,contains&f.range=%5B1987%20TO%201988%5D,equals');
});

it('should include configuration in opensearch url if provided', () => {
const route = comp.formulateRoute(uuid, 'opensearch/search', options, query, null, 'adminConfiguration');
expect(route).toBe('/opensearch/search?format=atom&scope=2cfcf65e-0a51-4bcb-8592-b8db7b064790&sort=dc.title&sort_direction=DESC&query=test&configuration=adminConfiguration');
});

it('should include rpp in opensearch url if provided', () => {
const route = comp.formulateRoute(uuid, 'opensearch/search', options, query, null, null, 50);
expect(route).toBe('/opensearch/search?format=atom&scope=2cfcf65e-0a51-4bcb-8592-b8db7b064790&sort=dc.title&sort_direction=DESC&query=test&rpp=50');
});
});

64 changes: 50 additions & 14 deletions src/app/shared/rss-feed/rss.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,38 @@ import {
OnInit,
ViewEncapsulation,
} from '@angular/core';
import { Router } from '@angular/router';
import { TranslateModule } from '@ngx-translate/core';
import {
ActivatedRoute,
Router,
} from '@angular/router';
import {
TranslateModule,
TranslateService,
} from '@ngx-translate/core';
import {
BehaviorSubject,
Observable,
Subscription,
} from 'rxjs';
import {
map,
switchMap,
} from 'rxjs/operators';

import { environment } from '../../../../src/environments/environment';
import { environment } from '../../../environments/environment';
import { SortOptions } from '../../core/cache/models/sort-options.model';
import { ConfigurationDataService } from '../../core/data/configuration-data.service';
import { RemoteData } from '../../core/data/remote-data';
import { GroupDataService } from '../../core/eperson/group-data.service';
import { PaginationService } from '../../core/pagination/pagination.service';
import { LinkHeadService } from '../../core/services/link-head.service';
import { getFirstCompletedRemoteData } from '../../core/shared/operators';
import { SearchConfigurationService } from '../../core/shared/search/search-configuration.service';
import {
hasValue,
isUndefined,
} from '../empty.util';
import { PaginatedSearchOptions } from '../search/models/paginated-search-options.model';


import { SearchFilter } from '../search/models/search-filter.model';
/**
* The Rss feed button component.
*/
Expand All @@ -51,8 +60,9 @@ export class RSSComponent implements OnInit, OnDestroy {

isEnabled$: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(null);

isActivated$: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);

uuid: string;
configuration$: Observable<string>;

subs: Subscription[] = [];

Expand All @@ -61,7 +71,9 @@ export class RSSComponent implements OnInit, OnDestroy {
private configurationService: ConfigurationDataService,
private searchConfigurationService: SearchConfigurationService,
private router: Router,
protected paginationService: PaginationService) {
private route: ActivatedRoute,
protected paginationService: PaginationService,
protected translateService: TranslateService) {
}
/**
* Removes the linktag created when the component gets removed from the page.
Expand All @@ -78,8 +90,11 @@ export class RSSComponent implements OnInit, OnDestroy {
* Generates the link tags and the url to opensearch when the component is loaded.
*/
ngOnInit(): void {
this.configuration$ = this.searchConfigurationService.getCurrentConfiguration('default');

if (hasValue(this.route.snapshot.data?.enableRSS)) {
this.isActivated$.next(this.route.snapshot.data.enableRSS);
} else if (isUndefined(this.route.snapshot.data?.enableRSS)) {
this.isActivated$.next(false);
}
this.subs.push(this.configurationService.findByPropertyName('websvc.opensearch.enable').pipe(
getFirstCompletedRemoteData(),
).subscribe((result) => {
Expand All @@ -106,7 +121,7 @@ export class RSSComponent implements OnInit, OnDestroy {
return null;
}
this.uuid = this.groupDataService.getUUIDFromString(this.router.url);
const route = environment.rest.baseUrl + this.formulateRoute(this.uuid, openSearchUri, searchOptions.query);
const route = environment.rest.baseUrl + this.formulateRoute(this.uuid, openSearchUri, searchOptions.sort, searchOptions.query, searchOptions.filters, searchOptions.configuration, searchOptions.pagination?.pageSize, searchOptions.fixedFilter);
this.addLinks(route);
this.linkHeadService.addTag({
href: environment.rest.baseUrl + '/' + openSearchUri + '/service',
Expand All @@ -122,20 +137,40 @@ export class RSSComponent implements OnInit, OnDestroy {
* Function created a route given the different params available to opensearch
* @param uuid The uuid if a scope is present
* @param opensearch openSearch uri
* @param sort The sort options for the opensearch request
* @param query The query string that was provided in the search
* @returns The combine URL to opensearch
*/
formulateRoute(uuid: string, opensearch: string, query: string): string {
let route = '?format=atom';
formulateRoute(uuid: string, opensearch: string, sort?: SortOptions, query?: string, searchFilters?: SearchFilter[], configuration?: string, pageSize?: number, fixedFilter?: string): string {
let route = 'format=atom';
if (uuid) {
route += `&scope=${uuid}`;
}
if (sort && sort.direction && sort.field && sort.field !== 'id') {
route += `&sort=${sort.field}&sort_direction=${sort.direction}`;
}
if (query) {
route += `&query=${query}`;
} else {
route += `&query=*`;
}
route = '/' + opensearch + route;
if (configuration) {
route += `&configuration=${configuration}`;
}
if (pageSize) {
route += `&rpp=${pageSize}`;
}
if (searchFilters) {
for (const filter of searchFilters) {
for (const val of filter.values) {
route += '&' + filter.key + '=' + encodeURIComponent(val) + (filter.operator ? ',' + filter.operator : '');
}
}
}
if (fixedFilter) {
route += '&' + fixedFilter;
}
route = '/' + opensearch + '?' + route;
return route;
}

Expand Down Expand Up @@ -169,4 +204,5 @@ export class RSSComponent implements OnInit, OnDestroy {
title: 'Sitewide RSS feed',
});
}

}

0 comments on commit d13d886

Please sign in to comment.