Files
openproject/modules/wikis/app/services/wikis/page_link_metadata_service.rb
T
Marcello Rocha d03d22edc8 Implements the GET /work_packages/:id/wiki_page_links endpoint (#22834)
* Introduce the PageLinkQuery
* Update page link query and some copyright
* Adds PageLinkCollectionRepresenter
* Introduces the endpoint /work_packages/id/wiki_page_links
* MetadaService tests, most of it is a work of fiction
* Adds urns for page link type improve some tests
* Removes mentions to view_wiki_page_links permissions
* Adds a test for multiple providers and same identifiers

---

Co-authored-by: Jan Sandbrink <j.sandbrink@openproject.com>
2026-05-08 17:38:52 +02:00

75 lines
2.7 KiB
Ruby

# frozen_string_literal: true
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
#++
module Wikis
class PageLinkMetadataService
# @param page_links [ActiveRecord::Relation<Wikis::PageLink>]
def initialize(page_links)
@result = ServiceResult.success(errors: ActiveModel::Errors.new(self))
@relation = page_links
end
# @return [ServiceResult<ActiveRecord::Relation<Wikis::PageLink>]
def call
metadata = relation.group_by(&:provider).flat_map do |provider, page_links|
build_inputs(page_links).filter_map do |input_data|
provider.resolve("queries.page_info").call(input_data).value_or(nil)
end
end
@result.result = enrich_models(metadata)
@result
end
private
attr_reader :relation
def build_inputs(page_links)
page_links.filter_map do |page_link|
Adapters::Input::PageInfo.build(identifier: page_link.identifier).value_or(nil)
end
end
def enrich_models(metadata)
identifier_title_map = metadata.map { [it.identifier, it.title, it.provider.id] }
variable_placeholders = Array.new(identifier_title_map.size, "(?,?,?)").join(",")
join_string = <<~SQL.squish
LEFT JOIN (VALUES #{variable_placeholders}) AS metadata(identifier, title, provider_id)
ON metadata.identifier = wiki_page_links.identifier AND metadata.provider_id = wiki_page_links.provider_id
SQL
join_expression = ActiveRecord::Base.sanitize_sql_array([join_string, *identifier_title_map.flatten])
relation.joins(join_expression).select("wiki_page_links.*, metadata.title as title")
end
end
end