1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { LinkService } from "../link-service/link-service";
6 import { GroupsService, GroupContentsResource } from "../groups-service/groups-service";
7 import { LinkClass } from "models/link";
8 import { FilterBuilder, joinFilters } from "services/api/filter-builder";
9 import { ListResults } from 'services/common-service/common-service';
11 export interface FavoriteListArguments {
16 contentOrder?: string;
19 export class FavoriteService {
21 private linkService: LinkService,
22 private groupsService: GroupsService,
25 create(data: { userUuid: string; resource: { uuid: string; name: string } }) {
26 return this.linkService.create({
27 ownerUuid: data.userUuid,
28 tailUuid: data.userUuid,
29 headUuid: data.resource.uuid,
30 linkClass: LinkClass.STAR,
31 name: data.resource.name
35 delete(data: { userUuid: string; resourceUuid: string; }) {
36 return this.linkService
38 filters: new FilterBuilder()
39 .addEqual('owner_uuid', data.userUuid)
40 .addEqual('head_uuid', data.resourceUuid)
41 .addEqual('link_class', LinkClass.STAR)
44 .then(results => Promise.all(
45 results.items.map(item => this.linkService.delete(item.uuid))));
48 list(userUuid: string, { filters, limit, offset, linkOrder, contentOrder }: FavoriteListArguments = {}, showOnlyOwned: boolean = true): Promise<ListResults<GroupContentsResource>> {
49 const listFilters = new FilterBuilder()
50 .addEqual('owner_uuid', userUuid)
51 .addEqual('link_class', LinkClass.STAR)
54 return this.linkService
56 filters: joinFilters(filters || '', listFilters),
62 const uuids = results.items.map(item => item.headUuid);
63 return this.groupsService.contents(showOnlyOwned ? userUuid : '', {
67 filters: new FilterBuilder().addIn('uuid', uuids).getFilters(),
73 checkPresenceInFavorites(userUuid: string, resourceUuids: string[]): Promise<Record<string, boolean>> {
74 return this.linkService
76 filters: new FilterBuilder()
77 .addIn("head_uuid", resourceUuids)
78 .addEqual("owner_uuid", userUuid)
79 .addEqual("link_class", LinkClass.STAR)
82 .then(({ items }) => resourceUuids.reduce((results, uuid) => {
83 const isFavorite = items.some(item => item.headUuid === uuid);
84 return { ...results, [uuid]: isFavorite };