2018-04-24 19:34:03 +00:00
|
|
|
import React from 'react';
|
2021-11-10 17:56:19 +00:00
|
|
|
import { StackNavigationOptions, StackNavigationProp } from '@react-navigation/stack';
|
2021-12-03 19:27:57 +00:00
|
|
|
import { CompositeNavigationProp, RouteProp } from '@react-navigation/core';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { FlatList, Text, View } from 'react-native';
|
2020-09-11 14:31:38 +00:00
|
|
|
import { Q } from '@nozbe/watermelondb';
|
2018-04-24 19:34:03 +00:00
|
|
|
import { connect } from 'react-redux';
|
2021-02-26 16:01:45 +00:00
|
|
|
import { dequal } from 'dequal';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2022-01-11 13:51:48 +00:00
|
|
|
import { ISubscription, SubscriptionType } from '../../definitions/ISubscription';
|
2021-12-03 19:27:57 +00:00
|
|
|
import { IAttachment } from '../../definitions/IAttachment';
|
2018-04-24 19:34:03 +00:00
|
|
|
import RCTextInput from '../../containers/TextInput';
|
2019-12-04 16:39:53 +00:00
|
|
|
import ActivityIndicator from '../../containers/ActivityIndicator';
|
2019-08-27 12:25:38 +00:00
|
|
|
import Markdown from '../../containers/markdown';
|
2018-04-24 19:34:03 +00:00
|
|
|
import debounce from '../../utils/debounce';
|
|
|
|
import RocketChat from '../../lib/rocketchat';
|
2020-05-08 17:07:58 +00:00
|
|
|
import Message from '../../containers/message';
|
2018-04-24 19:34:03 +00:00
|
|
|
import scrollPersistTaps from '../../utils/scrollPersistTaps';
|
2021-12-03 19:27:57 +00:00
|
|
|
import { IMessage } from '../../containers/message/interfaces';
|
2018-06-01 17:38:13 +00:00
|
|
|
import I18n from '../../i18n';
|
2019-03-12 16:23:06 +00:00
|
|
|
import StatusBar from '../../containers/StatusBar';
|
2019-05-28 16:18:46 +00:00
|
|
|
import log from '../../utils/log';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { themes } from '../../constants/colors';
|
|
|
|
import { withTheme } from '../../theme';
|
2020-02-11 14:09:14 +00:00
|
|
|
import { getUserSelector } from '../../selectors/login';
|
2020-06-15 14:00:46 +00:00
|
|
|
import SafeAreaView from '../../containers/SafeAreaView';
|
2020-10-30 16:15:58 +00:00
|
|
|
import * as HeaderButton from '../../containers/HeaderButton';
|
2020-09-11 14:31:38 +00:00
|
|
|
import database from '../../lib/database';
|
2020-09-15 13:01:43 +00:00
|
|
|
import { sanitizeLikeString } from '../../lib/database/utils';
|
2021-05-26 17:24:54 +00:00
|
|
|
import getThreadName from '../../lib/methods/getThreadName';
|
|
|
|
import getRoomInfo from '../../lib/methods/getRoomInfo';
|
2021-10-05 13:58:21 +00:00
|
|
|
import { isIOS } from '../../utils/deviceInfo';
|
2022-02-07 18:44:04 +00:00
|
|
|
import { compareServerVersion } from '../../lib/utils';
|
2021-09-13 20:41:05 +00:00
|
|
|
import styles from './styles';
|
2021-12-03 19:27:57 +00:00
|
|
|
import { InsideStackParamList, ChatsStackParamList } from '../../stacks/types';
|
2022-02-19 02:09:05 +00:00
|
|
|
import { IRoom } from '../../definitions';
|
2022-02-17 15:27:01 +00:00
|
|
|
import { IEmoji } from '../../definitions/IEmoji';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2021-10-05 13:58:21 +00:00
|
|
|
const QUERY_SIZE = 50;
|
2021-11-10 17:56:19 +00:00
|
|
|
|
|
|
|
interface ISearchMessagesViewState {
|
|
|
|
loading: boolean;
|
|
|
|
messages: IMessage[];
|
|
|
|
searchText: string;
|
|
|
|
}
|
2021-12-03 19:27:57 +00:00
|
|
|
|
|
|
|
interface IRoomInfoParam {
|
2022-01-11 13:51:48 +00:00
|
|
|
room: ISubscription;
|
2021-12-03 19:27:57 +00:00
|
|
|
member: any;
|
|
|
|
rid: string;
|
2022-01-11 13:51:48 +00:00
|
|
|
t: SubscriptionType;
|
2021-12-03 19:27:57 +00:00
|
|
|
joined: boolean;
|
|
|
|
}
|
|
|
|
|
2021-11-10 17:56:19 +00:00
|
|
|
interface INavigationOption {
|
2021-12-03 19:27:57 +00:00
|
|
|
navigation: CompositeNavigationProp<
|
|
|
|
StackNavigationProp<ChatsStackParamList, 'SearchMessagesView'>,
|
|
|
|
StackNavigationProp<InsideStackParamList>
|
|
|
|
>;
|
|
|
|
route: RouteProp<ChatsStackParamList, 'SearchMessagesView'>;
|
2021-11-10 17:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface ISearchMessagesViewProps extends INavigationOption {
|
2022-01-17 16:10:39 +00:00
|
|
|
user: {
|
|
|
|
id: string;
|
|
|
|
username: string;
|
|
|
|
token: string;
|
|
|
|
};
|
2021-11-10 17:56:19 +00:00
|
|
|
baseUrl: string;
|
|
|
|
serverVersion: string;
|
|
|
|
customEmojis: {
|
2022-02-17 15:27:01 +00:00
|
|
|
[key: string]: IEmoji;
|
2021-11-10 17:56:19 +00:00
|
|
|
};
|
|
|
|
theme: string;
|
|
|
|
useRealName: boolean;
|
|
|
|
}
|
|
|
|
class SearchMessagesView extends React.Component<ISearchMessagesViewProps, ISearchMessagesViewState> {
|
|
|
|
private offset: number;
|
|
|
|
|
|
|
|
private rid: string;
|
|
|
|
|
|
|
|
private t: string | undefined;
|
|
|
|
|
|
|
|
private encrypted: boolean | undefined;
|
|
|
|
|
2022-02-19 02:09:05 +00:00
|
|
|
private room: Pick<IRoom, 'rid' | 'name' | 'fname' | 't'> | null | undefined;
|
2021-11-10 17:56:19 +00:00
|
|
|
|
|
|
|
static navigationOptions = ({ navigation, route }: INavigationOption) => {
|
|
|
|
const options: StackNavigationOptions = {
|
2020-06-15 14:00:46 +00:00
|
|
|
title: I18n.t('Search')
|
|
|
|
};
|
|
|
|
const showCloseModal = route.params?.showCloseModal;
|
|
|
|
if (showCloseModal) {
|
2020-10-30 16:15:58 +00:00
|
|
|
options.headerLeft = () => <HeaderButton.CloseModal navigation={navigation} />;
|
2020-06-15 14:00:46 +00:00
|
|
|
}
|
|
|
|
return options;
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2018-10-23 21:39:48 +00:00
|
|
|
|
2021-11-10 17:56:19 +00:00
|
|
|
constructor(props: ISearchMessagesViewProps) {
|
2019-05-28 13:03:08 +00:00
|
|
|
super(props);
|
2018-04-24 19:34:03 +00:00
|
|
|
this.state = {
|
2018-12-12 15:15:10 +00:00
|
|
|
loading: false,
|
2018-04-24 19:34:03 +00:00
|
|
|
messages: [],
|
2018-12-12 15:15:10 +00:00
|
|
|
searchText: ''
|
2018-04-24 19:34:03 +00:00
|
|
|
};
|
2021-10-05 13:58:21 +00:00
|
|
|
this.offset = 0;
|
2021-11-10 17:56:19 +00:00
|
|
|
this.rid = props.route.params.rid;
|
2021-05-26 17:24:54 +00:00
|
|
|
this.t = props.route.params?.t;
|
2020-09-11 14:31:38 +00:00
|
|
|
this.encrypted = props.route.params?.encrypted;
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
|
2021-05-26 17:24:54 +00:00
|
|
|
async componentDidMount() {
|
|
|
|
this.room = await getRoomInfo(this.rid);
|
|
|
|
}
|
|
|
|
|
2021-11-10 17:56:19 +00:00
|
|
|
shouldComponentUpdate(nextProps: ISearchMessagesViewProps, nextState: ISearchMessagesViewState) {
|
2018-12-21 10:55:35 +00:00
|
|
|
const { loading, searchText, messages } = this.state;
|
2019-12-04 16:39:53 +00:00
|
|
|
const { theme } = this.props;
|
|
|
|
if (nextProps.theme !== theme) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
if (nextState.loading !== loading) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextState.searchText !== searchText) {
|
|
|
|
return true;
|
|
|
|
}
|
2021-02-26 16:01:45 +00:00
|
|
|
if (!dequal(nextState.messages, messages)) {
|
2018-12-21 10:55:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
componentWillUnmount() {
|
2021-11-10 17:56:19 +00:00
|
|
|
this.searchDebounced?.stop?.();
|
2020-09-11 14:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle encrypted rooms search messages
|
2021-11-10 17:56:19 +00:00
|
|
|
searchMessages = async (searchText: string) => {
|
2021-10-05 13:58:21 +00:00
|
|
|
if (!searchText) {
|
|
|
|
return [];
|
|
|
|
}
|
2020-09-11 14:31:38 +00:00
|
|
|
// If it's a encrypted, room we'll search only on the local stored messages
|
|
|
|
if (this.encrypted) {
|
|
|
|
const db = database.active;
|
2021-02-26 16:25:51 +00:00
|
|
|
const messagesCollection = db.get('messages');
|
2020-09-15 13:01:43 +00:00
|
|
|
const likeString = sanitizeLikeString(searchText);
|
2020-09-11 14:31:38 +00:00
|
|
|
return messagesCollection
|
|
|
|
.query(
|
|
|
|
// Messages of this room
|
|
|
|
Q.where('rid', this.rid),
|
|
|
|
// Message content is like the search text
|
2021-09-13 20:41:05 +00:00
|
|
|
Q.where('msg', Q.like(`%${likeString}%`))
|
2020-09-11 14:31:38 +00:00
|
|
|
)
|
|
|
|
.fetch();
|
|
|
|
}
|
|
|
|
// If it's not a encrypted room, search messages on the server
|
2021-10-05 13:58:21 +00:00
|
|
|
const result = await RocketChat.searchMessages(this.rid, searchText, QUERY_SIZE, this.offset);
|
2020-09-11 14:31:38 +00:00
|
|
|
if (result.success) {
|
|
|
|
return result.messages;
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2021-11-10 17:56:19 +00:00
|
|
|
getMessages = async (searchText: string, debounced?: boolean) => {
|
2018-04-24 19:34:03 +00:00
|
|
|
try {
|
2020-09-11 14:31:38 +00:00
|
|
|
const messages = await this.searchMessages(searchText);
|
2021-10-05 13:58:21 +00:00
|
|
|
this.setState(prevState => ({
|
|
|
|
messages: debounced ? messages : [...prevState.messages, ...messages],
|
2020-09-11 14:31:38 +00:00
|
|
|
loading: false
|
2021-10-05 13:58:21 +00:00
|
|
|
}));
|
2019-08-23 13:18:47 +00:00
|
|
|
} catch (e) {
|
2018-12-12 15:15:10 +00:00
|
|
|
this.setState({ loading: false });
|
2019-08-23 13:18:47 +00:00
|
|
|
log(e);
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
2021-10-05 13:58:21 +00:00
|
|
|
};
|
|
|
|
|
2021-11-10 17:56:19 +00:00
|
|
|
search = (searchText: string) => {
|
2021-10-05 13:58:21 +00:00
|
|
|
this.offset = 0;
|
|
|
|
this.setState({ searchText, loading: true, messages: [] });
|
|
|
|
this.searchDebounced(searchText);
|
|
|
|
};
|
|
|
|
|
2021-11-10 17:56:19 +00:00
|
|
|
searchDebounced = debounce(async (searchText: string) => {
|
2021-10-05 13:58:21 +00:00
|
|
|
await this.getMessages(searchText, true);
|
2021-09-13 20:41:05 +00:00
|
|
|
}, 1000);
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2021-11-10 17:56:19 +00:00
|
|
|
getCustomEmoji = (name: string) => {
|
2019-09-16 20:26:32 +00:00
|
|
|
const { customEmojis } = this.props;
|
|
|
|
const emoji = customEmojis[name];
|
|
|
|
if (emoji) {
|
|
|
|
return emoji;
|
|
|
|
}
|
|
|
|
return null;
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2021-12-03 19:27:57 +00:00
|
|
|
showAttachment = (attachment: IAttachment) => {
|
2021-05-26 17:24:54 +00:00
|
|
|
const { navigation } = this.props;
|
|
|
|
navigation.navigate('AttachmentView', { attachment });
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2021-05-26 17:24:54 +00:00
|
|
|
|
2021-12-03 19:27:57 +00:00
|
|
|
navToRoomInfo = (navParam: IRoomInfoParam) => {
|
2020-02-17 16:06:46 +00:00
|
|
|
const { navigation, user } = this.props;
|
|
|
|
if (navParam.rid === user.id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
navigation.navigate('RoomInfoView', navParam);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-02-17 16:06:46 +00:00
|
|
|
|
2021-11-10 17:56:19 +00:00
|
|
|
jumpToMessage = async ({ item }: { item: IMessage }) => {
|
2021-05-26 17:24:54 +00:00
|
|
|
const { navigation } = this.props;
|
2021-11-10 17:56:19 +00:00
|
|
|
let params: any = {
|
2021-05-26 17:24:54 +00:00
|
|
|
rid: this.rid,
|
|
|
|
jumpToMessageId: item._id,
|
|
|
|
t: this.t,
|
|
|
|
room: this.room
|
|
|
|
};
|
|
|
|
if (item.tmid) {
|
|
|
|
navigation.pop();
|
|
|
|
params = {
|
|
|
|
...params,
|
|
|
|
tmid: item.tmid,
|
|
|
|
name: await getThreadName(this.rid, item.tmid, item._id),
|
|
|
|
t: 'thread'
|
|
|
|
};
|
|
|
|
navigation.push('RoomView', params);
|
|
|
|
} else {
|
|
|
|
navigation.navigate('RoomView', params);
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2021-05-26 17:24:54 +00:00
|
|
|
|
2021-10-05 13:58:21 +00:00
|
|
|
onEndReached = async () => {
|
|
|
|
const { serverVersion } = this.props;
|
|
|
|
const { searchText, messages, loading } = this.state;
|
|
|
|
if (
|
|
|
|
messages.length < this.offset ||
|
|
|
|
this.encrypted ||
|
|
|
|
loading ||
|
2022-02-07 18:44:04 +00:00
|
|
|
compareServerVersion(serverVersion, 'lowerThan', '3.17.0')
|
2021-10-05 13:58:21 +00:00
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.setState({ loading: true });
|
|
|
|
this.offset += QUERY_SIZE;
|
|
|
|
|
|
|
|
await this.getMessages(searchText);
|
|
|
|
};
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
renderEmpty = () => {
|
|
|
|
const { theme } = this.props;
|
|
|
|
return (
|
|
|
|
<View style={[styles.listEmptyContainer, { backgroundColor: themes[theme].backgroundColor }]}>
|
|
|
|
<Text style={[styles.noDataFound, { color: themes[theme].titleText }]}>{I18n.t('No_results_found')}</Text>
|
|
|
|
</View>
|
|
|
|
);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2021-11-10 17:56:19 +00:00
|
|
|
renderItem = ({ item }: { item: IMessage }) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
const { user, baseUrl, theme, useRealName } = this.props;
|
2018-09-25 19:28:42 +00:00
|
|
|
return (
|
|
|
|
<Message
|
2020-05-08 17:07:58 +00:00
|
|
|
item={item}
|
2018-12-12 15:15:10 +00:00
|
|
|
baseUrl={baseUrl}
|
2018-09-25 19:28:42 +00:00
|
|
|
user={user}
|
2021-05-26 17:24:54 +00:00
|
|
|
timeFormat='MMM Do YYYY, h:mm:ss a'
|
2019-05-20 20:43:50 +00:00
|
|
|
isHeader
|
2021-05-26 17:24:54 +00:00
|
|
|
isThreadRoom
|
|
|
|
showAttachment={this.showAttachment}
|
2019-09-16 20:26:32 +00:00
|
|
|
getCustomEmoji={this.getCustomEmoji}
|
2020-02-17 16:06:46 +00:00
|
|
|
navToRoomInfo={this.navToRoomInfo}
|
2021-02-11 21:43:57 +00:00
|
|
|
useRealName={useRealName}
|
2019-12-04 16:39:53 +00:00
|
|
|
theme={theme}
|
2021-05-26 17:24:54 +00:00
|
|
|
onPress={() => this.jumpToMessage({ item })}
|
|
|
|
jumpToMessage={() => this.jumpToMessage({ item })}
|
2018-12-12 15:15:10 +00:00
|
|
|
/>
|
|
|
|
);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2018-12-12 15:15:10 +00:00
|
|
|
|
|
|
|
renderList = () => {
|
|
|
|
const { messages, loading, searchText } = this.state;
|
2019-12-04 16:39:53 +00:00
|
|
|
const { theme } = this.props;
|
2018-12-12 15:15:10 +00:00
|
|
|
|
|
|
|
if (!loading && messages.length === 0 && searchText.length) {
|
|
|
|
return this.renderEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<FlatList
|
|
|
|
data={messages}
|
|
|
|
renderItem={this.renderItem}
|
2019-12-04 16:39:53 +00:00
|
|
|
style={[styles.list, { backgroundColor: themes[theme].backgroundColor }]}
|
2018-12-12 15:15:10 +00:00
|
|
|
keyExtractor={item => item._id}
|
2021-10-05 13:58:21 +00:00
|
|
|
onEndReached={this.onEndReached}
|
2019-12-04 16:39:53 +00:00
|
|
|
ListFooterComponent={loading ? <ActivityIndicator theme={theme} /> : null}
|
2021-10-05 13:58:21 +00:00
|
|
|
onEndReachedThreshold={0.5}
|
|
|
|
removeClippedSubviews={isIOS}
|
2018-12-12 15:15:10 +00:00
|
|
|
{...scrollPersistTaps}
|
2018-09-25 19:28:42 +00:00
|
|
|
/>
|
|
|
|
);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
|
|
render() {
|
2019-12-04 16:39:53 +00:00
|
|
|
const { theme } = this.props;
|
2018-04-24 19:34:03 +00:00
|
|
|
return (
|
2020-10-30 13:59:44 +00:00
|
|
|
<SafeAreaView style={{ backgroundColor: themes[theme].backgroundColor }} testID='search-messages-view'>
|
|
|
|
<StatusBar />
|
2018-04-24 19:34:03 +00:00
|
|
|
<View style={styles.searchContainer}>
|
|
|
|
<RCTextInput
|
2019-08-07 19:20:16 +00:00
|
|
|
autoFocus
|
2018-06-01 17:38:13 +00:00
|
|
|
label={I18n.t('Search')}
|
2018-12-12 15:15:10 +00:00
|
|
|
onChangeText={this.search}
|
2018-06-01 17:38:13 +00:00
|
|
|
placeholder={I18n.t('Search_Messages')}
|
2018-05-23 13:39:18 +00:00
|
|
|
testID='search-message-view-input'
|
2019-12-04 16:39:53 +00:00
|
|
|
theme={theme}
|
2018-04-24 19:34:03 +00:00
|
|
|
/>
|
2022-02-17 15:27:01 +00:00
|
|
|
<Markdown msg={I18n.t('You_can_search_using_RegExp_eg')} theme={theme} />
|
2019-12-04 16:39:53 +00:00
|
|
|
<View style={[styles.divider, { backgroundColor: themes[theme].separatorColor }]} />
|
2018-04-24 19:34:03 +00:00
|
|
|
</View>
|
2018-12-12 15:15:10 +00:00
|
|
|
{this.renderList()}
|
2018-08-01 19:35:06 +00:00
|
|
|
</SafeAreaView>
|
2018-04-24 19:34:03 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 13:51:34 +00:00
|
|
|
|
2021-11-10 17:56:19 +00:00
|
|
|
const mapStateToProps = (state: any) => ({
|
2021-10-05 13:58:21 +00:00
|
|
|
serverVersion: state.server.version,
|
2020-02-11 14:09:14 +00:00
|
|
|
baseUrl: state.server.server,
|
|
|
|
user: getUserSelector(state),
|
2021-02-11 21:43:57 +00:00
|
|
|
useRealName: state.settings.UI_Use_Real_Name,
|
2019-09-16 20:26:32 +00:00
|
|
|
customEmojis: state.customEmojis
|
2019-08-07 13:51:34 +00:00
|
|
|
});
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
export default connect(mapStateToProps)(withTheme(SearchMessagesView));
|