Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix key query and claim APIs to support async functionality #314

Merged
merged 2 commits into from
Apr 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 39 additions & 19 deletions src/appservice/Appservice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -958,16 +958,26 @@ export class Appservice extends EventEmitter {
}

let responded = false;
this.emit("query.key_claim", req.body, async (result: MSC3983KeyClaimResponse | Promise<MSC3983KeyClaimResponse> | undefined | null) => {
if (result?.then) result = await result;
if (!result) {
res.status(404).json({ errcode: "M_UNRECOGNIZED", error: "Endpoint not implemented" });
responded = true;
return;
}

res.status(200).json(result);
this.emit("query.key_claim", req.body, (result: MSC3983KeyClaimResponse | Promise<MSC3983KeyClaimResponse> | undefined | null) => {
responded = true;

const handleResult = (result2: MSC3983KeyClaimResponse) => {
if (!result2) {
res.status(404).json({ errcode: "M_UNRECOGNIZED", error: "Endpoint not implemented" });
return;
}

res.status(200).json(result2);
};

if ((result as Promise<MSC3983KeyClaimResponse>)?.then) {
(result as Promise<MSC3983KeyClaimResponse>).then(r => handleResult(r)).catch(e => {
LogService.error("Appservice", "Error handling key claim API", e);
res.status(500).json({ errcode: "M_UNKNOWN" });
});
} else {
handleResult(result as MSC3983KeyClaimResponse);
}
});
if (!responded) {
res.status(404).json({ errcode: "M_UNRECOGNIZED", error: "Endpoint not implemented" });
Expand All @@ -986,18 +996,28 @@ export class Appservice extends EventEmitter {
}

let responded = false;
this.emit("query.key", req.body, async (result: MSC3984KeyQueryResponse | Promise<MSC3984KeyQueryResponse> | undefined | null) => {
if ((result as any)?.then) result = await result;
if (!result) {
res.status(404).json({ errcode: "M_UNRECOGNIZED", error: "Endpoint not implemented" });
responded = true;
return;
}
this.emit("query.key", req.body, (result: MSC3984KeyQueryResponse | Promise<MSC3984KeyQueryResponse> | undefined | null) => {
responded = true;

// Implementation note: we could probably query the device keys from our storage if we wanted to.
const handleResult = (result2: MSC3984KeyQueryResponse) => {
if (!result2) {
res.status(404).json({ errcode: "M_UNRECOGNIZED", error: "Endpoint not implemented" });
return;
}

res.status(200).json(result);
responded = true;
// Implementation note: we could probably query the device keys from our storage if we wanted to.

res.status(200).json(result2);
};

if ((result as Promise<MSC3984KeyQueryResponse>)?.then) {
(result as Promise<MSC3984KeyQueryResponse>).then(r => handleResult(r)).catch(e => {
LogService.error("Appservice", "Error handling key query API", e);
res.status(500).json({ errcode: "M_UNKNOWN" });
});
} else {
handleResult(result as MSC3984KeyQueryResponse);
}
});
if (!responded) {
res.status(404).json({ errcode: "M_UNRECOGNIZED", error: "Endpoint not implemented" });
Expand Down