Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Message count is not changing after removing channel selection #30799

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/importer-messages-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@rocket.chat/meteor": patch
"@rocket.chat/core-typings": patch
---

chore: Message count is not changing after removing channel selection

✅ Fixed an issue where message count would not update after selecting users or channels for import
felipe-rod123 marked this conversation as resolved.
Show resolved Hide resolved
10 changes: 8 additions & 2 deletions apps/meteor/app/importer/server/classes/ImporterBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ImporterInfo } from '../../lib/ImporterInfo';
import { ProgressStep, ImportPreparingStartedStates } from '../../lib/ImporterProgressStep';
import { ImportDataConverter } from './ImportDataConverter';
import { Progress } from './ImporterProgress';
import { SelectionMessage } from './ImporterSelectionMessage';
import { ImporterWebsocket } from './ImporterWebsocket';

/**
Expand Down Expand Up @@ -344,6 +345,7 @@ export class Base {

const users = await ImportData.getAllUsersForSelection();
const channels = await ImportData.getAllChannelsForSelection();
const messages = await ImportData.getAllMessages().toArray();
const hasDM = await ImportData.checkIfDirectMessagesExists();

const selectionUsers = users.map(
Expand All @@ -362,13 +364,17 @@ export class Base {
c.data.t === 'd',
),
);
const selectionMessages = await ImportData.countMessages();

const selectionMessages = messages.map((m) => new SelectionMessage(m._id, m.data.rid, m.data.u));

const selectionMessagesCount = await ImportData.countMessages();
// In the future, iterate the cursor?

if (hasDM) {
selectionChannels.push(new SelectionChannel('__directMessages__', t('Direct_Messages'), false, true, true, undefined, true));
}

const results = new Selection(this.info.name, selectionUsers, selectionChannels, selectionMessages);
const results = new Selection(this.info.name, selectionUsers, selectionChannels, selectionMessagesCount, selectionMessages);

return results;
}
Expand Down
4 changes: 3 additions & 1 deletion apps/meteor/app/importer/server/classes/ImporterSelection.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ export class Selection {
* @param {SelectionUser[]} users the users which can be selected
* @param {SelectionChannel[]} channels the channels which can be selected
* @param {number} message_count the number of messages
* @param {SelectionMessage[]} messages the messages from this import (with only: _id, rid, u)
*/
constructor(name, users, channels, message_count) {
constructor(name, users, channels, message_count, messages = undefined) {
this.name = name;
this.users = users;
this.channels = channels;
this.message_count = message_count;
this.messages = messages; // not all importers use this
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* eslint-disable @typescript-eslint/naming-convention */
export class SelectionMessage {
message_id: string;

rid: string;

u: string;

/**
* Constructs a new selection message.
*
* @param {string} message_id
* @param {string} rid the rid of the channel
* @param {boolean} u user
*/
constructor(message_id: string, rid: string, u: string) {
this.message_id = message_id;
this.rid = rid;
this.u = u;
}
}
2 changes: 1 addition & 1 deletion apps/meteor/client/views/admin/import/NewImportPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ function NewImportPage() {
{fileType === 'upload' && (
<>
{maxFileSize > 0 ? (
<Callout type='warning' marginBlock='x16'>
<Callout type='warning' marginBlock='x16' color='default'>
felipe-rod123 marked this conversation as resolved.
Show resolved Hide resolved
{t('Importer_Upload_FileSize_Message', {
maxFileSize: formatMemorySize(maxFileSize),
})}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useTranslation } from '@rocket.chat/ui-contexts';
import type { FC, Dispatch, SetStateAction, ChangeEvent } from 'react';
import React, { useState, useCallback } from 'react';

type ChannelDescriptor = {
export type ChannelDescriptor = {
channel_id: string;
name: string;
is_archived: boolean;
Expand Down
37 changes: 25 additions & 12 deletions apps/meteor/client/views/admin/import/PrepareImportPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { numberFormat } from '../../../../lib/utils/stringUtils';
import Page from '../../../components/Page';
import PrepareChannels from './PrepareChannels';
import PrepareUsers from './PrepareUsers';
import { countMessages } from './countMessages';
import { useErrorHandler } from './useErrorHandler';

const waitFor = (fn, predicate) =>
Expand All @@ -40,13 +41,24 @@ function PrepareImportPage() {
const [isPreparing, setPreparing] = useSafely(useState(true));
const [progressRate, setProgressRate] = useSafely(useState(null));
const [status, setStatus] = useSafely(useState(null));
const [messageCount, setMessageCount] = useSafely(useState(0));
const [users, setUsers] = useState([]);
const [channels, setChannels] = useState([]);
const [messages, setMessages] = useState([]);
const [isImporting, setImporting] = useSafely(useState(false));

const usersCount = useMemo(() => users.filter(({ do_import }) => do_import).length, [users]);
const channelsCount = useMemo(() => channels.filter(({ do_import }) => do_import).length, [channels]);
// usersCount = selectedUsers.length
// channelsCount = selectedChannels.length
felipe-rod123 marked this conversation as resolved.
Show resolved Hide resolved
const selectedUsers = useMemo(() => {
return users.filter(({ do_import }) => do_import);
}, [users]);

const selectedChannels = useMemo(() => {
return channels.filter(({ do_import }) => do_import);
}, [channels]);

const messagesCount = useMemo(() => {
return countMessages(selectedUsers, selectedChannels, messages);
}, [selectedUsers, selectedChannels, messages]);

const router = useRouter();

Expand Down Expand Up @@ -81,9 +93,10 @@ function PrepareImportPage() {
return;
}

setMessageCount(data.message_count);
setUsers(data.users.map((user) => ({ ...user, do_import: true })));
setChannels(data.channels.map((channel) => ({ ...channel, do_import: true })));
setMessages(data.messages);

setPreparing(false);
setProgressRate(null);
} catch (error) {
Expand Down Expand Up @@ -139,7 +152,7 @@ function PrepareImportPage() {
};

loadCurrentOperation();
}, [getCurrentImportOperation, getImportFileData, handleError, router, setMessageCount, setPreparing, setProgressRate, setStatus, t]);
}, [getCurrentImportOperation, getImportFileData, handleError, messagesCount, router, setPreparing, setProgressRate, setStatus, t]);

const handleBackToImportsButtonClick = () => {
router.navigate('/admin/import');
Expand All @@ -163,8 +176,8 @@ function PrepareImportPage() {
const statusDebounced = useDebouncedValue(status, 100);

const handleMinimumImportData = !!(
(!usersCount && !channelsCount && !messageCount) ||
(!usersCount && !channelsCount && messageCount !== 0)
(!selectedUsers.length && !selectedChannels.length && !messagesCount) ||
(!selectedUsers.length && !selectedChannels.length && messagesCount !== 0)
);

return (
Expand All @@ -188,14 +201,14 @@ function PrepareImportPage() {
{!isPreparing && (
<Tabs flexShrink={0}>
<Tabs.Item selected={tab === 'users'} onClick={handleTabClick('users')}>
{t('Users')} <Badge>{usersCount}</Badge>
{t('Users')} <Badge>{selectedUsers.length}</Badge>
</Tabs.Item>
<Tabs.Item selected={tab === 'channels'} onClick={handleTabClick('channels')}>
{t('Channels')} <Badge>{channelsCount}</Badge>
{t('Channels')} <Badge>{selectedChannels.length}</Badge>
</Tabs.Item>
<Tabs.Item disabled>
{t('Messages')}
<Badge>{messageCount}</Badge>
<Badge>{messagesCount}</Badge>
</Tabs.Item>
</Tabs>
)}
Expand All @@ -214,9 +227,9 @@ function PrepareImportPage() {
)}
</>
)}
{!isPreparing && tab === 'users' && <PrepareUsers usersCount={usersCount} users={users} setUsers={setUsers} />}
{!isPreparing && tab === 'users' && <PrepareUsers usersCount={selectedUsers.length} users={users} setUsers={setUsers} />}
{!isPreparing && tab === 'channels' && (
<PrepareChannels channels={channels} channelsCount={channelsCount} setChannels={setChannels} />
<PrepareChannels channels={channels} channelsCount={selectedChannels.length} setChannels={setChannels} />
)}
</Margins>
</Box>
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/client/views/admin/import/PrepareUsers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useTranslation } from '@rocket.chat/ui-contexts';
import type { FC, Dispatch, SetStateAction, ChangeEvent } from 'react';
import React, { useState, useCallback } from 'react';

type UserDescriptor = {
export type UserDescriptor = {
user_id: string;
username: string;
email: string;
Expand Down
25 changes: 25 additions & 0 deletions apps/meteor/client/views/admin/import/countMessages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { ChannelDescriptor } from './PrepareChannels';
import type { UserDescriptor } from './PrepareUsers';

export type MessageDescriptor = {
_id?: string;
rid: string;
u: {
_id: string;
};
};

export const countMessages = (users: UserDescriptor[], channels: ChannelDescriptor[], messages: MessageDescriptor[]): number => {
let messageCount = 0;

const selectedUsers = new Set(users.map((user) => user.user_id));
const selectedChannels = new Set(channels.map((channel) => channel.channel_id));

for (const message of messages) {
if (selectedUsers.has(message.u._id) || selectedChannels.has(message.rid)) {
messageCount++;
}
}

return messageCount;
};
2 changes: 2 additions & 0 deletions packages/core-typings/src/import/IImportFileData.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { IImportChannel } from './IImportChannel';
import type { IImportMessage } from './IImportMessage';
import type { IImportUser } from './IImportUser';

export interface IImportFileData {
users: Array<IImportUser>;
channels: Array<IImportChannel>;
message_count: number;
messages: Array<Pick<IImportMessage, '_id' | 'rid' | 'u'>>;
}
Loading