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

Filtering channels does not work #2594

Closed
AGSTRANGER opened this issue Dec 27, 2024 · 3 comments · Fixed by #2625
Closed

Filtering channels does not work #2594

AGSTRANGER opened this issue Dec 27, 2024 · 3 comments · Fixed by #2625
Labels
feature New capability

Comments

@AGSTRANGER
Copy link

AGSTRANGER commented Dec 27, 2024

Describe the bug

Filtering channels does not work. The user is able to send a message to all users by typing their names in the channels filter.

To Reproduce
<ChannelList filters={{ type: "messaging", members: { $in: [connectedUserData._id] }, }} showChannelSearch options={{ state: true, presence: true, limit: 8 }} sort={{ last_message_at: -1 }} additionalChannelSearchProps={{ searchForChannels: true, searchQueryParams: { channelFilters: { filters: { members: { $in: [connectedUserData._id] } }, }, }, }} Preview={ChannelPreviewCustom} />

Steps to reproduce the behavior:

  1. Type a letter in the channels filter
  2. You will see all users that have the letter in their names

Expected behavior

Only see channels the user is already a part of. In other words, only existing conversations that the user have.

Screenshots

image

I don't have a conversation with Ali. I shouldn't be able to see him when I type his name in the channels filter.

image

Additional context
In my app, a user has connections. So when he tries to send a message, he can only send a message to his connection. I have implemented this successfully like this:

`
useEffect(() => {
const fetchUsers = async () => {
if (!connectedUserData) {
setFetchError("Connected user data is not available");
return;
}

  setIsFetching(true);
  setFetchError(null);

  try {
    const response = await client.queryUsers(
      {
        $and: [
          { id: { $ne: connectedUserData._id } }, 
          { id: { $in: connectedUserData.connections.map(user => user) } }, 
        ],
        role: { $ne: "admin" },
        ...(searchInputDebounced
          ? {
              $or: [
                { name: { $autocomplete: searchInputDebounced } },
                { username: { $autocomplete: searchInputDebounced } },
              ],
            }
          : {}),
      },
      { name: 1, username: 1 },
      { limit: 15 }
    );
    setUsers(response.users);
  } catch (error) {
    console.error("Error fetching users", error);
    setFetchError("An error occurred while loading users.");
  } finally {
    setIsFetching(false);
  }
};

if (searchInputDebounced) {
  fetchUsers();
} else {
  setUsers([]); // Clear users when search input is empty
}

}, [searchInputDebounced, connectedUserData, client]);
`

I tried the same thing with filtering channels but it didn't work.
<ChannelList filters={{ type: "messaging", members:{ $and: [ { members: { $in: connectedUserData.connections.map(user => user)} }, // Existing condition:connectedUserData._idis inmembers{ id: { $ne: connectedUserData._id } }, // New condition:idis not equal toconnectedUserData._id], }, }} showChannelSearch options={{ state: true, presence: true, limit: 8 }} sort={{ last_message_at: -1 }} additionalChannelSearchProps={{ searchForChannels: true, searchQueryParams: { channelFilters: { filters: { $and: [ { members: { $in: connectedUserData.connections.map(user => user)} }, // Existing condition:connectedUserData._idis inmembers{ id: { $ne: connectedUserData._id } }, // New condition:idis not equal toconnectedUserData._id ], }, }, }, }} Preview={ChannelPreviewCustom} />

So I also I tried a simpler filter like the initial example I provided where I only filter by the connected user id but it did not work.
(While writing this, I realized it's redudant to filter channels by both connected user id and his connection requests. Since the user channels can be filtered by either of them. So one is enough.)

Package version

  • stream-chat-react: ^11.23.0
  • stream-chat ^8.37.0:
@AGSTRANGER AGSTRANGER added bug Something isn't working status: unconfirmed labels Dec 27, 2024
@AGSTRANGER AGSTRANGER changed the title bug: Filtering channels does not work Dec 27, 2024
@MartinCupela
Copy link
Contributor

Hello @AGSTRANGER , it is hard to read your snippets due to the poor formatting. These are the things that come to my mind:

  1. Please do not use $ne operator in your queries. It degrades the DB performance and the operator is deprecated.
  2. Users are not channels, they are different entities. So if you want to query a channel that contains only two users - current user + 1 another - you can have a filter
const filters: ChannelFilters = {
  members: { $in: [userId] },
  member_count: 2,
  type: 'messaging',
};
  1. It is not clear to me, what is the workflow of creating a new channel with another user that you do not have conversation with yet.

@MartinCupela MartinCupela added question Support request pending-reply and removed bug Something isn't working status: unconfirmed labels Jan 14, 2025
@AGSTRANGER
Copy link
Author

AGSTRANGER commented Jan 26, 2025

Hello again @MartinCupela ,
Thank you for your answer.

I will try to reexplain my problem.
Here the user Ghrib is one of my connections in the app and I already have a conversation with him. Seeing this conversation in the channels makes sense:

Image

Since I already have this filter:

  <ChannelList
          filters={{
            type: "messaging",
            members: { $in: connectedUserData.connections.map(user => user)},
          }}

The problem is with Channels search bar:
If I type the name of any user in the app, I can see his name, and I can create a new conversation with that user.
For example this user Ahmed406:

Image

I should not be able to do this. The only was to start a new channel is with the send new message button as this allow any user to create a new channel with any user in the app but with the create new message I only allow users to create conversations with users they're already connected with which works fine:

Image

const response = await client.queryUsers(
          {
            $and: [
              { id: { $ne: connectedUserData._id } }, 
              { id: { $in: connectedUserData.connections.map(user => user) } }, 
            ],
            role: { $ne: "admin" },
            ...(searchInputDebounced
              ? {
                  $or: [
                    { name: { $autocomplete: searchInputDebounced } },
                    { username: { $autocomplete: searchInputDebounced } },
                  ],
                }
              : {}),
          },
          { name: 1, username: 1 },
          { limit: 15 }
        );
        setUsers(response.users);

When I search a channel, I tried to filter the channels to show only the channels the user is a member in by using additionalChannelSearchProps:

        additionalChannelSearchProps={{
            searchForChannels: true,
            searchQueryParams: {
              channelFilters: {
                filters:{
                  members: { $in: [connectedUserData._id] },
                  member_count: 2,
                  type: 'messaging',
                }
              }
            },
          }}

But this doesn't work as I can use the search bar to start new channels with other users.

@AGSTRANGER
Copy link
Author

AGSTRANGER commented Feb 6, 2025

@MartinCupela I see that you guys shipped a fix for this.
Thank you so much!

I assume the search bar for channels will only search the channels the user is a part of from now on?

When will this fix be available on the latest version of the module?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New capability
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants