2018-03-02 15:11:34 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-09-25 19:28:42 +00:00
|
|
|
import {
|
2018-10-23 21:39:48 +00:00
|
|
|
FlatList, View, Vibration, Platform
|
2018-09-25 19:28:42 +00:00
|
|
|
} from 'react-native';
|
2018-04-24 19:34:03 +00:00
|
|
|
import ActionSheet from 'react-native-actionsheet';
|
2018-09-19 14:18:32 +00:00
|
|
|
import { connect } from 'react-redux';
|
2018-10-23 21:39:48 +00:00
|
|
|
import { Navigation } from 'react-native-navigation';
|
|
|
|
import SafeAreaView from 'react-native-safe-area-view';
|
2018-03-02 15:11:34 +00:00
|
|
|
|
2018-04-03 16:24:59 +00:00
|
|
|
import LoggedView from '../View';
|
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';
|
|
|
|
import database from '../../lib/realm';
|
2018-04-24 19:34:03 +00:00
|
|
|
import { showToast } from '../../utils/info';
|
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';
|
2018-11-14 21:42:03 +00:00
|
|
|
import { DEFAULT_HEADER } from '../../constants/headerOptions';
|
2018-03-02 15:11:34 +00:00
|
|
|
|
2018-09-19 14:18:32 +00:00
|
|
|
@connect(state => ({
|
|
|
|
baseUrl: state.settings.Site_Url || state.server ? state.server.server : ''
|
|
|
|
}))
|
2018-07-10 13:40:32 +00:00
|
|
|
/** @extends React.Component */
|
|
|
|
export default class RoomMembersView extends LoggedView {
|
2018-10-23 21:39:48 +00:00
|
|
|
static options() {
|
|
|
|
return {
|
2018-11-14 21:42:03 +00:00
|
|
|
...DEFAULT_HEADER,
|
2018-10-23 21:39:48 +00:00
|
|
|
topBar: {
|
2018-11-14 21:42:03 +00:00
|
|
|
...DEFAULT_HEADER.topBar,
|
2018-10-23 21:39:48 +00:00
|
|
|
title: {
|
2018-11-14 21:42:03 +00:00
|
|
|
...DEFAULT_HEADER.topBar.title,
|
2018-10-23 21:39:48 +00:00
|
|
|
text: I18n.t('Members')
|
|
|
|
},
|
|
|
|
rightButtons: [{
|
|
|
|
id: 'toggleOnline',
|
|
|
|
text: I18n.t('Online'),
|
|
|
|
testID: 'room-members-view-toggle-status',
|
|
|
|
color: Platform.OS === 'android' ? '#FFF' : undefined
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2018-07-10 13:40:32 +00:00
|
|
|
|
2018-03-02 15:11:34 +00:00
|
|
|
static propTypes = {
|
2018-10-23 21:39:48 +00:00
|
|
|
componentId: PropTypes.string,
|
2018-07-10 13:40:32 +00:00
|
|
|
rid: PropTypes.string,
|
2018-09-19 14:18:32 +00:00
|
|
|
members: PropTypes.array,
|
|
|
|
baseUrl: PropTypes.string
|
2018-03-02 15:11:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
2018-04-03 16:24:59 +00:00
|
|
|
super('MentionedMessagesView', props);
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
this.CANCEL_INDEX = 0;
|
|
|
|
this.MUTE_INDEX = 1;
|
|
|
|
this.actionSheetOptions = [''];
|
2018-07-10 13:40:32 +00:00
|
|
|
const { rid, members } = props;
|
2018-04-24 19:34:03 +00:00
|
|
|
this.rooms = database.objects('subscriptions').filtered('rid = $0', rid);
|
|
|
|
this.permissions = RocketChat.hasPermission(['mute-user'], rid);
|
2018-03-02 15:11:34 +00:00
|
|
|
this.state = {
|
|
|
|
allUsers: false,
|
|
|
|
filtering: false,
|
|
|
|
rid,
|
|
|
|
members,
|
2018-04-24 19:34:03 +00:00
|
|
|
membersFiltered: [],
|
|
|
|
userLongPressed: {},
|
|
|
|
room: {}
|
2018-03-02 15:11:34 +00:00
|
|
|
};
|
2018-10-23 21:39:48 +00:00
|
|
|
Navigation.events().bindComponent(this);
|
2018-03-02 15:11:34 +00:00
|
|
|
}
|
|
|
|
|
2018-03-06 17:40:44 +00:00
|
|
|
componentDidMount() {
|
2018-04-24 19:34:03 +00:00
|
|
|
this.rooms.addListener(this.updateRoom);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.rooms.removeAllListeners();
|
|
|
|
}
|
|
|
|
|
2018-10-23 21:39:48 +00:00
|
|
|
navigationButtonPressed = async({ buttonId }) => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { rid, allUsers } = this.state;
|
2018-10-23 21:39:48 +00:00
|
|
|
const { componentId } = this.props;
|
|
|
|
|
|
|
|
if (buttonId === 'toggleOnline') {
|
|
|
|
try {
|
|
|
|
Navigation.mergeOptions(componentId, {
|
|
|
|
topBar: {
|
2018-07-10 13:40:32 +00:00
|
|
|
rightButtons: [{
|
|
|
|
id: 'toggleOnline',
|
2018-10-23 21:39:48 +00:00
|
|
|
text: allUsers ? I18n.t('Online') : I18n.t('All'),
|
2018-07-10 13:40:32 +00:00
|
|
|
testID: 'room-members-view-toggle-status'
|
|
|
|
}]
|
2018-10-23 21:39:48 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
const allUsersFilter = !allUsers;
|
|
|
|
const membersResult = await RocketChat.getRoomMembers(rid, allUsersFilter);
|
|
|
|
const members = membersResult.records;
|
|
|
|
this.setState({ allUsers: allUsersFilter, members });
|
|
|
|
} catch (e) {
|
|
|
|
log('RoomMembers.onNavigationButtonPressed', e);
|
2018-07-10 13:40:32 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-02 15:11:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onSearchChangeText = (text) => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { members } = this.state;
|
|
|
|
|
2018-03-02 15:11:34 +00:00
|
|
|
let membersFiltered = [];
|
|
|
|
if (text) {
|
2018-09-25 19:28:42 +00:00
|
|
|
membersFiltered = members.filter(m => m.username.toLowerCase().match(text.toLowerCase()));
|
2018-03-02 15:11:34 +00:00
|
|
|
}
|
|
|
|
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 {
|
|
|
|
const subscriptions = database.objects('subscriptions').filtered('name = $0', item.username);
|
|
|
|
if (subscriptions.length) {
|
2018-10-31 18:40:08 +00:00
|
|
|
this.goRoom({ rid: subscriptions[0].rid });
|
2018-05-18 17:55:08 +00:00
|
|
|
} else {
|
|
|
|
const room = await RocketChat.createDirectMessage(item.username);
|
2018-10-31 18:40:08 +00:00
|
|
|
this.goRoom({ rid: room.rid });
|
2018-05-18 17:55:08 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
log('onPressUser', 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;
|
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
const { room } = this.state;
|
|
|
|
const { muted } = room;
|
|
|
|
|
2018-06-01 17:38:13 +00:00
|
|
|
this.actionSheetOptions = [I18n.t('Cancel')];
|
2018-04-24 19:34:03 +00:00
|
|
|
const userIsMuted = !!muted.find(m => m.value === user.username);
|
|
|
|
user.muted = userIsMuted;
|
|
|
|
if (userIsMuted) {
|
2018-06-01 17:38:13 +00:00
|
|
|
this.actionSheetOptions.push(I18n.t('Unmute'));
|
2018-04-24 19:34:03 +00:00
|
|
|
} else {
|
2018-06-01 17:38:13 +00:00
|
|
|
this.actionSheetOptions.push(I18n.t('Mute'));
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
this.setState({ userLongPressed: user });
|
|
|
|
Vibration.vibrate(50);
|
2018-08-01 19:35:06 +00:00
|
|
|
if (this.actionSheet && this.actionSheet.show) {
|
|
|
|
this.actionSheet.show();
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
|
2018-07-10 13:40:32 +00:00
|
|
|
updateRoom = async() => {
|
|
|
|
const [room] = this.rooms;
|
|
|
|
await this.setState({ room });
|
|
|
|
}
|
|
|
|
|
2018-10-31 18:40:08 +00:00
|
|
|
goRoom = async({ rid }) => {
|
2018-10-23 21:39:48 +00:00
|
|
|
const { componentId } = this.props;
|
|
|
|
await Navigation.popToRoot(componentId);
|
|
|
|
Navigation.push('RoomsListView', {
|
|
|
|
component: {
|
|
|
|
name: 'RoomView',
|
|
|
|
passProps: {
|
|
|
|
rid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
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);
|
2018-06-01 17:38:13 +00:00
|
|
|
showToast(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) {
|
|
|
|
log('handleMute', 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
|
|
|
)
|
|
|
|
|
|
|
|
renderSeparator = () => <View style={styles.separator} />;
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
renderItem = ({ item }) => {
|
|
|
|
const { baseUrl } = this.props;
|
|
|
|
|
|
|
|
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 }`}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2018-03-02 15:11:34 +00:00
|
|
|
|
|
|
|
render() {
|
|
|
|
const { filtering, members, membersFiltered } = this.state;
|
|
|
|
return (
|
2018-10-23 21:39:48 +00:00
|
|
|
<SafeAreaView style={styles.list} testID='room-members-view' forceInset={{ bottom: 'never' }}>
|
2018-04-24 19:34:03 +00:00
|
|
|
<FlatList
|
|
|
|
data={filtering ? membersFiltered : members}
|
|
|
|
renderItem={this.renderItem}
|
|
|
|
style={styles.list}
|
|
|
|
keyExtractor={item => item._id}
|
|
|
|
ItemSeparatorComponent={this.renderSeparator}
|
|
|
|
ListHeaderComponent={this.renderSearchBar}
|
|
|
|
{...scrollPersistTaps}
|
2018-08-01 19:35:06 +00:00
|
|
|
/>
|
2018-04-24 19:34:03 +00:00
|
|
|
<ActionSheet
|
2018-08-01 19:35:06 +00:00
|
|
|
ref={o => this.actionSheet = o}
|
2018-06-01 17:38:13 +00:00
|
|
|
title={I18n.t('Actions')}
|
2018-04-24 19:34:03 +00:00
|
|
|
options={this.actionSheetOptions}
|
|
|
|
cancelButtonIndex={this.CANCEL_INDEX}
|
|
|
|
onPress={this.handleActionPress}
|
|
|
|
/>
|
2018-08-01 19:35:06 +00:00
|
|
|
</SafeAreaView>
|
2018-03-02 15:11:34 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|