Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

[#59391] rework file links to paginated collection

Eric Schubert4f47e3fdc4e8
diff --git a/frontend/src/app/core/state/file-links/file-links.service.ts b/frontend/src/app/core/state/file-links/file-links.service.ts
index 67735b9b7ed..c2626f87c19 100644
--- a/frontend/src/app/core/state/file-links/file-links.service.ts
+++ b/frontend/src/app/core/state/file-links/file-links.service.ts
@@ -88,7 +88,7 @@ export class FileLinksResourceService extends ResourceStoreService<IFileLink> {
}),
tap((fileLinkCollections) => {
const storageId = idFromLink(fileLinkCollections.storage);
- const collectionKey = `${fileLinksSelfLink}?filters=[{"storage":{"operator":"=","values":["${storageId}"]}}]`;
+ const collectionKey = `${fileLinksSelfLink}?pageSize=-1&filters=[{"storage":{"operator":"=","values":["${storageId}"]}}]`;
const collection = { _embedded: { elements: fileLinkCollections.fileLinks } } as IHALCollection<IFileLink>;
insertCollectionIntoState(this.store, collection, collectionKey);
}),
diff --git a/frontend/src/app/features/work-packages/components/wp-tabs/services/wp-tabs/wp-files-count.function.ts b/frontend/src/app/features/work-packages/components/wp-tabs/services/wp-tabs/wp-files-count.function.ts
index 36f6bed47e3..dfc00da48d0 100644
--- a/frontend/src/app/features/work-packages/components/wp-tabs/services/wp-tabs/wp-files-count.function.ts
+++ b/frontend/src/app/features/work-packages/components/wp-tabs/services/wp-tabs/wp-files-count.function.ts
@@ -27,26 +27,38 @@
//++
import { Injector } from '@angular/core';
+import { HttpClient } from '@angular/common/http';
import { combineLatest, Observable, of } from 'rxjs';
import { map } from 'rxjs/operators';
import { WorkPackageResource } from 'core-app/features/hal/resources/work-package-resource';
-import { FileLinksResourceService } from 'core-app/core/state/file-links/file-links.service';
import { AttachmentsResourceService } from 'core-app/core/state/attachments/attachments.service';
+import { IHALCollection } from 'core-app/core/apiv3/types/hal-collection.type';
+import { IFileLink } from 'core-app/core/state/file-links/file-link.model';
export function workPackageFilesCount(
workPackage:WorkPackageResource,
injector:Injector,
):Observable<number> {
const attachmentService = injector.get(AttachmentsResourceService);
- const fileLinkService = injector.get(FileLinksResourceService);
+ const http = injector.get(HttpClient);
const attachmentsCollection = workPackage.$links.attachments
? attachmentService.collection(workPackage.$links.attachments.href || '')
: of([]);
- const fileLinksCollection = fileLinkService.collection(workPackage.$links.fileLinks?.href || '');
+ const totalFileLinks = workPackage.$links.fileLinks
+ ? http.get<IHALCollection<IFileLink>>(href(workPackage))
+ : of({ total: 0 });
return combineLatest([
attachmentsCollection,
- fileLinksCollection,
- ]).pipe(map(([a, f]) => a.length + f.length));
+ totalFileLinks,
+ ]).pipe(map(([a, f]) => a.length + f.total));
+}
+
+function href(workPackage:WorkPackageResource):string {
+ if (!workPackage.$links.fileLinks) {
+ return '';
+ }
+
+ return `${workPackage.$links.fileLinks.href}?pageSize=0`;
}
diff --git a/frontend/src/app/shared/components/storages/storage/storage.component.ts b/frontend/src/app/shared/components/storages/storage/storage.component.ts
index dd967426192..9877227ab47 100644
--- a/frontend/src/app/shared/components/storages/storage/storage.component.ts
+++ b/frontend/src/app/shared/components/storages/storage/storage.component.ts
@@ -84,6 +84,7 @@ import { IUploadLink } from 'core-app/core/state/storage-files/upload-link.model
import { IStorageFile } from 'core-app/core/state/storage-files/storage-file.model';
import { TimezoneService } from 'core-app/core/datetime/timezone.service';
import { PathHelperService } from 'core-app/core/path-helper/path-helper.service';
+import { WorkPackageResource } from 'core-app/features/hal/resources/work-package-resource';
import {
UploadConflictModalComponent,
} from 'core-app/shared/components/storages/upload-conflict-modal/upload-conflict-modal.component';
@@ -546,8 +547,8 @@ export class StorageComponent extends UntilDestroyedMixin implements OnInit, OnD
}
private fileLinkSelfLink(storage:IStorage):string {
- const fileLinks = this.resource.fileLinks as { href:string };
- return `${fileLinks.href}?filters=[{"storage":{"operator":"=","values":["${storage.id}"]}}]`;
+ const fileLinks = (this.resource as WorkPackageResource).$links.fileLinks;
+ return `${fileLinks?.href}?pageSize=-1&filters=[{"storage":{"operator":"=","values":["${storage.id}"]}}]`;
}
public onDropFiles(event:DragEvent):void {
diff --git a/modules/storages/app/models/storages/file_link.rb b/modules/storages/app/models/storages/file_link.rb
index 11daa248a13..b98eff8e2d3 100644
--- a/modules/storages/app/models/storages/file_link.rb
+++ b/modules/storages/app/models/storages/file_link.rb
@@ -37,11 +37,7 @@ class Storages::FileLink < ApplicationRecord
validates :container_type, inclusion: { in: ["WorkPackage", nil] }
validates :origin_id, presence: true
- attr_writer :origin_status
-
- def origin_status
- @origin_status || nil
- end
+ attribute :origin_status
delegate :project, to: :container
diff --git a/modules/storages/lib/api/v3/file_links/file_link_collection_representer.rb b/modules/storages/lib/api/v3/file_links/file_link_collection_representer.rb
index 3ca1d17aaa5..7ecfca7c285 100644
--- a/modules/storages/lib/api/v3/file_links/file_link_collection_representer.rb
+++ b/modules/storages/lib/api/v3/file_links/file_link_collection_representer.rb
@@ -29,7 +29,8 @@
module API
module V3
module FileLinks
- class FileLinkCollectionRepresenter < ::API::Decorators::UnpaginatedCollection
+ class FileLinkCollectionRepresenter < ::API::Decorators::OffsetPaginatedCollection
+ property :count, getter: ->(*) { count(:id) }
end
end
end
diff --git a/modules/storages/lib/api/v3/file_links/file_link_representer.rb b/modules/storages/lib/api/v3/file_links/file_link_representer.rb
index 28269707da6..5d792d40334 100644
--- a/modules/storages/lib/api/v3/file_links/file_link_representer.rb
+++ b/modules/storages/lib/api/v3/file_links/file_link_representer.rb
@@ -91,7 +91,7 @@ module API::V3::FileLinks
link :status, uncacheable: true do
next if represented.origin_status.nil?
- PERMISSION_LINKS[represented.origin_status]
+ PERMISSION_LINKS[represented.origin_status.to_sym]
end
link :staticOriginOpen do
diff --git a/modules/storages/lib/api/v3/file_links/work_packages_file_links_api.rb b/modules/storages/lib/api/v3/file_links/work_packages_file_links_api.rb
index 488ab74bc69..0ac11d3f4a3 100644
--- a/modules/storages/lib/api/v3/file_links/work_packages_file_links_api.rb
+++ b/modules/storages/lib/api/v3/file_links/work_packages_file_links_api.rb
@@ -29,7 +29,27 @@
#++
class API::V3::FileLinks::WorkPackagesFileLinksAPI < API::OpenProjectAPI
- # The `:resources` keyword defines the API namespace -> /api/v3/work_packages/:id/file_links/...
+ helpers do
+ def sync_and_convert_relation(file_links)
+ sync_result = ::Storages::FileLinkSyncService
+ .new(user: current_user)
+ .call(file_links)
+ .result
+
+ value_list = sync_result
+ .map { |file_link| "(#{file_link.id},'#{file_link.origin_status}')" }
+ .join(",")
+
+ origin_status_attribute = <<-SQL.squish
+ LEFT JOIN (VALUES #{value_list}) AS origin_status (id,status) ON origin_status.id = file_links.id
+ SQL
+
+ ::Storages::FileLink.where(id: sync_result.map(&:id))
+ .joins(origin_status_attribute)
+ .select("file_links.*, origin_status.status AS origin_status")
+ end
+ end
+
resources :file_links do
get do
query = ParamsToQueryService
@@ -43,19 +63,23 @@ class API::V3::FileLinks::WorkPackagesFileLinksAPI < API::OpenProjectAPI
raise ::API::Errors::InvalidQuery.new(message)
end
- result = if current_user.allowed_in_project?(:view_file_links, @work_package.project)
- file_links = query.results.where(container_id: @work_package.id,
- container_type: "WorkPackage",
- storage: @work_package.project.storages)
- ::Storages::FileLinkSyncService
- .new(user: current_user)
- .call(file_links)
- .result
- else
- []
- end
+ relation = if current_user.allowed_in_project?(:view_file_links, @work_package.project)
+ file_links = query.results.where(container_id: @work_package.id,
+ container_type: "WorkPackage",
+ storage: @work_package.project.storages)
+
+ if params[:pageSize] == "0"
+ file_links
+ else
+ sync_and_convert_relation(file_links)
+ end
+ else
+ ::Storages::FileLink.none
+ end
+
::API::V3::FileLinks::FileLinkCollectionRepresenter.new(
- result,
+ relation,
+ per_page: params[:pageSize],
self_link: api_v3_paths.file_links(@work_package.id),
current_user:
)