Skip to content

Commit fd5bbbe

Browse files
committed
improve save to semble/margin
1 parent 3c72251 commit fd5bbbe

7 files changed

Lines changed: 503 additions & 131 deletions

File tree

backend/src/routes/integrations.ts

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ export async function handleCreateSembleCard(request: Request, env: Env): Promis
9696
description?: string;
9797
author?: string;
9898
publishedAt?: string;
99+
collections?: { uri: string; cid: string }[];
100+
// Legacy single-collection fields — still accepted from in-flight queued entries
99101
collectionUri?: string;
100102
collectionCid?: string;
101103
};
@@ -115,6 +117,13 @@ export async function handleCreateSembleCard(request: Request, env: Env): Promis
115117
});
116118
}
117119

120+
const collections: { uri: string; cid: string }[] =
121+
body.collections && body.collections.length > 0
122+
? body.collections
123+
: body.collectionUri && body.collectionCid
124+
? [{ uri: body.collectionUri, cid: body.collectionCid }]
125+
: [];
126+
118127
const rkey = generateTID();
119128
const metadata: Record<string, string> = {};
120129
if (body.title) metadata.title = body.title;
@@ -142,12 +151,13 @@ export async function handleCreateSembleCard(request: Request, env: Env): Promis
142151
});
143152
}
144153

145-
// If a collection was specified, add the card to it via collectionLink
146-
if (body.collectionUri && body.collectionCid) {
154+
// For each selected collection, create a collectionLink record.
155+
const collectionResults: { uri: string; error?: string }[] = [];
156+
for (const col of collections) {
147157
const linkRkey = generateTID();
148158
const collectionLink = {
149159
$type: 'network.cosmik.collectionLink',
150-
collection: { uri: body.collectionUri, cid: body.collectionCid },
160+
collection: { uri: col.uri, cid: col.cid },
151161
card: { uri: result.data.uri, cid: result.data.cid },
152162
addedBy: session.did,
153163
addedAt: new Date().toISOString(),
@@ -158,23 +168,19 @@ export async function handleCreateSembleCard(request: Request, env: Env): Promis
158168
linkRkey,
159169
collectionLink
160170
);
161-
if (!linkResult.success) {
162-
// Card was created but collection link failed — return success with warning
163-
return new Response(
164-
JSON.stringify({
165-
uri: result.data.uri,
166-
cid: result.data.cid,
167-
collectionError: linkResult.error,
168-
}),
169-
{ status: 201, headers: { 'Content-Type': 'application/json' } }
170-
);
171-
}
171+
collectionResults.push(
172+
linkResult.success ? { uri: col.uri } : { uri: col.uri, error: linkResult.error }
173+
);
172174
}
173175

174-
return new Response(JSON.stringify({ uri: result.data.uri, cid: result.data.cid }), {
175-
status: 201,
176-
headers: { 'Content-Type': 'application/json' },
177-
});
176+
return new Response(
177+
JSON.stringify({
178+
uri: result.data.uri,
179+
cid: result.data.cid,
180+
...(collectionResults.length > 0 ? { collectionResults } : {}),
181+
}),
182+
{ status: 201, headers: { 'Content-Type': 'application/json' } }
183+
);
178184
}
179185

180186
/**
@@ -238,6 +244,8 @@ export async function handleCreateMarginBookmark(request: Request, env: Env): Pr
238244
url: string;
239245
title?: string;
240246
description?: string;
247+
collectionUris?: string[];
248+
// Legacy single-collection field — still accepted from in-flight queued entries
241249
collectionUri?: string;
242250
};
243251
try {
@@ -256,6 +264,13 @@ export async function handleCreateMarginBookmark(request: Request, env: Env): Pr
256264
});
257265
}
258266

267+
const collectionUris: string[] =
268+
body.collectionUris && body.collectionUris.length > 0
269+
? body.collectionUris
270+
: body.collectionUri
271+
? [body.collectionUri]
272+
: [];
273+
259274
const rkey = generateTID();
260275
const record = {
261276
$type: 'at.margin.bookmark',
@@ -276,12 +291,13 @@ export async function handleCreateMarginBookmark(request: Request, env: Env): Pr
276291
});
277292
}
278293

279-
// If a collection was specified, add the bookmark to it
280-
if (body.collectionUri) {
294+
// For each selected collection, create a collectionItem record.
295+
const collectionResults: { uri: string; error?: string }[] = [];
296+
for (const uri of collectionUris) {
281297
const itemRkey = generateTID();
282298
const collectionItem = {
283299
$type: 'at.margin.collectionItem',
284-
collection: body.collectionUri,
300+
collection: uri,
285301
annotation: result.data.uri,
286302
createdAt: new Date().toISOString(),
287303
};
@@ -290,22 +306,17 @@ export async function handleCreateMarginBookmark(request: Request, env: Env): Pr
290306
itemRkey,
291307
collectionItem
292308
);
293-
if (!itemResult.success) {
294-
return new Response(
295-
JSON.stringify({
296-
uri: result.data.uri,
297-
cid: result.data.cid,
298-
collectionError: itemResult.error,
299-
}),
300-
{ status: 201, headers: { 'Content-Type': 'application/json' } }
301-
);
302-
}
309+
collectionResults.push(itemResult.success ? { uri } : { uri, error: itemResult.error });
303310
}
304311

305-
return new Response(JSON.stringify({ uri: result.data.uri, cid: result.data.cid }), {
306-
status: 201,
307-
headers: { 'Content-Type': 'application/json' },
308-
});
312+
return new Response(
313+
JSON.stringify({
314+
uri: result.data.uri,
315+
cid: result.data.cid,
316+
...(collectionResults.length > 0 ? { collectionResults } : {}),
317+
}),
318+
{ status: 201, headers: { 'Content-Type': 'application/json' } }
319+
);
309320
}
310321

311322
/**

0 commit comments

Comments
 (0)