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
+119
-29
No files matched your search
@@ -52,22 +52,87 @@ export function useGroupsApi() {
|
||||
}
|
||||
}
|
||||
|
||||
async function save(next: Group[]): Promise<void> {
|
||||
await apiFetch("/admin/api/groups", {
|
||||
method: "PUT",
|
||||
body: JSON.stringify({ groups: next }),
|
||||
});
|
||||
/** Apply a write's result locally; a load that started earlier must not land. */
|
||||
function settle(next: Group[]): void {
|
||||
request++;
|
||||
loading.value = false;
|
||||
groups.value = next;
|
||||
}
|
||||
|
||||
/** Rename a group; routes, webhook secret and invites follow automatically. */
|
||||
async function rename(oldId: string, newId: string): Promise<void> {
|
||||
await apiFetch(`/admin/api/groups/${encodeURIComponent(oldId)}/rename`, {
|
||||
method: "PUT",
|
||||
body: JSON.stringify({ newId }),
|
||||
/** Reload after a refused write (409): someone changed the group meanwhile. */
|
||||
async function guard<T>(write: () => Promise<T>): Promise<T> {
|
||||
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",
|
||||
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 {
|
||||
@@ -82,7 +147,9 @@ export function useGroupsApi() {
|
||||
needLogin,
|
||||
error,
|
||||
load,
|
||||
save,
|
||||
update,
|
||||
create,
|
||||
remove,
|
||||
rename,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user