Skip to content

Commit

Permalink
Fix key query and claim APIs to support async functionality (turt2live#9
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Half-Shot authored Apr 6, 2023
2 parents 2485ea1 + 33c7242 commit db33128
Showing 1 changed file with 31 additions and 19 deletions.
50 changes: 31 additions & 19 deletions src/appservice/Appservice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -958,16 +958,22 @@ 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);
};

Promise.resolve(result).then(r => handleResult(r)).catch(e => {
LogService.error("Appservice", "Error handling key claim API", e);
res.status(500).json({ errcode: "M_UNKNOWN", error: "Error handling key claim API" });
});
});
if (!responded) {
res.status(404).json({ errcode: "M_UNRECOGNIZED", error: "Endpoint not implemented" });
Expand All @@ -986,18 +992,24 @@ 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);
};

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

0 comments on commit db33128

Please sign in to comment.