Skip to content

Self-hosted project management for devs and their clients: tickets, time tracking, docs and meetings, with an AI assistant that cites its sources. AGPL-3.0.

Serialize meeting moves with links, bookings and a second move

Guido Boyked708486b2ac5
diff --git a/app/controllers/organisations/meeting_ticket_links_controller.rb b/app/controllers/organisations/meeting_ticket_links_controller.rb
index 84ec80b..0217a61 100644
--- a/app/controllers/organisations/meeting_ticket_links_controller.rb
+++ b/app/controllers/organisations/meeting_ticket_links_controller.rb
@@ -34,13 +34,22 @@ module Organisations
# covers the normal "clicked twice" case; the rescue is the race backstop.
return respond_link_result(:alert, 'flash.meeting_ticket_links.already_linked') if @meeting.outgoing_references.exists?(target: ticket)
- @meeting.outgoing_references.create!(
- organisation: @current_organisation,
- target: ticket,
- # Snapshot the live title for the tombstone shown after a target delete.
- # Truncate: the snapshot column is capped at 255 and must never fail the save.
- target_title_snapshot: ticket.display_title&.truncate(255)
- )
+ # The meeting's row lock, the one Actors::MoveMeeting holds: a move that
+ # committed while this waited has taken the meeting into another project,
+ # and the ticket of this one may not follow it there (404, as for a
+ # foreign meeting).
+ Meeting.transaction do
+ @meeting.lock!
+ raise ActiveRecord::RecordNotFound unless @meeting.project_id == @project.id
+
+ @meeting.outgoing_references.create!(
+ organisation: @current_organisation,
+ target: ticket,
+ # Snapshot the live title for the tombstone shown after a target delete.
+ # Truncate: the snapshot column is capped at 255 and must never fail the save.
+ target_title_snapshot: ticket.display_title&.truncate(255)
+ )
+ end
respond_link_result(:notice, 'flash.meeting_ticket_links.created')
rescue ActiveRecord::RecordNotUnique
respond_link_result(:alert, 'flash.meeting_ticket_links.already_linked')
diff --git a/app/services/actors/move_meeting.rb b/app/services/actors/move_meeting.rb
index 94c179b..f4f8388 100644
--- a/app/services/actors/move_meeting.rb
+++ b/app/services/actors/move_meeting.rb
@@ -39,8 +39,9 @@ module Actors
def perform
guard_target!
- # Row lock first, as AcceptTicketSuggestion does: a link or a run created
- # concurrently waits for the move instead of slipping past the guards.
+ # Row lock first. Everything that ties the meeting to its project takes the
+ # same lock and looks again (AcceptTicketSuggestion, a manual ticket link,
+ # SaveTimeSession), so none of them lands in the old project after a move.
@meeting.lock!
guard_movable!
@@ -51,6 +52,7 @@ module Actors
Notification.where(notifiable: @meeting).update_all(project_id: @target_project.id)
@meeting.meeting_ticket_suggestions.open
.update_all(status: MeetingTicketSuggestion.statuses[:dismissed], updated_at: Time.current)
+ carry_meeting_citations
@source_project.track_meeting_moved_out(@meeting, @target_project)
@target_project.track_meeting_moved_in(@meeting, @source_project)
@@ -71,7 +73,10 @@ module Actors
refuse!(:forbidden_target)
end
+ # lock! reloads the row: a move that committed while this one waited has
+ # taken the meeting out of the project this move was checked against.
def guard_movable!
+ refuse!(:moved_meanwhile) if @meeting.project_id != @source_project.id
refuse!(:has_ticket_links) if ticket_links.exists?
refuse!(:suggestion_run_active) if suggestion_run_active?
end
@@ -87,6 +92,26 @@ module Actors
MeetingTicketSuggestionRun.where(meeting_id: @meeting.id).any? { |run| run.active? && !run.stale? }
end
+ # A saved analysis may be published when its sources are this meeting
+ # (Assistant::AnalysisSources.publishable?), and a meeting citation names
+ # the project it was read in. The meeting is the same one after the move,
+ # so its citations follow it; every other source keeps its project and
+ # stays a reason not to publish (a chat narrowed to the source project too).
+ def carry_meeting_citations
+ @meeting.meeting_analyses.where.not(source_context: nil).find_each do |analysis|
+ provenance = analysis.source_context['provenance']
+ next unless provenance.is_a?(Array)
+
+ carried = provenance.map do |citation|
+ own = citation.is_a?(Hash) && citation['type'] == 'meeting' && citation['id'] == @meeting.id
+ own ? citation.merge('project_id' => @target_project.id) : citation
+ end
+ next if carried == provenance
+
+ analysis.update_columns(source_context: analysis.source_context.merge('provenance' => carried))
+ end
+ end
+
def refuse!(code)
raise ActorError.new(I18n.t("services.actors.move_meeting.#{code}"), code: code)
end
diff --git a/app/services/actors/save_time_session.rb b/app/services/actors/save_time_session.rb
index ea4f8bc..102b7b2 100644
--- a/app/services/actors/save_time_session.rb
+++ b/app/services/actors/save_time_session.rb
@@ -36,6 +36,7 @@ module Actors
def perform
refresh_persisted_state
+ guard_meeting_project!
guard_group_transition!
@time_session.assign_attributes(@attributes)
apply_group_transition
@@ -58,6 +59,20 @@ module Actors
@time_session.assign_attributes(pending)
end
+ # A booking on a meeting takes the meeting's row lock, the one
+ # Actors::MoveMeeting holds while it moves the meeting and its bookings. A
+ # move that committed meanwhile has taken the meeting into another project;
+ # the booking would land in the old one, so it is refused.
+ def guard_meeting_project!
+ meeting = @time_session.meeting
+ return unless meeting
+
+ meeting.lock!
+ return if meeting.project_id == @time_session.project_id
+
+ raise ActorError.new(I18n.t("#{ERROR_SCOPE}.meeting_moved"), code: :meeting_moved)
+ end
+
def target_group
@group_change[:group]
end
diff --git a/config/locales/services.de.yml b/config/locales/services.de.yml
index cd9b8b3..1895792 100644
--- a/config/locales/services.de.yml
+++ b/config/locales/services.de.yml
@@ -51,6 +51,7 @@ de:
same_project: "Das Meeting liegt schon in diesem Projekt."
forbidden_target: "In diesem Projekt darfst du keine Meetings anlegen."
has_ticket_links: "Das Meeting ist mit Tickets verknüpft. Entferne die Verknüpfungen, dann kannst du es verschieben."
+ moved_meanwhile: "Das Meeting wurde gerade schon verschoben. Lade die Seite neu."
suggestion_run_active: "Für das Meeting werden gerade Ticket-Vorschläge erzeugt. Versuche es gleich noch einmal."
move_ticket:
number_conflict: "Nummernkonflikt nach %{retries} Versuchen: %{error}"
@@ -84,6 +85,7 @@ de:
group_billed: "Die gewählte Abrechnung ist bereits abgerechnet — die Zeit-Session kann ihr nicht mehr zugeordnet werden."
session_billed: "Diese Zeit-Session ist bereits abgerechnet und kann nicht mehr geändert oder aus ihrer Abrechnung entfernt werden."
generic_error: "Die Zeit-Session konnte nicht gespeichert werden. Bitte versuchen Sie es später erneut."
+ meeting_moved: "Das Meeting liegt inzwischen in einem anderen Projekt. Bitte buchen Sie die Zeit dort."
bulk_customer_description_generator:
empty_result: "Zeit-Session %{id}: Die KI hat keine Beschreibung geliefert."
embedding_reprocessing:
diff --git a/config/locales/services.en.yml b/config/locales/services.en.yml
index 3750c04..184abf8 100644
--- a/config/locales/services.en.yml
+++ b/config/locales/services.en.yml
@@ -51,6 +51,7 @@ en:
same_project: "The meeting is already in this project."
forbidden_target: "You may not add meetings to this project."
has_ticket_links: "The meeting is linked to tickets. Remove the links, then you can move it."
+ moved_meanwhile: "The meeting was just moved already. Reload the page."
suggestion_run_active: "Ticket suggestions are being generated for this meeting. Try again in a moment."
move_ticket:
number_conflict: "Number conflict after %{retries} retries: %{error}"
@@ -84,6 +85,7 @@ en:
group_billed: "The selected billing group is already invoiced — the time session can no longer be assigned to it."
session_billed: "This time session is already invoiced and can no longer be changed or removed from its billing group."
generic_error: "The time session could not be saved. Please try again later."
+ meeting_moved: "The meeting is in another project now. Please book the time there."
bulk_customer_description_generator:
empty_result: "Time session %{id}: The AI returned no description."
embedding_reprocessing:
diff --git a/spec/requests/organisations/meeting_ticket_links_spec.rb b/spec/requests/organisations/meeting_ticket_links_spec.rb
index 43d269f..233c90f 100644
--- a/spec/requests/organisations/meeting_ticket_links_spec.rb
+++ b/spec/requests/organisations/meeting_ticket_links_spec.rb
@@ -54,6 +54,23 @@ RSpec.describe 'Organisations::MeetingTicketLinks', type: :request do
end
describe 'POST create' do
+ # A move of the meeting (Actors::MoveMeeting) refuses a meeting with links,
+ # under the meeting's row lock. A link has to take the same lock and look
+ # again: a move that committed while it waited has taken the meeting into
+ # another project, and the ticket stays behind in this one.
+ it 'links nothing when the meeting moved away while the link waited for its lock' do
+ elsewhere = ActsAsTenant.with_tenant(organisation) { create(:project, organisation: organisation) }
+ ticket
+ allow_any_instance_of(Meeting).to receive(:lock!).and_wrap_original do |original, *args|
+ Meeting.where(id: original.receiver.id).update_all(project_id: elsewhere.id)
+ original.call(*args)
+ end
+ sign_in writer
+
+ expect { post create_path, params: { ticket_id: ticket.id } }.not_to(change { reference_count })
+ expect(response).to have_http_status(:not_found)
+ end
+
it 'links a ticket for a meetings writer, snapshotting its title' do
sign_in writer
expect { post create_path, params: { ticket_id: ticket.id } }.to change { reference_count }.by(1)
diff --git a/spec/services/actors/move_meeting_spec.rb b/spec/services/actors/move_meeting_spec.rb
index f2f8de8..5fa0138 100644
--- a/spec/services/actors/move_meeting_spec.rb
+++ b/spec/services/actors/move_meeting_spec.rb
@@ -75,6 +75,25 @@ RSpec.describe Actors::MoveMeeting, type: :service do
expect(moved_in.parameters).to include('meeting_title' => meeting.title, 'from_project_id' => source_project.id)
end
+ it 'keeps a saved analysis publishable when its only source is this meeting' do
+ own = create(:meeting_analysis, meeting: meeting, organisation: organisation, creator: admin,
+ source_context: { 'provenance' => [
+ { 'type' => 'meeting', 'id' => meeting.id, 'project_id' => source_project.id }
+ ] })
+ scoped = create(:meeting_analysis, meeting: meeting, organisation: organisation, creator: admin,
+ source_context: { 'provenance' => [
+ { 'type' => 'meeting', 'id' => meeting.id, 'project_id' => source_project.id },
+ { 'type' => 'project', 'id' => source_project.id }
+ ] })
+
+ move
+
+ expect(own.reload).to be_sources_publishable
+ expect(own.source_context['provenance'].first['project_id']).to eq(target_project.id)
+ # The chat was narrowed to the source project: that stays a foreign source.
+ expect(scoped.reload).not_to be_sources_publishable
+ end
+
describe '#preflight' do
it 'names the booked time, the billed part and the open suggestions' do
create(:time_session, project: source_project, organisation: organisation, meeting: meeting)
@@ -129,6 +148,26 @@ RSpec.describe Actors::MoveMeeting, type: :service do
expect(move).to be_success
end
+ # Two moves of the same meeting race: the second passed its checks against
+ # the project the meeting was in, then waited for the row lock while the
+ # first moved it. It must not move the meeting out of a project it was
+ # never checked against.
+ it 'refuses when the meeting moved while the move waited for its lock' do
+ elsewhere = create(:project, organisation: organisation)
+ allow(meeting).to receive(:lock!).and_wrap_original do |original, *args|
+ Meeting.where(id: meeting.id).update_all(project_id: elsewhere.id)
+ original.call(*args)
+ end
+
+ result = move
+
+ expect(result).not_to be_success
+ expect(result.code).to eq(:moved_meanwhile)
+ # The stand-in for the other move ran inside this transaction and rolled
+ # back with the refusal; what counts is that this move wrote nothing.
+ expect(meeting.reload.project_id).not_to eq(target_project.id)
+ end
+
it 'refuses the project the meeting is already in' do
expect_refusal(move(target: source_project), :same_project)
end
diff --git a/spec/services/actors/save_time_session_spec.rb b/spec/services/actors/save_time_session_spec.rb
index 2db4298..0c209ce 100644
--- a/spec/services/actors/save_time_session_spec.rb
+++ b/spec/services/actors/save_time_session_spec.rb
@@ -40,6 +40,32 @@ RSpec.describe Actors::SaveTimeSession, type: :service do
end
describe 'create' do
+ # A booking on a meeting takes the meeting's row lock, the one
+ # Actors::MoveMeeting holds: a move that committed while the booking waited
+ # has taken the meeting into another project, and the booking must not land
+ # in the old one.
+ it 'refuses a booking on a meeting that is no longer in the booking project' do
+ meeting = create(:meeting, project: project, organisation: organisation, creator: user)
+ elsewhere = create(:project, organisation: organisation)
+ session = build_session(meeting: meeting)
+ allow(meeting).to receive(:lock!).and_wrap_original do |original, *args|
+ Meeting.where(id: meeting.id).update_all(project_id: elsewhere.id)
+ original.call(*args)
+ end
+
+ result = call_actor(session)
+
+ expect(result).not_to be_success
+ expect(result.code).to eq(:meeting_moved)
+ expect(session).not_to be_persisted
+ end
+
+ it 'books on a meeting of the booking project' do
+ meeting = create(:meeting, project: project, organisation: organisation, creator: user)
+
+ expect(call_actor(build_session(meeting: meeting))).to be_success
+ end
+
it 'saves a new record and assigns the requested unbilled group without billing it' do
group = create(:time_session_group, organisation: organisation, creator: user)
session = build_session