All editors now work on a server document at the same time instead of taking turns behind an edit lock. - The document's content is a sequence of Yjs updates (document_updates); DocumentChannel stores and relays them immediately, checks access on subscribe and again whenever links or memberships change. - app/frontend/collab/: the store stays position-based; a binding maps it to a Yjs form with stable row/column ids, so a row inserted by one person doesn't misplace what another is typing. Remote changes are applied only after pending local ones went into Yjs. - Per-person undo (Y.UndoManager), presence via awareness: the others' selections as coloured boxes with names, avatars in the toolbar. - Existing JSON documents convert on first open, deterministically, so two people opening one at once end up with the same Yjs state. - Editors compact the stored updates after 200 and keep a JSON snapshot current for export and duplicating. Edit lock and polling are gone. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
79 lines
2.7 KiB
Ruby
79 lines
2.7 KiB
Ruby
module Api
|
|
# Server documents. Listing and creating need an account; reading and
|
|
# writing a single document also work with just a share link token.
|
|
class DocumentsController < BaseController
|
|
before_action :require_authentication, only: %i[index create]
|
|
before_action :load_document, except: %i[index create]
|
|
|
|
# GET /api/documents -- owned + shared with me
|
|
def index
|
|
user = Current.user
|
|
memberships = user.document_memberships.index_by(&:document_id)
|
|
docs = user.accessible_documents.includes(:owner, :updated_by).order(content_updated_at: :desc)
|
|
render json: {
|
|
documents: docs.map { |d| d.meta_json(role: d.owner_id == user.id ? :owner : memberships[d.id]&.role) }
|
|
}
|
|
end
|
|
|
|
# POST /api/documents { title, content } -- also used to upload a local document
|
|
def create
|
|
doc = Current.user.owned_documents.create!(
|
|
title: params.require(:title), content: content_param, updated_by: Current.user
|
|
)
|
|
render json: { document: doc.meta_json(role: :owner) }, status: :created
|
|
end
|
|
|
|
# GET /api/documents/:id[?content=0]
|
|
# The content is the JSON snapshot the editors keep current (the live
|
|
# content comes through DocumentChannel).
|
|
def show
|
|
touch_membership
|
|
meta = @doc.meta_json(role: @role)
|
|
meta[:content] = @doc.content unless params[:content] == "0"
|
|
render json: { document: meta }
|
|
end
|
|
|
|
# PATCH /api/documents/:id { title }
|
|
def update
|
|
return forbidden unless Document.role_at_least?(@role, :editor)
|
|
@doc.update!(title: params.require(:title))
|
|
@doc.broadcast({ type: "meta", title: @doc.title })
|
|
render json: { document: @doc.meta_json(role: @role) }
|
|
end
|
|
|
|
# DELETE /api/documents/:id -- the owner deletes it; anyone else leaves it
|
|
def destroy
|
|
if @role == :owner
|
|
@doc.destroy!
|
|
@doc.broadcast({ type: "revoked" })
|
|
elsif Current.user
|
|
@doc.document_memberships.where(user: Current.user).delete_all
|
|
@doc.revalidate_access!
|
|
else
|
|
return forbidden
|
|
end
|
|
head :no_content
|
|
end
|
|
|
|
private
|
|
|
|
def load_document
|
|
@doc = Document.find(params[:id])
|
|
@role = @doc.role_for(user: Current.user, token: share_token)
|
|
raise ActiveRecord::RecordNotFound unless @role # 404, not 403: don't confirm it exists
|
|
end
|
|
|
|
def content_param
|
|
c = params.require(:content)
|
|
c.is_a?(String) ? c : c.to_unsafe_h.to_json
|
|
end
|
|
|
|
def touch_membership
|
|
return unless Current.user
|
|
@doc.document_memberships.where(user: Current.user).update_all(last_opened_at: Time.current)
|
|
end
|
|
|
|
def forbidden = render(json: { error: "forbidden" }, status: :forbidden)
|
|
end
|
|
end
|