diff options
| author | erdgeist <erdgeist@erdgeist.org> | 2026-08-02 22:45:51 +0200 |
|---|---|---|
| committer | erdgeist <erdgeist@erdgeist.org> | 2026-08-02 22:45:51 +0200 |
| commit | da59dea46f55c64a3090b83a6321fbecd2ee6f60 (patch) | |
| tree | ea64bb3e3d124f8141ae0cfb07e1dfffc19162bb /app/models | |
| parent | 30d85119ba5cda8a23ca99df5af9e94f5391eb78 (diff) | |
Witness calendar entries
Diffstat (limited to 'app/models')
| -rw-r--r-- | app/models/event.rb | 77 | ||||
| -rw-r--r-- | app/models/node_action.rb | 30 |
2 files changed, 107 insertions, 0 deletions
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 | |||
| 20 | title.presence || node&.head&.title || "Untitled event" | 20 | title.presence || node&.head&.title || "Untitled event" |
| 21 | end | 21 | end |
| 22 | 22 | ||
| 23 | def save_witnessed(actor:) | ||
| 24 | saved = false | ||
| 25 | transaction do | ||
| 26 | saved = save | ||
| 27 | raise ActiveRecord::Rollback unless saved | ||
| 28 | witness_event!("event_create", actor) | ||
| 29 | end | ||
| 30 | saved | ||
| 31 | end | ||
| 32 | |||
| 33 | def update_witnessed(attributes, actor:) | ||
| 34 | updated = false | ||
| 35 | transaction do | ||
| 36 | updated = update(attributes) | ||
| 37 | raise ActiveRecord::Rollback unless updated | ||
| 38 | changes = event_changes | ||
| 39 | witness_event!("event_update", actor, changes) if changes.any? | ||
| 40 | end | ||
| 41 | updated | ||
| 42 | end | ||
| 43 | |||
| 44 | def destroy_witnessed(actor:) | ||
| 45 | destroyed = false | ||
| 46 | transaction do | ||
| 47 | witness_event!("event_destroy", actor) | ||
| 48 | destroyed = destroy | ||
| 49 | raise ActiveRecord::Rollback unless destroyed | ||
| 50 | end | ||
| 51 | destroyed | ||
| 52 | end | ||
| 53 | |||
| 23 | private | 54 | private |
| 24 | def generate_occurrences | 55 | def generate_occurrences |
| 25 | Occurrence.generate self | 56 | Occurrence.generate self |
| 26 | end | 57 | end |
| 58 | |||
| 59 | def event_snapshot | ||
| 60 | { | ||
| 61 | :event_title => title.presence || node&.unique_name || "##{id}", | ||
| 62 | :start_time => start_time&.iso8601, | ||
| 63 | :end_time => end_time&.iso8601, | ||
| 64 | :allday => allday, | ||
| 65 | :rrule => rrule.presence, | ||
| 66 | :location => location.presence, | ||
| 67 | :url => url.presence, | ||
| 68 | :event_tags => tag_list.to_a.sort, | ||
| 69 | :path => node&.unique_name | ||
| 70 | }.compact | ||
| 71 | end | ||
| 72 | |||
| 73 | def event_changes | ||
| 74 | pairs = {} | ||
| 75 | |||
| 76 | saved_changes.except("updated_at", "created_at", "description") | ||
| 77 | .each do |attribute, (before, after)| | ||
| 78 | key, pair = | ||
| 79 | case attribute | ||
| 80 | when "node_id" | ||
| 81 | ["node_path", { "from" => Node.find_by(:id => before)&.unique_name, | ||
| 82 | "to" => Node.find_by(:id => after)&.unique_name }] | ||
| 83 | when "start_time", "end_time" | ||
| 84 | [attribute, { "from" => before&.iso8601, "to" => after&.iso8601 }] | ||
| 85 | when "tag_list" | ||
| 86 | ["tags", { "from" => Array(before).sort, "to" => Array(after).sort }] | ||
| 87 | else | ||
| 88 | [attribute, { "from" => before, "to" => after }] | ||
| 89 | end | ||
| 90 | pairs[key] = pair | ||
| 91 | end | ||
| 92 | |||
| 93 | extra = {} | ||
| 94 | extra[:changes] = pairs if pairs.any? | ||
| 95 | extra[:description_changed] = true if saved_changes.key?("description") | ||
| 96 | extra | ||
| 97 | end | ||
| 98 | |||
| 99 | def witness_event!(verb, actor, extra = {}) | ||
| 100 | NodeAction.record!(:node => node, :participants => [node, self].compact, | ||
| 101 | :action => verb, :user => actor, | ||
| 102 | **event_snapshot, **extra) | ||
| 103 | end | ||
| 27 | end | 104 | 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 | |||
| 107 | # participant differ: | 107 | # participant differ: |
| 108 | # "target_login" -- flat string, the affected account's login | 108 | # "target_login" -- flat string, the affected account's login |
| 109 | # | 109 | # |
| 110 | # "event_create" / "event_update" / "event_destroy" (calendar | ||
| 111 | # entries; participants: the Event and, when it has one, its Node, | ||
| 112 | # which also fills the node column. Deliberately not gated -- | ||
| 113 | # events reach chapter pages and widgets, never the feeds, and | ||
| 114 | # protection here follows emission, not position). | ||
| 115 | # | ||
| 116 | # Events carry no revisions, so the log is their only history: | ||
| 117 | # every entry holds a full snapshot of the state it produced, and | ||
| 118 | # walking an event's entries reconstructs it. Times are ISO 8601 | ||
| 119 | # and the rrule is raw, never humanised -- entries are read in both | ||
| 120 | # locales and event_schedule_text resolves that at render time. | ||
| 121 | # "event_title" -- flat string; the title, else the node's | ||
| 122 | # unique_name, else "#<id>" | ||
| 123 | # "start_time", "end_time" -- ISO 8601, when set | ||
| 124 | # "allday" -- boolean | ||
| 125 | # "rrule" -- raw RRULE, when set | ||
| 126 | # "location", "url" -- flat strings, when set | ||
| 127 | # "event_tags" -- array of names, sorted, always. Named apart | ||
| 128 | # from the node verbs' "tags", which is a pair, | ||
| 129 | # so one renderer cannot mistake the other. | ||
| 130 | # "path" -- the node's unique_name, when it has a node | ||
| 131 | # | ||
| 132 | # On "event_update" only, and only when something changed -- an | ||
| 133 | # update that changes nothing records no entry at all: | ||
| 134 | # "changes" -- {field => pair}, node_id resolved to paths | ||
| 135 | # under "node_path", tag_list under "tags", | ||
| 136 | # times as ISO 8601 | ||
| 137 | # "description_changed" -- boolean; prose, flagged not quoted, | ||
| 138 | # as abstract_changed and body_changed are | ||
| 139 | # | ||
| 110 | # Reserved: "demote" (via "trash" | "depublish") for an explicit | 140 | # Reserved: "demote" (via "trash" | "depublish") for an explicit |
| 111 | # depublish workflow, if ever built. | 141 | # depublish workflow, if ever built. |
| 112 | # | 142 | # |
