Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/JAVA/Openclaw/extensions/matrix/src/matrix/actions/   (KI Agentensystem Version 22©)  Datei vom 26.3.2026 mit Größe 3 kB image not shown  

Quelle  polls.ts

  Sprache: JAVA
 

Spracherkennung für: .ts vermutete Sprache: Unknown {[0] [0] [0]} [Methode: Schwerpunktbildung, einfache Gewichte, sechs Dimensionen]

import {
  buildPollResponseContent,
  isPollStartType,
  parsePollStart,
  type PollStartContent,
} from "../poll-types.js";
import { withResolvedRoomAction } from "./client.js";
import type { MatrixActionClientOpts } from "./types.js";

function normalizeOptionIndexes(indexes: number[]): number[] {
  const normalized = indexes
    .map((index) => Math.trunc(index))
    .filter((index) => Number.isFinite(index) && index > 0);
  return Array.from(new Set(normalized));
}

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) {
    throw new 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) {
      throw new 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) {
    throw new Error("Matrix poll vote requires at least one poll option id or index.");
  }
  if (answerIds.length > parsed.maxSelections) {
    throw new 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) {
      throw new Error(
        `Matrix poll option id "${answerId}" is not valid for poll ${parsed.question}.`,
      );
    }
    return label;
  });

  return {
    answerIds,
    labels,
    maxSelections: parsed.maxSelections,
  };
}

export async function voteMatrixPoll(
  roomId: string,
  pollId: string,
  opts: MatrixActionClientOpts & {
    optionId?: string;
    optionIds?: string[];
    optionIndex?: number;
    optionIndexes?: number[];
  } = {},
) {
  return await withResolvedRoomAction(roomId, opts, async (client, resolvedRoom) => {
    const pollEvent = await client.getEvent(resolvedRoom, pollId);
    const eventType = typeof pollEvent.type === "string" ? pollEvent.type : "";
    if (!isPollStartType(eventType)) {
      throw new Error(`Event ${pollId} is not a Matrix poll start event.`);
    }

    const { answerIds, labels, maxSelections } = resolveSelectedAnswerIds({
      optionIds: [...(opts.optionIds ?? []), ...(opts.optionId ? [opts.optionId] : [])],
      optionIndexes: [
        ...(opts.optionIndexes ?? []),
        ...(opts.optionIndex !== undefined ? [opts.optionIndex] : []),
      ],
      pollContent: pollEvent.content as PollStartContent,
    });

    const content = buildPollResponseContent(pollId, answerIds);
    const eventId = await client.sendEvent(resolvedRoom, "m.poll.response", content);
    return {
      eventId: eventId ?? null,
      roomId: resolvedRoom,
      pollId,
      answerIds,
      labels,
      maxSelections,
    };
  });
}

¤ Dauer der Verarbeitung: 0.17 Sekunden  (vorverarbeitet am  2026-04-27) ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

Beweissystem der NASA

Beweissystem Isabelle

NIST Cobol Testsuite

Cephes Mathematical Library

Wiener Entwicklungsmethode

Haftungshinweis

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.