2022-03-08 14:24:21 +00:00
|
|
|
import { Q } from '@nozbe/watermelondb';
|
|
|
|
|
2023-05-29 15:03:24 +00:00
|
|
|
import { sanitizeLikeString, slugifyLikeString } from '../database/utils';
|
2022-04-07 16:53:07 +00:00
|
|
|
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';
|
2023-08-04 14:09:36 +00:00
|
|
|
import { ISearch, ISearchLocal, IUserMessage, SubscriptionType, TSubscriptionModel } from '../../definitions';
|
|
|
|
import { isGroupChat, isReadOnly } 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;
|
|
|
|
|
2023-08-04 14:09:36 +00:00
|
|
|
export const localSearchSubscription = async ({
|
|
|
|
text = '',
|
|
|
|
filterUsers = true,
|
|
|
|
filterRooms = true,
|
|
|
|
filterMessagingAllowed = false
|
|
|
|
}): Promise<ISearchLocal[]> => {
|
2022-03-08 14:24:21 +00:00
|
|
|
const searchText = text.trim();
|
|
|
|
const db = database.active;
|
|
|
|
const likeString = sanitizeLikeString(searchText);
|
2023-05-29 15:03:24 +00:00
|
|
|
const slugifiedString = slugifyLikeString(searchText);
|
2022-03-08 14:24:21 +00:00
|
|
|
let subscriptions = await db
|
|
|
|
.get('subscriptions')
|
|
|
|
.query(
|
2023-05-29 15:03:24 +00:00
|
|
|
Q.or(
|
|
|
|
// `sanitized_fname` is an optional column, so it's going to start null and it's going to get filled over time
|
|
|
|
Q.where('sanitized_fname', Q.like(`%${slugifiedString}%`)),
|
|
|
|
// TODO: Remove the conditionals below at some point. It is merged at 4.39
|
|
|
|
// the param 'name' is slugified by the server when the slugify setting is enable, just for channels and teams
|
|
|
|
Q.where('name', Q.like(`%${slugifiedString}%`)),
|
|
|
|
// Still need the below conditionals because at the first moment the the sanitized_fname won't be filled
|
|
|
|
Q.where('name', Q.like(`%${likeString}%`)),
|
|
|
|
Q.where('fname', Q.like(`%${likeString}%`))
|
|
|
|
),
|
2023-08-18 17:48:33 +00:00
|
|
|
Q.experimentalSortBy('room_updated_at', Q.desc)
|
2022-03-08 14:24:21 +00:00
|
|
|
)
|
|
|
|
.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));
|
|
|
|
}
|
|
|
|
|
2023-08-04 14:09:36 +00:00
|
|
|
if (filterMessagingAllowed) {
|
|
|
|
const username = reduxStore.getState().login.user.username as string;
|
|
|
|
const filteredSubscriptions = await Promise.all(
|
|
|
|
subscriptions.map(async item => {
|
|
|
|
const isItemReadOnly = await isReadOnly(item, username);
|
|
|
|
return isItemReadOnly ? null : item;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
subscriptions = filteredSubscriptions.filter(item => item !== null) as TSubscriptionModel[];
|
|
|
|
}
|
|
|
|
|
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,
|
2023-02-02 03:04:17 +00:00
|
|
|
status: item.status,
|
2023-05-29 15:03:24 +00:00
|
|
|
teamMain: item.teamMain,
|
2023-08-21 13:45:37 +00:00
|
|
|
prid: item.prid,
|
|
|
|
f: item.f
|
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)),
|
2023-08-18 17:48:33 +00:00
|
|
|
Q.experimentalSortBy('ts', Q.desc),
|
|
|
|
Q.experimentalTake(50)
|
2022-10-31 16:43:47 +00:00
|
|
|
)
|
|
|
|
.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 = [];
|
2023-09-26 18:47:12 +00:00
|
|
|
// the users provided by localSearchUsersMessageByRid return the username properly, data.username
|
|
|
|
// Example: Diego Mello's user -> {name: "Diego Mello", username: "diego.mello"}
|
|
|
|
// Meanwhile, the username provided by localSearchSubscription is in name's property
|
|
|
|
// Example: Diego Mello's subscription -> {fname: "Diego Mello", name: "diego.mello"}
|
|
|
|
let usernames = [];
|
2022-10-31 16:43:47 +00:00
|
|
|
if (rid) {
|
|
|
|
localSearchData = await localSearchUsersMessageByRid({ text, rid });
|
2023-09-26 18:47:12 +00:00
|
|
|
usernames = localSearchData.map(sub => sub.username as string);
|
2022-10-31 16:43:47 +00:00
|
|
|
} else {
|
|
|
|
localSearchData = await localSearchSubscription({ text, filterUsers, filterRooms });
|
2023-09-26 18:47:12 +00:00
|
|
|
usernames = localSearchData.map(sub => sub.name as string);
|
2022-10-31 16:43:47 +00:00
|
|
|
}
|
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
|
2023-09-26 18:47:12 +00:00
|
|
|
.filter(
|
|
|
|
user =>
|
|
|
|
!data.some(sub =>
|
|
|
|
// Check comments at usernames' declaration
|
|
|
|
rid && 'username' in sub ? user.username === sub.username : user.username === sub.name
|
|
|
|
)
|
|
|
|
) // 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,
|
2023-05-10 19:32:39 +00:00
|
|
|
fname: user.name,
|
2022-03-08 14:24:21 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|