2018-03-02 15:11:34 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-04-24 19:34:03 +00:00
|
|
|
import { FlatList, Text, View, TextInput, Vibration } from 'react-native';
|
2018-03-02 15:11:34 +00:00
|
|
|
import { connect } from 'react-redux';
|
2018-04-24 19:34:03 +00:00
|
|
|
import ActionSheet from 'react-native-actionsheet';
|
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-04-24 19:34:03 +00:00
|
|
|
import RoomItem from '../../presentation/RoomItem';
|
2018-03-02 15:11:34 +00:00
|
|
|
import Touch from '../../utils/touch';
|
|
|
|
import scrollPersistTaps from '../../utils/scrollPersistTaps';
|
|
|
|
import RocketChat from '../../lib/rocketchat';
|
|
|
|
import { goRoom } from '../../containers/routes/NavigationService';
|
|
|
|
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-03-02 15:11:34 +00:00
|
|
|
|
|
|
|
@connect(state => ({
|
|
|
|
user: state.login.user,
|
|
|
|
baseUrl: state.settings.Site_Url || state.server ? state.server.server : ''
|
|
|
|
}))
|
2018-04-03 16:24:59 +00:00
|
|
|
export default class MentionedMessagesView extends LoggedView {
|
2018-03-02 15:11:34 +00:00
|
|
|
static propTypes = {
|
|
|
|
navigation: PropTypes.object
|
|
|
|
}
|
|
|
|
|
|
|
|
static navigationOptions = ({ navigation }) => {
|
|
|
|
const params = navigation.state.params || {};
|
|
|
|
const label = params.allUsers ? 'All' : 'Online';
|
|
|
|
if (params.allUsers === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
headerRight: (
|
|
|
|
<Touch
|
|
|
|
onPress={params.onPressToogleStatus}
|
|
|
|
underlayColor='#ffffff'
|
|
|
|
activeOpacity={0.5}
|
|
|
|
accessibilityLabel={label}
|
|
|
|
accessibilityTraits='button'
|
|
|
|
style={styles.headerButtonTouchable}
|
2018-05-23 13:39:18 +00:00
|
|
|
testID='room-members-view-toggle-status'
|
2018-03-02 15:11:34 +00:00
|
|
|
>
|
|
|
|
<View style={styles.headerButton}>
|
|
|
|
<Text style={styles.headerButtonText}>{label}</Text>
|
|
|
|
</View>
|
|
|
|
</Touch>
|
|
|
|
)
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
2018-04-03 16:24:59 +00:00
|
|
|
super('MentionedMessagesView', props);
|
2018-04-24 19:34:03 +00:00
|
|
|
this.CANCEL_INDEX = 0;
|
|
|
|
this.MUTE_INDEX = 1;
|
|
|
|
this.actionSheetOptions = [''];
|
2018-03-02 15:11:34 +00:00
|
|
|
const { rid, members } = props.navigation.state.params;
|
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-03-06 17:40:44 +00:00
|
|
|
componentDidMount() {
|
2018-03-02 15:11:34 +00:00
|
|
|
this.props.navigation.setParams({
|
|
|
|
onPressToogleStatus: this.onPressToogleStatus,
|
|
|
|
allUsers: this.state.allUsers
|
|
|
|
});
|
2018-04-24 19:34:03 +00:00
|
|
|
this.rooms.addListener(this.updateRoom);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.rooms.removeAllListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
updateRoom = async() => {
|
|
|
|
const [room] = this.rooms;
|
|
|
|
await this.setState({ room });
|
2018-03-02 15:11:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onSearchChangeText = (text) => {
|
|
|
|
let membersFiltered = [];
|
|
|
|
if (text) {
|
|
|
|
membersFiltered = this.state.members.filter(m => m.username.toLowerCase().match(text.toLowerCase()));
|
|
|
|
}
|
|
|
|
this.setState({ filtering: !!text, membersFiltered });
|
|
|
|
}
|
|
|
|
|
|
|
|
onPressToogleStatus = async() => {
|
2018-05-18 17:55:08 +00:00
|
|
|
try {
|
|
|
|
const allUsers = !this.state.allUsers;
|
|
|
|
this.props.navigation.setParams({ allUsers });
|
|
|
|
const membersResult = await RocketChat.getRoomMembers(this.state.rid, allUsers);
|
|
|
|
const members = membersResult.records;
|
|
|
|
this.setState({ allUsers, members });
|
|
|
|
} catch (e) {
|
|
|
|
log('onPressToogleStatus', e);
|
|
|
|
}
|
2018-03-02 15:11:34 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
goRoom({ rid: subscriptions[0].rid, name: subscriptions[0].name });
|
|
|
|
} else {
|
|
|
|
const room = await RocketChat.createDirectMessage(item.username);
|
|
|
|
goRoom({ rid: room.rid, name: item.username });
|
|
|
|
}
|
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
this.actionSheetOptions = ['Cancel'];
|
|
|
|
const { muted } = this.state.room;
|
|
|
|
const userIsMuted = !!muted.find(m => m.value === user.username);
|
|
|
|
user.muted = userIsMuted;
|
|
|
|
if (userIsMuted) {
|
|
|
|
this.actionSheetOptions.push('Unmute');
|
|
|
|
} else {
|
|
|
|
this.actionSheetOptions.push('Mute');
|
|
|
|
}
|
|
|
|
this.setState({ userLongPressed: user });
|
|
|
|
Vibration.vibrate(50);
|
|
|
|
this.ActionSheet.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleMute = async() => {
|
|
|
|
const { rid, userLongPressed } = this.state;
|
|
|
|
try {
|
|
|
|
await RocketChat.toggleMuteUserInRoom(rid, userLongPressed.username, !userLongPressed.muted);
|
|
|
|
showToast(`User has been ${ userLongPressed.muted ? 'unmuted' : '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 = () => (
|
|
|
|
<View style={styles.searchBoxView}>
|
|
|
|
<TextInput
|
|
|
|
underlineColorAndroid='transparent'
|
|
|
|
style={styles.searchBox}
|
|
|
|
onChangeText={text => this.onSearchChangeText(text)}
|
|
|
|
returnKeyType='search'
|
|
|
|
placeholder='Search'
|
|
|
|
clearButtonMode='while-editing'
|
|
|
|
blurOnSubmit
|
2018-05-18 17:55:08 +00:00
|
|
|
autoCorrect={false}
|
|
|
|
autoCapitalize='none'
|
2018-05-23 13:39:18 +00:00
|
|
|
testID='room-members-view-search'
|
2018-03-02 15:11:34 +00:00
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
)
|
|
|
|
|
|
|
|
renderSeparator = () => <View style={styles.separator} />;
|
|
|
|
|
|
|
|
renderItem = ({ item }) => (
|
2018-04-24 19:34:03 +00:00
|
|
|
<RoomItem
|
|
|
|
name={item.username}
|
|
|
|
type='d'
|
|
|
|
baseUrl={this.props.baseUrl}
|
|
|
|
onPress={() => this.onPressUser(item)}
|
|
|
|
onLongPress={() => this.onLongPressUser(item)}
|
|
|
|
id={item._id}
|
|
|
|
showLastMessage={false}
|
|
|
|
avatarSize={30}
|
|
|
|
statusStyle={styles.status}
|
2018-05-23 13:39:18 +00:00
|
|
|
testID={`room-members-view-item-${ item.username }`}
|
2018-04-24 19:34:03 +00:00
|
|
|
/>
|
2018-03-02 15:11:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { filtering, members, membersFiltered } = this.state;
|
|
|
|
return (
|
2018-04-24 19:34:03 +00:00
|
|
|
[
|
|
|
|
<FlatList
|
|
|
|
key='room-members-view-list'
|
2018-05-23 13:39:18 +00:00
|
|
|
testID='room-members-view'
|
2018-04-24 19:34:03 +00:00
|
|
|
data={filtering ? membersFiltered : members}
|
|
|
|
renderItem={this.renderItem}
|
|
|
|
style={styles.list}
|
|
|
|
keyExtractor={item => item._id}
|
|
|
|
ItemSeparatorComponent={this.renderSeparator}
|
|
|
|
ListHeaderComponent={this.renderSearchBar}
|
|
|
|
{...scrollPersistTaps}
|
|
|
|
/>,
|
|
|
|
<ActionSheet
|
|
|
|
key='room-members-actionsheet'
|
|
|
|
ref={o => this.ActionSheet = o}
|
|
|
|
title='Actions'
|
|
|
|
options={this.actionSheetOptions}
|
|
|
|
cancelButtonIndex={this.CANCEL_INDEX}
|
|
|
|
onPress={this.handleActionPress}
|
|
|
|
/>
|
|
|
|
]
|
2018-03-02 15:11:34 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|