Chat List: Consider draft date in ordering

This commit is contained in:
Alexander Zinchuk 2021-07-03 16:10:02 +03:00
parent 7c860f27b3
commit 86053cefaf
10 changed files with 20 additions and 19 deletions

View File

@ -76,7 +76,7 @@ export function buildApiChatFromDialog(
unreadMentionsCount,
isMuted,
...(unreadMark && { hasUnreadMark: true }),
...(draft instanceof GramJs.DraftMessage && { hasDraft: true }),
...(draft instanceof GramJs.DraftMessage && { draftDate: draft.date }),
...buildApiChatFieldsFromPeerEntity(peerEntity),
};
}

View File

@ -188,9 +188,14 @@ export function buildMessageDraft(draft: GramJs.TypeDraftMessage) {
return undefined;
}
const {
message, entities, replyToMsgId, date,
} = draft;
return {
formattedText: draft.message ? buildMessageTextContent(draft.message, draft.entities) : undefined,
replyingToId: draft.replyToMsgId,
formattedText: message ? buildMessageTextContent(message, entities) : undefined,
replyingToId: replyToMsgId,
date,
};
}

View File

@ -982,7 +982,7 @@ function updateLocalDb(result: (
}
}
export async function importChatInvite({ hash }: {hash: string}) {
export async function importChatInvite({ hash }: { hash: string }) {
const updates = await invokeRequest(new GramJs.messages.ImportChatInvite({ hash }), true);
if (!(updates instanceof GramJs.Updates) || !updates.chats.length) {
return undefined;

View File

@ -771,12 +771,10 @@ export function updater(update: Update, originRequest?: GramJs.AnyRequest) {
// Misc
} else if (update instanceof GramJs.UpdateDraftMessage) {
const { replyingToId, formattedText } = buildMessageDraft(update.draft) || {};
onUpdate({
'@type': 'draftMessage',
chatId: getApiChatIdFromMtpPeer(update.peer),
formattedText,
replyingToId,
...buildMessageDraft(update.draft),
});
} else if (update instanceof GramJs.UpdateContactsReset) {
onUpdate({ '@type': 'updateResetContactList' });

View File

@ -29,7 +29,7 @@ export interface ApiChat {
joinDate?: number;
isSupport?: boolean;
photos?: ApiPhoto[];
hasDraft?: boolean;
draftDate?: number;
// Calls
isCallActive?: boolean;

View File

@ -268,6 +268,7 @@ export type ApiUpdateDraftMessage = {
'@type': 'draftMessage';
chatId: number;
formattedText?: ApiFormattedText;
date?: number;
replyingToId?: number;
};

View File

@ -300,7 +300,7 @@ addReducer('saveDraft', (global, actions, payload) => {
}
global = replaceThreadParam(global, chatId, threadId, 'draft', draft);
global = updateChat(global, chatId, { hasDraft: true });
global = updateChat(global, chatId, { draftDate: Math.round(Date.now() / 1000) });
return global;
});
@ -318,7 +318,7 @@ addReducer('clearDraft', (global, actions, payload) => {
}
global = replaceThreadParam(global, chatId, threadId, 'draft', undefined);
global = updateChat(global, chatId, { hasDraft: false });
global = updateChat(global, chatId, { draftDate: undefined });
return global;
});

View File

@ -21,7 +21,6 @@ import {
updateChatListSecondaryInfo,
updateThreadInfos,
replaceThreadParam,
updateChat,
} from '../../reducers';
import {
selectUser, selectChat, selectCurrentMessageList, selectDraft, selectChatMessage, selectThreadInfo,
@ -143,10 +142,7 @@ async function loadAndReplaceChats() {
global = updateChatListSecondaryInfo(global, 'active', result);
Object.keys(result.draftsById).map(Number).forEach((chatId) => {
global = replaceThreadParam(
global, chatId, MAIN_THREAD_ID, 'draft', result.draftsById[chatId],
);
global = updateChat(global, chatId, { hasDraft: Boolean(result.draftsById[chatId]) });
global = replaceThreadParam(global, chatId, MAIN_THREAD_ID, 'draft', result.draftsById[chatId]);
});
Object.keys(result.replyingToById).map(Number).forEach((chatId) => {

View File

@ -363,13 +363,15 @@ addReducer('apiUpdate', (global, actions, update: ApiUpdate) => {
}
case 'draftMessage': {
const { chatId, formattedText, replyingToId } = update;
const {
chatId, formattedText, date, replyingToId,
} = update;
const chat = global.chats.byId[chatId];
if (chat) {
global = replaceThreadParam(global, chatId, MAIN_THREAD_ID, 'draft', formattedText);
global = replaceThreadParam(global, chatId, MAIN_THREAD_ID, 'replyingToId', replyingToId);
global = updateChat(global, chatId, { hasDraft: Boolean(formattedText) });
global = updateChat(global, chatId, { draftDate: date });
setGlobal(global);
}

View File

@ -196,9 +196,8 @@ export function getChatSlowModeOptions(chat?: ApiChat) {
export function getChatOrder(chat: ApiChat) {
return Math.max(
chat.joinDate || 0,
chat.draftDate || 0,
chat.lastMessage ? chat.lastMessage.date : 0,
) + (
chat.hasDraft ? Date.now() / 1000 : 0
);
}