Merge branch '16683-fed-sharing' refs #16683
[arvados.git] / src / store / collections-content-address-panel / collections-content-address-middleware-service.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { ServiceRepository } from '~/services/services';
6 import { MiddlewareAPI, Dispatch } from 'redux';
7 import { DataExplorerMiddlewareService } from '~/store/data-explorer/data-explorer-middleware-service';
8 import { RootState } from '~/store/store';
9 import { getUserUuid } from "~/common/getuser";
10 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
11 import { getDataExplorer } from '~/store/data-explorer/data-explorer-reducer';
12 import { resourcesActions } from '~/store/resources/resources-actions';
13 import { FilterBuilder } from '~/services/api/filter-builder';
14 import { SortDirection } from '~/components/data-table/data-column';
15 import { OrderDirection, OrderBuilder } from '~/services/api/order-builder';
16 import { getSortColumn } from "~/store/data-explorer/data-explorer-reducer";
17 import { FavoritePanelColumnNames } from '~/views/favorite-panel/favorite-panel';
18 import { GroupContentsResource, GroupContentsResourcePrefix } from '~/services/groups-service/groups-service';
19 import { progressIndicatorActions } from '~/store/progress-indicator/progress-indicator-actions';
20 import { collectionsContentAddressActions } from './collections-content-address-panel-actions';
21 import { navigateTo } from '~/store/navigation/navigation-action';
22 import { updateFavorites } from '~/store/favorites/favorites-actions';
23 import { updatePublicFavorites } from '~/store/public-favorites/public-favorites-actions';
24 import { setBreadcrumbs } from '../breadcrumbs/breadcrumbs-actions';
25 import { ResourceKind, extractUuidKind } from '~/models/resource';
26 import { ownerNameActions } from '~/store/owner-name/owner-name-actions';
27 import { getUserDisplayName } from '~/models/user';
28
29 export class CollectionsWithSameContentAddressMiddlewareService extends DataExplorerMiddlewareService {
30     constructor(private services: ServiceRepository, id: string) {
31         super(id);
32     }
33
34     async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
35         const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
36         if (!dataExplorer) {
37             api.dispatch(collectionPanelDataExplorerIsNotSet());
38         } else {
39             const sortColumn = getSortColumn(dataExplorer);
40
41             const contentOrder = new OrderBuilder<GroupContentsResource>();
42
43             if (sortColumn && sortColumn.name === FavoritePanelColumnNames.NAME) {
44                 const direction = sortColumn.sortDirection === SortDirection.ASC
45                     ? OrderDirection.ASC
46                     : OrderDirection.DESC;
47
48                 contentOrder
49                     .addOrder(direction, "name", GroupContentsResourcePrefix.COLLECTION);
50             }
51             try {
52                 api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
53                 const userUuid = getUserUuid(api.getState());
54                 const pathname = api.getState().router.location!.pathname;
55                 const contentAddress = pathname.split('/')[2];
56                 const response = await this.services.collectionService.list({
57                     limit: dataExplorer.rowsPerPage,
58                     offset: dataExplorer.page * dataExplorer.rowsPerPage,
59                     filters: new FilterBuilder()
60                         .addEqual('portable_data_hash', contentAddress)
61                         .addILike("name", dataExplorer.searchValue)
62                         .getFilters()
63                 });
64                 const userUuids = response.items.map(it => {
65                     if (extractUuidKind(it.ownerUuid) === ResourceKind.USER) {
66                         return it.ownerUuid;
67                     } else {
68                         return '';
69                     }
70                 }
71                 );
72                 const groupUuids = response.items.map(it => {
73                     if (extractUuidKind(it.ownerUuid) === ResourceKind.GROUP) {
74                         return it.ownerUuid;
75                     } else {
76                         return '';
77                     }
78                 });
79                 const responseUsers = await this.services.userService.list({
80                     filters: new FilterBuilder()
81                         .addIn('uuid', userUuids)
82                         .getFilters(),
83                     count: "none"
84                 });
85                 const responseGroups = await this.services.groupsService.list({
86                     filters: new FilterBuilder()
87                         .addIn('uuid', groupUuids)
88                         .getFilters(),
89                     count: "none"
90                 });
91                 responseUsers.items.map(it => {
92                     api.dispatch<any>(ownerNameActions.SET_OWNER_NAME({
93                         name: it.uuid === userUuid
94                             ? 'User: Me'
95                             : `User: ${getUserDisplayName(it)}`,
96                         uuid: it.uuid
97                     }));
98                 });
99                 responseGroups.items.map(it => {
100                     api.dispatch<any>(ownerNameActions.SET_OWNER_NAME({ name: `Project: ${it.name}`, uuid: it.uuid }));
101                 });
102                 api.dispatch<any>(setBreadcrumbs([{ label: 'Projects', uuid: userUuid }]));
103                 api.dispatch<any>(updateFavorites(response.items.map(item => item.uuid)));
104                 api.dispatch<any>(updatePublicFavorites(response.items.map(item => item.uuid)));
105                 if (response.itemsAvailable === 1) {
106                     api.dispatch<any>(navigateTo(response.items[0].uuid));
107                     api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
108                 } else {
109                     api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
110                     api.dispatch(resourcesActions.SET_RESOURCES(response.items));
111                     api.dispatch(collectionsContentAddressActions.SET_ITEMS({
112                         items: response.items.map((resource: any) => resource.uuid),
113                         itemsAvailable: response.itemsAvailable,
114                         page: Math.floor(response.offset / response.limit),
115                         rowsPerPage: response.limit
116                     }));
117                 }
118             } catch (e) {
119                 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
120                 api.dispatch(collectionsContentAddressActions.SET_ITEMS({
121                     items: [],
122                     itemsAvailable: 0,
123                     page: 0,
124                     rowsPerPage: dataExplorer.rowsPerPage
125                 }));
126                 api.dispatch(couldNotFetchCollections());
127             }
128         }
129     }
130 }
131
132 const collectionPanelDataExplorerIsNotSet = () =>
133     snackbarActions.OPEN_SNACKBAR({
134         message: 'Collection panel is not ready.',
135         kind: SnackbarKind.ERROR
136     });
137
138 const couldNotFetchCollections = () =>
139     snackbarActions.OPEN_SNACKBAR({
140         message: 'Could not fetch collection with this content address.',
141         kind: SnackbarKind.ERROR
142     });