discourse-shared-edits/app/services/discourse_shared_edits/revise.rb
Sam 50d9317272
FEATURE: yjs-based collaborative editing with state recovery and cursor sync
Major overhaul of the shared edits plugin to improve reliability, robustness,
and developer experience:

**Backend**
- Extract Revise service for cleaner controller orchestration
- Add StateValidator for base64/Yjs safety, corruption detection, and recovery
- Centralize protocol constants (Ruby + JS) to avoid hardcoded strings
- Add state hash sync verification and vector validation endpoints
- Harden security (guardian checks), error handling, and resource cleanup
- Resize shared edit columns migration; add state_hash column

**Frontend**
- Decompose shared-edit-manager into focused modules: yjs-document,
  markdown-sync, rich-mode-sync, network-manager, encoding-utils
- Add cursor overlay and caret coordinate tracking for selection sharing
- Add ProseMirror extension for rich-mode collaborative editing
- Cache-busted Yjs bundle loading via hashed filenames
- Fix scroll drift during sync

**Testing & Tooling**
- Extensive new specs: state_validator, revision_controller, model, revise service
- New Ember acceptance tests: cursor, lifecycle, sync flows
- Add support scripts: fake_writer (Playwright), state_corruptor, debug_recovery
- Add support/lint wrapper for full CI lint suite
- Update dependencies and rebuild Yjs/y-prosemirror bundles
2026-02-13 11:34:52 +11:00

43 lines
1.2 KiB
Ruby

# frozen_string_literal: true
module DiscourseSharedEdits
class Revise
include Service::Base
step :validate_client_id
step :apply_revision
private
def validate_client_id(post:, client_id:)
validation = StateValidator.validate_client_id(client_id)
return if validation[:valid]
raise StateValidator::InvalidUpdateError.new(
"Invalid client_id: #{validation[:error]}",
post_id: post.id,
)
end
def apply_revision(post:, current_user:, client_id:, update:)
version, update_payload, state_hash =
SharedEditRevision.revise!(
post_id: post.id,
user_id: current_user.id,
client_id: client_id,
update: update,
cursor: context[:cursor],
awareness: context[:awareness],
post: post,
username: current_user.username,
allow_blank_state: context[:allow_blank_state] || false,
state_vector: context[:state_vector],
)
SharedEditRevision.ensure_will_commit(post.id)
context[:version] = version
context[:update] = update_payload
context[:state_hash] = state_hash
end
end
end