Fix syncing after editing draft on another device (again) (#2113)

This commit is contained in:
Alexander Zinchuk 2022-11-16 16:16:25 +04:00
parent ee85cb4441
commit 6fb1f87b17
3 changed files with 27 additions and 14 deletions

View File

@ -30,7 +30,7 @@ import {
selectChat, selectUser, selectChatListType, selectIsChatPinned,
selectChatFolder, selectSupportChat, selectChatByUsername, selectThreadTopMessageId,
selectCurrentMessageList, selectThreadInfo, selectCurrentChat, selectLastServiceNotification,
selectVisibleUsers, selectUserByPhoneNumber,
selectVisibleUsers, selectUserByPhoneNumber, selectDraft,
} from '../../selectors';
import { buildCollectionByKey, omit } from '../../../util/iteratees';
import { debounce, pause, throttle } from '../../../util/schedulers';
@ -209,7 +209,7 @@ addActionHandler('loadAllChats', async (global, actions, payload) => {
.sort((chat1, chat2) => getOrderDate(chat1)! - getOrderDate(chat2)!)[0]
: undefined;
await loadChats(listType, oldestChat?.id, oldestChat ? getOrderDate(oldestChat) : undefined, shouldReplace);
await loadChats(listType, oldestChat?.id, oldestChat ? getOrderDate(oldestChat) : undefined, shouldReplace, true);
if (shouldReplace) {
onReplace?.();
@ -1174,7 +1174,11 @@ addActionHandler('processAttachBotParameters', async (global, actions, payload)
});
async function loadChats(
listType: 'active' | 'archived', offsetId?: string, offsetDate?: number, shouldReplace = false,
listType: 'active' | 'archived',
offsetId?: string,
offsetDate?: number,
shouldReplace = false,
isFullDraftSync?: boolean,
) {
let global = getGlobal();
const lastLocalServiceMessage = selectLastServiceNotification(global)?.message;
@ -1245,13 +1249,17 @@ async function loadChats(
global = updateChatListSecondaryInfo(global, listType, result);
Object.keys(result.draftsById).forEach((chatId) => {
global = replaceThreadParam(
global, chatId, MAIN_THREAD_ID, 'draft', result.draftsById[chatId],
);
const idsToUpdateDraft = isFullDraftSync ? result.chatIds : Object.keys(result.draftsById);
idsToUpdateDraft.forEach((chatId) => {
if (!selectDraft(global, chatId, MAIN_THREAD_ID)?.isLocal) {
global = replaceThreadParam(
global, chatId, MAIN_THREAD_ID, 'draft', result.draftsById[chatId],
);
}
});
Object.keys(result.replyingToById).forEach((chatId) => {
const idsToUpdateReplyingToId = isFullDraftSync ? result.chatIds : Object.keys(result.replyingToById);
idsToUpdateReplyingToId.forEach((chatId) => {
global = replaceThreadParam(
global, chatId, MAIN_THREAD_ID, 'replyingToId', result.replyingToById[chatId],
);

View File

@ -326,30 +326,35 @@ addActionHandler('cancelSendingMessage', (global, actions, payload) => {
});
});
addActionHandler('saveDraft', (global, actions, payload) => {
addActionHandler('saveDraft', async (global, actions, payload) => {
const { chatId, threadId, draft } = payload!;
if (!draft) {
return undefined;
return;
}
const { text, entities } = draft;
const chat = selectChat(global, chatId)!;
const user = selectUser(global, chatId)!;
if (user && isDeletedUser(user)) return undefined;
if (user && isDeletedUser(user)) return;
if (threadId === MAIN_THREAD_ID) {
void callApi('saveDraft', {
const result = await callApi('saveDraft', {
chat,
text,
entities,
replyToMsgId: selectReplyingToId(global, chatId, threadId),
});
if (!result) {
draft.isLocal = true;
}
}
global = getGlobal();
global = replaceThreadParam(global, chatId, threadId, 'draft', draft);
global = updateChat(global, chatId, { draftDate: Math.round(Date.now() / 1000) });
return global;
setGlobal(global);
});
addActionHandler('clearDraft', (global, actions, payload) => {

View File

@ -113,7 +113,7 @@ export interface Thread {
editingScheduledId?: number;
editingDraft?: ApiFormattedText;
editingScheduledDraft?: ApiFormattedText;
draft?: ApiFormattedText;
draft?: ApiFormattedText & { isLocal?: boolean };
noWebPage?: boolean;
threadInfo?: ApiThreadInfo;
firstMessageId?: number;