TheRare Visuals

Two people, one album: revisions, undo and going back to what the customer sent

Letting the customer and the studio edit the same album: edit rules on the status enum, revision numbers against lost updates, undo that groups a drag into one step, and a backfill that only filled what it actually knew.

TheRare Engineering27 October 20266 min readIntermediate

From hand-over to shared editing

The album designer started as a one-way hand-over. The customer picks a template and their photos, arranges the spreads, and presses Send to studio. From then on the album was locked for them and the studio worked from it.

Real albums don't work like that. The studio spots a blurry photo on spread 12, or a crop that cuts off a grandmother's head, and wants to fix it rather than send it back with a note. The customer remembers, two days after sending, that their cousin's photo is missing. Both sides need to edit the same album, and both need to trust that nothing they did is lost.

That raised three questions:

  1. Who may edit, and when?
  2. What happens when both sides edit at once?
  3. How does the studio undo its own changes if the customer doesn't like them?

Who may edit, and when

An album moves through DRAFT -> SUBMITTED -> IN_REVIEW -> CHANGES_REQUESTED -> APPROVED -> PRINTING. Rather than sprinkling status checks through the service, the enum answers the question itself:

Java
/** States in which the customer may still edit (and re-send) the design. */
public boolean customerEditable() {
    return this == DRAFT || this == SUBMITTED || this == IN_REVIEW || this == CHANGES_REQUESTED;
}

/** States in which the studio (Owner/Manager, or the booking's editor) may edit. */
public boolean studioEditable() {
    return this == SUBMITTED || this == IN_REVIEW || this == CHANGES_REQUESTED || this == APPROVED;
}

Reading the two lists side by side shows the design. The studio can't touch a draft (the customer hasn't sent it yet). The customer can't touch an approved album (the studio has signed it off for print). In between, both can edit. Once it's printing, nobody can.

"The studio" also includes the booking's photo editor, not just the Owner and Managers. An editor only sees albums on bookings they're assigned to, and asking for any other album gets the same "Album not found" as an album that doesn't exist. Answering "that's not yours" would tell someone the album id is real, which is a small leak worth closing.

When the customer edits an album the studio already has, the edit is saved but flagged as unsent until they press Send updated album. The studio sees the flag and knows the version in front of them may be about to change.

Two editors, one album: revisions

The classic bug with shared editing is the lost update. The studio opens the album at 10:00. The customer opens it at 10:05, swaps a photo and saves. At 10:10 the studio, still looking at the 10:00 version, moves a caption and saves. The whole album is saved as one JSON document, so the studio's save writes back the old photo and the customer's swap disappears. Nobody sees an error.

The fix is optimistic locking: every album has a revision number that goes up on every save, and every save says which revision it was made on top of.

Java
private static void checkRevision(AlbumDesign a, Integer revision) {
    if (revision != null && revision != a.getRevision()) {
        throw new IllegalStateException("This album was just updated by "
                + (a.getLastEditedBy() != null ? a.getLastEditedBy() : "someone else")
                + ". Reload to see the latest version before making more changes.");
    }
}

"Optimistic" because we don't lock the album while someone has it open. We assume conflicts are rare and check at save time. IllegalStateException is turned into HTTP 409 Conflict by the global exception handler, and the editor's save hook listens for exactly that:

TypeScript
const onSave = useCallback(async (designJson: string) => {
  try {
    const saved = await save(designJson, revision.current);
    revision.current = saved.revision;
  } catch (e) {
    if (e instanceof ApiError && e.status === 409) setConflict(e.message);
    throw e;
  }
}, [save]);

On a conflict the page makes the editor read-only and offers a reload. That's less clever than merging both people's changes, but it's honest: nobody keeps working on a version that can no longer be saved, and the message names who made the newer change.

The revision != null check lets older browser tabs that don't send a revision keep working. They just don't get the protection.

Undo and redo

Once two people are rearranging the same album, undo stops being a nice-to-have. The editor records every change to the design, whoever made it: placing a photo, swapping, cropping, changing layouts, editing words.

Recording every change literally would make undo useless. Dragging a crop fires dozens of updates, and typing a caption fires one per letter, so a single undo would move the crop by one pixel. The editor groups changes that happen close together:

TypeScript
/** Changes closer together than this (a drag, typing a caption) undo as one step. */
const UNDO_GROUP_MS = 700;

if (now - lastChangeAt.current > UNDO_GROUP_MS) {
  past.current = [...past.current, before.current].slice(-MAX_UNDO);
}

A new step is only recorded when 700 ms have passed since the last change, so a whole drag or a typed word undoes in one go. History is capped at 100 steps.

The key decision: undoing is saved like any other edit. It isn't a special operation on the server. Undo puts back an older design, and that design is saved with a new revision, so it reaches the other side through the same path as any change, with the same conflict check.

"Go back to what the customer sent"

Undo covers your own session. It doesn't help when the studio rearranged an album yesterday and the customer says today, "I liked mine better".

For that we needed a copy of the album exactly as the customer last sent it. Migration V127 added one column:

SQL
ALTER TABLE album_designs
    ADD COLUMN sent_design_json TEXT;

Every send and re-send stores the design in it. The studio's Undo studio changes button, shown only when the album differs from that copy, puts it back. If the customer has also edited since sending, the confirmation says so: going back discards their unsent changes too. Like undo, it's a normal edit with a new revision, so a customer with the album open gets the usual "reload to see the latest" instead of overwriting it.

The interesting part was albums that already existed. The column was new, so they had no "as sent" copy. For albums that were already with the studio, the best honest answer depends on whether the studio had edited them:

SQL
UPDATE album_designs
   SET sent_design_json = design_json
 WHERE submitted_at IS NOT NULL AND last_edited_by_studio = FALSE;

If the studio hadn't touched an album since it was sent, the current design is the sent design. If it had, we don't know what the customer sent, so the column stays empty and the button doesn't appear (the server also refuses with "There's no copy of the album as the customer sent it to go back to"). Backfilling a guess would have been worse than admitting there's no copy: the button would "restore" something the customer never sent.

The restore is also audited: "Undid the studio's changes to ... back to the customer's sent version", with who did it.

Takeaways

  • Put "who may do what in which state" on the state itself. Two small methods on the enum replaced status checks scattered across the service.
  • Shared documents need a revision number. Without one, the second save silently erases the first.
  • When you can't merge, stop clearly. A read-only editor and "reload to see the latest" beat a clever merge that's wrong once in a while.
  • Group rapid changes into one undo step, or undo is unusable for drags and typing.
  • Make undo and restore ordinary edits, so they go through the same conflict checks and sync as everything else.
  • When backfilling a new column, only fill what you actually know. An empty value with a clear message is better than a confident wrong one.

Found this useful?

Built something with what you learned here, or spotted a better way? Tell us. We work with freelance developers on Shootlo.