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

bug/63545 When editing a comment in the activity tab and polling occurs, the form can be force closed (#19507)

Kabiru Mwenja33d3953701ac
diff --git a/app/components/work_packages/activities_tab/journals/item_component/edit.html.erb b/app/components/work_packages/activities_tab/journals/item_component/edit.html.erb
index bb97da905c3..db7035d8e0c 100644
--- a/app/components/work_packages/activities_tab/journals/item_component/edit.html.erb
+++ b/app/components/work_packages/activities_tab/journals/item_component/edit.html.erb
@@ -5,7 +5,13 @@
id: "work-package-journal-form-element", # required for specs
model: journal,
method: :put,
- data: { turbo: true, turbo_stream: true, test_selector: "op-work-package-journal-form-element" },
+ data: {
+ turbo: true,
+ turbo_stream: true,
+ test_selector: "op-work-package-journal-form-element",
+ "work-packages--activities-tab--index-target": "editForm",
+ journal_id: journal.id
+ },
url: work_package_activity_path(work_package_id: work_package.id, id: journal.id, filter:)
) do |f|
flex_layout do |form_container|
diff --git a/app/controllers/work_packages/activities_tab_controller.rb b/app/controllers/work_packages/activities_tab_controller.rb
index 5a37b3f36b6..7a538028f36 100644
--- a/app/controllers/work_packages/activities_tab_controller.rb
+++ b/app/controllers/work_packages/activities_tab_controller.rb
@@ -254,10 +254,11 @@ class WorkPackages::ActivitiesTabController < ApplicationController
def perform_update_streams_from_last_update_timestamp
last_update_timestamp = params[:last_update_timestamp] || params.dig(:journal, :last_update_timestamp)
+ editing_journals = params[:editing_journals]&.split(",")&.map(&:to_i) || []
if last_update_timestamp.present?
last_updated_at = Time.zone.parse(last_update_timestamp)
- generate_time_based_update_streams(last_updated_at)
+ generate_time_based_update_streams(last_updated_at, editing_journals)
generate_work_package_journals_emoji_reactions_update_streams
else
@turbo_status = :bad_request
@@ -322,7 +323,7 @@ class WorkPackages::ActivitiesTabController < ApplicationController
Journals::UpdateService.new(model: @journal, user: User.current).call(notes:)
end
- def generate_time_based_update_streams(last_update_timestamp)
+ def generate_time_based_update_streams(last_update_timestamp, editing_journals)
journals = @work_package
.journals
.internal_visible
@@ -336,8 +337,8 @@ class WorkPackages::ActivitiesTabController < ApplicationController
reactable_id: journals.pluck(:id), reactable_type: "Journal"
)
- rerender_updated_journals(journals, last_update_timestamp, grouped_emoji_reactions)
- rerender_journals_with_updated_notification(journals, last_update_timestamp, grouped_emoji_reactions)
+ rerender_updated_journals(journals, last_update_timestamp, grouped_emoji_reactions, editing_journals)
+ rerender_journals_with_updated_notification(journals, last_update_timestamp, grouped_emoji_reactions, editing_journals)
append_or_prepend_journals(journals, last_update_timestamp, grouped_emoji_reactions)
if journals.present?
@@ -359,13 +360,15 @@ class WorkPackages::ActivitiesTabController < ApplicationController
end
end
- def rerender_updated_journals(journals, last_update_timestamp, grouped_emoji_reactions)
+ def rerender_updated_journals(journals, last_update_timestamp, grouped_emoji_reactions, editing_journals)
journals.where("updated_at > ?", last_update_timestamp).find_each do |journal|
+ next if editing_journals.include?(journal.id)
+
update_item_show_component(journal:, grouped_emoji_reactions: grouped_emoji_reactions.fetch(journal.id, {}))
end
end
- def rerender_journals_with_updated_notification(journals, last_update_timestamp, grouped_emoji_reactions)
+ def rerender_journals_with_updated_notification(journals, last_update_timestamp, grouped_emoji_reactions, editing_journals)
# Case: the user marked the journal as read somewhere else and expects the bubble to disappear
#
# below code stopped working with the introduction of the sequence_version query
@@ -392,6 +395,8 @@ class WorkPackages::ActivitiesTabController < ApplicationController
.where(recipient_id: User.current.id)
.where("notifications.updated_at > ?", last_update_timestamp)
.find_each do |notification|
+ next if editing_journals.include?(notification.journal_id)
+
update_item_show_component(
journal: journals.find(notification.journal_id), # take the journal from the journals querried with sequence_version!
grouped_emoji_reactions: grouped_emoji_reactions.fetch(notification.journal_id, {})
diff --git a/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/index.controller.ts b/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/index.controller.ts
index f65fe36a5c8..c702901ef3b 100644
--- a/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/index.controller.ts
+++ b/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/index.controller.ts
@@ -61,12 +61,13 @@ export default class IndexController extends Controller {
unsavedChangesConfirmationMessage: String,
};
- static targets = ['journalsContainer', 'buttonRow', 'formRow', 'form', 'formSubmitButton', 'reactionButton'];
+ static targets = ['journalsContainer', 'buttonRow', 'formRow', 'form', 'editForm', 'formSubmitButton', 'reactionButton'];
declare readonly journalsContainerTarget:HTMLElement;
declare readonly buttonRowTarget:HTMLInputElement;
declare readonly formRowTarget:HTMLElement;
declare readonly formTarget:HTMLFormElement;
+ declare readonly editFormTargets:HTMLFormElement[];
declare readonly formSubmitButtonTarget:HTMLButtonElement;
declare readonly reactionButtonTargets:HTMLElement[];
@@ -226,13 +227,15 @@ export default class IndexController extends Controller {
this.updateInProgress = true;
+ const editingJournals = this.captureEditingJournals();
+
// Unfocus any reaction buttons that may have been focused
// otherwise the browser will perform an auto scroll to the before focused button after the stream update was applied
this.unfocusReactionButtons();
const journalsContainerAtBottom = this.isJournalsContainerScrolledToBottom();
- void this.performUpdateStreamsRequest(this.prepareUpdateStreamsUrl())
+ void this.performUpdateStreamsRequest(this.prepareUpdateStreamsUrl(editingJournals))
.then(({ html, headers }) => {
this.handleUpdateStreamsResponse(html, headers, journalsContainerAtBottom);
}).catch((error) => {
@@ -242,16 +245,34 @@ export default class IndexController extends Controller {
});
}
+ private captureEditingJournals():Set<string> {
+ const editingJournals = new Set<string>();
+
+ const editForms = this.editFormTargets;
+ editForms.forEach((form) => {
+ const journalId = form.dataset.journalId;
+ if (journalId) { editingJournals.add(journalId); }
+ });
+
+ return editingJournals;
+ }
+
private unfocusReactionButtons() {
this.reactionButtonTargets.forEach((button) => button.blur());
}
- private prepareUpdateStreamsUrl():string {
+ private prepareUpdateStreamsUrl(editingJournals:Set<string>):string {
const baseUrl = window.location.origin;
const url = new URL(this.updateStreamsPathValue, baseUrl);
+
url.searchParams.set('sortBy', this.sortingValue);
url.searchParams.set('filter', this.filterValue);
url.searchParams.set('last_update_timestamp', this.lastServerTimestampValue);
+
+ if (editingJournals.size > 0) {
+ url.searchParams.set('editing_journals', Array.from(editingJournals).join(','));
+ }
+
return url.toString();
}
diff --git a/spec/features/activities/work_package/activities_spec.rb b/spec/features/activities/work_package/activities_spec.rb
index 96a1c55094c..8b3536542be 100644
--- a/spec/features/activities/work_package/activities_spec.rb
+++ b/spec/features/activities/work_package/activities_spec.rb
@@ -839,14 +839,14 @@ RSpec.describe "Work package activity", :js, :with_cuprite do
create(:work_package_journal, user: member, notes: "First comment by member", journable: work_package, version: 3)
end
- context "when admin is visiting the work package" do
- current_user { admin }
+ current_user { admin }
- before do
- wp_page.visit!
- wp_page.wait_for_activity_tab
- end
+ before do
+ wp_page.visit!
+ wp_page.wait_for_activity_tab
+ end
+ context "when admin is visiting the work package" do
it "can edit own comments" do
# edit own comment
activity_tab.edit_comment(first_comment_by_admin, text: "First comment by admin edited")
@@ -864,6 +864,19 @@ RSpec.describe "Work package activity", :js, :with_cuprite do
end
end
end
+
+ context "when editing a comment included in the polling update" do
+ it "preserves the edit state" do
+ activity_tab.type_comment_in_edit(first_comment_by_admin, "Editing comment")
+ first_comment_by_admin.update_column(:updated_at, Time.current)
+
+ activity_tab.trigger_update_streams_poll
+
+ activity_tab.within_journal_entry(first_comment_by_admin) do
+ activity_tab.expect_journal_notes(text: "Editing comment")
+ end
+ end
+ end
end
describe "quote comments" do
diff --git a/spec/support/components/work_packages/activities.rb b/spec/support/components/work_packages/activities.rb
index 7d76f358b41..54ffd22c9eb 100644
--- a/spec/support/components/work_packages/activities.rb
+++ b/spec/support/components/work_packages/activities.rb
@@ -397,6 +397,16 @@ module Components
wait_for { page }.to have_test_selector("op-wp-journals-#{default_filter}-#{sorting}")
end
+
+ def trigger_update_streams_poll
+ page.execute_script(<<~JS)
+ var target = document.querySelector('[data-controller*="work-packages--activities-tab--index"]')
+ var controller = window.Stimulus.getControllerForElementAndIdentifier(target, 'work-packages--activities-tab--index')
+ controller.updateActivitiesList();
+ JS
+
+ wait_for_network_idle
+ end
end
end
end