-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Chore: Migrate search and localSearch methods to Typescript #3751
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3020526
chore: migrate search to typescript
dnlsilva d74defe
Merge branch 'develop' into chore/migrate-search-to-typescript
dnlsilva fff2170
fix types
dnlsilva c9ce3d5
Merge branch 'develop' into chore/migrate-search-to-typescript
diegolmello be5dd51
Merge branch 'develop' into chore/migrate-search-to-typescript
dnlsilva 27b6fed
fix type
dnlsilva 1698d4a
Merge branch 'develop' into chore/migrate-search-to-typescript
dnlsilva File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import { ILastMessage } from './IMessage'; | ||
|
|
||
| export interface ISearchLocal { | ||
| avatarETag: string; | ||
| rid: string; | ||
| name: string; | ||
| t: string; | ||
| fname: string; | ||
| encrypted: boolean | null; | ||
| lastMessage?: ILastMessage; | ||
| } | ||
|
|
||
| export interface ISearch extends ISearchLocal { | ||
| // return only from api search | ||
| _id: string; | ||
| status?: string; | ||
| username: string; | ||
| outside?: boolean; | ||
| search?: boolean; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import { Q } from '@nozbe/watermelondb'; | ||
|
|
||
| import { sanitizeLikeString } from '../../database/utils'; | ||
| import database from '../../database/index'; | ||
| import { spotlight } from '../services/restApi'; | ||
| import isGroupChat from './isGroupChat'; | ||
| import { ISearch, ISearchLocal, SubscriptionType } from '../../../definitions'; | ||
|
|
||
| let debounce: null | ((reason: string) => void) = null; | ||
|
|
||
| export const localSearch = async ({ text = '', filterUsers = true, filterRooms = true }): Promise<ISearchLocal[]> => { | ||
| const searchText = text.trim(); | ||
| const db = database.active; | ||
| const likeString = sanitizeLikeString(searchText); | ||
| let subscriptions = await db | ||
| .get('subscriptions') | ||
| .query( | ||
| Q.or(Q.where('name', Q.like(`%${likeString}%`)), Q.where('fname', Q.like(`%${likeString}%`))), | ||
| Q.experimentalSortBy('room_updated_at', Q.desc) | ||
| ) | ||
| .fetch(); | ||
|
|
||
| if (filterUsers && !filterRooms) { | ||
| subscriptions = subscriptions.filter(item => item.t === 'd' && !isGroupChat(item)); | ||
| } else if (!filterUsers && filterRooms) { | ||
| subscriptions = subscriptions.filter(item => item.t !== 'd' || isGroupChat(item)); | ||
| } | ||
|
|
||
| const sliceSubscriptions = subscriptions.slice(0, 7); | ||
|
|
||
| const search = sliceSubscriptions.map(sub => ({ | ||
| rid: sub.rid, | ||
| name: sub.name, | ||
| fname: sub?.fname || '', | ||
| avatarETag: sub?.avatarETag || '', | ||
| t: sub.t, | ||
| encrypted: sub?.encrypted || null, | ||
| lastMessage: sub.lastMessage, | ||
| ...(sub.teamId && { teamId: sub.teamId }) | ||
| })); | ||
|
|
||
| return search; | ||
| }; | ||
|
|
||
| export const search = async ({ text = '', filterUsers = true, filterRooms = true }): Promise<(ISearch | ISearchLocal)[]> => { | ||
| const searchText = text.trim(); | ||
|
|
||
| if (debounce) { | ||
| debounce('cancel'); | ||
| } | ||
|
|
||
| const localSearchData = await localSearch({ text, filterUsers, filterRooms }); | ||
| const usernames = localSearchData.map(sub => sub.name); | ||
|
|
||
| const data: (ISearch | ISearchLocal)[] = localSearchData; | ||
|
|
||
| try { | ||
| if (localSearchData.length < 7) { | ||
| const { users, rooms } = (await Promise.race([ | ||
| spotlight(searchText, usernames, { users: filterUsers, rooms: filterRooms }), | ||
| new Promise((resolve, reject) => (debounce = reject)) | ||
| ])) as { users: ISearch[]; rooms: ISearch[] }; | ||
|
|
||
| if (filterUsers) { | ||
| users | ||
| .filter((item1, index) => users.findIndex(item2 => item2._id === item1._id) === index) // Remove duplicated data from response | ||
| .filter(user => !data.some(sub => user.username === sub.name)) // Make sure to remove users already on local database | ||
| .forEach(user => { | ||
| data.push({ | ||
| ...user, | ||
| rid: user.username, | ||
| name: user.username, | ||
| t: SubscriptionType.DIRECT, | ||
| search: true | ||
| }); | ||
| }); | ||
| } | ||
| if (filterRooms) { | ||
| rooms.forEach(room => { | ||
| // Check if it exists on local database | ||
| const index = data.findIndex(item => item.rid === room._id); | ||
| if (index === -1) { | ||
| data.push({ | ||
| ...room, | ||
| rid: room._id, | ||
| search: true | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| debounce = null; | ||
| return data; | ||
| } catch (e) { | ||
| console.warn(e); | ||
| return data; | ||
| } | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.