Skip to content

Commit

Permalink
Add CommandContext translator to interface adaptor.
Browse files Browse the repository at this point in the history
We might want to make the translation function async
i'm not sure yet, it depends if we need it once integration
into draupnir progresses a little more.
  • Loading branch information
Gnuxie committed Aug 22, 2024
1 parent 628149d commit 370fdba
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
75 changes: 75 additions & 0 deletions src/MatrixInterfaceAdaptor/AdaptorToCommandContextTranslator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// SPDX-FileCopyrightText: 2024 Gnuxie <Gnuxie@protonmail.com>
//
// SPDX-License-Identifier: Apache-2.0
//
// SPDX-FileAttributionText: <text>
// This modified file incorporates work from @the-draupnir-project/interface-manager
// https://github.com/the-draupnir-project/interface-manager
// </text>

import { CommandDescription } from "../Command";

/**
* This is used to add clue code to take what is essentially a god context into a more specific
* attenuated one that can be unit tested easily.
* So basically, rather than giving a command the entirity of Draupnir, we can
* give it juts the capability to ban a user. Which simplifies test setup.
*/
export type AdaptorToCommandContextTranslationFunction<
AdaptorContext,
CommandContext,
> = (adaptorContext: AdaptorContext) => CommandContext;

export interface AdaptorToCommandContextTranslator<AdaptorContext> {
translateContext<CommandContext>(
commandDescription: CommandDescription<CommandContext>,
adaptorContext: AdaptorContext
): CommandContext;
registerTranslation<CommandContext>(
commandDescription: CommandDescription<CommandContext>,
translationFunction: AdaptorToCommandContextTranslationFunction<
AdaptorContext,
CommandContext
>
): AdaptorToCommandContextTranslator<AdaptorContext>;
}

export class StandardAdaptorToCommandContextTranslator<AdaptorContext>
implements AdaptorToCommandContextTranslator<AdaptorContext>
{
private readonly translators = new Map<
CommandDescription,
AdaptorToCommandContextTranslationFunction<AdaptorContext, unknown>
>();
translateContext<CommandContext>(
commandDescription: CommandDescription<CommandContext>,
adaptorContext: AdaptorContext
): CommandContext {
const entry = this.translators.get(
commandDescription as CommandDescription
);
if (entry === undefined) {
return adaptorContext as unknown as CommandContext;
} else {
return entry(adaptorContext) as CommandContext;
}
}
registerTranslation<CommandContext>(
commandDescription: CommandDescription<CommandContext>,
translationFunction: AdaptorToCommandContextTranslationFunction<
AdaptorContext,
CommandContext
>
): AdaptorToCommandContextTranslator<AdaptorContext> {
if (this.translators.has(commandDescription as CommandDescription)) {
throw new TypeError(
`There is already a translation function registered for the command ${commandDescription.summary}`
);
}
this.translators.set(
commandDescription as CommandDescription,
translationFunction
);
return this;
}
}
9 changes: 8 additions & 1 deletion src/MatrixInterfaceAdaptor/MatrixInterfaceAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from "../Command";
import { MatrixRendererDescription } from "./MatrixRendererDescription";
import { DocumentNode } from "../DeadDocument";
import { AdaptorToCommandContextTranslator } from "./AdaptorToCommandContextTranslator";

export interface MatrixInterfaceAdaptor<MatrixEventContext> {
invoke<CommandResult>(
Expand Down Expand Up @@ -69,6 +70,7 @@ export class StandardMatrixInterfaceAdaptor<AdaptorContext, MatrixEventContext>
>();
public constructor(
private readonly adaptorContext: AdaptorContext,
private readonly adaptorToCommandContextTranslator: AdaptorToCommandContextTranslator<AdaptorContext>,
private readonly defaultRenderer: MatrixInterfaceDefaultRenderer<
AdaptorContext,
MatrixEventContext
Expand All @@ -91,8 +93,13 @@ export class StandardMatrixInterfaceAdaptor<AdaptorContext, MatrixEventContext>
const renderer = this.findRendererForCommandDescription(
command.description
);
const commandContext =
this.adaptorToCommandContextTranslator.translateContext(
command.description,
this.adaptorContext
);
const commandResult = await command.description.executor(
this.adaptorContext,
commandContext,
command.keywords,
...command.immediateArguments,
...(command.rest ?? [])
Expand Down
1 change: 1 addition & 0 deletions src/MatrixInterfaceAdaptor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//
// SPDX-License-Identifier: Apache-2.0

export * from "./AdaptorToCommandContextTranslator";
export * from "./MatrixInterfaceAdaptor";
export * from "./MatrixInterfaceCommandDispatcher";
export * from "./MatrixRendererDescription";

0 comments on commit 370fdba

Please sign in to comment.