2018-08-31 18:13:30 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2021-10-01 18:12:19 +00:00
|
|
|
import { FlatList, StyleSheet, Text, View } from 'react-native';
|
2019-01-31 16:08:38 +00:00
|
|
|
import { connect } from 'react-redux';
|
2019-09-16 20:26:32 +00:00
|
|
|
import { Q } from '@nozbe/watermelondb';
|
2021-10-28 17:49:08 +00:00
|
|
|
import { dequal } from 'dequal';
|
2018-08-31 18:13:30 +00:00
|
|
|
|
2021-10-01 18:12:19 +00:00
|
|
|
import * as List from '../containers/List';
|
2019-12-04 16:39:53 +00:00
|
|
|
import Touch from '../utils/touch';
|
2019-09-16 20:26:32 +00:00
|
|
|
import database from '../lib/database';
|
2018-08-31 18:13:30 +00:00
|
|
|
import RocketChat from '../lib/rocketchat';
|
|
|
|
import UserItem from '../presentation/UserItem';
|
|
|
|
import I18n from '../i18n';
|
2021-10-01 18:12:19 +00:00
|
|
|
import log, { events, logEvent } from '../utils/log';
|
2018-08-31 18:13:30 +00:00
|
|
|
import SearchBox from '../containers/SearchBox';
|
2019-03-12 16:23:06 +00:00
|
|
|
import { CustomIcon } from '../lib/Icons';
|
2020-10-30 16:15:58 +00:00
|
|
|
import * as HeaderButton from '../containers/HeaderButton';
|
2019-03-12 16:23:06 +00:00
|
|
|
import StatusBar from '../containers/StatusBar';
|
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-03-30 19:50:27 +00:00
|
|
|
import Navigation from '../lib/Navigation';
|
2020-04-01 12:28:54 +00:00
|
|
|
import { createChannelRequest } from '../actions/createChannel';
|
2020-05-13 19:02:57 +00:00
|
|
|
import { goRoom } from '../utils/goRoom';
|
2020-06-15 14:00:46 +00:00
|
|
|
import SafeAreaView from '../containers/SafeAreaView';
|
2021-06-15 20:13:42 +00:00
|
|
|
import { compareServerVersion, methods } from '../lib/utils';
|
2021-10-01 18:12:19 +00:00
|
|
|
import sharedStyles from './Styles';
|
2018-08-31 18:13:30 +00:00
|
|
|
|
2021-03-15 20:16:34 +00:00
|
|
|
const QUERY_SIZE = 50;
|
|
|
|
|
2018-08-31 18:13:30 +00:00
|
|
|
const styles = StyleSheet.create({
|
2020-04-01 12:28:54 +00:00
|
|
|
button: {
|
2019-12-04 16:39:53 +00:00
|
|
|
height: 46,
|
2018-08-31 18:13:30 +00:00
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center'
|
|
|
|
},
|
2020-04-01 12:28:54 +00:00
|
|
|
buttonIcon: {
|
2019-06-10 16:22:35 +00:00
|
|
|
marginLeft: 18,
|
2020-03-30 19:50:27 +00:00
|
|
|
marginRight: 16
|
2018-08-31 18:13:30 +00:00
|
|
|
},
|
2020-04-01 12:28:54 +00:00
|
|
|
buttonText: {
|
2019-03-29 19:36:07 +00:00
|
|
|
fontSize: 17,
|
|
|
|
...sharedStyles.textRegular
|
2020-04-01 12:28:54 +00:00
|
|
|
},
|
|
|
|
buttonContainer: {
|
|
|
|
paddingVertical: 25
|
2018-08-31 18:13:30 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-08-07 13:51:34 +00:00
|
|
|
class NewMessageView extends React.Component {
|
2020-06-15 14:00:46 +00:00
|
|
|
static navigationOptions = ({ navigation }) => ({
|
2020-10-30 16:15:58 +00:00
|
|
|
headerLeft: () => <HeaderButton.CloseModal navigation={navigation} testID='new-message-view-close' />,
|
2019-03-12 16:23:06 +00:00
|
|
|
title: I18n.t('New_Message')
|
2021-10-01 18:12:19 +00:00
|
|
|
});
|
2018-08-31 18:13:30 +00:00
|
|
|
|
|
|
|
static propTypes = {
|
2019-03-12 16:23:06 +00:00
|
|
|
navigation: PropTypes.object,
|
2018-09-11 16:32:52 +00:00
|
|
|
baseUrl: PropTypes.string,
|
2019-02-07 19:58:20 +00:00
|
|
|
user: PropTypes.shape({
|
|
|
|
id: PropTypes.string,
|
2021-10-28 17:49:08 +00:00
|
|
|
token: PropTypes.string,
|
|
|
|
roles: PropTypes.array
|
2019-12-04 16:39:53 +00:00
|
|
|
}),
|
2021-06-15 20:13:42 +00:00
|
|
|
create: PropTypes.func,
|
2020-04-01 12:28:54 +00:00
|
|
|
maxUsers: PropTypes.number,
|
2020-06-15 14:00:46 +00:00
|
|
|
theme: PropTypes.string,
|
2021-06-15 20:13:42 +00:00
|
|
|
isMasterDetail: PropTypes.bool,
|
2021-10-28 17:49:08 +00:00
|
|
|
serverVersion: PropTypes.string,
|
|
|
|
createTeamPermission: PropTypes.array,
|
|
|
|
createDirectMessagePermission: PropTypes.array,
|
|
|
|
createPublicChannelPermission: PropTypes.array,
|
|
|
|
createPrivateChannelPermission: PropTypes.array,
|
|
|
|
createDiscussionPermission: PropTypes.array
|
2018-08-31 18:13:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
2019-05-28 13:03:08 +00:00
|
|
|
super(props);
|
2019-09-16 20:26:32 +00:00
|
|
|
this.init();
|
2018-08-31 18:13:30 +00:00
|
|
|
this.state = {
|
2019-09-16 20:26:32 +00:00
|
|
|
search: [],
|
2021-10-28 17:49:08 +00:00
|
|
|
chats: [],
|
|
|
|
permissions: []
|
2018-08-31 18:13:30 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
// eslint-disable-next-line react/sort-comp
|
2021-10-01 18:12:19 +00:00
|
|
|
init = async () => {
|
2019-09-16 20:26:32 +00:00
|
|
|
try {
|
|
|
|
const db = database.active;
|
2021-03-15 20:16:34 +00:00
|
|
|
const chats = await db.collections
|
2019-09-16 20:26:32 +00:00
|
|
|
.get('subscriptions')
|
2021-10-01 18:12:19 +00:00
|
|
|
.query(Q.where('t', 'd'), Q.experimentalTake(QUERY_SIZE), Q.experimentalSortBy('room_updated_at', Q.desc))
|
2021-03-15 20:16:34 +00:00
|
|
|
.fetch();
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2021-03-15 20:16:34 +00:00
|
|
|
this.setState({ chats });
|
2019-09-16 20:26:32 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2018-08-31 18:13:30 +00:00
|
|
|
|
2021-10-28 17:49:08 +00:00
|
|
|
componentDidMount() {
|
|
|
|
this.handleHasPermission();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
const {
|
|
|
|
createTeamPermission,
|
|
|
|
createPublicChannelPermission,
|
|
|
|
createPrivateChannelPermission,
|
|
|
|
createDirectMessagePermission,
|
|
|
|
createDiscussionPermission
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
if (
|
|
|
|
!dequal(createTeamPermission, prevProps.createTeamPermission) ||
|
|
|
|
!dequal(createPublicChannelPermission, prevProps.createPublicChannelPermission) ||
|
|
|
|
!dequal(createPrivateChannelPermission, prevProps.createPrivateChannelPermission) ||
|
|
|
|
!dequal(createDirectMessagePermission, prevProps.createDirectMessagePermission) ||
|
|
|
|
!dequal(createDiscussionPermission, prevProps.createDiscussionPermission)
|
|
|
|
) {
|
|
|
|
this.handleHasPermission();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-31 18:13:30 +00:00
|
|
|
onSearchChangeText(text) {
|
|
|
|
this.search(text);
|
|
|
|
}
|
|
|
|
|
2018-10-23 21:39:48 +00:00
|
|
|
dismiss = () => {
|
2019-03-12 16:23:06 +00:00
|
|
|
const { navigation } = this.props;
|
|
|
|
return navigation.pop();
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2018-10-23 21:39:48 +00:00
|
|
|
|
2021-10-01 18:12:19 +00:00
|
|
|
search = async text => {
|
2018-08-31 18:13:30 +00:00
|
|
|
const result = await RocketChat.search({ text, filterRooms: false });
|
|
|
|
this.setState({
|
|
|
|
search: result
|
|
|
|
});
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2018-08-31 18:13:30 +00:00
|
|
|
|
|
|
|
createChannel = () => {
|
2020-07-30 13:26:17 +00:00
|
|
|
logEvent(events.NEW_MSG_CREATE_CHANNEL);
|
2019-03-12 16:23:06 +00:00
|
|
|
const { navigation } = this.props;
|
2020-04-01 12:28:54 +00:00
|
|
|
navigation.navigate('SelectedUsersViewCreateChannel', { nextAction: () => navigation.navigate('CreateChannelView') });
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2020-04-01 12:28:54 +00:00
|
|
|
|
2021-06-15 20:13:42 +00:00
|
|
|
createTeam = () => {
|
|
|
|
logEvent(events.NEW_MSG_CREATE_TEAM);
|
|
|
|
const { navigation } = this.props;
|
2021-10-01 18:12:19 +00:00
|
|
|
navigation.navigate('SelectedUsersViewCreateChannel', {
|
|
|
|
nextAction: () => navigation.navigate('CreateChannelView', { isTeam: true })
|
|
|
|
});
|
|
|
|
};
|
2021-06-15 20:13:42 +00:00
|
|
|
|
2020-04-01 12:28:54 +00:00
|
|
|
createGroupChat = () => {
|
2020-07-30 13:26:17 +00:00
|
|
|
logEvent(events.NEW_MSG_CREATE_GROUP_CHAT);
|
2021-06-15 20:13:42 +00:00
|
|
|
const { create, maxUsers, navigation } = this.props;
|
2020-04-01 12:28:54 +00:00
|
|
|
navigation.navigate('SelectedUsersViewCreateChannel', {
|
2021-06-15 20:13:42 +00:00
|
|
|
nextAction: () => create({ group: true }),
|
2020-04-01 12:28:54 +00:00
|
|
|
buttonText: I18n.t('Create'),
|
|
|
|
maxUsers
|
|
|
|
});
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2020-04-01 12:28:54 +00:00
|
|
|
|
2021-10-01 18:12:19 +00:00
|
|
|
goRoom = item => {
|
2020-07-30 13:26:17 +00:00
|
|
|
logEvent(events.NEW_MSG_CHAT_WITH_USER);
|
2020-06-15 14:00:46 +00:00
|
|
|
const { isMasterDetail, navigation } = this.props;
|
|
|
|
if (isMasterDetail) {
|
|
|
|
navigation.pop();
|
|
|
|
}
|
|
|
|
goRoom({ item, isMasterDetail });
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2020-06-15 14:00:46 +00:00
|
|
|
|
2021-10-01 18:12:19 +00:00
|
|
|
renderButton = ({ onPress, testID, title, icon, first }) => {
|
2020-04-01 12:28:54 +00:00
|
|
|
const { theme } = this.props;
|
|
|
|
return (
|
2021-10-01 18:12:19 +00:00
|
|
|
<Touch onPress={onPress} style={{ backgroundColor: themes[theme].backgroundColor }} testID={testID} theme={theme}>
|
|
|
|
<View
|
|
|
|
style={[
|
|
|
|
first ? sharedStyles.separatorVertical : sharedStyles.separatorBottom,
|
|
|
|
styles.button,
|
|
|
|
{ borderColor: themes[theme].separatorColor }
|
|
|
|
]}>
|
2020-04-01 12:28:54 +00:00
|
|
|
<CustomIcon style={[styles.buttonIcon, { color: themes[theme].tintColor }]} size={24} name={icon} />
|
|
|
|
<Text style={[styles.buttonText, { color: themes[theme].tintColor }]}>{title}</Text>
|
|
|
|
</View>
|
|
|
|
</Touch>
|
|
|
|
);
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2018-08-31 18:13:30 +00:00
|
|
|
|
2020-03-30 19:50:27 +00:00
|
|
|
createDiscussion = () => {
|
2020-07-30 13:26:17 +00:00
|
|
|
logEvent(events.NEW_MSG_CREATE_DISCUSSION);
|
2020-03-30 19:50:27 +00:00
|
|
|
Navigation.navigate('CreateDiscussionView');
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2020-03-30 19:50:27 +00:00
|
|
|
|
2021-10-28 17:49:08 +00:00
|
|
|
handleHasPermission = async () => {
|
|
|
|
const {
|
|
|
|
createTeamPermission,
|
|
|
|
createDirectMessagePermission,
|
|
|
|
createPublicChannelPermission,
|
|
|
|
createPrivateChannelPermission,
|
|
|
|
createDiscussionPermission
|
|
|
|
} = this.props;
|
|
|
|
const permissions = [
|
|
|
|
createPublicChannelPermission,
|
|
|
|
createPrivateChannelPermission,
|
|
|
|
createTeamPermission,
|
|
|
|
createDirectMessagePermission,
|
|
|
|
createDiscussionPermission
|
|
|
|
];
|
|
|
|
const permissionsToCreate = await RocketChat.hasPermission(permissions);
|
|
|
|
this.setState({ permissions: permissionsToCreate });
|
|
|
|
};
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
renderHeader = () => {
|
2021-06-15 20:13:42 +00:00
|
|
|
const { maxUsers, theme, serverVersion } = this.props;
|
2021-10-28 17:49:08 +00:00
|
|
|
const { permissions } = this.state;
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
return (
|
|
|
|
<View style={{ backgroundColor: themes[theme].auxiliaryBackground }}>
|
|
|
|
<SearchBox onChangeText={text => this.onSearchChangeText(text)} testID='new-message-view-search' />
|
2020-04-01 12:28:54 +00:00
|
|
|
<View style={styles.buttonContainer}>
|
2021-10-28 17:49:08 +00:00
|
|
|
{permissions[0] || permissions[1]
|
|
|
|
? this.renderButton({
|
|
|
|
onPress: this.createChannel,
|
|
|
|
title: I18n.t('Create_Channel'),
|
|
|
|
icon: 'channel-public',
|
|
|
|
testID: 'new-message-view-create-channel',
|
|
|
|
first: true
|
|
|
|
})
|
|
|
|
: null}
|
|
|
|
{compareServerVersion(serverVersion, '3.13.0', methods.greaterThanOrEqualTo) && permissions[2]
|
2021-10-01 18:12:19 +00:00
|
|
|
? this.renderButton({
|
|
|
|
onPress: this.createTeam,
|
|
|
|
title: I18n.t('Create_Team'),
|
|
|
|
icon: 'teams',
|
|
|
|
testID: 'new-message-view-create-team'
|
|
|
|
})
|
|
|
|
: null}
|
2021-10-28 17:49:08 +00:00
|
|
|
{maxUsers > 2 && permissions[3]
|
2021-10-01 18:12:19 +00:00
|
|
|
? this.renderButton({
|
|
|
|
onPress: this.createGroupChat,
|
|
|
|
title: I18n.t('Create_Direct_Messages'),
|
|
|
|
icon: 'message',
|
|
|
|
testID: 'new-message-view-create-direct-message'
|
|
|
|
})
|
|
|
|
: null}
|
2021-10-28 17:49:08 +00:00
|
|
|
{permissions[4]
|
|
|
|
? this.renderButton({
|
|
|
|
onPress: this.createDiscussion,
|
|
|
|
title: I18n.t('Create_Discussion'),
|
|
|
|
icon: 'discussions',
|
|
|
|
testID: 'new-message-view-create-discussion'
|
|
|
|
})
|
|
|
|
: null}
|
2020-04-01 12:28:54 +00:00
|
|
|
</View>
|
2019-12-04 16:39:53 +00:00
|
|
|
</View>
|
|
|
|
);
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2018-08-31 18:13:30 +00:00
|
|
|
|
|
|
|
renderItem = ({ item, index }) => {
|
2019-09-16 20:26:32 +00:00
|
|
|
const { search, chats } = this.state;
|
2019-12-04 16:39:53 +00:00
|
|
|
const { baseUrl, user, theme } = this.props;
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
let style = { borderColor: themes[theme].separatorColor };
|
2018-08-31 18:13:30 +00:00
|
|
|
if (index === 0) {
|
2019-12-04 16:39:53 +00:00
|
|
|
style = { ...style, ...sharedStyles.separatorTop };
|
2018-08-31 18:13:30 +00:00
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
if (search.length > 0 && index === search.length - 1) {
|
2018-08-31 18:13:30 +00:00
|
|
|
style = { ...style, ...sharedStyles.separatorBottom };
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
if (search.length === 0 && index === chats.length - 1) {
|
2018-08-31 18:13:30 +00:00
|
|
|
style = { ...style, ...sharedStyles.separatorBottom };
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<UserItem
|
|
|
|
name={item.search ? item.name : item.fname}
|
|
|
|
username={item.search ? item.username : item.name}
|
2020-06-15 14:00:46 +00:00
|
|
|
onPress={() => this.goRoom(item)}
|
2018-09-25 19:28:42 +00:00
|
|
|
baseUrl={baseUrl}
|
2021-10-01 18:12:19 +00:00
|
|
|
testID={`new-message-view-item-${item.name}`}
|
2018-08-31 18:13:30 +00:00
|
|
|
style={style}
|
2019-02-07 19:58:20 +00:00
|
|
|
user={user}
|
2019-12-04 16:39:53 +00:00
|
|
|
theme={theme}
|
2018-08-31 18:13:30 +00:00
|
|
|
/>
|
|
|
|
);
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2018-08-31 18:13:30 +00:00
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
renderList = () => {
|
2019-09-16 20:26:32 +00:00
|
|
|
const { search, chats } = this.state;
|
2019-12-04 16:39:53 +00:00
|
|
|
const { theme } = this.props;
|
2018-09-25 19:28:42 +00:00
|
|
|
return (
|
|
|
|
<FlatList
|
2019-09-16 20:26:32 +00:00
|
|
|
data={search.length > 0 ? search : chats}
|
2018-09-25 19:28:42 +00:00
|
|
|
extraData={this.state}
|
|
|
|
keyExtractor={item => item._id}
|
|
|
|
ListHeaderComponent={this.renderHeader}
|
|
|
|
renderItem={this.renderItem}
|
2021-03-15 20:16:34 +00:00
|
|
|
ItemSeparatorComponent={List.Separator}
|
2019-12-04 16:39:53 +00:00
|
|
|
contentContainerStyle={{ backgroundColor: themes[theme].backgroundColor }}
|
2018-09-25 19:28:42 +00:00
|
|
|
keyboardShouldPersistTaps='always'
|
|
|
|
/>
|
|
|
|
);
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2018-08-31 18:13:30 +00:00
|
|
|
|
2020-10-30 13:59:44 +00:00
|
|
|
render() {
|
2019-12-04 16:39:53 +00:00
|
|
|
return (
|
2020-10-30 13:59:44 +00:00
|
|
|
<SafeAreaView testID='new-message-view'>
|
|
|
|
<StatusBar />
|
2019-12-04 16:39:53 +00:00
|
|
|
{this.renderList()}
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
}
|
2018-08-31 18:13:30 +00:00
|
|
|
}
|
2019-08-07 13:51:34 +00:00
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
2021-06-15 20:13:42 +00:00
|
|
|
serverVersion: state.server.version,
|
2020-06-15 14:00:46 +00:00
|
|
|
isMasterDetail: state.app.isMasterDetail,
|
2020-02-11 14:09:14 +00:00
|
|
|
baseUrl: state.server.server,
|
2020-04-01 12:28:54 +00:00
|
|
|
maxUsers: state.settings.DirectMesssage_maxUsers || 1,
|
2021-10-28 17:49:08 +00:00
|
|
|
user: getUserSelector(state),
|
|
|
|
createTeamPermission: state.permissions['create-team'],
|
|
|
|
createDirectMessagePermission: state.permissions['create-d'],
|
|
|
|
createPublicChannelPermission: state.permissions['create-c'],
|
|
|
|
createPrivateChannelPermission: state.permissions['create-p'],
|
|
|
|
createDiscussionPermission: state.permissions['start-discussion']
|
2019-08-07 13:51:34 +00:00
|
|
|
});
|
|
|
|
|
2020-04-01 12:28:54 +00:00
|
|
|
const mapDispatchToProps = dispatch => ({
|
2021-06-15 20:13:42 +00:00
|
|
|
create: params => dispatch(createChannelRequest(params))
|
2020-04-01 12:28:54 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(withTheme(NewMessageView));
|