Skip to content

Commit d13d886

Browse files
ConfusionOrb221Nathan Buckingham
andauthored
RSS feed from search results (Angular) (#3227)
* 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]>
1 parent 0ade76a commit d13d886

File tree

4 files changed

+107
-28
lines changed

4 files changed

+107
-28
lines changed

src/app/app-routes.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export const APP_ROUTES: Route[] = [
6363
path: 'home',
6464
loadChildren: () => import('./home-page/home-page-routes')
6565
.then((m) => m.ROUTES),
66-
data: { showBreadcrumbs: false },
66+
data: { showBreadcrumbs: false, enableRSS: true },
6767
providers: [provideSuggestionNotificationsState()],
6868
canActivate: [endUserAgreementCurrentUserGuard],
6969
},
@@ -101,12 +101,14 @@ export const APP_ROUTES: Route[] = [
101101
path: COMMUNITY_MODULE_PATH,
102102
loadChildren: () => import('./community-page/community-page-routes')
103103
.then((m) => m.ROUTES),
104+
data: { enableRSS: true },
104105
canActivate: [endUserAgreementCurrentUserGuard],
105106
},
106107
{
107108
path: COLLECTION_MODULE_PATH,
108109
loadChildren: () => import('./collection-page/collection-page-routes')
109110
.then((m) => m.ROUTES),
111+
data: { showBreadcrumbs: false, enableRSS: true },
110112
canActivate: [endUserAgreementCurrentUserGuard],
111113
},
112114
{
@@ -137,13 +139,15 @@ export const APP_ROUTES: Route[] = [
137139
path: 'mydspace',
138140
loadChildren: () => import('./my-dspace-page/my-dspace-page-routes')
139141
.then((m) => m.ROUTES),
142+
data: { enableRSS: true },
140143
providers: [provideSuggestionNotificationsState()],
141144
canActivate: [authenticatedGuard, endUserAgreementCurrentUserGuard],
142145
},
143146
{
144147
path: 'search',
145148
loadChildren: () => import('./search-page/search-page-routes')
146149
.then((m) => m.ROUTES),
150+
data: { enableRSS: true },
147151
canActivate: [endUserAgreementCurrentUserGuard],
148152
},
149153
{
@@ -156,6 +160,7 @@ export const APP_ROUTES: Route[] = [
156160
path: ADMIN_MODULE_PATH,
157161
loadChildren: () => import('./admin/admin-routes')
158162
.then((m) => m.ROUTES),
163+
data: { enableRSS: true },
159164
canActivate: [siteAdministratorGuard, endUserAgreementCurrentUserGuard],
160165
},
161166
{
@@ -200,6 +205,7 @@ export const APP_ROUTES: Route[] = [
200205
providers: [provideSubmissionState()],
201206
loadChildren: () => import('./workflowitems-edit-page/workflowitems-edit-page-routes')
202207
.then((m) => m.ROUTES),
208+
data: { enableRSS: true },
203209
canActivate: [endUserAgreementCurrentUserGuard],
204210
},
205211
{
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
<ng-container *ngIf="(isEnabled$ | async) && (hasRoute('home') || hasRoute('collections') || hasRoute('communities'))">
1+
<ng-container
2+
*ngIf="(isEnabled$ | async) && (isActivated$ | async)">
23
<div *ngIf="route$ | async as route" class="d-inline-block float-right margin-right">
3-
<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>
4+
<a target="_blank" rel="noopener noreferrer" [href]="route" class="btn btn-secondary"
5+
[title]="'feed.description' | translate" [attr.aria-label]="'feed.description' | translate">
6+
<i class="fas fa-rss-square"></i>
7+
</a>
48
</div>
5-
</ng-container>
9+
</ng-container>

src/app/shared/rss-feed/rss.component.spec.ts

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@ import {
33
TestBed,
44
waitForAsync,
55
} from '@angular/core/testing';
6-
import { Router } from '@angular/router';
6+
import {
7+
ActivatedRoute,
8+
Router,
9+
} from '@angular/router';
10+
import { TranslateService } from '@ngx-translate/core';
711
import { of as observableOf } from 'rxjs';
812

13+
import {
14+
SortDirection,
15+
SortOptions,
16+
} from '../../core/cache/models/sort-options.model';
917
import { ConfigurationDataService } from '../../core/data/configuration-data.service';
1018
import { RemoteData } from '../../core/data/remote-data';
1119
import { GroupDataService } from '../../core/eperson/group-data.service';
@@ -14,22 +22,24 @@ import { LinkHeadService } from '../../core/services/link-head.service';
1422
import { Collection } from '../../core/shared/collection.model';
1523
import { ConfigurationProperty } from '../../core/shared/configuration-property.model';
1624
import { SearchConfigurationService } from '../../core/shared/search/search-configuration.service';
25+
import { MockActivatedRoute } from '../mocks/active-router.mock';
1726
import { RouterMock } from '../mocks/router.mock';
27+
import { getMockTranslateService } from '../mocks/translate.service.mock';
1828
import { PaginationComponentOptions } from '../pagination/pagination-component-options.model';
1929
import {
2030
createSuccessfulRemoteDataObject,
2131
createSuccessfulRemoteDataObject$,
2232
} from '../remote-data.utils';
2333
import { PaginatedSearchOptions } from '../search/models/paginated-search-options.model';
34+
import { SearchFilter } from '../search/models/search-filter.model';
2435
import { PaginationServiceStub } from '../testing/pagination-service.stub';
2536
import { SearchConfigurationServiceStub } from '../testing/search-configuration-service.stub';
2637
import { createPaginatedList } from '../testing/utils.test';
2738
import { RSSComponent } from './rss.component';
2839

29-
30-
3140
describe('RssComponent', () => {
3241
let comp: RSSComponent;
42+
let options: SortOptions;
3343
let fixture: ComponentFixture<RSSComponent>;
3444
let uuid: string;
3545
let query: string;
@@ -69,6 +79,7 @@ describe('RssComponent', () => {
6979
pageSize: 10,
7080
currentPage: 1,
7181
}),
82+
sort: new SortOptions('dc.title', SortDirection.ASC),
7283
}));
7384
groupDataService = jasmine.createSpyObj('groupsDataService', {
7485
findListByHref: createSuccessfulRemoteDataObject$(createPaginatedList([])),
@@ -80,38 +91,60 @@ describe('RssComponent', () => {
8091
paginatedSearchOptions: mockSearchOptions,
8192
};
8293
TestBed.configureTestingModule({
83-
imports: [RSSComponent],
8494
providers: [
8595
{ provide: GroupDataService, useValue: groupDataService },
8696
{ provide: LinkHeadService, useValue: linkHeadService },
8797
{ provide: ConfigurationDataService, useValue: configurationDataService },
8898
{ provide: SearchConfigurationService, useValue: new SearchConfigurationServiceStub() },
8999
{ provide: PaginationService, useValue: paginationService },
90100
{ provide: Router, useValue: new RouterMock() },
101+
{ provide: ActivatedRoute, useValue: new MockActivatedRoute },
102+
{ provide: TranslateService, useValue: getMockTranslateService() },
91103
],
104+
declarations: [],
92105
}).compileComponents();
93106
}));
94107

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

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

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

112126
it('should default to query * if none provided', () => {
113-
const route = comp.formulateRoute(null, 'opensearch/search', null);
114-
expect(route).toBe('/opensearch/search?format=atom&query=*');
127+
const route = comp.formulateRoute(null, 'opensearch/search', options, null);
128+
expect(route).toBe('/opensearch/search?format=atom&sort=dc.title&sort_direction=DESC&query=*');
129+
});
130+
131+
it('should include filters in opensearch url if provided', () => {
132+
const filters = [
133+
new SearchFilter('f.test', ['value','another value'], 'contains'), // should be split into two arguments, spaces should be URI-encoded
134+
new SearchFilter('f.range', ['[1987 TO 1988]'], 'equals'), // value should be URI-encoded, ',equals' should not
135+
];
136+
const route = comp.formulateRoute(uuid, 'opensearch/search', options, query, filters);
137+
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');
138+
});
139+
140+
it('should include configuration in opensearch url if provided', () => {
141+
const route = comp.formulateRoute(uuid, 'opensearch/search', options, query, null, 'adminConfiguration');
142+
expect(route).toBe('/opensearch/search?format=atom&scope=2cfcf65e-0a51-4bcb-8592-b8db7b064790&sort=dc.title&sort_direction=DESC&query=test&configuration=adminConfiguration');
143+
});
144+
145+
it('should include rpp in opensearch url if provided', () => {
146+
const route = comp.formulateRoute(uuid, 'opensearch/search', options, query, null, null, 50);
147+
expect(route).toBe('/opensearch/search?format=atom&scope=2cfcf65e-0a51-4bcb-8592-b8db7b064790&sort=dc.title&sort_direction=DESC&query=test&rpp=50');
115148
});
116149
});
117150

src/app/shared/rss-feed/rss.component.ts

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,38 @@ import {
99
OnInit,
1010
ViewEncapsulation,
1111
} from '@angular/core';
12-
import { Router } from '@angular/router';
13-
import { TranslateModule } from '@ngx-translate/core';
12+
import {
13+
ActivatedRoute,
14+
Router,
15+
} from '@angular/router';
16+
import {
17+
TranslateModule,
18+
TranslateService,
19+
} from '@ngx-translate/core';
1420
import {
1521
BehaviorSubject,
16-
Observable,
1722
Subscription,
1823
} from 'rxjs';
1924
import {
2025
map,
2126
switchMap,
2227
} from 'rxjs/operators';
2328

24-
import { environment } from '../../../../src/environments/environment';
29+
import { environment } from '../../../environments/environment';
30+
import { SortOptions } from '../../core/cache/models/sort-options.model';
2531
import { ConfigurationDataService } from '../../core/data/configuration-data.service';
2632
import { RemoteData } from '../../core/data/remote-data';
2733
import { GroupDataService } from '../../core/eperson/group-data.service';
2834
import { PaginationService } from '../../core/pagination/pagination.service';
2935
import { LinkHeadService } from '../../core/services/link-head.service';
3036
import { getFirstCompletedRemoteData } from '../../core/shared/operators';
3137
import { SearchConfigurationService } from '../../core/shared/search/search-configuration.service';
38+
import {
39+
hasValue,
40+
isUndefined,
41+
} from '../empty.util';
3242
import { PaginatedSearchOptions } from '../search/models/paginated-search-options.model';
33-
34-
43+
import { SearchFilter } from '../search/models/search-filter.model';
3544
/**
3645
* The Rss feed button component.
3746
*/
@@ -51,8 +60,9 @@ export class RSSComponent implements OnInit, OnDestroy {
5160

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

63+
isActivated$: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
64+
5465
uuid: string;
55-
configuration$: Observable<string>;
5666

5767
subs: Subscription[] = [];
5868

@@ -61,7 +71,9 @@ export class RSSComponent implements OnInit, OnDestroy {
6171
private configurationService: ConfigurationDataService,
6272
private searchConfigurationService: SearchConfigurationService,
6373
private router: Router,
64-
protected paginationService: PaginationService) {
74+
private route: ActivatedRoute,
75+
protected paginationService: PaginationService,
76+
protected translateService: TranslateService) {
6577
}
6678
/**
6779
* Removes the linktag created when the component gets removed from the page.
@@ -78,8 +90,11 @@ export class RSSComponent implements OnInit, OnDestroy {
7890
* Generates the link tags and the url to opensearch when the component is loaded.
7991
*/
8092
ngOnInit(): void {
81-
this.configuration$ = this.searchConfigurationService.getCurrentConfiguration('default');
82-
93+
if (hasValue(this.route.snapshot.data?.enableRSS)) {
94+
this.isActivated$.next(this.route.snapshot.data.enableRSS);
95+
} else if (isUndefined(this.route.snapshot.data?.enableRSS)) {
96+
this.isActivated$.next(false);
97+
}
8398
this.subs.push(this.configurationService.findByPropertyName('websvc.opensearch.enable').pipe(
8499
getFirstCompletedRemoteData(),
85100
).subscribe((result) => {
@@ -106,7 +121,7 @@ export class RSSComponent implements OnInit, OnDestroy {
106121
return null;
107122
}
108123
this.uuid = this.groupDataService.getUUIDFromString(this.router.url);
109-
const route = environment.rest.baseUrl + this.formulateRoute(this.uuid, openSearchUri, searchOptions.query);
124+
const route = environment.rest.baseUrl + this.formulateRoute(this.uuid, openSearchUri, searchOptions.sort, searchOptions.query, searchOptions.filters, searchOptions.configuration, searchOptions.pagination?.pageSize, searchOptions.fixedFilter);
110125
this.addLinks(route);
111126
this.linkHeadService.addTag({
112127
href: environment.rest.baseUrl + '/' + openSearchUri + '/service',
@@ -122,20 +137,40 @@ export class RSSComponent implements OnInit, OnDestroy {
122137
* Function created a route given the different params available to opensearch
123138
* @param uuid The uuid if a scope is present
124139
* @param opensearch openSearch uri
140+
* @param sort The sort options for the opensearch request
125141
* @param query The query string that was provided in the search
126142
* @returns The combine URL to opensearch
127143
*/
128-
formulateRoute(uuid: string, opensearch: string, query: string): string {
129-
let route = '?format=atom';
144+
formulateRoute(uuid: string, opensearch: string, sort?: SortOptions, query?: string, searchFilters?: SearchFilter[], configuration?: string, pageSize?: number, fixedFilter?: string): string {
145+
let route = 'format=atom';
130146
if (uuid) {
131147
route += `&scope=${uuid}`;
132148
}
149+
if (sort && sort.direction && sort.field && sort.field !== 'id') {
150+
route += `&sort=${sort.field}&sort_direction=${sort.direction}`;
151+
}
133152
if (query) {
134153
route += `&query=${query}`;
135154
} else {
136155
route += `&query=*`;
137156
}
138-
route = '/' + opensearch + route;
157+
if (configuration) {
158+
route += `&configuration=${configuration}`;
159+
}
160+
if (pageSize) {
161+
route += `&rpp=${pageSize}`;
162+
}
163+
if (searchFilters) {
164+
for (const filter of searchFilters) {
165+
for (const val of filter.values) {
166+
route += '&' + filter.key + '=' + encodeURIComponent(val) + (filter.operator ? ',' + filter.operator : '');
167+
}
168+
}
169+
}
170+
if (fixedFilter) {
171+
route += '&' + fixedFilter;
172+
}
173+
route = '/' + opensearch + '?' + route;
139174
return route;
140175
}
141176

@@ -169,4 +204,5 @@ export class RSSComponent implements OnInit, OnDestroy {
169204
title: 'Sitewide RSS feed',
170205
});
171206
}
207+
172208
}

0 commit comments

Comments
 (0)