2018-03-02 15:11:34 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { FlatList, View } from 'react-native';
|
2019-01-30 12:11:02 +00:00
|
|
|
import ActionSheet from 'react-native-action-sheet';
|
2018-09-19 14:18:32 +00:00
|
|
|
import { connect } from 'react-redux';
|
2019-06-28 17:07:17 +00:00
|
|
|
import * as Haptics from 'expo-haptics';
|
2019-09-16 20:26:32 +00:00
|
|
|
import { Q } from '@nozbe/watermelondb';
|
2018-03-02 15:11:34 +00:00
|
|
|
|
|
|
|
import styles from './styles';
|
2018-08-31 16:46:33 +00:00
|
|
|
import UserItem from '../../presentation/UserItem';
|
2018-03-02 15:11:34 +00:00
|
|
|
import scrollPersistTaps from '../../utils/scrollPersistTaps';
|
|
|
|
import RocketChat from '../../lib/rocketchat';
|
2019-09-16 20:26:32 +00:00
|
|
|
import database from '../../lib/database';
|
2019-07-23 14:02:57 +00:00
|
|
|
import { LISTENER } from '../../containers/Toast';
|
|
|
|
import EventEmitter from '../../utils/events';
|
2018-05-18 17:55:08 +00:00
|
|
|
import log from '../../utils/log';
|
2018-06-01 17:38:13 +00:00
|
|
|
import I18n from '../../i18n';
|
2018-08-31 18:13:30 +00:00
|
|
|
import SearchBox from '../../containers/SearchBox';
|
2019-02-26 12:46:27 +00:00
|
|
|
import protectedFunction from '../../lib/methods/helpers/protectedFunction';
|
2019-03-12 16:23:06 +00:00
|
|
|
import { CustomHeaderButtons, Item } from '../../containers/HeaderButton';
|
|
|
|
import StatusBar from '../../containers/StatusBar';
|
2019-12-04 16:39:53 +00:00
|
|
|
import ActivityIndicator from '../../containers/ActivityIndicator';
|
|
|
|
import { withTheme } from '../../theme';
|
|
|
|
import { themes } from '../../constants/colors';
|
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';
|
|
|
|
import { goRoom } from '../../utils/goRoom';
|
2018-03-02 15:11:34 +00:00
|
|
|
|
2019-04-17 17:01:03 +00:00
|
|
|
const PAGE_SIZE = 25;
|
|
|
|
|
2019-08-07 13:51:34 +00:00
|
|
|
class RoomMembersView extends React.Component {
|
2018-03-02 15:11:34 +00:00
|
|
|
static propTypes = {
|
2019-03-12 16:23:06 +00:00
|
|
|
navigation: PropTypes.object,
|
2020-06-15 14:00:46 +00:00
|
|
|
route: PropTypes.object,
|
2018-07-10 13:40:32 +00:00
|
|
|
rid: PropTypes.string,
|
2018-09-19 14:18:32 +00:00
|
|
|
members: PropTypes.array,
|
2018-12-21 10:55:35 +00:00
|
|
|
baseUrl: PropTypes.string,
|
2019-02-07 19:58:20 +00:00
|
|
|
room: PropTypes.object,
|
|
|
|
user: PropTypes.shape({
|
|
|
|
id: PropTypes.string,
|
|
|
|
token: PropTypes.string
|
2019-12-04 16:39:53 +00:00
|
|
|
}),
|
2020-06-15 14:00:46 +00:00
|
|
|
theme: PropTypes.string,
|
|
|
|
isMasterDetail: PropTypes.bool
|
2018-03-02 15:11:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
2019-05-28 13:03:08 +00:00
|
|
|
super(props);
|
2019-09-16 20:26:32 +00:00
|
|
|
this.mounted = false;
|
2018-04-24 19:34:03 +00:00
|
|
|
this.CANCEL_INDEX = 0;
|
|
|
|
this.MUTE_INDEX = 1;
|
|
|
|
this.actionSheetOptions = [''];
|
2020-06-15 14:00:46 +00:00
|
|
|
const rid = props.route.params?.rid;
|
|
|
|
const room = props.route.params?.room;
|
2018-03-02 15:11:34 +00:00
|
|
|
this.state = {
|
2019-04-17 17:01:03 +00:00
|
|
|
isLoading: false,
|
2018-03-02 15:11:34 +00:00
|
|
|
allUsers: false,
|
|
|
|
filtering: false,
|
|
|
|
rid,
|
2019-04-17 17:01:03 +00:00
|
|
|
members: [],
|
2018-04-24 19:34:03 +00:00
|
|
|
membersFiltered: [],
|
|
|
|
userLongPressed: {},
|
2019-09-16 20:26:32 +00:00
|
|
|
room: room || {},
|
2019-04-17 17:01:03 +00:00
|
|
|
end: false
|
2018-03-02 15:11:34 +00:00
|
|
|
};
|
2019-09-17 14:43:49 +00:00
|
|
|
if (room && room.observe) {
|
|
|
|
this.roomObservable = room.observe();
|
|
|
|
this.subscription = this.roomObservable
|
|
|
|
.subscribe((changes) => {
|
|
|
|
if (this.mounted) {
|
|
|
|
this.setState({ room: changes });
|
|
|
|
} else {
|
|
|
|
this.state.room = changes;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-06-15 14:00:46 +00:00
|
|
|
this.setHeader();
|
2018-03-02 15:11:34 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
async componentDidMount() {
|
|
|
|
this.mounted = true;
|
2018-12-05 20:52:08 +00:00
|
|
|
this.fetchMembers();
|
2019-03-12 16:23:06 +00:00
|
|
|
|
2020-06-15 14:00:46 +00:00
|
|
|
const { route } = this.props;
|
|
|
|
const rid = route.params?.rid;
|
2019-09-16 20:26:32 +00:00
|
|
|
this.permissions = await RocketChat.hasPermission(['mute-user'], rid);
|
2018-12-21 10:55:35 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
componentWillUnmount() {
|
2019-09-16 20:26:32 +00:00
|
|
|
if (this.subscription && this.subscription.unsubscribe) {
|
|
|
|
this.subscription.unsubscribe();
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 14:00:46 +00:00
|
|
|
setHeader = () => {
|
|
|
|
const { allUsers } = this.state;
|
|
|
|
const { navigation } = this.props;
|
|
|
|
const toggleText = allUsers ? I18n.t('Online') : I18n.t('All');
|
|
|
|
navigation.setOptions({
|
|
|
|
title: I18n.t('Members'),
|
|
|
|
headerRight: () => (
|
|
|
|
<CustomHeaderButtons>
|
|
|
|
<Item title={toggleText} onPress={this.toggleStatus} testID='room-members-view-toggle-status' />
|
|
|
|
</CustomHeaderButtons>
|
|
|
|
)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-26 12:46:27 +00:00
|
|
|
onSearchChangeText = protectedFunction((text) => {
|
|
|
|
const { members } = this.state;
|
|
|
|
let membersFiltered = [];
|
|
|
|
|
|
|
|
if (members && members.length > 0 && text) {
|
|
|
|
membersFiltered = members.filter(m => m.username.toLowerCase().match(text.toLowerCase()));
|
|
|
|
}
|
|
|
|
this.setState({ filtering: !!text, membersFiltered });
|
|
|
|
})
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
onPressUser = async(item) => {
|
2018-05-18 17:55:08 +00:00
|
|
|
try {
|
2019-09-16 20:26:32 +00:00
|
|
|
const db = database.active;
|
|
|
|
const subsCollection = db.collections.get('subscriptions');
|
|
|
|
const query = await subsCollection.query(Q.where('name', item.username)).fetch();
|
2020-01-07 17:31:37 +00:00
|
|
|
if (query.length) {
|
2019-09-16 20:26:32 +00:00
|
|
|
const [room] = query;
|
2020-06-15 14:00:46 +00:00
|
|
|
this.goRoom(room);
|
2018-05-18 17:55:08 +00:00
|
|
|
} else {
|
2018-12-05 20:52:08 +00:00
|
|
|
const result = await RocketChat.createDirectMessage(item.username);
|
|
|
|
if (result.success) {
|
2020-06-15 14:00:46 +00:00
|
|
|
this.goRoom({ rid: result.room?._id, name: item.username, t: 'd' });
|
2018-12-05 20:52:08 +00:00
|
|
|
}
|
2018-05-18 17:55:08 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2019-08-23 13:18:47 +00:00
|
|
|
log(e);
|
2018-03-02 15:11:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
onLongPressUser = (user) => {
|
|
|
|
if (!this.permissions['mute-user']) {
|
|
|
|
return;
|
|
|
|
}
|
2019-01-30 12:11:02 +00:00
|
|
|
const { room } = this.state;
|
|
|
|
const { muted } = room;
|
2018-12-21 10:55:35 +00:00
|
|
|
|
2019-01-30 12:11:02 +00:00
|
|
|
this.actionSheetOptions = [I18n.t('Cancel')];
|
2019-09-16 20:26:32 +00:00
|
|
|
const userIsMuted = !!(muted || []).find(m => m === user.username);
|
2019-01-30 12:11:02 +00:00
|
|
|
user.muted = userIsMuted;
|
|
|
|
if (userIsMuted) {
|
|
|
|
this.actionSheetOptions.push(I18n.t('Unmute'));
|
|
|
|
} else {
|
|
|
|
this.actionSheetOptions.push(I18n.t('Mute'));
|
2018-08-01 19:35:06 +00:00
|
|
|
}
|
2019-01-30 12:11:02 +00:00
|
|
|
this.setState({ userLongPressed: user });
|
2019-06-28 17:07:17 +00:00
|
|
|
Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light);
|
2019-01-30 12:11:02 +00:00
|
|
|
this.showActionSheet();
|
|
|
|
}
|
|
|
|
|
2019-03-12 16:23:06 +00:00
|
|
|
toggleStatus = () => {
|
|
|
|
try {
|
|
|
|
const { allUsers } = this.state;
|
2019-04-17 17:01:03 +00:00
|
|
|
this.setState({ members: [], allUsers: !allUsers, end: false }, () => {
|
|
|
|
this.fetchMembers();
|
|
|
|
});
|
2019-03-12 16:23:06 +00:00
|
|
|
} catch (e) {
|
2019-08-23 13:18:47 +00:00
|
|
|
log(e);
|
2019-03-12 16:23:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-30 12:11:02 +00:00
|
|
|
showActionSheet = () => {
|
|
|
|
ActionSheet.showActionSheetWithOptions({
|
|
|
|
options: this.actionSheetOptions,
|
|
|
|
cancelButtonIndex: this.CANCEL_INDEX,
|
|
|
|
title: I18n.t('Actions')
|
|
|
|
}, (actionIndex) => {
|
|
|
|
this.handleActionPress(actionIndex);
|
|
|
|
});
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
|
2019-04-17 17:01:03 +00:00
|
|
|
// eslint-disable-next-line react/sort-comp
|
|
|
|
fetchMembers = async() => {
|
|
|
|
const {
|
|
|
|
rid, members, isLoading, allUsers, end
|
|
|
|
} = this.state;
|
|
|
|
if (isLoading || end) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({ isLoading: true });
|
2019-04-08 12:35:28 +00:00
|
|
|
try {
|
2019-04-17 17:01:03 +00:00
|
|
|
const membersResult = await RocketChat.getRoomMembers(rid, allUsers, members.length, PAGE_SIZE);
|
|
|
|
const newMembers = membersResult.records;
|
|
|
|
this.setState({
|
|
|
|
members: members.concat(newMembers || []),
|
|
|
|
isLoading: false,
|
|
|
|
end: newMembers.length < PAGE_SIZE
|
|
|
|
});
|
2020-06-15 14:00:46 +00:00
|
|
|
this.setHeader();
|
2019-08-23 13:18:47 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2019-04-08 12:35:28 +00:00
|
|
|
this.setState({ isLoading: false });
|
|
|
|
}
|
2018-12-05 20:52:08 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 14:00:46 +00:00
|
|
|
goRoom = (item) => {
|
|
|
|
const { navigation, isMasterDetail } = this.props;
|
|
|
|
if (isMasterDetail) {
|
|
|
|
navigation.navigate('DrawerNavigator');
|
|
|
|
} else {
|
|
|
|
navigation.popToTop();
|
|
|
|
}
|
|
|
|
goRoom({ item, isMasterDetail });
|
2018-07-10 13:40:32 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
handleMute = async() => {
|
|
|
|
const { rid, userLongPressed } = this.state;
|
|
|
|
try {
|
|
|
|
await RocketChat.toggleMuteUserInRoom(rid, userLongPressed.username, !userLongPressed.muted);
|
2019-07-23 14:02:57 +00:00
|
|
|
EventEmitter.emit(LISTENER, { message: I18n.t('User_has_been_key', { key: userLongPressed.muted ? I18n.t('unmuted') : I18n.t('muted') }) });
|
2018-05-18 17:55:08 +00:00
|
|
|
} catch (e) {
|
2019-08-23 13:18:47 +00:00
|
|
|
log(e);
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleActionPress = (actionIndex) => {
|
|
|
|
switch (actionIndex) {
|
|
|
|
case this.MUTE_INDEX:
|
|
|
|
this.handleMute();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-02 15:11:34 +00:00
|
|
|
renderSearchBar = () => (
|
2018-08-31 18:13:30 +00:00
|
|
|
<SearchBox onChangeText={text => this.onSearchChangeText(text)} testID='room-members-view-search' />
|
2018-03-02 15:11:34 +00:00
|
|
|
)
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
renderSeparator = () => {
|
|
|
|
const { theme } = this.props;
|
|
|
|
return <View style={[styles.separator, { backgroundColor: themes[theme].separatorColor }]} />;
|
|
|
|
}
|
2018-03-02 15:11:34 +00:00
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
renderItem = ({ item }) => {
|
2019-12-04 16:39:53 +00:00
|
|
|
const { baseUrl, user, theme } = this.props;
|
2018-09-25 19:28:42 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<UserItem
|
|
|
|
name={item.name}
|
|
|
|
username={item.username}
|
|
|
|
onPress={() => this.onPressUser(item)}
|
|
|
|
onLongPress={() => this.onLongPressUser(item)}
|
|
|
|
baseUrl={baseUrl}
|
|
|
|
testID={`room-members-view-item-${ item.username }`}
|
2019-02-07 19:58:20 +00:00
|
|
|
user={user}
|
2019-12-04 16:39:53 +00:00
|
|
|
theme={theme}
|
2018-09-25 19:28:42 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2018-03-02 15:11:34 +00:00
|
|
|
|
|
|
|
render() {
|
2018-12-21 10:55:35 +00:00
|
|
|
const {
|
2019-03-18 19:13:59 +00:00
|
|
|
filtering, members, membersFiltered, isLoading
|
2018-12-21 10:55:35 +00:00
|
|
|
} = this.state;
|
2019-12-04 16:39:53 +00:00
|
|
|
const { theme } = this.props;
|
2018-03-02 15:11:34 +00:00
|
|
|
return (
|
2020-06-15 14:00:46 +00:00
|
|
|
<SafeAreaView testID='room-members-view' theme={theme}>
|
2019-12-04 16:39:53 +00:00
|
|
|
<StatusBar theme={theme} />
|
2018-04-24 19:34:03 +00:00
|
|
|
<FlatList
|
|
|
|
data={filtering ? membersFiltered : members}
|
|
|
|
renderItem={this.renderItem}
|
2019-12-04 16:39:53 +00:00
|
|
|
style={[styles.list, { backgroundColor: themes[theme].backgroundColor }]}
|
2018-04-24 19:34:03 +00:00
|
|
|
keyExtractor={item => item._id}
|
|
|
|
ItemSeparatorComponent={this.renderSeparator}
|
|
|
|
ListHeaderComponent={this.renderSearchBar}
|
2019-04-17 17:01:03 +00:00
|
|
|
ListFooterComponent={() => {
|
|
|
|
if (isLoading) {
|
2019-12-04 16:39:53 +00:00
|
|
|
return <ActivityIndicator theme={theme} />;
|
2019-04-17 17:01:03 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}}
|
|
|
|
onEndReachedThreshold={0.1}
|
|
|
|
onEndReached={this.fetchMembers}
|
|
|
|
maxToRenderPerBatch={5}
|
|
|
|
windowSize={10}
|
2018-04-24 19:34:03 +00:00
|
|
|
{...scrollPersistTaps}
|
2018-08-01 19:35:06 +00:00
|
|
|
/>
|
|
|
|
</SafeAreaView>
|
2018-03-02 15:11:34 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 13:51:34 +00:00
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
2020-02-11 14:09:14 +00:00
|
|
|
baseUrl: state.server.server,
|
2020-06-15 14:00:46 +00:00
|
|
|
user: getUserSelector(state),
|
|
|
|
isMasterDetail: state.app.isMasterDetail
|
2019-08-07 13:51:34 +00:00
|
|
|
});
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
export default connect(mapStateToProps)(withTheme(RoomMembersView));
|