sessions->find($this->userId(), (string) $request->route('id')); if ($session === null) { return $this->notFound(__('That session does not exist or was deleted.', 'maze-graph')); } $id = (int) $request->route('change'); $change = $this->journal->find($id); if ($change === null || (int) $change['session_id'] !== $session->id) { return $this->notFound(__('That change does not exist.', 'maze-graph')); } $result = $this->revert($id, $change); if (is_wp_error($result)) { return $this->error($result->get_error_message(), 409); } return $this->ok(['session' => SessionResource::make($session), 'entries' => Transcript::forClient($result === null ? [] : [$result])]); } /** * `POST /changes/{change}/undo`: the same, from the Activity page or the Extensions page, where a change may * belong to no session (an import) or to a Studio document. */ public function undoChange(Request $request): JsonResponse { $id = (int) $request->route('change'); $change = $this->journal->find($id); if ($change === null || (int) $change['user_id'] !== $this->userId()) { return $this->notFound(__('That change does not exist.', 'maze-graph')); } $result = $this->revert($id, $change); return is_wp_error($result) ? $this->error($result->get_error_message(), 409) : $this->ok(['change' => $id, 'status' => 'undone']); } /** * Undo, report it, and add a `change_undone` line to the change's session when it has one. * * @param array $change * @return array|\WP_Error|null The transcript line added, if any. */ protected function revert(int $id, array $change): array|\WP_Error|null { $result = $this->journal->undo($this->userId(), $id); if (is_wp_error($result)) { return $result; } $this->events->record(new Event('change.undone', ['change' => $id, 'tool' => (string) $change['tool'], 'user' => $this->userId(), 'session' => (int) $change['session_id']])); if ((int) $change['session_id'] <= 0) { return null; } return $this->store->append((int) $change['session_id'], Transcript::event('change_undone', [ 'change' => $id, 'tool' => (string) $change['tool'], 'message' => StoredMessage::encode('change.undone', [(string) $change['summary']]), ])); } }