From 79cde7292beccb9e4d74201e316da2db3e8ef3e7 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Thu, 6 May 2021 01:47:44 +0300 Subject: [PATCH] Group Chat Info: Fix online members count --- src/api/gramjs/methods/chats.ts | 21 ++------------------- src/api/gramjs/methods/index.ts | 2 +- src/api/types/chats.ts | 3 +-- src/components/common/GroupChatInfo.tsx | 11 +++-------- src/global/types.ts | 2 +- src/modules/actions/api/chats.ts | 11 ----------- src/modules/selectors/chats.ts | 19 +++++++------------ 7 files changed, 15 insertions(+), 54 deletions(-) diff --git a/src/api/gramjs/methods/chats.ts b/src/api/gramjs/methods/chats.ts index 22a72d41..ff8b716a 100644 --- a/src/api/gramjs/methods/chats.ts +++ b/src/api/gramjs/methods/chats.ts @@ -163,25 +163,6 @@ export function fetchFullChat(chat: ApiChat) { : getFullChatInfo(input as number); } -export async function fetchSuperGroupOnlines(chat: ApiChat) { - const { id, accessHash } = chat; - - const peer = buildInputPeer(id, accessHash); - const result = await invokeRequest(new GramJs.messages.GetOnlines({ peer })); - - if (!result) { - return; - } - - const { onlines } = result; - - onUpdate({ - '@type': 'updateChat', - id, - chat: { onlineCount: onlines }, - }); -} - export async function searchChats({ query, limit }: { query: string; limit?: number }) { const result = await invokeRequest(new GramJs.contacts.Search({ q: query, limit })); if (!result) { @@ -358,6 +339,7 @@ async function getFullChannelInfo( const { about, + onlineCount, exportedInvite, slowmodeSeconds, slowmodeNextSendDate, @@ -383,6 +365,7 @@ async function getFullChannelInfo( return { fullInfo: { about, + onlineCount, inviteLink, slowMode: slowmodeSeconds ? { seconds: slowmodeSeconds, diff --git a/src/api/gramjs/methods/index.ts b/src/api/gramjs/methods/index.ts index 6ae1d87c..2596fc05 100644 --- a/src/api/gramjs/methods/index.ts +++ b/src/api/gramjs/methods/index.ts @@ -7,7 +7,7 @@ export { } from './auth'; export { - fetchChats, fetchFullChat, fetchSuperGroupOnlines, searchChats, requestChatUpdate, + fetchChats, fetchFullChat, searchChats, requestChatUpdate, saveDraft, clearDraft, fetchChat, updateChatMutedState, createChannel, joinChannel, leaveChannel, deleteChannel, createGroupChat, editChatPhoto, toggleChatPinned, toggleChatArchived, toggleDialogUnread, diff --git a/src/api/types/chats.ts b/src/api/types/chats.ts index 07d01463..3bd2033c 100644 --- a/src/api/types/chats.ts +++ b/src/api/types/chats.ts @@ -46,8 +46,6 @@ export interface ApiChat { // Obtained from GetFullChat / GetFullChannel fullInfo?: ApiChatFullInfo; - // Obtained from GetOnlines - onlineCount?: number; // Obtained with UpdateUserTyping or UpdateChatUserTyping updates typingStatus?: ApiTypingStatus; } @@ -60,6 +58,7 @@ export interface ApiTypingStatus { export interface ApiChatFullInfo { about?: string; + onlineCount?: number; members?: ApiChatMember[]; kickedMembers?: ApiChatMember[]; adminMembers?: ApiChatMember[]; diff --git a/src/components/common/GroupChatInfo.tsx b/src/components/common/GroupChatInfo.tsx index c0b24373..694b7834 100644 --- a/src/components/common/GroupChatInfo.tsx +++ b/src/components/common/GroupChatInfo.tsx @@ -39,7 +39,7 @@ type StateProps = { areMessagesLoaded: boolean; } & Pick; -type DispatchProps = Pick; +type DispatchProps = Pick; const GroupChatInfo: FC = ({ typingStatus, @@ -54,7 +54,6 @@ const GroupChatInfo: FC = ({ areMessagesLoaded, lastSyncTime, loadFullChat, - loadSuperGroupOnlines, openMediaViewer, }) => { const isSuperGroup = chat && isChatSuperGroup(chat); @@ -63,12 +62,8 @@ const GroupChatInfo: FC = ({ useEffect(() => { if (chatId && !isMin && withFullInfo && lastSyncTime) { loadFullChat({ chatId }); - - if (isSuperGroup) { - loadSuperGroupOnlines({ chatId }); - } } - }, [chatId, isMin, lastSyncTime, withFullInfo, loadFullChat, isSuperGroup, loadSuperGroupOnlines]); + }, [chatId, isMin, lastSyncTime, withFullInfo, loadFullChat, isSuperGroup]); const handleAvatarViewerOpen = useCallback((e: ReactMouseEvent, hasPhoto: boolean) => { if (chat && hasPhoto) { @@ -167,5 +162,5 @@ export default memo(withGlobal( lastSyncTime, chat, onlineCount, areMessagesLoaded, }; }, - (setGlobal, actions): DispatchProps => pick(actions, ['loadFullChat', 'loadSuperGroupOnlines', 'openMediaViewer']), + (setGlobal, actions): DispatchProps => pick(actions, ['loadFullChat', 'openMediaViewer']), )(GroupChatInfo)); diff --git a/src/global/types.ts b/src/global/types.ts index 43f13da8..ea072a8a 100644 --- a/src/global/types.ts +++ b/src/global/types.ts @@ -393,7 +393,7 @@ export type ActionTypes = ( // chats 'preloadTopChatMessages' | 'loadChats' | 'loadMoreChats' | 'openChat' | 'openChatWithInfo' | 'openSupportChat' | 'openTipsChat' | - 'loadFullChat' | 'loadSuperGroupOnlines' | 'loadTopChats' | 'requestChatUpdate' | 'updateChatMutedState' | + 'loadFullChat' | 'loadTopChats' | 'requestChatUpdate' | 'updateChatMutedState' | 'joinChannel' | 'leaveChannel' | 'deleteChannel' | 'toggleChatPinned' | 'toggleChatArchived' | 'toggleChatUnread' | 'loadChatFolders' | 'loadRecommendedChatFolders' | 'editChatFolder' | 'addChatFolder' | 'deleteChatFolder' | 'updateChat' | 'toggleSignatures' | 'loadGroupsForDiscussion' | 'linkDiscussionGroup' | 'unlinkDiscussionGroup' | diff --git a/src/modules/actions/api/chats.ts b/src/modules/actions/api/chats.ts index ab05e792..146e634f 100644 --- a/src/modules/actions/api/chats.ts +++ b/src/modules/actions/api/chats.ts @@ -53,7 +53,6 @@ const TMP_CHAT_ID = -1; const runThrottledForLoadChats = throttle((cb) => cb(), 1000, true); const runThrottledForLoadTopChats = throttle((cb) => cb(), 3000, true); const runDebouncedForFetchFullChat = debounce((cb) => cb(), 500, false, true); -const runDebouncedForFetchOnlines = debounce((cb) => cb(), 500, false, true); addReducer('preloadTopChatMessages', (global, actions) => { (async () => { @@ -178,16 +177,6 @@ addReducer('loadFullChat', (global, actions, payload) => { } }); -addReducer('loadSuperGroupOnlines', (global, actions, payload) => { - const { chatId } = payload!; - const chat = selectChat(global, chatId); - if (!chat) { - return; - } - - runDebouncedForFetchOnlines(() => callApi('fetchSuperGroupOnlines', chat)); -}); - addReducer('loadTopChats', () => { runThrottledForLoadTopChats(() => loadChats('active')); }); diff --git a/src/modules/selectors/chats.ts b/src/modules/selectors/chats.ts index 60900bdb..5dd43ccf 100644 --- a/src/modules/selectors/chats.ts +++ b/src/modules/selectors/chats.ts @@ -2,10 +2,10 @@ import { ApiChat, MAIN_THREAD_ID } from '../../api/types'; import { GlobalState } from '../../global/types'; import { - getPrivateChatUserId, isChatChannel, isChatPrivate, isChatSuperGroup, isHistoryClearMessage, isUserBot, isUserOnline, + getPrivateChatUserId, isChatChannel, isChatPrivate, isHistoryClearMessage, isUserBot, isUserOnline, } from '../helpers'; import { selectUser } from './users'; -import { ALL_FOLDER_ID, ARCHIVED_FOLDER_ID } from '../../config'; +import { ALL_FOLDER_ID, ARCHIVED_FOLDER_ID, CHANNEL_MEMBERS_LIMIT } from '../../config'; export function selectChat(global: GlobalState, chatId: number): ApiChat | undefined { return global.chats.byId[chatId]; @@ -34,21 +34,16 @@ export function selectSupportChat(global: GlobalState) { } export function selectChatOnlineCount(global: GlobalState, chat: ApiChat) { - if (isChatPrivate(chat.id) || isChatChannel(chat)) { + if (isChatPrivate(chat.id) || isChatChannel(chat) || !chat.fullInfo) { return undefined; } - if (isChatSuperGroup(chat)) { - return chat.onlineCount; + if (!chat.fullInfo.members || chat.fullInfo.members.length === CHANNEL_MEMBERS_LIMIT) { + return chat.fullInfo.onlineCount; } - if (!chat.fullInfo || !chat.fullInfo.members) { - return undefined; - } - - const memberIds = chat.fullInfo.members.map((m) => m.userId); - return memberIds.reduce((onlineCount, memberId) => { - if (global.users.byId[memberId] && isUserOnline(global.users.byId[memberId])) { + return chat.fullInfo.members.reduce((onlineCount, { userId }) => { + if (global.users.byId[userId] && isUserOnline(global.users.byId[userId]) && userId !== global.currentUserId) { return onlineCount + 1; }