Save a guest edit on the published Rule, and publish a new one when that update is refused.
A leftover editing id was treated as a successful finish in this browser only, so the public Rule stayed stale and a later Finish could skip creating a new Rule.
This commit is contained in:
1 parent
0535f6f167
commit
2644023b5a
6 files changed
+207
-44
No files matched your search
@@ -485,10 +485,13 @@ function CreateFlowLayoutContent({
|
||||
* Still merge {@link methodSectionsPinsFromPublishedHydratePatch}: selections
|
||||
* may already match draft state while compact CardStack pins stayed false
|
||||
* (pins are normally set only on facet **Confirm**).
|
||||
*
|
||||
* An empty id with that same content is a finished edit. Writing the id
|
||||
* back would make the next Finish update the old Rule.
|
||||
*/
|
||||
if (
|
||||
titleOk &&
|
||||
editingId === last.id &&
|
||||
(editingId === last.id || editingId.length === 0) &&
|
||||
sectionsClear &&
|
||||
!isPublishedRuleHydratePatchIncomplete(state, patch)
|
||||
) {
|
||||
|
||||
@@ -132,28 +132,14 @@ export function useCreateFlowFinalize({
|
||||
? state.editingPublishedRuleId.trim()
|
||||
: "";
|
||||
|
||||
if (editingId.length > 0 && sessionUser === null) {
|
||||
setIsPublishing(false);
|
||||
writeLastPublishedRule({
|
||||
id: editingId,
|
||||
title,
|
||||
summary: summary ?? null,
|
||||
document: ruleDocument,
|
||||
});
|
||||
updateState({ editingPublishedRuleId: undefined });
|
||||
onGuestPublished?.({ skippedInvites: false });
|
||||
router.push(createFlowStepPath("completed"));
|
||||
return;
|
||||
}
|
||||
|
||||
if (editingId.length > 0) {
|
||||
const updateResult = await updatePublishedRule(editingId, {
|
||||
title,
|
||||
summary: summary ?? null,
|
||||
document: ruleDocument,
|
||||
});
|
||||
setIsPublishing(false);
|
||||
if (updateResult.ok === true) {
|
||||
setIsPublishing(false);
|
||||
writeLastPublishedRule({
|
||||
id: editingId,
|
||||
title,
|
||||
@@ -161,28 +147,39 @@ export function useCreateFlowFinalize({
|
||||
document: ruleDocument,
|
||||
});
|
||||
updateState({ editingPublishedRuleId: undefined });
|
||||
void deleteServerDraftAfterPublish();
|
||||
if (sessionUser === null) {
|
||||
onGuestPublished?.({ skippedInvites: false });
|
||||
} else {
|
||||
void deleteServerDraftAfterPublish();
|
||||
}
|
||||
router.push(createFlowStepPath("completed"));
|
||||
return;
|
||||
}
|
||||
if (updateResult.status === 401) {
|
||||
openLogin({
|
||||
variant: sessionUser === null ? "keepRule" : "default",
|
||||
nextPath:
|
||||
sessionUser === null
|
||||
? createFlowStepPath("completed")
|
||||
: loginReturnPath,
|
||||
backdropVariant: "blurredYellow",
|
||||
});
|
||||
// A leftover id must not block a new Rule. This browser cannot update
|
||||
// that id, so publish a new one instead of pretending the edit saved.
|
||||
const guestCannotUpdate =
|
||||
sessionUser === null &&
|
||||
(updateResult.status === 401 ||
|
||||
updateResult.status === 403 ||
|
||||
updateResult.status === 404);
|
||||
if (!guestCannotUpdate) {
|
||||
setIsPublishing(false);
|
||||
if (updateResult.status === 401) {
|
||||
openLogin({
|
||||
variant: "default",
|
||||
nextPath: loginReturnPath,
|
||||
backdropVariant: "blurredYellow",
|
||||
});
|
||||
return;
|
||||
}
|
||||
showFailedBanner(
|
||||
updateResult.error.trim() !== ""
|
||||
? updateResult.error
|
||||
: messages.create.reviewAndComplete.publish.genericPublishFailed,
|
||||
false,
|
||||
);
|
||||
return;
|
||||
}
|
||||
showFailedBanner(
|
||||
updateResult.error.trim() !== ""
|
||||
? updateResult.error
|
||||
: messages.create.reviewAndComplete.publish.genericPublishFailed,
|
||||
false,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const isGuest = sessionUser === null;
|
||||
@@ -212,6 +209,7 @@ export function useCreateFlowFinalize({
|
||||
? { finishedAt: publishResult.createdAt }
|
||||
: {}),
|
||||
});
|
||||
updateState({ editingPublishedRuleId: undefined });
|
||||
if (isGuest) {
|
||||
onGuestPublished?.({ skippedInvites: skippedGuestInvites });
|
||||
} else {
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
notFound,
|
||||
unauthorized,
|
||||
} from "../../../../lib/server/responses";
|
||||
import { browserHoldsClaimForHash } from "../../../../lib/server/guestRuleClaim";
|
||||
import { getReadablePublishedRule } from "../../../../lib/server/publishedRules";
|
||||
import { getSessionUser } from "../../../../lib/server/session";
|
||||
import { apiRoute } from "../../../../lib/server/apiRoute";
|
||||
@@ -54,20 +55,25 @@ export const PATCH = apiRoute<RouteContext>(
|
||||
}
|
||||
|
||||
const user = await getSessionUser();
|
||||
if (!user) {
|
||||
return unauthorized();
|
||||
}
|
||||
|
||||
const { id } = await context.params;
|
||||
|
||||
const row = await prisma.publishedRule.findUnique({
|
||||
where: { id },
|
||||
select: { id: true, userId: true },
|
||||
select: { id: true, userId: true, claimTokenHash: true },
|
||||
});
|
||||
// This browser published the Rule and nobody has claimed it yet.
|
||||
const claimUpdate =
|
||||
row != null &&
|
||||
row.userId == null &&
|
||||
(await browserHoldsClaimForHash(row.claimTokenHash));
|
||||
|
||||
if (!row) {
|
||||
return notFound();
|
||||
return user ? notFound() : unauthorized();
|
||||
}
|
||||
if (row.userId !== user.id) {
|
||||
if (!user && !claimUpdate) {
|
||||
return unauthorized();
|
||||
}
|
||||
if (user && row.userId !== user.id && !claimUpdate) {
|
||||
return forbidden("You do not have permission to update this rule");
|
||||
}
|
||||
|
||||
@@ -95,7 +101,7 @@ export const PATCH = apiRoute<RouteContext>(
|
||||
await linkUploadsToRule({
|
||||
ruleId: row.id,
|
||||
document,
|
||||
userId: user.id,
|
||||
userId: user?.id ?? null,
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true });
|
||||
|
||||
Reference in new issue
Block a user