|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +#-- copyright |
| 4 | +# OpenProject is an open source project management software. |
| 5 | +# Copyright (C) the OpenProject GmbH |
| 6 | +# |
| 7 | +# This program is free software; you can redistribute it and/or |
| 8 | +# modify it under the terms of the GNU General Public License version 3. |
| 9 | +# |
| 10 | +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: |
| 11 | +# Copyright (C) 2006-2013 Jean-Philippe Lang |
| 12 | +# Copyright (C) 2010-2013 the ChiliProject Team |
| 13 | +# |
| 14 | +# This program is free software; you can redistribute it and/or |
| 15 | +# modify it under the terms of the GNU General Public License |
| 16 | +# as published by the Free Software Foundation; either version 2 |
| 17 | +# of the License, or (at your option) any later version. |
| 18 | +# |
| 19 | +# This program is distributed in the hope that it will be useful, |
| 20 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | +# GNU General Public License for more details. |
| 23 | +# |
| 24 | +# You should have received a copy of the GNU General Public License |
| 25 | +# along with this program; if not, write to the Free Software |
| 26 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 27 | +# |
| 28 | +# See COPYRIGHT and LICENSE files for more details. |
| 29 | +#++ |
| 30 | + |
| 31 | +class WorkPackages::ActivitiesTab::Paginator |
| 32 | + include Pagy::Backend |
| 33 | + include WorkPackages::ActivitiesTab::JournalSortingInquirable |
| 34 | + |
| 35 | + def self.paginate(work_package, params = {}) |
| 36 | + new(work_package, params).call |
| 37 | + end |
| 38 | + |
| 39 | + def initialize(work_package, params = {}) |
| 40 | + @work_package = work_package |
| 41 | + @params = params |
| 42 | + end |
| 43 | + |
| 44 | + def call |
| 45 | + anchor_type, target_record_id = extract_target_record_id |
| 46 | + |
| 47 | + pagy, records = |
| 48 | + if anchor_type && target_record_id |
| 49 | + pagy_array_for_target_journal(anchor_type, target_record_id) |
| 50 | + else |
| 51 | + pagy_array(base_journals) |
| 52 | + end |
| 53 | + |
| 54 | + # For UI display: if user wants "oldest first" UI, reverse the array |
| 55 | + records = records.reverse if journal_sorting.asc? |
| 56 | + |
| 57 | + [pagy, records] |
| 58 | + end |
| 59 | + |
| 60 | + private |
| 61 | + |
| 62 | + attr_reader :work_package, :params |
| 63 | + |
| 64 | + def extract_target_record_id |
| 65 | + anchor = params[:anchor] # e.g., "comment-78758" (without #) |
| 66 | + return nil unless anchor |
| 67 | + |
| 68 | + match = anchor.match(/^(comment|activity)-(\d+)$/) |
| 69 | + match && match.length == 3 ? [match[1].inquiry, match[2].to_i] : [] |
| 70 | + end |
| 71 | + |
| 72 | + def pagy_array_for_target_journal(anchor_type, target_record_id) |
| 73 | + journals = base_journals |
| 74 | + |
| 75 | + target_index = journals.find_index do |record| |
| 76 | + if anchor_type.comment? |
| 77 | + record.id == target_record_id |
| 78 | + elsif anchor_type.activity? |
| 79 | + record.sequence_version == target_record_id |
| 80 | + else |
| 81 | + false |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + if target_index |
| 86 | + target_page = (target_index / Pagy::DEFAULT[:limit]) + 1 |
| 87 | + pagy_array(journals, page: target_page) |
| 88 | + else |
| 89 | + # Journal might be filtered out or deleted - fallback to page 1 |
| 90 | + pagy_array(journals, page: 1) |
| 91 | + end |
| 92 | + end |
| 93 | + |
| 94 | + def base_journals |
| 95 | + combine_and_sort_records(fetch_journals, fetch_revisions) |
| 96 | + end |
| 97 | + |
| 98 | + def fetch_journals |
| 99 | + API::V3::Activities::ActivityEagerLoadingWrapper.wrap(fetch_ar_journals) |
| 100 | + end |
| 101 | + |
| 102 | + def fetch_ar_journals |
| 103 | + work_package |
| 104 | + .journals |
| 105 | + .internal_visible |
| 106 | + .includes(:user, :customizable_journals, :attachable_journals, :storable_journals, :notifications) |
| 107 | + .reorder(version: :desc) # Always fetch newest first for pagination |
| 108 | + .with_sequence_version |
| 109 | + end |
| 110 | + |
| 111 | + def fetch_revisions |
| 112 | + work_package.changesets.includes(:user, :repository) |
| 113 | + end |
| 114 | + |
| 115 | + def combine_and_sort_records(journals, revisions) |
| 116 | + (journals + revisions).sort_by do |record| |
| 117 | + timestamp = record_timestamp(record) |
| 118 | + [-timestamp, -record.id] # Always sort DESC (newest first) |
| 119 | + end |
| 120 | + end |
| 121 | + |
| 122 | + def record_timestamp(record) |
| 123 | + if record.is_a?(API::V3::Activities::ActivityEagerLoadingWrapper) |
| 124 | + record.created_at&.to_i |
| 125 | + elsif record.is_a?(Changeset) |
| 126 | + record.committed_on.to_i |
| 127 | + end |
| 128 | + end |
| 129 | +end |
0 commit comments