-
Notifications
You must be signed in to change notification settings - Fork 324
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: remove nin and ne operator usage in the SDK and the sample app #2672
Changes from 3 commits
989e317
de71d29
841d764
3c70c9c
b6ba16a
0b1bdf4
3cf72a7
a996e3b
f50d7d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,11 @@ import type React from 'react'; | |
|
||
import dayjs from 'dayjs'; | ||
import EmojiRegex from 'emoji-regex'; | ||
import type { DebouncedFunc } from 'lodash'; | ||
import { DebouncedFunc } from 'lodash'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's revert this one. I'm not sure if this change will lead to bundling the whole |
||
import debounce from 'lodash/debounce'; | ||
import type { | ||
Channel, | ||
ChannelMemberAPIResponse, | ||
ChannelMemberResponse, | ||
CommandResponse, | ||
FormatMessageResponse, | ||
MessageResponse, | ||
|
@@ -152,14 +151,7 @@ const getMembers = < | |
channel: Channel<StreamChatGenerics>, | ||
) => { | ||
const members = channel.state.members; | ||
|
||
return Object.values(members).length | ||
? ( | ||
Object.values(members).filter((member) => member.user) as Array< | ||
ChannelMemberResponse<StreamChatGenerics> & { user: UserResponse<StreamChatGenerics> } | ||
> | ||
).map((member) => member.user) | ||
: []; | ||
return members ? Object.values(members).map(({ user }) => user) : []; | ||
}; | ||
|
||
const getWatchers = < | ||
|
@@ -168,25 +160,28 @@ const getWatchers = < | |
channel: Channel<StreamChatGenerics>, | ||
) => { | ||
const watchers = channel.state.watchers; | ||
return Object.values(watchers).length ? [...Object.values(watchers)] : []; | ||
return watchers ? Object.values(watchers) : []; | ||
}; | ||
|
||
const getMembersAndWatchers = < | ||
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics, | ||
>( | ||
channel: Channel<StreamChatGenerics>, | ||
) => { | ||
const users = [...getMembers(channel), ...getWatchers(channel)]; | ||
const members = getMembers(channel); | ||
const watchers = getWatchers(channel); | ||
const users = [...members, ...watchers]; | ||
|
||
return Object.values( | ||
users.reduce((acc, cur) => { | ||
if (!acc[cur.id]) { | ||
acc[cur.id] = cur; | ||
} | ||
// make sure we don't list users twice | ||
const uniqueUsers = {} as Record<string, UserResponse<StreamChatGenerics>>; | ||
|
||
users.forEach((user) => { | ||
if (user && !uniqueUsers[user.id]) { | ||
uniqueUsers[user.id] = user; | ||
} | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unrelated, but I think you can speed up this function and save one full object iteration ( const seenUsers = new Set<string>();
const uniqueUsers: User[] = [];
for (const user of users) {
if (user && !seenUsers.has(user.id)) {
uniqueUsers.push(user);
seenUsers.add(user.id);
}
}
return uniqueUsers; |
||
|
||
return acc; | ||
}, {} as { [key: string]: SuggestionUser<StreamChatGenerics> }), | ||
); | ||
return Object.values(uniqueUsers); | ||
}; | ||
|
||
const queryMembers = async < | ||
|
@@ -234,32 +229,30 @@ const queryUsers = async < | |
mentionAllAppUsersQuery?: MentionAllAppUsersQuery<StreamChatGenerics>; | ||
} = {}, | ||
): Promise<void> => { | ||
if (typeof query === 'string') { | ||
if (!query) return; | ||
try { | ||
const { | ||
limit = defaultAutoCompleteSuggestionsLimit, | ||
mentionAllAppUsersQuery = defaultMentionAllAppUsersQuery, | ||
} = options; | ||
|
||
const filters = { | ||
id: { $ne: client.userID }, | ||
$or: [{ id: { $autocomplete: query } }, { name: { $autocomplete: query } }], | ||
...mentionAllAppUsersQuery?.filters, | ||
}; | ||
|
||
if (query) { | ||
// @ts-ignore | ||
filters.$or = [{ id: { $autocomplete: query } }, { name: { $autocomplete: query } }]; | ||
} | ||
|
||
const response = await client.queryUsers( | ||
const { users } = await client.queryUsers( | ||
// @ts-ignore | ||
filters, | ||
{ id: 1, ...mentionAllAppUsersQuery?.sort }, | ||
{ limit, ...mentionAllAppUsersQuery?.options }, | ||
); | ||
const users: SuggestionUser<StreamChatGenerics>[] = []; | ||
response.users.forEach((user) => isUserResponse(user) && users.push(user)); | ||
const usersWithoutClientUserId = users.filter((user) => user.id !== client.userID); | ||
if (onReady && users) { | ||
onReady(users); | ||
onReady(usersWithoutClientUserId); | ||
} | ||
} catch (error) { | ||
console.warn('Error querying users:', error); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we bubble this error up? How can one recover from an error that can happen here? |
||
} | ||
}; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again unrelated but, this is quite confusing to me. What does this code do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can remove it imo. Seems useless now