Skip to content

Commit 5a10761

Browse files
committed
fix(app-shell): take back an own reaction through the unreact door
Claude-Session: https://claude.ai/code/session_01ADHkmPysaCwFvT4Pqb88z8 Co-authored-by: Claude <noreply@anthropic.com>
1 parent f02e644 commit 5a10761

4 files changed

Lines changed: 127 additions & 31 deletions

File tree

‎ui/packages/app-shell/src/views/RecordDetailView.feedGrant-12082.test.tsx‎

Lines changed: 86 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
*
1717
* - the composer: `feedComment`, the create grant on `sys_comment`;
1818
* - the reaction control: `feedReactionAdd`, the create grant on
19-
* `sys_comment_reaction`, with a take-back asking `feedReactionRemove`, its
20-
* delete grant, per click;
19+
* `sys_comment_reaction`, with a take-back asking `feedReactionRemove` per
20+
* click: the same create grant, since the take-back goes through the
21+
* unreact door (`DELETE /api/v1/comments/reactions/:id`), which removes the
22+
* caller's own row with no `delete` grant;
2123
* - on a deployment with no `sys_comment_reaction` (the column store), the
2224
* reaction control: `feedReactionColumn`, the update grant on `sys_comment`.
2325
*
@@ -36,10 +38,13 @@ import { MePermissionsProvider, type MePermissionsResponse } from '@object-ui/pe
3638
import { DETAIL_DEFAULT_TRANSLATIONS } from '@object-ui/plugin-detail';
3739
import { effectiveOperationsArray, resolveEffectiveApiMethods } from '@objectstack/spec/data';
3840

41+
/** The page's authenticated fetch: the unreact door is called through it. */
42+
const authFetch = vi.hoisted(() => vi.fn());
43+
3944
vi.mock('@object-ui/auth', async (importOriginal) => ({
4045
...(await importOriginal<Record<string, unknown>>()),
4146
useAuth: () => ({ user: { id: 'u1', name: 'u1', image: null }, activeOrganization: null }),
42-
createAuthenticatedFetch: () => vi.fn(),
47+
createAuthenticatedFetch: () => authFetch,
4348
}));
4449

