discourse-shared-edits/support/reset_post.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

53 lines
1.3 KiB
Ruby

# frozen_string_literal: true
# Load plugin code
require_relative "../lib/discourse_shared_edits/yjs"
require_relative "../lib/discourse_shared_edits/state_validator"
require_relative "../app/models/shared_edit_revision"
post_id = ARGV[0]&.to_i || 2
post = Post.find(post_id)
puts "Current post content:"
puts post.raw.truncate(200)
puts
puts "Current revisions:"
DiscourseSharedEdits::SharedEditRevision
.where(post_id: post_id)
.each do |r|
text =
begin
DiscourseSharedEdits::Yjs.text_from_state(r.raw)
rescue StandardError
"ERROR"
end
puts "v#{r.version}: #{text.truncate(100)}"
end
puts
puts "Clearing rate limit keys..."
%w[cooldown count].each do |type|
key = "shared_edits_recovery_#{type}_#{post_id}"
Discourse.redis.del(key)
puts "Deleted #{key}"
end
puts
puts "Resetting post to clean state..."
new_content = "Sam was here\n\ntest test\n\nI am"
post.update!(raw: new_content)
DiscourseSharedEdits::SharedEditRevision.where(post_id: post_id).delete_all
initial_state = DiscourseSharedEdits::Yjs.state_from_text(post.raw)
DiscourseSharedEdits::SharedEditRevision.create!(
post_id: post_id,
client_id: "reset",
user_id: Discourse.system_user.id,
version: 1,
revision: "",
raw: initial_state[:state],
)
puts "Done! New state:"
puts DiscourseSharedEdits::Yjs.text_from_state(initial_state[:state])