Skip to content

Commit

Permalink
Fix key query and claim APIs to support async functionality (#314)
Browse files Browse the repository at this point in the history
* Fix key query and claim APIs to support async functionality

* fix copy/paste fail
  • Loading branch information
turt2live authored Apr 5, 2023
1 parent 1ae614f commit 1c203f8
Showing 1 changed file with 39 additions and 19 deletions.
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

0 comments on commit 1c203f8

Please sign in to comment.