Skip to content

Commit

Permalink
Inform da-collab about deleted document
Browse files Browse the repository at this point in the history
  • Loading branch information
bosschaert committed Apr 25, 2024
1 parent c6df3d6 commit b7f2d24
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/handlers/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
*/
import { deleteSource } from '../routes/source.js';

export default async function deleteHandler({ env, daCtx }) {
export default async function deleteHandler({ req, env, daCtx }) {
const { path } = daCtx;

if (path.startsWith('/source')) return deleteSource({ env, daCtx });
if (path.startsWith('/source')) return deleteSource({ req, env, daCtx });

return undefined;
}
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default {
respObj = await postHandler({ req, env, daCtx });
break;
case 'DELETE':
respObj = await deleteHandler({ env, daCtx });
respObj = await deleteHandler({ req, env, daCtx });
break;
default:
respObj = unkownHandler();
Expand Down
24 changes: 16 additions & 8 deletions src/routes/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,8 @@ import deleteObject from '../storage/object/delete.js';
import putHelper from '../helpers/source.js';
import { postObjectVersion } from '../storage/version/put.js';

export async function deleteSource({ env, daCtx }) {
await postObjectVersion(env, daCtx);
return deleteObject(env, daCtx);
}

async function invalidateCollab(url, env) {
const invPath = `/api/v1/syncadmin?doc=${url}`;
async function invalidateCollab(api, url, env) {
const invPath = `/api/v1/${api}?doc=${url}`;
if (env.dacollab) {
// service binding is configured, hostname is not relevant
console.log('Using service binding');

Check warning on line 23 in src/routes/source.js

View workflow job for this annotation

GitHub Actions / Running tests (20.x)

Unexpected console statement
Expand All @@ -39,14 +34,27 @@ async function invalidateCollab(url, env) {
}
}

export async function deleteSource({ req, env, daCtx }) {
await postObjectVersion(env, daCtx);
const resp = await deleteObject(env, daCtx);

if (resp.status === 204) {
const initiator = req.headers.get('x-da-initiator');
if (initiator !== 'collab') {
await invalidateCollab('deleteadmin', req.url, env);
}
}
return resp;
}

export async function postSource({ req, env, daCtx }) {
const obj = await putHelper(req, env, daCtx);
const resp = await putObject(env, daCtx, obj);

if (resp.status === 201 || resp.status === 200) {
const initiator = req.headers.get('x-da-initiator');
if (initiator !== 'collab') {
await invalidateCollab(req.url, env);
await invalidateCollab('syncadmin', req.url, env);
}
}
return resp;
Expand Down
2 changes: 1 addition & 1 deletion src/storage/object/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,5 @@ export default async function deleteObjects(env, daCtx) {
}
} while (ContinuationToken);

return { body: '', status: 204 };
return { body: null, status: 204 };

Check warning on line 74 in src/storage/object/delete.js

View check run for this annotation

Codecov / codecov/patch

src/storage/object/delete.js#L74

Added line #L74 was not covered by tests
}
14 changes: 12 additions & 2 deletions test/routes/source.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,15 @@ describe('Source Route', () => {
});

it('Test deleteSource', async () => {
const env = {};
const req = {
headers: new Map(),
url: 'http://somehost.com/somedoc.html'
};

const daCalled = []
const dacollab = { fetch: (u) => daCalled.push(u) };

const env = { dacollab };
const daCtx = {};

const called = [];
Expand All @@ -204,8 +212,10 @@ describe('Source Route', () => {
}
}
);
const resp = await deleteSource({env, daCtx});
const resp = await deleteSource({req, env, daCtx});
assert.equal(204, resp.status);
assert.deepStrictEqual(called, ['deleteObject']);
assert.deepStrictEqual(daCalled,
['https://localhost/api/v1/deleteadmin?doc=http://somehost.com/somedoc.html']);
});
});

0 comments on commit b7f2d24

Please sign in to comment.