2022-03-08 14:24:21 +00:00
|
|
|
import { Q } from '@nozbe/watermelondb';
|
|
|
|
|
2022-04-07 16:53:07 +00:00
|
|
|
import { sanitizeLikeString } from '../database/utils';
|
|
|
|
import database from '../database/index';
|
2022-10-31 16:43:47 +00:00
|
|
|
import { store as reduxStore } from '../store/auxStore';
|
2022-03-08 14:24:21 +00:00
|
|
|
import { spotlight } from '../services/restApi';
|
2022-10-31 16:43:47 +00:00
|
|
|
import { ISearch, ISearchLocal, IUserMessage, SubscriptionType } from '../../definitions';
|
2022-04-04 19:15:29 +00:00
|
|
|
import { isGroupChat } from './helpers';
|
2022-03-08 14:24:21 +00:00
|
|
|
|
2022-10-31 16:43:47 +00:00
|
|
|
export type TSearch = ISearchLocal | IUserMessage | ISearch;
|
|
|
|
|
2022-03-08 14:24:21 +00:00
|
|
|
let debounce: null | ((reason: string) => void) = null;
|
|
|
|
|
2022-10-31 16:43:47 +00:00
|
|
|
export const localSearchSubscription = async ({ text = '', filterUsers = true, filterRooms = true }): Promise<ISearchLocal[]> => {
|
2022-03-08 14:24:21 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2022-09-23 01:39:52 +00:00
|
|
|
const search = subscriptions.slice(0, 7).map(item => ({
|
2022-09-27 18:08:44 +00:00
|
|
|
_id: item._id,
|
2022-09-23 01:39:52 +00:00
|
|
|
rid: item.rid,
|
|
|
|
name: item.name,
|
|
|
|
fname: item.fname,
|
2022-09-27 18:08:44 +00:00
|
|
|
avatarETag: item.avatarETag,
|
|
|
|
t: item.t,
|
2022-09-23 01:39:52 +00:00
|
|
|
encrypted: item.encrypted,
|
|
|
|
lastMessage: item.lastMessage,
|
2022-09-27 18:08:44 +00:00
|
|
|
status: item.status
|
2022-09-23 01:39:52 +00:00
|
|
|
})) as ISearchLocal[];
|
2022-03-08 14:24:21 +00:00
|
|
|
|
|
|
|
return search;
|
|
|
|
};
|
|
|
|
|
2022-10-31 16:43:47 +00:00
|
|
|
export const localSearchUsersMessageByRid = async ({ text = '', rid = '' }): Promise<IUserMessage[]> => {
|
|
|
|
const userId = reduxStore.getState().login.user.id;
|
|
|
|
const numberOfSuggestions = reduxStore.getState().settings.Number_of_users_autocomplete_suggestions as number;
|
|
|
|
const searchText = text.trim();
|
|
|
|
const db = database.active;
|
|
|
|
const likeString = sanitizeLikeString(searchText);
|
|
|
|
const messages = await db
|
|
|
|
.get('messages')
|
|
|
|
.query(
|
|
|
|
Q.and(Q.where('rid', rid), Q.where('u', Q.notLike(`%${userId}%`)), Q.where('t', null)),
|
|
|
|
Q.experimentalSortBy('ts', Q.desc),
|
|
|
|
Q.experimentalTake(50)
|
|
|
|
)
|
|
|
|
.fetch();
|
|
|
|
|
|
|
|
const regExp = new RegExp(`${likeString}`, 'i');
|
|
|
|
const users = messages.map(message => message.u);
|
|
|
|
|
|
|
|
const usersFromLocal = users
|
|
|
|
.filter((item1, index) => users.findIndex(item2 => item2._id === item1._id) === index) // Remove duplicated data from response
|
|
|
|
.filter(user => user?.name?.match(regExp) || user?.username?.match(regExp))
|
|
|
|
.slice(0, text ? 2 : numberOfSuggestions);
|
|
|
|
|
|
|
|
return usersFromLocal;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const search = async ({ text = '', filterUsers = true, filterRooms = true, rid = '' }): Promise<TSearch[]> => {
|
2022-03-08 14:24:21 +00:00
|
|
|
const searchText = text.trim();
|
|
|
|
|
|
|
|
if (debounce) {
|
|
|
|
debounce('cancel');
|
|
|
|
}
|
|
|
|
|
2022-10-31 16:43:47 +00:00
|
|
|
let localSearchData = [];
|
|
|
|
if (rid) {
|
|
|
|
localSearchData = await localSearchUsersMessageByRid({ text, rid });
|
|
|
|
} else {
|
|
|
|
localSearchData = await localSearchSubscription({ text, filterUsers, filterRooms });
|
|
|
|
}
|
|
|
|
const usernames = localSearchData.map(sub => sub.name as string);
|
2022-03-08 14:24:21 +00:00
|
|
|
|
2022-10-31 16:43:47 +00:00
|
|
|
const data: TSearch[] = localSearchData;
|
2022-03-08 14:24:21 +00:00
|
|
|
|
|
|
|
try {
|
2022-10-31 16:43:47 +00:00
|
|
|
if (searchText && localSearchData.length < 7) {
|
2022-03-08 14:24:21 +00:00
|
|
|
const { users, rooms } = (await Promise.race([
|
2022-10-31 16:43:47 +00:00
|
|
|
spotlight(searchText, usernames, { users: filterUsers, rooms: filterRooms }, rid),
|
2022-03-08 14:24:21 +00:00
|
|
|
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
|
2022-10-31 16:43:47 +00:00
|
|
|
.filter(user => !data.some(sub => 'username' in sub && user.username === sub.username)) // Make sure to remove users already on local database
|
2022-03-08 14:24:21 +00:00
|
|
|
.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
|
2022-10-31 16:43:47 +00:00
|
|
|
const index = data.findIndex(item => 'rid' in item && item.rid === room._id);
|
2022-03-08 14:24:21 +00:00
|
|
|
if (index === -1) {
|
|
|
|
data.push({
|
|
|
|
...room,
|
|
|
|
rid: room._id,
|
|
|
|
search: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debounce = null;
|
|
|
|
return data;
|
|
|
|
} catch (e) {
|
|
|
|
console.warn(e);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
};
|