Poll: Fix updates (#5167)

This commit is contained in:
zubiden 2024-11-09 15:43:32 +04:00 committed by Alexander Zinchuk
parent 7e83932c96
commit 2c482f82db
6 changed files with 72 additions and 19 deletions

View File

@ -53,7 +53,9 @@ import {
buildApiSendAsPeerId, buildApiSendAsPeerId,
} from '../apiBuilders/chats'; } from '../apiBuilders/chats';
import { buildApiFormattedText } from '../apiBuilders/common'; import { buildApiFormattedText } from '../apiBuilders/common';
import { buildMessageMediaContent, buildMessageTextContent, buildWebPage } from '../apiBuilders/messageContent'; import {
buildMessageMediaContent, buildMessageTextContent, buildPollFromMedia, buildWebPage,
} from '../apiBuilders/messageContent';
import { import {
buildApiFactCheck, buildApiFactCheck,
buildApiMessage, buildApiMessage,
@ -1920,6 +1922,7 @@ function handleLocalMessageUpdate(localMessage: ApiMessage, update: GramJs.TypeU
} }
let newContent: MediaContent | undefined; let newContent: MediaContent | undefined;
let poll: ApiPoll | undefined;
if (messageUpdate instanceof GramJs.UpdateShortSentMessage) { if (messageUpdate instanceof GramJs.UpdateShortSentMessage) {
if (localMessage.content.text && messageUpdate.entities) { if (localMessage.content.text && messageUpdate.entities) {
newContent = { newContent = {
@ -1933,6 +1936,7 @@ function handleLocalMessageUpdate(localMessage: ApiMessage, update: GramJs.TypeU
peerId: buildPeer(localMessage.chatId), id: messageUpdate.id, peerId: buildPeer(localMessage.chatId), id: messageUpdate.id,
}), }),
}; };
poll = buildPollFromMedia(messageUpdate.media);
} }
const mtpMessage = buildMessageFromUpdate(messageUpdate.id, localMessage.chatId, messageUpdate); const mtpMessage = buildMessageFromUpdate(messageUpdate.id, localMessage.chatId, messageUpdate);
@ -1960,6 +1964,7 @@ function handleLocalMessageUpdate(localMessage: ApiMessage, update: GramJs.TypeU
sendingState: undefined, sendingState: undefined,
...('date' in messageUpdate && { date: messageUpdate.date }), ...('date' in messageUpdate && { date: messageUpdate.date }),
}, },
poll,
}); });
handleGramJsUpdate(update); handleGramJsUpdate(update);

View File