4550
vi.mock('@object-ui/collaboration', async (importOriginal) => ({
@@ -69,6 +74,7 @@ vi.mock('./MetadataInspector', () => ({
6974
}));
7075

7176
import { RecordDetailView } from './RecordDetailView';
77+
import { toast } from 'sonner';
7278

7379
const COMMENT = 'sys_comment';
7480
const REACTION = 'sys_comment_reaction';
@@ -127,18 +133,21 @@ function answer(feed: { [COMMENT]?: Bits; [REACTION]?: Bits }): MePermissionsRes
127133
} as MePermissionsResponse;
128134
}
129135

130-
/** One comment by another member; the caller (`u1`) has already reacted 👍 on it. */
131-
function makeDataSource() {
136+
/**
137+
* One comment by another member; `reactor` (the caller `u1` unless named) has
138+
* already reacted 👍 on it.
139+
*/
140+
function makeDataSource(reactor = 'u1') {
132141
const comment = {
133142
id: COMMENT_ID,
134143
thread_id: THREAD,
135144
author_id: 'u3',
136145
author_name: 'u3',
137146
body: COMMENT_BODY,
138147
created_at: '2026-10-10T08:00:00.000Z',
139-
reactions: JSON.stringify({ '👍': ['u1'] }),
148+
reactions: JSON.stringify({ '👍': [reactor] }),
140149
};
141-
const ownReaction = { id: 'r1', comment_id: COMMENT_ID, emoji: '👍', user_id: 'u1', created_at: '2026-10-10T09:00:00.000Z' };
150+
const ownReaction = { id: 'r1', comment_id: COMMENT_ID, emoji: '👍', user_id: reactor, created_at: '2026-10-10T09:00:00.000Z' };
142151
return {
143152
find: vi.fn(async (objectName: string) => {
144153
if (objectName === COMMENT) return { data: [{ ...comment }] };
@@ -210,7 +219,23 @@ const thumbsChip = (view: View) =>
210219
(b) => !!b.getAttribute('aria-label') && b.children.length === 2 && b.children[0].textContent === '👍',
211220
);
212221

222+
/** The unreact door's answer to a removal of the caller's own row. */
223+
const unreacted = (id: string) =>
224+
new Response(JSON.stringify({ success: true, data: { id, deleted: true } }), {
225+
status: 200,
226+
headers: { 'content-type': 'application/json' },
227+
});
228+
229+
/** The calls that reached the unreact door: `[url, init]`. */
230+
const unreactCalls = () =>
231+
authFetch.mock.calls.filter(([url]) => String(url).includes('/api/v1/comments/reactions/'));
232+
213233
beforeEach(() => {
234+
authFetch.mockImplementation(async (url: string) =>
235+
String(url).includes('/api/v1/comments/reactions/')
236+
? unreacted(String(url).split('/').pop() as string)
237+
: new Response(JSON.stringify({ data: [] }), { status: 200, headers: { 'content-type': 'application/json' } }),
238+
);
214239
vi.stubGlobal(
215240
'fetch',
216241
vi.fn(async () =>
@@ -267,19 +292,22 @@ describe("the record feed's reaction control reads feedReactionAdd / feedReactio
267292
expect((thumbsChip(view) as HTMLButtonElement).disabled).toBe(!offered);
268293
});
269294

270-
it('a take-back asks the delete grant: with create but not delete, clicking the own reaction writes nothing and keeps it shown', async () => {
295+
it('a take-back reads no delete grant: with create but not delete, clicking the own reaction removes it through the unreact door', async () => {
271296
const { view, dataSource } = await mount(answer({ [COMMENT]: SHAPE.full, [REACTION]: SHAPE.createOnly }));
272297
fireEvent.click(thumbsChip(view)!);
273298
await act(async () => {});
299+
expect(unreactCalls()).toEqual([['/api/v1/comments/reactions/r1', { method: 'DELETE', credentials: 'include' }]]);
274300
expect(dataSource.delete).not.toHaveBeenCalled();
275-
expect(thumbsChip(view)!.children[1].textContent).toBe('1');
301+
expect(thumbsChip(view)).toBeUndefined();
276302
});
277303

278-
it('CONTROL — with the delete grant the same click takes the reaction back by deleting the own row', async () => {
304+
it('CONTROL — with the delete grant the same click takes the reaction back through the same door, never the data door', async () => {
279305
const { view, dataSource } = await mount(answer({ [COMMENT]: SHAPE.full, [REACTION]: SHAPE.full }));
280306
fireEvent.click(thumbsChip(view)!);
281307
await act(async () => {});
282-
expect(dataSource.delete.mock.calls).toEqual([[REACTION, 'r1']]);
308+
expect(unreactCalls()).toEqual([['/api/v1/comments/reactions/r1', { method: 'DELETE', credentials: 'include' }]]);
309+
expect(dataSource.delete).not.toHaveBeenCalled();
310+
expect(thumbsChip(view)).toBeUndefined();
283311
});
284312

285313
it('adding through the picker creates the own row where create is held', async () => {
@@ -291,6 +319,51 @@ describe("the record feed's reaction control reads feedReactionAdd / feedReactio
291319
});
292320
});
293321

322+
describe("a take-back goes through the unreact door, and only on the caller's own reaction (objectstack#26346)", () => {
323+
it('a member with no grant beyond create on sys_comment_reaction adds a reaction and takes it back', async () => {
324+
const { view, dataSource } = await mount(answer({ [COMMENT]: SHAPE.readOnly, [REACTION]: SHAPE.createOnly }));
325+
fireEvent.click(addReaction(view)!);
326+
fireEvent.click(within(screen.getByRole('listbox')).getByRole('option', { name: '🎉' }));
327+
await act(async () => {});
328+
const party = () =>
329+
(view.queryAllByRole('button') as HTMLElement[]).find(
330+
(b) => !!b.getAttribute('aria-label') && b.children.length === 2 && b.children[0].textContent === '🎉',
331+
);
332+
expect(party()).toBeDefined();
333+
fireEvent.click(party()!);
334+
await act(async () => {});
335+
expect(dataSource.create.mock.calls).toEqual([[REACTION, { comment_id: COMMENT_ID, emoji: '🎉' }]]);
336+
expect(unreactCalls()).toEqual([['/api/v1/comments/reactions/new-row', { method: 'DELETE', credentials: 'include' }]]);
337+
expect(dataSource.delete).not.toHaveBeenCalled();
338+
expect(party()).toBeUndefined();
339+
});
340+
341+
it("another member's reaction offers no take-back: a click on their chip adds the caller's own row and reaches no door that removes", async () => {
342+
const { view, dataSource } = await mount(answer({ [COMMENT]: SHAPE.full, [REACTION]: SHAPE.full }), makeDataSource('u2'));
343+
fireEvent.click(thumbsChip(view)!);
344+
await act(async () => {});
345+
expect(dataSource.create.mock.calls).toEqual([[REACTION, { comment_id: COMMENT_ID, emoji: '👍' }]]);
346+
expect(unreactCalls()).toEqual([]);
347+
expect(dataSource.delete).not.toHaveBeenCalled();
348+
expect(thumbsChip(view)!.children[1].textContent).toBe('2');
349+
});
350+
351+
it('a take-back the door refuses puts the reaction back and says it was not saved', async () => {
352+
authFetch.mockImplementation(async () =>
353+
new Response(JSON.stringify({ success: false, error: { code: 'PERMISSION_DENIED' } }), {
354+
status: 403,
355+
headers: { 'content-type': 'application/json' },
356+
}),
357+
);
358+
const { view } = await mount(answer({ [COMMENT]: SHAPE.full, [REACTION]: SHAPE.createOnly }));
359+
fireEvent.click(thumbsChip(view)!);
360+
await act(async () => {});
361+
expect(unreactCalls().map(([url]) => url)).toEqual(['/api/v1/comments/reactions/r1']);
362+
expect(thumbsChip(view)!.children[1].textContent).toBe('1');
363+
expect(toast.error).toHaveBeenCalledTimes(1);
364+
});
365+
});
366+
294367
/**
295368
* The two feed objects as objectstack registers them (`plugin-audit`): their
296369
* policy keys (`managedBy`, `userActions`) and the `enable` block the server
@@ -336,7 +409,8 @@ describe('against the registered feed schemas in the page objects (objectui#1208
336409
expect(addReaction(view)).not.toBeNull();
337410
fireEvent.click(thumbsChip(view)!);
338411
await act(async () => {});
339-
expect(dataSource.delete.mock.calls).toEqual([[REACTION, 'r1']]);
412+
expect(unreactCalls().map(([url]) => url)).toEqual(['/api/v1/comments/reactions/r1']);
413+
expect(dataSource.delete).not.toHaveBeenCalled();
340414
});
341415
});
342416

‎ui/packages/app-shell/src/views/RecordDetailView.tsx‎

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,9 @@ interface ReactionLedger {
368368
* ruling A amended on objectstack-ai/objectstack#22505; objectui#12078).
369369
*
370370
* A member adds a reaction by creating their row (`comment_id`, `emoji`; the
371-
* server stamps `user_id` from the session) and takes it back by deleting that
372-
* row. Nobody's write touches anyone else's reaction, so two members reacting
371+
* server stamps `user_id` from the session) and takes it back by removing that
372+
* row through the unreact door ({@link COMMENT_REACTION_REMOVE_PATH}). Nobody's
373+
* write touches anyone else's reaction, so two members reacting
373374
* at the same moment both stay stored, which a whole-set write to
374375
* `sys_comment.reactions` could not do: the later write replaced the earlier.
375376
*
@@ -380,6 +381,18 @@ interface ReactionLedger {
380381
*/
381382
const COMMENT_REACTION_OBJECT = 'sys_comment_reaction';
382383

384+
/**
385+
* The unreact door, `DELETE` + `/:id` of a `sys_comment_reaction` row
386+
* (objectstack-ai/objectstack#26161): it removes the caller's own reaction
387+
* row and refuses anyone else's (`403 PERMISSION_DENIED`). A take-back goes
388+
* through it and not through the data door's `delete`, because that one needs
389+
* an object-level `delete` grant, which no every-member set may carry
390+
* (ADR-0090 D5): a member who reacts on the every-member grants alone could
391+
* add a reaction and never take it back. The door reads no body and refuses
392+
* any query parameter.
393+
*/
394+
const COMMENT_REACTION_REMOVE_PATH = '/api/v1/comments/reactions';
395+
383396
/** The object the record feed's comments and replies are rows of. */
384397
const COMMENT_OBJECT = 'sys_comment';
385398

@@ -2869,9 +2882,9 @@ export function RecordDetailView({ dataSource, objects, onEdit, objectNameOverri
28692882
* All of the above is the `column` store, which runs only where the
28702883
* deployment has no `sys_comment_reaction`. On the `records` store
28712884
* (objectui#12078) a click writes nothing but the clicker's own row: it
2872-
* creates that row, or deletes it, so no write can replace another member's
2885+
* creates that row, or removes it through the unreact door, so no write can replace another member's
28732886
* reaction and two members reacting at once both stay stored. The writes of
2874-
* one comment and emoji run one after another (a take-back deletes the row an
2887+
* one comment and emoji run one after another (a take-back removes the row an
28752888
* earlier click is still creating). Once the last of them answers, the row
28762889
* must show the clicker's reaction exactly when the server holds their row;
28772890
* if it does not, a write was refused, and the clicker's own id on that emoji
@@ -2900,11 +2913,12 @@ export function RecordDetailView({ dataSource, objects, onEdit, objectNameOverri
29002913
const userId = currentUser.id;
29012914
const want = !holdsOwnReaction(row.reactions, emoji, userId);
29022915
// The control is offered on the add row (`feedReactionAdd`, where it is
2903-
// handed over below); taking a reaction back is the delete of the
2904-
// caller's own row, its own row of the map. A click that would take
2905-
// back with that row closed writes nothing and shows nothing changed,
2906-
// rather than showing a take-back the server then refuses
2907-
// (objectui#12082).
2916+
// handed over below); taking a reaction back is the removal of the
2917+
// caller's own row through the unreact door, its own row of the map
2918+
// (`feedReactionRemove`, which reads no `delete` grant). A click that
2919+
// would take back with that row closed writes nothing and shows
2920+
// nothing changed, rather than showing a take-back the server then
2921+
// refuses (objectui#12082).
29082922
if (!want && !reactionRemoveGranted) return;
29092923
const commentKey = JSON.stringify([key, String(itemId)]);
29102924
const writeKey = JSON.stringify([key, String(itemId), emoji]);
@@ -2944,9 +2958,11 @@ export function RecordDetailView({ dataSource, objects, onEdit, objectNameOverri
29442958
}
29452959
ownRows().set(emoji, String(created.id));
29462960
} else if (!want && rowId !== undefined) {
2947-
if ((await dataSource.delete(COMMENT_REACTION_OBJECT, rowId)) === false) {
2948-
throw new Error(`The ${emoji} reaction was not deleted.`);
2949-
}
2961+
const res = await authFetch(
2962+
`${import.meta.env.VITE_SERVER_URL || ''}${COMMENT_REACTION_REMOVE_PATH}/${encodeURIComponent(rowId)}`,
2963+
{ method: 'DELETE', credentials: 'include' },
2964+
);
2965+
if (!res.ok) throw new Error(`The ${emoji} reaction was not removed (HTTP ${res.status}).`);
29502966
ownRows().delete(emoji);
29512967
}
29522968
};
@@ -3041,7 +3057,7 @@ export function RecordDetailView({ dataSource, objects, onEdit, objectNameOverri
30413057
() => settle('failed'),
30423058
);
30433059
},
3044-
[currentUser.id, dataSource, feedRecordKey, feedItemsByRecord, reactionStore, reactionRemoveGranted, t],
3060+
[authFetch, currentUser.id, dataSource, feedRecordKey, feedItemsByRecord, reactionStore, reactionRemoveGranted, t],
30453061
);
30463062

30473063
useEffect(() => {

‎ui/packages/core/src/utils/affordanceGrants.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,15 @@ export const AFFORDANCE_GRANTS = {
208208
feedReactionAdd: { crud: 'create', grant: 'create' },
209209
/**
210210
* The same control taking the caller's reaction back, asked of
211-
* `sys_comment_reaction`: it deletes the caller's own reaction row.
211+
* `sys_comment_reaction`: it removes the caller's own reaction row through
212+
* the unreact door (`DELETE /api/v1/comments/reactions/:id`), not the data
213+
* door, so no CRUD bit is read. That door admits exactly the caller's own
214+
* row and needs no `delete` grant, which no every-member set may carry
215+
* (ADR-0090 D5), so the take-back asks the grant the reaction was added
216+
* under: whoever may react may take their own reaction back. Whose reaction
217+
* it is decides which chips offer it; the door's own rule decides the write.
212218
*/
213-
feedReactionRemove: { crud: 'delete', grant: 'delete' },
219+
feedReactionRemove: { crud: null, grant: 'create' },
214220
/**
215221
* The same control on a deployment with no `sys_comment_reaction` (the
216222
* reactions column store), asked of `sys_comment`: a click updates the

‎ui/packages/plugin-form/src/affordanceGrantMap-12082.test.tsx‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,12 @@ const EXPECTED_GRANT: Record<string, Grant> = {
121121
kanbanCardMove: 'update',
122122
// The record feed's writes (the census backlog's leg B), each asked of the
123123
// object it writes: a comment or reply, a reaction added or taken back, and
124-
// a reaction on the column store.
124+
// a reaction on the column store. The take-back removes the caller's own
125+
// row through the unreact door, which needs no delete grant, so it asks the
126+
// grant the reaction was added under (objectstack#26346).
125127
feedComment: 'create',
126128
feedReactionAdd: 'create',
127-
feedReactionRemove: 'delete',
129+
feedReactionRemove: 'create',
128130
feedReactionColumn: 'update',
129131
// objectui#12268: the author's Edit and Delete on their own feed comment,
130132
// asked of `sys_comment`.
@@ -695,8 +697,6 @@ const WRITE_SITES: Record<string, WriteSite> = {
695697
mapped(['feedReactionColumn'], RECORD_PAGE),
696698
'packages/app-shell/src/views/RecordDetailView.tsx :: RecordDetailView > handleToggleReaction > write :: create':
697699
mapped(['feedReactionAdd'], RECORD_PAGE),
698-
'packages/app-shell/src/views/RecordDetailView.tsx :: RecordDetailView > handleToggleReaction > write :: delete':
699-
mapped(['feedReactionRemove'], RECORD_PAGE),
700700
'packages/app-shell/src/views/RecordDetailView.tsx :: RecordDetailView > onClick :: delete':
701701
mapped(['recordDelete'], RECORD_PAGE),
702702
'packages/app-shell/src/views/RelatedRecordActionsBridge.tsx :: RelatedRecordActionsBridge > value > resolve :: delete':

0 commit comments

Comments
 (0)