Skip to content
Extraits de code Groupes Projets
Valider 57e81e5c rédigé par Kai A. Hiller's avatar Kai A. Hiller
Parcourir les fichiers

Refactor code into buildOwnContext function


Signed-off-by: default avatarKai A. Hiller <V02460@gmail.com>
parent 5005c488
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -20,6 +20,7 @@ import { ...@@ -20,6 +20,7 @@ import {
BridgeContext, BridgeContext,
Cli, Cli,
ClientFactory, ClientFactory,
default_message,
thirdPartyLookup, thirdPartyLookup,
} from "matrix-appservice-bridge"; } from "matrix-appservice-bridge";
import * as yaml from "js-yaml"; import * as yaml from "js-yaml";
...@@ -115,30 +116,22 @@ async function run(port: number, fileConfig: DiscordBridgeConfig) { ...@@ -115,30 +116,22 @@ async function run(port: number, fileConfig: DiscordBridgeConfig) {
onEvent: async (request) => { onEvent: async (request) => {
const data = request.getData(); const data = request.getData();
try { try {
MetricPeg.get.registerRequest(data.event_id);
// Build our own context.
if (!store.roomStore) {
log.warn("Discord store not ready yet, dropping message");
MetricPeg.get.requestOutcome(data.event_id, false, "dropped");
return;
}
const roomId = data.room_id; const roomId = data.room_id;
MetricPeg.get.registerRequest(data.event_id);
const context: BridgeContext = { const context = await buildOwnContext(roomId, store);
rooms: {}, const callback = callbacks.onEvent(request, context);
}; request.outcomeFrom(callback);
if (roomId) {
const entries = await store.roomStore.getEntriesByMatrixId(roomId);
context.rooms = entries[0] || {};
}
await request.outcomeFrom(callbacks.onEvent(request, context));
MetricPeg.get.requestOutcome(data.event_id, false, "success"); MetricPeg.get.requestOutcome(data.event_id, false, "success");
} catch (err) { } catch (err) {
MetricPeg.get.requestOutcome(data.event_id, false, "fail"); if (err instanceof NotReadyError) {
log.error("Exception thrown while handling \"onEvent\" event", err); log.warn("Discord store not ready yet, dropping message");
await request.outcomeFrom(Promise.reject("Failed to handle")); MetricPeg.get.requestOutcome(data.event_id, false, "dropped");
} else {
MetricPeg.get.requestOutcome(data.event_id, false, "fail");
log.error("Exception thrown while handling \"onEvent\" event", err);
await request.outcomeFrom(Promise.reject("Failed to handle"));
}
} }
}, },
onLog: (line, isError) => { onLog: (line, isError) => {
...@@ -229,3 +222,33 @@ async function run(port: number, fileConfig: DiscordBridgeConfig) { ...@@ -229,3 +222,33 @@ async function run(port: number, fileConfig: DiscordBridgeConfig) {
process.exit(1); process.exit(1);
} }
} }
/**
* Builds a custom BridgeContext with rooms known to the bridge.
*/
async function buildOwnContext(
roomId: string,
store: DiscordStore,
): Promise<BridgeContext> {
if (!store.roomStore) {
throw new NotReadyError("Discord store not ready yet, will retry later");
}
const entries = await store.roomStore.getEntriesByMatrixId(roomId);
return {
rooms: entries[0] || {},
senders: {},
targets: {},
};
}
class NotReadyError extends Error {
public name: string;
constructor(...params) {
default_message(params, "The bridge was not ready when the message was sent");
super(...params);
this.name = "NotReadyError";
}
}
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter