Group Chat Info: Fix online members count

This commit is contained in:
Alexander Zinchuk 2021-05-06 01:47:44 +03:00
parent ca65c04884
commit 79cde7292b
7 changed files with 15 additions and 54 deletions

View File

@ -163,25 +163,6 @@ export function fetchFullChat(chat: ApiChat) {
: getFullChatInfo(input as number); : 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 }) { export async function searchChats({ query, limit }: { query: string; limit?: number }) {
const result = await invokeRequest(new GramJs.contacts.Search({ q: query, limit })); const result = await invokeRequest(new GramJs.contacts.Search({ q: query, limit }));
if (!result) { if (!result) {
@ -358,6 +339,7 @@ async function getFullChannelInfo(
const { const {
about, about,
onlineCount,
exportedInvite, exportedInvite,
slowmodeSeconds, slowmodeSeconds,
slowmodeNextSendDate, slowmodeNextSendDate,
@ -383,6 +365,7 @@ async function getFullChannelInfo(
return { return {
fullInfo: { fullInfo: {
about, about,
onlineCount,
inviteLink, inviteLink,
slowMode: slowmodeSeconds ? { slowMode: slowmodeSeconds ? {
seconds: slowmodeSeconds, seconds: slowmodeSeconds,

View File

@ -7,7 +7,7 @@ export {
} from './auth'; } from './auth';
export { export {
fetchChats, fetchFullChat, fetchSuperGroupOnlines, searchChats, requestChatUpdate, fetchChats, fetchFullChat, searchChats, requestChatUpdate,
saveDraft, clearDraft, fetchChat, updateChatMutedState, saveDraft, clearDraft, fetchChat, updateChatMutedState,
createChannel, joinChannel, leaveChannel, deleteChannel, createGroupChat, editChatPhoto, createChannel, joinChannel, leaveChannel, deleteChannel, createGroupChat, editChatPhoto,
toggleChatPinned, toggleChatArchived, toggleDialogUnread, toggleChatPinned, toggleChatArchived, toggleDialogUnread,

View File

@ -46,8 +46,6 @@ export interface ApiChat {
// Obtained from GetFullChat / GetFullChannel // Obtained from GetFullChat / GetFullChannel
fullInfo?: ApiChatFullInfo; fullInfo?: ApiChatFullInfo;
// Obtained from GetOnlines
onlineCount?: number;
// Obtained with UpdateUserTyping or UpdateChatUserTyping updates // Obtained with UpdateUserTyping or UpdateChatUserTyping updates
typingStatus?: ApiTypingStatus; typingStatus?: ApiTypingStatus;
} }
@ -60,6 +58,7 @@ export interface ApiTypingStatus {
export interface ApiChatFullInfo { export interface ApiChatFullInfo {
about?: string; about?: string;
onlineCount?: number;
members?: ApiChatMember[]; members?: ApiChatMember[];
kickedMembers?: ApiChatMember[]; kickedMembers?: ApiChatMember[];
adminMembers?: ApiChatMember[]; adminMembers?: ApiChatMember[];

View File

@ -39,7 +39,7 @@ type StateProps = {
areMessagesLoaded: boolean; areMessagesLoaded: boolean;
} & Pick<GlobalState, 'lastSyncTime'>; } & Pick<GlobalState, 'lastSyncTime'>;
type DispatchProps = Pick<GlobalActions, 'loadFullChat' | 'loadSuperGroupOnlines' | 'openMediaViewer'>; type DispatchProps = Pick<GlobalActions, 'loadFullChat' | 'openMediaViewer'>;
const GroupChatInfo: FC<OwnProps & StateProps & DispatchProps> = ({ const GroupChatInfo: FC<OwnProps & StateProps & DispatchProps> = ({
typingStatus, typingStatus,
@ -54,7 +54,6 @@ const GroupChatInfo: FC<OwnProps & StateProps & DispatchProps> = ({
areMessagesLoaded, areMessagesLoaded,
lastSyncTime, lastSyncTime,
loadFullChat, loadFullChat,
loadSuperGroupOnlines,
openMediaViewer, openMediaViewer,
}) => { }) => {
const isSuperGroup = chat && isChatSuperGroup(chat); const isSuperGroup = chat && isChatSuperGroup(chat);
@ -63,12 +62,8 @@ const GroupChatInfo: FC<OwnProps & StateProps & DispatchProps> = ({
useEffect(() => { useEffect(() => {
if (chatId && !isMin && withFullInfo && lastSyncTime) { if (chatId && !isMin && withFullInfo && lastSyncTime) {
loadFullChat({ chatId }); loadFullChat({ chatId });
if (isSuperGroup) {
loadSuperGroupOnlines({ chatId });
}
} }
}, [chatId, isMin, lastSyncTime, withFullInfo, loadFullChat, isSuperGroup, loadSuperGroupOnlines]); }, [chatId, isMin, lastSyncTime, withFullInfo, loadFullChat, isSuperGroup]);
const handleAvatarViewerOpen = useCallback((e: ReactMouseEvent<HTMLDivElement, MouseEvent>, hasPhoto: boolean) => { const handleAvatarViewerOpen = useCallback((e: ReactMouseEvent<HTMLDivElement, MouseEvent>, hasPhoto: boolean) => {
if (chat && hasPhoto) { if (chat && hasPhoto) {
@ -167,5 +162,5 @@ export default memo(withGlobal<OwnProps>(
lastSyncTime, chat, onlineCount, areMessagesLoaded, lastSyncTime, chat, onlineCount, areMessagesLoaded,
}; };
}, },
(setGlobal, actions): DispatchProps => pick(actions, ['loadFullChat', 'loadSuperGroupOnlines', 'openMediaViewer']), (setGlobal, actions): DispatchProps => pick(actions, ['loadFullChat', 'openMediaViewer']),
)(GroupChatInfo)); )(GroupChatInfo));

View File

@ -393,7 +393,7 @@ export type ActionTypes = (
// chats // chats
'preloadTopChatMessages' | 'loadChats' | 'loadMoreChats' | 'openChat' | 'openChatWithInfo' | 'preloadTopChatMessages' | 'loadChats' | 'loadMoreChats' | 'openChat' | 'openChatWithInfo' |
'openSupportChat' | 'openTipsChat' | 'openSupportChat' | 'openTipsChat' |
'loadFullChat' | 'loadSuperGroupOnlines' | 'loadTopChats' | 'requestChatUpdate' | 'updateChatMutedState' | 'loadFullChat' | 'loadTopChats' | 'requestChatUpdate' | 'updateChatMutedState' |
'joinChannel' | 'leaveChannel' | 'deleteChannel' | 'toggleChatPinned' | 'toggleChatArchived' | 'toggleChatUnread' | 'joinChannel' | 'leaveChannel' | 'deleteChannel' | 'toggleChatPinned' | 'toggleChatArchived' | 'toggleChatUnread' |
'loadChatFolders' | 'loadRecommendedChatFolders' | 'editChatFolder' | 'addChatFolder' | 'deleteChatFolder' | 'loadChatFolders' | 'loadRecommendedChatFolders' | 'editChatFolder' | 'addChatFolder' | 'deleteChatFolder' |
'updateChat' | 'toggleSignatures' | 'loadGroupsForDiscussion' | 'linkDiscussionGroup' | 'unlinkDiscussionGroup' | 'updateChat' | 'toggleSignatures' | 'loadGroupsForDiscussion' | 'linkDiscussionGroup' | 'unlinkDiscussionGroup' |

View File

@ -53,7 +53,6 @@ const TMP_CHAT_ID = -1;
const runThrottledForLoadChats = throttle((cb) => cb(), 1000, true); const runThrottledForLoadChats = throttle((cb) => cb(), 1000, true);
const runThrottledForLoadTopChats = throttle((cb) => cb(), 3000, true); const runThrottledForLoadTopChats = throttle((cb) => cb(), 3000, true);
const runDebouncedForFetchFullChat = debounce((cb) => cb(), 500, false, true); const runDebouncedForFetchFullChat = debounce((cb) => cb(), 500, false, true);
const runDebouncedForFetchOnlines = debounce((cb) => cb(), 500, false, true);
addReducer('preloadTopChatMessages', (global, actions) => { addReducer('preloadTopChatMessages', (global, actions) => {
(async () => { (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', () => { addReducer('loadTopChats', () => {
runThrottledForLoadTopChats(() => loadChats('active')); runThrottledForLoadTopChats(() => loadChats('active'));
}); });

View File

@ -2,10 +2,10 @@ import { ApiChat, MAIN_THREAD_ID } from '../../api/types';
import { GlobalState } from '../../global/types'; import { GlobalState } from '../../global/types';
import { import {
getPrivateChatUserId, isChatChannel, isChatPrivate, isChatSuperGroup, isHistoryClearMessage, isUserBot, isUserOnline, getPrivateChatUserId, isChatChannel, isChatPrivate, isHistoryClearMessage, isUserBot, isUserOnline,
} from '../helpers'; } from '../helpers';
import { selectUser } from './users'; 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 { export function selectChat(global: GlobalState, chatId: number): ApiChat | undefined {
return global.chats.byId[chatId]; return global.chats.byId[chatId];
@ -34,21 +34,16 @@ export function selectSupportChat(global: GlobalState) {
} }
export function selectChatOnlineCount(global: GlobalState, chat: ApiChat) { export function selectChatOnlineCount(global: GlobalState, chat: ApiChat) {
if (isChatPrivate(chat.id) || isChatChannel(chat)) { if (isChatPrivate(chat.id) || isChatChannel(chat) || !chat.fullInfo) {
return undefined; return undefined;
} }
if (isChatSuperGroup(chat)) { if (!chat.fullInfo.members || chat.fullInfo.members.length === CHANNEL_MEMBERS_LIMIT) {
return chat.onlineCount; return chat.fullInfo.onlineCount;
} }
if (!chat.fullInfo || !chat.fullInfo.members) { return chat.fullInfo.members.reduce((onlineCount, { userId }) => {
return undefined; if (global.users.byId[userId] && isUserOnline(global.users.byId[userId]) && userId !== global.currentUserId) {
}
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 onlineCount + 1; return onlineCount + 1;
} }