mirror of
https://github.com/Elysola/WebHooker.git
synced 2026-11-21 01:23:40 +00:00
fix(console): save groups with guarded writes
Whole-list saves can overwrite unrelated groups. Use per-group revision-aware writes, retain rename results and confirm deletion on the exact group, routes and version shown to the user.
This commit is contained in:
1 parent
85c73d1122
commit
6eb9cbab4e
2 files changed
+118
-28
No files matched your search
@@ -568,7 +568,9 @@ const {
|
|||||||
needLogin,
|
needLogin,
|
||||||
error: groupsError,
|
error: groupsError,
|
||||||
load: loadGroups,
|
load: loadGroups,
|
||||||
save: saveGroups,
|
update: updateGroup,
|
||||||
|
create: createGroup,
|
||||||
|
remove: removeGroup,
|
||||||
rename: groupsRename,
|
rename: groupsRename,
|
||||||
} = useGroupsApi();
|
} = useGroupsApi();
|
||||||
const {
|
const {
|
||||||
@@ -1036,8 +1038,8 @@ function openEditGroup(group: Group): void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Group writes replace the whole group list, so they run one at a time, each
|
* Group writes run one at a time, each built on what the previous one saved;
|
||||||
* built from the list the previous one saved; true when one may start.
|
* true when one may start.
|
||||||
*/
|
*/
|
||||||
function canWriteGroups(): boolean {
|
function canWriteGroups(): boolean {
|
||||||
if (!savingGroup.value) return true;
|
if (!savingGroup.value) return true;
|
||||||
@@ -1049,8 +1051,7 @@ async function onSaveGroupFromPanel(group: Group): Promise<void> {
|
|||||||
if (!canWriteGroups()) return;
|
if (!canWriteGroups()) return;
|
||||||
savingGroup.value = true;
|
savingGroup.value = true;
|
||||||
try {
|
try {
|
||||||
const next = groups.value.map((g) => (g.id === group.id ? group : g));
|
await updateGroup(group);
|
||||||
await saveGroups(next);
|
|
||||||
push(t("toast.groupSaved"));
|
push(t("toast.groupSaved"));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
push(t("toast.saveFailed", { msg: err instanceof Error ? err.message : String(err) }), "bad");
|
push(t("toast.saveFailed", { msg: err instanceof Error ? err.message : String(err) }), "bad");
|
||||||
@@ -1066,25 +1067,34 @@ async function onSaveGroup(group: Group): Promise<void> {
|
|||||||
savingGroup.value = true;
|
savingGroup.value = true;
|
||||||
try {
|
try {
|
||||||
const editing = editingGroup.value;
|
const editing = editingGroup.value;
|
||||||
let next: Group[];
|
|
||||||
if (editing) {
|
if (editing) {
|
||||||
|
// The editor does not show these fields; keep what the group has.
|
||||||
|
const edited: Group = { ...group, isolated: editing.isolated, revision: editing.revision };
|
||||||
if (group.id !== editing.id) {
|
if (group.id !== editing.id) {
|
||||||
// Id changed: rename first so routes/webhook secret/invites follow,
|
// Id changed: rename first so routes/webhook secret/invites follow
|
||||||
// then persist the remaining edits under the new id.
|
// (refused if the group changed since the editor opened), then
|
||||||
await groupsRename(editing.id, group.id);
|
// persist the remaining edits under the new id, on top of exactly the
|
||||||
// A list loaded meanwhile may already hold the new id as well.
|
// renamed group. If that second step fails, the editor already points
|
||||||
next = [...groups.value.filter((g) => g.id !== editing.id && g.id !== group.id), group];
|
// at the new id, so saving again only retries the update.
|
||||||
|
const renamed = await groupsRename(editing.id, group.id, editing.revision);
|
||||||
|
if (session === groupEditorSession) editingGroup.value = renamed;
|
||||||
|
// Server-decided fields (e.g. isolation made explicit by the rename)
|
||||||
|
// come from the renamed group, not from the editor's older copy.
|
||||||
|
await updateGroup({
|
||||||
|
...edited,
|
||||||
|
isolated: renamed.isolated,
|
||||||
|
revision: renamed.revision,
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
next = groups.value.map((g) => (g.id === editing.id ? group : g));
|
await updateGroup(edited);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (groups.value.some((g) => g.id === group.id)) {
|
if (groups.value.some((g) => g.id === group.id)) {
|
||||||
push(t("toast.groupIdExists"), "bad");
|
push(t("toast.groupIdExists"), "bad");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
next = [...groups.value, group];
|
await createGroup(group);
|
||||||
}
|
}
|
||||||
await saveGroups(next);
|
|
||||||
if (session === groupEditorSession) groupEditorOpen.value = false;
|
if (session === groupEditorSession) groupEditorOpen.value = false;
|
||||||
push(t("toast.groupSaved"));
|
push(t("toast.groupSaved"));
|
||||||
await loadGroups();
|
await loadGroups();
|
||||||
@@ -1101,13 +1111,25 @@ async function onDeleteGroup(group: Group): Promise<void> {
|
|||||||
// Fetch the real route count for this group instead of relying on the
|
// Fetch the real route count for this group instead of relying on the
|
||||||
// routes of whichever group happens to be open in the detail view.
|
// routes of whichever group happens to be open in the detail view.
|
||||||
let used = 0;
|
let used = 0;
|
||||||
|
let routesRevision: string | undefined;
|
||||||
|
let version: number | null = null;
|
||||||
try {
|
try {
|
||||||
const data = await apiFetch<{ routes?: Route[] }>(
|
const data = await apiFetch<{ routes?: Route[]; revision?: string; version?: number | null }>(
|
||||||
`/admin/api/groups/${encodeURIComponent(group.id)}/routes`,
|
`/admin/api/groups/${encodeURIComponent(group.id)}/routes`,
|
||||||
);
|
);
|
||||||
used = (data.routes ?? []).length;
|
used = (data.routes ?? []).length;
|
||||||
} catch {
|
routesRevision = data.revision;
|
||||||
// Count is best-effort; proceed without the warning.
|
version = data.version ?? null;
|
||||||
|
} catch (err) {
|
||||||
|
// The deletion is confirmed on what the routes are now: without them,
|
||||||
|
// a route added meanwhile could be deleted unseen.
|
||||||
|
if (visit === pageVisit) {
|
||||||
|
push(
|
||||||
|
t("toast.deleteFailed", { msg: err instanceof Error ? err.message : String(err) }),
|
||||||
|
"bad",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
// The list was left while the count loaded: never ask on another page.
|
// The list was left while the count loaded: never ask on another page.
|
||||||
if (visit !== pageVisit) return;
|
if (visit !== pageVisit) return;
|
||||||
@@ -1118,7 +1140,8 @@ async function onDeleteGroup(group: Group): Promise<void> {
|
|||||||
if (!confirmed || visit !== pageVisit || !canWriteGroups()) return;
|
if (!confirmed || visit !== pageVisit || !canWriteGroups()) return;
|
||||||
savingGroup.value = true;
|
savingGroup.value = true;
|
||||||
try {
|
try {
|
||||||
await saveGroups(groups.value.filter((g) => g.id !== group.id));
|
// Deletes the group as confirmed: a route added meanwhile makes it fail.
|
||||||
|
await removeGroup(group.id, group.revision, routesRevision, version);
|
||||||
push(t("toast.groupDeleted"));
|
push(t("toast.groupDeleted"));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
push(t("toast.deleteFailed", { msg: err instanceof Error ? err.message : String(err) }), "bad");
|
push(t("toast.deleteFailed", { msg: err instanceof Error ? err.message : String(err) }), "bad");
|
||||||
|
|||||||
@@ -52,22 +52,87 @@ export function useGroupsApi() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function save(next: Group[]): Promise<void> {
|
/** Apply a write's result locally; a load that started earlier must not land. */
|
||||||
await apiFetch("/admin/api/groups", {
|
function settle(next: Group[]): void {
|
||||||
method: "PUT",
|
|
||||||
body: JSON.stringify({ groups: next }),
|
|
||||||
});
|
|
||||||
request++;
|
request++;
|
||||||
loading.value = false;
|
loading.value = false;
|
||||||
groups.value = next;
|
groups.value = next;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Rename a group; routes, webhook secret and invites follow automatically. */
|
/** Reload after a refused write (409): someone changed the group meanwhile. */
|
||||||
async function rename(oldId: string, newId: string): Promise<void> {
|
async function guard<T>(write: () => Promise<T>): Promise<T> {
|
||||||
await apiFetch(`/admin/api/groups/${encodeURIComponent(oldId)}/rename`, {
|
try {
|
||||||
|
return await write();
|
||||||
|
} catch (err) {
|
||||||
|
if (isConflict(err)) void load();
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update one group. Its `revision` (from the last load) travels along, so an
|
||||||
|
* edit made from a stale copy is refused instead of reverting newer changes.
|
||||||
|
*/
|
||||||
|
async function update(group: Group): Promise<Group> {
|
||||||
|
const data = await guard(() =>
|
||||||
|
apiFetch<{ group: Group }>(`/admin/api/groups/${encodeURIComponent(group.id)}`, {
|
||||||
method: "PUT",
|
method: "PUT",
|
||||||
body: JSON.stringify({ newId }),
|
body: JSON.stringify({ group }),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
settle(groups.value.map((g) => (g.id === data.group.id ? data.group : g)));
|
||||||
|
return data.group;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Create one group (super admins). */
|
||||||
|
async function create(group: Group): Promise<Group> {
|
||||||
|
const data = await apiFetch<{ group: Group }>("/admin/api/groups", {
|
||||||
|
method: "POST",
|
||||||
|
body: JSON.stringify({ group }),
|
||||||
});
|
});
|
||||||
|
settle([...groups.value.filter((g) => g.id !== data.group.id), data.group]);
|
||||||
|
return data.group;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete one group with its routes. `revision` (the group) and
|
||||||
|
* `routesRevision` (its routes) are what the deletion was confirmed on: if
|
||||||
|
* either changed meanwhile, the server refuses (409) instead of deleting
|
||||||
|
* something nobody saw.
|
||||||
|
*/
|
||||||
|
async function remove(
|
||||||
|
id: string,
|
||||||
|
revision?: string,
|
||||||
|
routesRevision?: string,
|
||||||
|
version?: number | null,
|
||||||
|
): Promise<void> {
|
||||||
|
const query = new URLSearchParams();
|
||||||
|
if (revision) query.set("revision", revision);
|
||||||
|
if (routesRevision) query.set("routesRevision", routesRevision);
|
||||||
|
if (version != null) query.set("version", String(version));
|
||||||
|
const qs = query.toString();
|
||||||
|
await guard(() =>
|
||||||
|
apiFetch(`/admin/api/groups/${encodeURIComponent(id)}${qs ? `?${qs}` : ""}`, {
|
||||||
|
method: "DELETE",
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
settle(groups.value.filter((g) => g.id !== id));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rename a group; routes, webhook secret and invites follow automatically.
|
||||||
|
* `revision` is the group's revision the rename was decided on.
|
||||||
|
*/
|
||||||
|
async function rename(oldId: string, newId: string, revision?: string): Promise<Group> {
|
||||||
|
const data = await guard(() =>
|
||||||
|
apiFetch<{ group: Group }>(`/admin/api/groups/${encodeURIComponent(oldId)}/rename`, {
|
||||||
|
method: "PUT",
|
||||||
|
body: JSON.stringify({ newId, revision }),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
// Show the group under its new id right away (a later step may still fail).
|
||||||
|
settle(groups.value.map((g) => (g.id === oldId ? data.group : g)));
|
||||||
|
return data.group;
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -82,7 +147,9 @@ export function useGroupsApi() {
|
|||||||
needLogin,
|
needLogin,
|
||||||
error,
|
error,
|
||||||
load,
|
load,
|
||||||
save,
|
update,
|
||||||
|
create,
|
||||||
|
remove,
|
||||||
rename,
|
rename,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user