import {
buildPollResponseContent,
isPollStartType,
parsePollStart,
type PollStartContent,
} from "../poll-types.js"; import { withResolvedRoomAction } from "./client.js"; import type { MatrixActionClientOpts } from "./types.js";
function normalizeOptionIds(optionIds: string[]): string[] { return Array.from( new Set(optionIds.map((optionId) => optionId.trim()).filter((optionId) => optionId.length > 0)),
);
}
function resolveSelectedAnswerIds(params: {
optionIds?: string[];
optionIndexes?: number[];
pollContent: PollStartContent;
}): { answerIds: string[]; labels: string[]; maxSelections: number } { const parsed = parsePollStart(params.pollContent); if (!parsed) { thrownew Error("Matrix poll vote requires a valid poll start event.");
}
const selectedById = normalizeOptionIds(params.optionIds ?? []); const selectedByIndex = normalizeOptionIndexes(params.optionIndexes ?? []).map((index) => { const answer = parsed.answers[index - 1]; if (!answer) { thrownew Error(
`Matrix poll option index ${index} is out of range for a poll with ${parsed.answers.length} options.`,
);
} return answer.id;
});
const answerIds = normalizeOptionIds([...selectedById, ...selectedByIndex]); if (answerIds.length === 0) { thrownew Error("Matrix poll vote requires at least one poll option id or index.");
} if (answerIds.length > parsed.maxSelections) { thrownew Error(
`Matrix poll allows at most ${parsed.maxSelections} selection${parsed.maxSelections === 1 ? "" : "s"}.`,
);
}
const answerMap = new Map(parsed.answers.map((answer) => [answer.id, answer.text] as const)); const labels = answerIds.map((answerId) => { const label = answerMap.get(answerId); if (!label) { thrownew Error(
`Matrix poll option id "${answerId}" is not valid for poll ${parsed.question}.`,
);
} return label;
});
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.