Initial Poll message receive support

This commit is contained in:
yash-signal
2025-09-18 11:06:43 -05:00
committed by GitHub
parent 976a3135e5
commit 93ae2a4c48
15 changed files with 1072 additions and 6 deletions

View File

@@ -2,6 +2,7 @@
// SPDX-License-Identifier: AGPL-3.0-only
import { isNumber } from 'lodash';
import type { z } from 'zod';
import { createLogger } from '../logging/log.js';
import * as Errors from '../types/errors.js';
@@ -56,6 +57,8 @@ import {
} from '../util/modifyTargetMessage.js';
import { saveAndNotify } from './saveAndNotify.js';
import { MessageModel } from '../models/messages.js';
import { safeParsePartial } from '../util/schemas.js';
import { PollCreateSchema, isPollReceiveEnabled } from '../types/Polls.js';
import type { SentEventData } from '../textsecure/messageReceiverEvents.js';
import type {
@@ -481,6 +484,35 @@ export async function handleDataMessage(
}
}
let validatedPollCreate: z.infer<typeof PollCreateSchema> | undefined;
if (initialMessage.pollCreate) {
if (!isPollReceiveEnabled()) {
log.warn(`${idLog}: Dropping PollCreate because flag is not enabled`);
confirm();
return;
}
if (!isGroup(conversation.attributes)) {
log.warn(
`${idLog}: Dropping PollCreate in non-group conversation ${conversation.idForLogging()}`
);
confirm();
return;
}
const result = safeParsePartial(
PollCreateSchema,
initialMessage.pollCreate
);
if (!result.success) {
log.warn(
`${idLog}: Dropping invalid PollCreate:`,
result.error.flatten()
);
confirm();
return;
}
validatedPollCreate = result.data;
}
const withQuoteReference = {
...message.attributes,
...initialMessage,
@@ -576,6 +608,14 @@ export async function handleDataMessage(
quote: dataMessage.quote,
schemaVersion: dataMessage.schemaVersion,
sticker: dataMessage.sticker,
poll: validatedPollCreate
? {
question: validatedPollCreate.question,
options: validatedPollCreate.options,
allowMultiple: Boolean(validatedPollCreate.allowMultiple),
votes: [],
}
: undefined,
storyId: dataMessage.storyId,
});