@ -26,7 +26,7 @@ export function processAndUpdateEntities(response?: GramJs.AnyRequest['__respons
const polls: ApiPoll[] | undefined = []; const polls: ApiPoll[] | undefined = [];
if ('users' in response && Array.isArray(response.users) && TYPE_USER.has(response.users[0]?.className)) { if ('users' in response && Array.isArray(response.users) && TYPE_USER.has(response.users[0]?.className)) {
const users = response.users.map((user) => { const users = response.users.map((user: GramJs.TypeUser) => {
if (user instanceof GramJs.User) { if (user instanceof GramJs.User) {
addUserToLocalDb(user); addUserToLocalDb(user);
} }
@ -36,7 +36,7 @@ export function processAndUpdateEntities(response?: GramJs.AnyRequest['__respons
} }
if ('chats' in response && Array.isArray(response.chats) && TYPE_CHAT.has(response.chats[0]?.className)) { if ('chats' in response && Array.isArray(response.chats) && TYPE_CHAT.has(response.chats[0]?.className)) {
const chats = response.chats.map((chat) => { const chats = response.chats.map((chat: GramJs.TypeChat) => {
if ((chat instanceof GramJs.Chat || chat instanceof GramJs.Channel)) { if ((chat instanceof GramJs.Chat || chat instanceof GramJs.Channel)) {
addChatToLocalDb(chat); addChatToLocalDb(chat);
} }
@ -46,7 +46,7 @@ export function processAndUpdateEntities(response?: GramJs.AnyRequest['__respons
} }
if ('messages' in response && Array.isArray(response.messages) && TYPE_MESSAGE.has(response.messages[0]?.className)) { if ('messages' in response && Array.isArray(response.messages) && TYPE_MESSAGE.has(response.messages[0]?.className)) {
response.messages.forEach((message) => { response.messages.forEach((message: GramJs.TypeMessage) => {
addMessageToLocalDb(message); addMessageToLocalDb(message);
const threadInfo = buildApiThreadInfoFromMessage(message); const threadInfo = buildApiThreadInfoFromMessage(message);
@ -54,7 +54,7 @@ export function processAndUpdateEntities(response?: GramJs.AnyRequest['__respons
threadInfos.push(threadInfo); threadInfos.push(threadInfo);
} }
const poll = buildPollFromMedia(message.media); const poll = 'media' in message && message.media && buildPollFromMedia(message.media);
if (poll) { if (poll) {
polls.push(poll); polls.push(poll);
} }

View File

@ -2,7 +2,7 @@ import { Api as GramJs, connection } from '../../../lib/gramjs';
import type { GroupCallConnectionData } from '../../../lib/secret-sauce'; import type { GroupCallConnectionData } from '../../../lib/secret-sauce';
import type { import type {
ApiMessage, ApiStory, ApiStorySkipped, ApiMessage, ApiPoll, ApiStory, ApiStorySkipped,
ApiUpdateConnectionStateType, ApiUpdateConnectionStateType,
} from '../../types'; } from '../../types';
@ -34,6 +34,7 @@ import {
buildApiMessageExtendedMediaPreview, buildApiMessageExtendedMediaPreview,
buildBoughtMediaContent, buildBoughtMediaContent,
buildPoll, buildPoll,
buildPollFromMedia,
buildPollResults, buildPollResults,
} from '../apiBuilders/messageContent'; } from '../apiBuilders/messageContent';
import { import {
@ -125,6 +126,7 @@ export function updater(update: Update) {
|| update instanceof GramJs.UpdateShortMessage || update instanceof GramJs.UpdateShortMessage
) { ) {
let message: ApiMessage | undefined; let message: ApiMessage | undefined;
let poll: ApiPoll | undefined;
let shouldForceReply: boolean | undefined; let shouldForceReply: boolean | undefined;
if (update instanceof GramJs.UpdateShortChatMessage) { if (update instanceof GramJs.UpdateShortChatMessage) {
@ -132,8 +134,9 @@ export function updater(update: Update) {
} else if (update instanceof GramJs.UpdateShortMessage) { } else if (update instanceof GramJs.UpdateShortMessage) {
message = buildApiMessageFromShort(update); message = buildApiMessageFromShort(update);
} else { } else {
const mtpMessage = update.message;
// TODO Remove if proven not reproducing // TODO Remove if proven not reproducing
if (update.message instanceof GramJs.MessageEmpty) { if (mtpMessage instanceof GramJs.MessageEmpty) {
if (DEBUG) { if (DEBUG) {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.error('Unexpected update:', update.className, update); console.error('Unexpected update:', update.className, update);
@ -142,9 +145,13 @@ export function updater(update: Update) {
return; return;
} }
processMessageAndUpdateThreadInfo(update.message); processMessageAndUpdateThreadInfo(mtpMessage);
message = buildApiMessage(update.message)!; message = buildApiMessage(mtpMessage)!;
if (mtpMessage instanceof GramJs.Message) {
poll = mtpMessage.media && buildPollFromMedia(mtpMessage.media);
}
shouldForceReply = 'replyMarkup' in update.message shouldForceReply = 'replyMarkup' in update.message
&& update.message?.replyMarkup instanceof GramJs.ReplyKeyboardForceReply && update.message?.replyMarkup instanceof GramJs.ReplyKeyboardForceReply
@ -157,6 +164,7 @@ export function updater(update: Update) {
id: message.id, id: message.id,
chatId: message.chatId, chatId: message.chatId,
message, message,
poll,
}); });
} else { } else {
// We don't have preview for action or 'via bot' messages, so `newMessage` update here is required // We don't have preview for action or 'via bot' messages, so `newMessage` update here is required
@ -167,6 +175,7 @@ export function updater(update: Update) {
chatId: message.chatId, chatId: message.chatId,
message, message,
shouldForceReply, shouldForceReply,
poll,
}); });
} }
@ -302,8 +311,9 @@ export function updater(update: Update) {
update instanceof GramJs.UpdateEditMessage update instanceof GramJs.UpdateEditMessage
|| update instanceof GramJs.UpdateEditChannelMessage || update instanceof GramJs.UpdateEditChannelMessage
) { ) {
const mtpMessage = update.message;
// TODO Remove if proven not reproducing // TODO Remove if proven not reproducing
if (update.message instanceof GramJs.MessageEmpty) { if (mtpMessage instanceof GramJs.MessageEmpty) {
if (DEBUG) { if (DEBUG) {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.error('Unexpected update:', update.className, update); console.error('Unexpected update:', update.className, update);
@ -312,16 +322,20 @@ export function updater(update: Update) {
return; return;
} }
processMessageAndUpdateThreadInfo(update.message); processMessageAndUpdateThreadInfo(mtpMessage);
// Workaround for a weird server behavior when own message is marked as incoming // Workaround for a weird server behavior when own message is marked as incoming
const message = omit(buildApiMessage(update.message)!, ['isOutgoing']); const message = omit(buildApiMessage(mtpMessage)!, ['isOutgoing']);
const poll = mtpMessage instanceof GramJs.Message && mtpMessage.media
? buildPollFromMedia(mtpMessage.media) : undefined;
sendApiUpdate({ sendApiUpdate({
'@type': 'updateMessage', '@type': 'updateMessage',
id: message.id, id: message.id,
chatId: message.chatId, chatId: message.chatId,
message, message,
poll,
}); });
} else if (update instanceof GramJs.UpdateMessageReactions) { } else if (update instanceof GramJs.UpdateMessageReactions) {
sendApiUpdate({ sendApiUpdate({

View File

@ -223,6 +223,7 @@ export type ApiUpdateMessage = {
chatId: string; chatId: string;
id: number; id: number;
message: Partial<ApiMessage>; message: Partial<ApiMessage>;
poll?: ApiPoll;
}; };
export type ApiUpdateScheduledMessage = { export type ApiUpdateScheduledMessage = {
@ -230,12 +231,14 @@ export type ApiUpdateScheduledMessage = {
chatId: string; chatId: string;
id: number; id: number;
message: Partial<ApiMessage>; message: Partial<ApiMessage>;
poll?: ApiPoll;
}; };
export type ApiUpdateQuickReplyMessage = { export type ApiUpdateQuickReplyMessage = {
'@type': 'updateQuickReplyMessage'; '@type': 'updateQuickReplyMessage';
id: number; id: number;
message: Partial<ApiMessage>; message: Partial<ApiMessage>;
poll?: ApiPoll;
}; };
export type ApiUpdateDeleteQuickReplyMessages = { export type ApiUpdateDeleteQuickReplyMessages = {
@ -271,6 +274,7 @@ export type ApiUpdateScheduledMessageSendSucceeded = {
chatId: string; chatId: string;
localId: number; localId: number;
message: ApiMessage; message: ApiMessage;
poll?: ApiPoll;
}; };
export type ApiUpdateMessageSendSucceeded = { export type ApiUpdateMessageSendSucceeded = {
@ -278,6 +282,7 @@ export type ApiUpdateMessageSendSucceeded = {
chatId: string; chatId: string;
localId: number; localId: number;
message: ApiMessage; message: ApiMessage;
poll?: ApiPoll;
}; };
export type ApiUpdateMessageSendFailed = { export type ApiUpdateMessageSendFailed = {

View File

@ -39,10 +39,10 @@
.Checkbox, .Checkbox,
.Radio { .Radio {
padding-left: 2.25rem; padding-left: 2.25rem;
padding-bottom: 1rem; margin-bottom: 1.5rem;
&:last-child { &:last-child {
margin-bottom: 0; margin-bottom: 0.75rem;
} }
&:first-child { &:first-child {

View File

@ -239,7 +239,9 @@ addActionHandler('apiUpdate', (global, actions, update): ActionReturnType => {
} }
case 'updateMessage': { case 'updateMessage': {
const { chatId, id, message } = update; const {
chatId, id, message, poll,
} = update;
const currentMessage = selectChatMessage(global, chatId, id); const currentMessage = selectChatMessage(global, chatId, id);
const chat = selectChat(global, chatId); const chat = selectChat(global, chatId);
@ -258,13 +260,19 @@ addActionHandler('apiUpdate', (global, actions, update): ActionReturnType => {
global = clearMessageTranslation(global, chatId, id); global = clearMessageTranslation(global, chatId, id);
} }
if (poll) {
global = updatePoll(global, poll.id, poll);
}
setGlobal(global); setGlobal(global);
break; break;
} }
case 'updateScheduledMessage': { case 'updateScheduledMessage': {
const { chatId, id, message } = update; const {
chatId, id, message, poll,
} = update;
const currentMessage = selectScheduledMessage(global, chatId, id); const currentMessage = selectScheduledMessage(global, chatId, id);
if (!currentMessage) { if (!currentMessage) {
@ -280,15 +288,24 @@ addActionHandler('apiUpdate', (global, actions, update): ActionReturnType => {
const threadScheduledIds = selectScheduledIds(global, chatId, threadId) || []; const threadScheduledIds = selectScheduledIds(global, chatId, threadId) || [];
global = replaceThreadParam(global, chatId, threadId, 'scheduledIds', threadScheduledIds.sort((a, b) => b - a)); global = replaceThreadParam(global, chatId, threadId, 'scheduledIds', threadScheduledIds.sort((a, b) => b - a));
} }
if (poll) {
global = updatePoll(global, poll.id, poll);
}
setGlobal(global); setGlobal(global);
break; break;
} }
case 'updateQuickReplyMessage': { case 'updateQuickReplyMessage': {
const { id, message } = update; const { id, message, poll } = update;
global = updateQuickReplyMessage(global, id, message); global = updateQuickReplyMessage(global, id, message);
if (poll) {
global = updatePoll(global, poll.id, poll);
}
setGlobal(global); setGlobal(global);
break; break;
@ -319,7 +336,9 @@ addActionHandler('apiUpdate', (global, actions, update): ActionReturnType => {
} }
case 'updateMessageSendSucceeded': { case 'updateMessageSendSucceeded': {
const { chatId, localId, message } = update; const {
chatId, localId, message, poll,
} = update;
global = updateListedAndViewportIds(global, actions, message as ApiMessage); global = updateListedAndViewportIds(global, actions, message as ApiMessage);
@ -338,6 +357,10 @@ addActionHandler('apiUpdate', (global, actions, update): ActionReturnType => {
previousLocalId: localId, previousLocalId: localId,
}); });
if (poll) {
global = updatePoll(global, poll.id, poll);
}
global = { global = {
...global, ...global,
fileUploads: { fileUploads: {
@ -389,7 +412,9 @@ addActionHandler('apiUpdate', (global, actions, update): ActionReturnType => {
} }
case 'updateScheduledMessageSendSucceeded': { case 'updateScheduledMessageSendSucceeded': {
const { chatId, localId, message } = update; const {
chatId, localId, message, poll,
} = update;
const scheduledIds = selectScheduledIds(global, chatId, MAIN_THREAD_ID) || []; const scheduledIds = selectScheduledIds(global, chatId, MAIN_THREAD_ID) || [];
global = replaceThreadParam(global, chatId, MAIN_THREAD_ID, 'scheduledIds', [...scheduledIds, message.id]); global = replaceThreadParam(global, chatId, MAIN_THREAD_ID, 'scheduledIds', [...scheduledIds, message.id]);
@ -408,6 +433,10 @@ addActionHandler('apiUpdate', (global, actions, update): ActionReturnType => {
previousLocalId: localId, previousLocalId: localId,
}); });
if (poll) {
global = updatePoll(global, poll.id, poll);
}
setGlobal(global); setGlobal(global);
break; break;
} }