diff --git a/src/config.ts b/src/config.ts index d77736f50..2946c632f 100644 --- a/src/config.ts +++ b/src/config.ts @@ -197,6 +197,7 @@ export const COUNTRIES_WITH_12H_TIME_FORMAT = new Set(['AU', 'BD', 'CA', 'CO', ' // MTProto constants export const SERVICE_NOTIFICATIONS_USER_ID = '777000'; +export const SERVICE_NOTIFICATIONS_NUMBER = '42777'; export const REPLIES_USER_ID = '1271266957'; // TODO For Test connection ID must be equal to 708513 export const ALL_FOLDER_ID = 0; export const ARCHIVED_FOLDER_ID = 1; diff --git a/src/global/actions/api/chats.ts b/src/global/actions/api/chats.ts index f6158b0c3..77ec733c4 100644 --- a/src/global/actions/api/chats.ts +++ b/src/global/actions/api/chats.ts @@ -15,7 +15,7 @@ import { CHAT_LIST_LOAD_SLICE, RE_TG_LINK, SERVICE_NOTIFICATIONS_USER_ID, - TMP_CHAT_ID, ALL_FOLDER_ID, DEBUG, + TMP_CHAT_ID, ALL_FOLDER_ID, DEBUG, SERVICE_NOTIFICATIONS_NUMBER, } from '../../../config'; import { callApi } from '../../../api/gramjs'; import { @@ -1068,14 +1068,14 @@ async function loadChats( listType: 'active' | 'archived', offsetId?: string, offsetDate?: number, shouldReplace = false, ) { let global = getGlobal(); - + const lastLocalServiceMessage = selectLastServiceNotification(global)?.message; const result = await callApi('fetchChats', { limit: CHAT_LIST_LOAD_SLICE, offsetDate, archived: listType === 'archived', withPinned: shouldReplace, serverTimeOffset: global.serverTimeOffset, - lastLocalServiceMessage: selectLastServiceNotification(global)?.message, + lastLocalServiceMessage, }); if (!result) { @@ -1091,6 +1091,18 @@ async function loadChats( global = getGlobal(); if (shouldReplace && listType === 'active') { + // Always include service notifications chat + if (!chatIds.includes(SERVICE_NOTIFICATIONS_USER_ID)) { + const chat = await callApi('getChatByPhoneNumber', SERVICE_NOTIFICATIONS_NUMBER); + global = getGlobal(); + if (chat) { + chatIds.unshift(chat.id); + result.chats.unshift(chat); + if (lastLocalServiceMessage) { + chat.lastMessage = lastLocalServiceMessage; + } + } + } const currentChat = selectCurrentChat(global); const visibleChats = currentChat ? [currentChat] : []; diff --git a/src/global/actions/apiUpdaters/messages.ts b/src/global/actions/apiUpdaters/messages.ts index 1188bc2d1..6fe93c900 100644 --- a/src/global/actions/apiUpdaters/messages.ts +++ b/src/global/actions/apiUpdaters/messages.ts @@ -4,8 +4,10 @@ import type { ApiChat, ApiMessage, ApiPollResult, ApiReactions, ApiThreadInfo, } from '../../../api/types'; +import type { ActiveEmojiInteraction, GlobalActions, GlobalState } from '../../types'; import { MAIN_THREAD_ID } from '../../../api/types'; +import { SERVICE_NOTIFICATIONS_USER_ID } from '../../../config'; import { unique } from '../../../util/iteratees'; import { areDeepEqual } from '../../../util/areDeepEqual'; import { notifyAboutMessage } from '../../../util/notifications'; @@ -21,7 +23,6 @@ import { deleteChatScheduledMessages, updateThreadUnreadFromForwardedMessage, } from '../../reducers'; -import type { ActiveEmojiInteraction, GlobalActions, GlobalState } from '../../types'; import { selectChatMessage, selectChatMessages, @@ -346,9 +347,22 @@ addActionHandler('apiUpdate', (global, actions, update) => { case 'deleteHistory': { const { chatId } = update; const chatMessages = global.messages.byChatId[chatId]; + if (chatId === SERVICE_NOTIFICATIONS_USER_ID) { + const lastNotification = global.serviceNotifications.sort((a, b) => b.id - a.id)?.[0]; + const serviceNotifications = lastNotification ? [{ + ...lastNotification, + isHidden: true, + }] : []; + + setGlobal({ + ...global, + serviceNotifications, + }); + } + if (chatMessages) { const ids = Object.keys(chatMessages.byId).map(Number); - deleteMessages(chatId, ids, actions, global); + deleteMessages(chatId, ids, actions, getGlobal()); } else { actions.requestChatUpdate({ chatId }); } diff --git a/src/global/selectors/messages.ts b/src/global/selectors/messages.ts index 5f8a5548d..0fb868cce 100644 --- a/src/global/selectors/messages.ts +++ b/src/global/selectors/messages.ts @@ -894,7 +894,7 @@ export function selectLastServiceNotification(global: GlobalState) { const { serviceNotifications } = global; const maxId = Math.max(...serviceNotifications.map(({ id }) => id)); - return serviceNotifications.find(({ id }) => id === maxId); + return serviceNotifications.find(({ id, isHidden }) => !isHidden && id === maxId); } export function selectIsMessageProtected(global: GlobalState, message?: ApiMessage) { diff --git a/src/global/types.ts b/src/global/types.ts index 2f1bc5a8b..66711eba3 100644 --- a/src/global/types.ts +++ b/src/global/types.ts @@ -122,6 +122,7 @@ export interface ServiceNotification { message: ApiMessage; version?: string; isUnread?: boolean; + isHidden?: boolean; } export type ApiLimitType = (