mirror of
https://github.com/the-draupnir-project/Draupnir.git
synced 2026-05-13 19:13:19 +00:00
Fix prompt for accept.
Avoid presentation conflict while finding prestnation renderer for DeadDocuments https://github.com/the-draupnir-project/Draupnir/issues/403 Make sure we printReadably when serializing `ReadItem`s.
This commit is contained in:
@@ -3,8 +3,9 @@
|
||||
* All rights reserved.
|
||||
*/
|
||||
|
||||
import { MatrixRoomAlias, MatrixRoomID } from "matrix-protection-suite";
|
||||
import { DocumentNode } from "./DeadDocument"
|
||||
import { PresentationType } from "./ParameterParsing";
|
||||
import { PresentationType, findPresentationType, presentationTypeOf } from "./ParameterParsing";
|
||||
|
||||
type PresentationRenderer = (presentation: unknown) => DocumentNode;
|
||||
|
||||
@@ -26,3 +27,19 @@ export function findPresentationRenderer(presentationType: PresentationType): Pr
|
||||
}
|
||||
return entry;
|
||||
}
|
||||
|
||||
export const DeadDocumentPresentationMirror = Object.freeze({
|
||||
present(object: unknown): DocumentNode {
|
||||
if (object instanceof MatrixRoomID || object instanceof MatrixRoomAlias) {
|
||||
return findPresentationRenderer(findPresentationType('MatrixRoomReference'))(object)
|
||||
} else {
|
||||
const presentationType = presentationTypeOf(object);
|
||||
if (presentationType !== undefined) {
|
||||
const renderer = findPresentationRenderer(presentationType);
|
||||
return renderer(object);
|
||||
} else {
|
||||
throw new TypeError(`Unable to present: ${object}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
@@ -4,8 +4,7 @@
|
||||
*/
|
||||
|
||||
import { DocumentNode, LeafNode, makeDocumentNode, makeLeafNode, NodeTag, TextNode } from "./DeadDocument";
|
||||
import { findPresentationRenderer } from "./DeadDocumentPresentation";
|
||||
import { presentationTypeOf } from "./ParameterParsing";
|
||||
import { DeadDocumentPresentationMirror } from "./DeadDocumentPresentation";
|
||||
|
||||
type rawJSX = DocumentNode|LeafNode|string|number|Array<rawJSX>;
|
||||
|
||||
@@ -31,13 +30,7 @@ export function JSXFactory(tag: NodeTag, properties: unknown, ...rawChildren: (D
|
||||
node.addChild(rawChild);
|
||||
}
|
||||
} else {
|
||||
const presentationType = presentationTypeOf(rawChild);
|
||||
if (presentationType !== undefined) {
|
||||
const renderer = findPresentationRenderer(presentationType);
|
||||
node.addChild(renderer(rawChild));
|
||||
} else {
|
||||
throw new TypeError(`Unexpected raw child ${JSON.stringify(rawChild)}`);
|
||||
}
|
||||
node.addChild(DeadDocumentPresentationMirror.present(rawChild));
|
||||
}
|
||||
}
|
||||
rawChildren.forEach(ensureChild);
|
||||
|
||||
@@ -98,6 +98,7 @@ export class MatrixInterfaceAdaptor<C extends MatrixContext, ExecutorType extend
|
||||
} else {
|
||||
await promptSuggestions.call(matrixContext, parameter, this.interfaceCommand, promptOptions.suggestions, args);
|
||||
}
|
||||
return;
|
||||
} else {
|
||||
this.reportValidationError(matrixContext.client, matrixContext.roomID, matrixContext.event, executorResult.error);
|
||||
return;
|
||||
|
||||
@@ -8,7 +8,7 @@ import { findPresentationType, makePresentationType, simpleTypeValidator } from
|
||||
import { definePresentationRenderer } from "./DeadDocumentPresentation";
|
||||
import { JSXFactory } from "./JSXFactory";
|
||||
import { DocumentNode } from "./DeadDocument";
|
||||
import { MatrixEventViaAlias, MatrixEventViaRoomID, MatrixRoomAlias, MatrixRoomID, UserID } from "matrix-protection-suite";
|
||||
import { MatrixEventViaAlias, MatrixEventViaRoomID, MatrixRoomAlias, MatrixRoomID, MatrixRoomReference, UserID } from "matrix-protection-suite";
|
||||
|
||||
|
||||
makePresentationType({
|
||||
@@ -21,6 +21,10 @@ makePresentationType({
|
||||
validator: simpleTypeValidator('MatrixRoomReference', (item: ReadItem) => item instanceof MatrixRoomID || item instanceof MatrixRoomAlias),
|
||||
})
|
||||
|
||||
definePresentationRenderer(findPresentationType('MatrixRoomReference'), function(presentation: MatrixRoomReference): DocumentNode {
|
||||
return <a href={presentation.toPermalink()}>{presentation.toRoomIDOrAlias()}</a>
|
||||
})
|
||||
|
||||
makePresentationType({
|
||||
name: 'MatrixRoomID',
|
||||
validator: simpleTypeValidator('MatrixRoomID', (item: ReadItem) => item instanceof MatrixRoomID)
|
||||
|
||||
@@ -13,6 +13,7 @@ import { MatrixSendClient } from "matrix-protection-suite-for-matrix-bot-sdk";
|
||||
import { MatrixReactionHandler, ReactionListener } from "./MatrixReactionHandler";
|
||||
import { StaticDecode, Type } from "@sinclair/typebox";
|
||||
import { ReadItem, readCommand } from "./CommandReader";
|
||||
import { printReadably } from "./PrintReadably";
|
||||
|
||||
const log = new Logger('MatrixPromptForAccept');
|
||||
|
||||
@@ -150,8 +151,8 @@ export async function promptDefault<PresentationType extends ReadItem>(
|
||||
reactionMap,
|
||||
{
|
||||
command_designator: command.designator,
|
||||
read_items: existingArguments.map(item => item.toString()),
|
||||
default: defaultPrompt.toString()
|
||||
read_items: existingArguments.map(printReadably),
|
||||
default: printReadably(defaultPrompt)
|
||||
}
|
||||
)
|
||||
);
|
||||
@@ -173,7 +174,7 @@ export async function promptSuggestions(
|
||||
existingArguments: ReadItem[],
|
||||
): Promise<void> {
|
||||
const reactionMap = MatrixReactionHandler.createItemizedReactionMap(
|
||||
suggestions.map(item => item.toString())
|
||||
suggestions.map(printReadably)
|
||||
);
|
||||
const events = await renderMatrixAndSend(
|
||||
<root>Please select one of the following options to provide as an argument for the parameter <code>{parameter.name}</code>:
|
||||
@@ -190,7 +191,7 @@ export async function promptSuggestions(
|
||||
ARGUMENT_PROMPT_LISTENER,
|
||||
reactionMap,
|
||||
{
|
||||
read_items: existingArguments,
|
||||
read_items: existingArguments.map(printReadably),
|
||||
command_designator: command.designator
|
||||
}
|
||||
)
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
// SPDX-FileCopyrightText: 2024 Gnuxie <Gnuxie@protonmail.com>
|
||||
//
|
||||
// SPDX-License-Identifier: AFL-3.0
|
||||
|
||||
import { MatrixEventViaAlias, MatrixEventViaRoomID, MatrixRoomAlias, MatrixRoomID, Permalinks, UserID } from "matrix-protection-suite";
|
||||
import { ReadItem } from "./CommandReader";
|
||||
|
||||
export function printReadably(item: ReadItem): string {
|
||||
if (item instanceof MatrixRoomID || item instanceof MatrixRoomAlias) {
|
||||
return item.toPermalink();
|
||||
} else if (item instanceof UserID) {
|
||||
return item.toString();
|
||||
} else if (item instanceof MatrixEventViaAlias || item instanceof MatrixEventViaRoomID) {
|
||||
return Permalinks.forEvent(item.reference.toRoomIDOrAlias(), item.eventID, item.reference.getViaServers());
|
||||
} else {
|
||||
return item.toString()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user