From da59dea46f55c64a3090b83a6321fbecd2ee6f60 Mon Sep 17 00:00:00 2001 From: erdgeist Date: Sun, 2 Aug 2026 22:45:51 +0200 Subject: Witness calendar entries --- app/controllers/events_controller.rb | 6 +- app/helpers/node_actions_helper.rb | 87 ++++++++++++++++++++++++- app/models/event.rb | 77 ++++++++++++++++++++++ app/models/node_action.rb | 30 +++++++++ app/views/node_actions/_change_details.html.erb | 6 ++ 5 files changed, 202 insertions(+), 4 deletions(-) (limited to 'app') diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index a322181d..818459c4 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -65,7 +65,7 @@ class EventsController < ApplicationController @event = Event.new(event_params) respond_to do |format| - if @event.save + if @event.save_witnessed(:actor => current_user) flash[:notice] = t("flash.events.created") format.html { redirect_to(safe_return_to(params[:return_to] || (@event.node ? edit_node_path(@event.node) : edit_event_path(@event)))) } format.xml { render :xml => @event, :status => :created, :location => @event } @@ -82,7 +82,7 @@ class EventsController < ApplicationController @event = Event.find(params[:id]) respond_to do |format| - if @event.update(event_params) + if @event.update_witnessed(event_params, :actor => current_user) flash[:notice] = t("flash.events.updated") format.html { redirect_to(safe_return_to(params[:return_to] || events_path)) } format.xml { head :ok } @@ -97,7 +97,7 @@ class EventsController < ApplicationController # DELETE /events/1.xml def destroy @event = Event.find(params[:id]) - @event.destroy + @event.destroy_witnessed(:actor => current_user) respond_to do |format| format.html { redirect_to(events_url) } diff --git a/app/helpers/node_actions_helper.rb b/app/helpers/node_actions_helper.rb index 019a9cf7..672dda36 100644 --- a/app/helpers/node_actions_helper.rb +++ b/app/helpers/node_actions_helper.rb @@ -23,7 +23,10 @@ module NodeActionsHelper "user_deactivate" => "user-off", "user_reactivate" => "user-check", "redaktion_grant" => "users-plus", - "redaktion_revoke" => "users-minus" + "redaktion_revoke" => "users-minus", + "event_create" => "calendar-plus", + "event_update" => "calendar-event", + "event_destroy" => "calendar-x" }.freeze def verb_icon action @@ -57,6 +60,7 @@ module NodeActionsHelper def action_details? action m = action.metadata return true if m["translation_diff"].present? + return true if m["changes"].present? || m["description_changed"] return true if m["title"].is_a?(Hash) && m.dig("title", "from") != m.dig("title", "to") %w[author tags template_changed assets assets_changed assets_reordered abstract_changed body_changed].any? { |key| m[key].present? } @@ -92,6 +96,59 @@ module NodeActionsHelper items end + def event_changes_list action + m = action.metadata + c = m["changes"] || {} + items = [] + + if c["title"] + items << t("node_actions.detail_title", + :from => c.dig("title", "from"), :to => c.dig("title", "to")) + end + + if %w[rrule start_time end_time].any? { |field| c.key?(field) } + items << t("node_actions.detail_event_schedule", + :from => event_schedule_side(m, c, "from"), + :to => event_schedule_side(m, c, "to")) + end + + if c["allday"] + items << t("node_actions.detail_event_allday", + :from => t("admin.common.#{c.dig("allday", "from") ? "yes" : "no"}"), + :to => t("admin.common.#{c.dig("allday", "to") ? "yes" : "no"}")) + end + + %w[location url].each do |field| + next unless c[field] + items << t("node_actions.detail_event_#{field}", + :from => c.dig(field, "from").presence || t("node_actions.event_none"), + :to => c.dig(field, "to").presence || t("node_actions.event_none")) + end + + if c["tags"] + items << t("node_actions.detail_tags", + :from => Array(c.dig("tags", "from")).join(", "), + :to => Array(c.dig("tags", "to")).join(", ")) + end + + if c["node_path"] + items << t("node_actions.detail_event_moved", + :from => c.dig("node_path", "from") || t("node_actions.event_none"), + :to => c.dig("node_path", "to") || t("node_actions.event_none")) + end + + items << t("node_actions.detail_event_description") if m["description_changed"] + items << t("node_actions.detail_event_coordinates") if c["latitude"] || c["longitude"] + items + end + + def event_schedule_side metadata, changes, side + attributes = %w[rrule start_time end_time].each_with_object({}) do |field, acc| + acc[field.to_sym] = changes.key?(field) ? changes.dig(field, side) : metadata[field] + end + event_schedule_text(Event.new(attributes)).presence || t("node_actions.event_none") + end + def translation_changes diff case diff["status"] when "added" then [t("node_actions.locale_added", :title => diff.dig("title", "to"))] @@ -188,6 +245,12 @@ module NodeActionsHelper participant ? link_to(name, edit_user_path(participant)) : name end + def event_ref action + event = action.action_participants.detect { |p| p.subject_type == "Event" }&.subject + name = h(action.metadata["event_title"].presence || t("node_actions.unknown_event")) + event ? link_to(name, edit_event_path(event)) : name + end + def summarize_publish action if action.metadata["via"] == "revision" t("node_actions.publish_rollback", @@ -301,4 +364,26 @@ module NodeActionsHelper t("node_actions.user_create", :actor => actor_ref(action), :target => user_participant_ref(action)).html_safe end + + def summarize_event_create action + event_sentence(action, "event_create") + end + + def summarize_event_update action + event_sentence(action, "event_update") + end + + def summarize_event_destroy action + event_sentence(action, "event_destroy") + end + + def event_sentence action, key + if action.node + t("node_actions.#{key}_on", :actor => actor_ref(action), + :event => event_ref(action), :subject => subject_ref(action)).html_safe + else + t("node_actions.#{key}", :actor => actor_ref(action), + :event => event_ref(action)).html_safe + end + end end diff --git a/app/models/event.rb b/app/models/event.rb index b8651a81..7726f9bf 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -20,8 +20,85 @@ class Event < ApplicationRecord title.presence || node&.head&.title || "Untitled event" end + def save_witnessed(actor:) + saved = false + transaction do + saved = save + raise ActiveRecord::Rollback unless saved + witness_event!("event_create", actor) + end + saved + end + + def update_witnessed(attributes, actor:) + updated = false + transaction do + updated = update(attributes) + raise ActiveRecord::Rollback unless updated + changes = event_changes + witness_event!("event_update", actor, changes) if changes.any? + end + updated + end + + def destroy_witnessed(actor:) + destroyed = false + transaction do + witness_event!("event_destroy", actor) + destroyed = destroy + raise ActiveRecord::Rollback unless destroyed + end + destroyed + end + private def generate_occurrences Occurrence.generate self end + + def event_snapshot + { + :event_title => title.presence || node&.unique_name || "##{id}", + :start_time => start_time&.iso8601, + :end_time => end_time&.iso8601, + :allday => allday, + :rrule => rrule.presence, + :location => location.presence, + :url => url.presence, + :event_tags => tag_list.to_a.sort, + :path => node&.unique_name + }.compact + end + + def event_changes + pairs = {} + + saved_changes.except("updated_at", "created_at", "description") + .each do |attribute, (before, after)| + key, pair = + case attribute + when "node_id" + ["node_path", { "from" => Node.find_by(:id => before)&.unique_name, + "to" => Node.find_by(:id => after)&.unique_name }] + when "start_time", "end_time" + [attribute, { "from" => before&.iso8601, "to" => after&.iso8601 }] + when "tag_list" + ["tags", { "from" => Array(before).sort, "to" => Array(after).sort }] + else + [attribute, { "from" => before, "to" => after }] + end + pairs[key] = pair + end + + extra = {} + extra[:changes] = pairs if pairs.any? + extra[:description_changed] = true if saved_changes.key?("description") + extra + end + + def witness_event!(verb, actor, extra = {}) + NodeAction.record!(:node => node, :participants => [node, self].compact, + :action => verb, :user => actor, + **event_snapshot, **extra) + end end diff --git a/app/models/node_action.rb b/app/models/node_action.rb index 82a3ef44..0167762b 100644 --- a/app/models/node_action.rb +++ b/app/models/node_action.rb @@ -107,6 +107,36 @@ class NodeAction < ApplicationRecord # participant differ: # "target_login" -- flat string, the affected account's login # + # "event_create" / "event_update" / "event_destroy" (calendar + # entries; participants: the Event and, when it has one, its Node, + # which also fills the node column. Deliberately not gated -- + # events reach chapter pages and widgets, never the feeds, and + # protection here follows emission, not position). + # + # Events carry no revisions, so the log is their only history: + # every entry holds a full snapshot of the state it produced, and + # walking an event's entries reconstructs it. Times are ISO 8601 + # and the rrule is raw, never humanised -- entries are read in both + # locales and event_schedule_text resolves that at render time. + # "event_title" -- flat string; the title, else the node's + # unique_name, else "#" + # "start_time", "end_time" -- ISO 8601, when set + # "allday" -- boolean + # "rrule" -- raw RRULE, when set + # "location", "url" -- flat strings, when set + # "event_tags" -- array of names, sorted, always. Named apart + # from the node verbs' "tags", which is a pair, + # so one renderer cannot mistake the other. + # "path" -- the node's unique_name, when it has a node + # + # On "event_update" only, and only when something changed -- an + # update that changes nothing records no entry at all: + # "changes" -- {field => pair}, node_id resolved to paths + # under "node_path", tag_list under "tags", + # times as ISO 8601 + # "description_changed" -- boolean; prose, flagged not quoted, + # as abstract_changed and body_changed are + # # Reserved: "demote" (via "trash" | "depublish") for an explicit # depublish workflow, if ever built. # diff --git a/app/views/node_actions/_change_details.html.erb b/app/views/node_actions/_change_details.html.erb index 2da8bfd5..98e70d8f 100644 --- a/app/views/node_actions/_change_details.html.erb +++ b/app/views/node_actions/_change_details.html.erb @@ -17,6 +17,12 @@ <% end %> + <% if (event_items = event_changes_list(action_entry)).any? %> + + + <%= safe_join(event_items, tag.br) %> + + <% end %> <% (action_entry.metadata["translation_diff"] || {}).each do |locale, diff| %> <%= locale.upcase %> -- cgit v1.3