2018-02-19 21:19:39 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-03-23 16:49:51 +00:00
|
|
|
import { View, SectionList, Text, Alert } from 'react-native';
|
2018-02-19 21:19:39 +00:00
|
|
|
import Icon from 'react-native-vector-icons/Ionicons';
|
|
|
|
import MaterialIcon from 'react-native-vector-icons/MaterialIcons';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
2018-04-03 16:24:59 +00:00
|
|
|
import LoggedView from '../View';
|
2018-02-19 21:19:39 +00:00
|
|
|
import styles from './styles';
|
2018-03-29 17:55:37 +00:00
|
|
|
import sharedStyles from '../Styles';
|
2018-02-19 21:19:39 +00:00
|
|
|
import Avatar from '../../containers/Avatar';
|
2018-03-29 17:55:37 +00:00
|
|
|
import Status from '../../containers/status';
|
2018-02-19 21:19:39 +00:00
|
|
|
import Touch from '../../utils/touch';
|
|
|
|
import database from '../../lib/realm';
|
2018-03-02 15:11:34 +00:00
|
|
|
import RocketChat from '../../lib/rocketchat';
|
2018-03-23 16:49:51 +00:00
|
|
|
import { leaveRoom } from '../../actions/room';
|
2018-04-24 19:34:03 +00:00
|
|
|
import { setLoading } from '../../actions/selectedUsers';
|
2018-05-18 17:55:08 +00:00
|
|
|
import log from '../../utils/log';
|
2018-05-18 16:41:47 +00:00
|
|
|
import RoomTypeIcon from '../../containers/RoomTypeIcon';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
|
|
const renderSeparator = () => <View style={styles.separator} />;
|
2018-05-18 16:41:47 +00:00
|
|
|
const getRoomTitle = room => (room.t === 'd' ? <Text>{room.fname}</Text> : <Text><RoomTypeIcon type={room.t} /> {room.name}</Text>);
|
2018-02-19 21:19:39 +00:00
|
|
|
|
|
|
|
@connect(state => ({
|
2018-04-24 19:34:03 +00:00
|
|
|
user_id: state.login.user.id,
|
|
|
|
user_username: state.login.user.username
|
2018-03-23 16:49:51 +00:00
|
|
|
}), dispatch => ({
|
2018-04-24 19:34:03 +00:00
|
|
|
leaveRoom: rid => dispatch(leaveRoom(rid)),
|
|
|
|
setLoadingInvite: loading => dispatch(setLoading(loading))
|
2018-02-19 21:19:39 +00:00
|
|
|
}))
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-04-03 16:24:59 +00:00
|
|
|
export default class RoomActionsView extends LoggedView {
|
2018-02-19 21:19:39 +00:00
|
|
|
static propTypes = {
|
|
|
|
baseUrl: PropTypes.string,
|
2018-03-23 16:49:51 +00:00
|
|
|
user: PropTypes.object,
|
|
|
|
navigation: PropTypes.object,
|
|
|
|
leaveRoom: PropTypes.func
|
2018-02-19 21:19:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
2018-04-03 16:24:59 +00:00
|
|
|
super('RoomActionsView', props);
|
2018-02-19 21:19:39 +00:00
|
|
|
const { rid } = props.navigation.state.params;
|
|
|
|
this.rooms = database.objects('subscriptions').filtered('rid = $0', rid);
|
2018-04-24 19:34:03 +00:00
|
|
|
[this.room] = this.rooms;
|
2018-02-19 21:19:39 +00:00
|
|
|
this.state = {
|
2018-04-24 19:34:03 +00:00
|
|
|
room: this.room,
|
|
|
|
onlineMembers: [],
|
|
|
|
allMembers: [],
|
2018-03-29 17:55:37 +00:00
|
|
|
member: {}
|
2018-02-19 21:19:39 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-23 16:49:51 +00:00
|
|
|
async componentDidMount() {
|
2018-02-19 21:19:39 +00:00
|
|
|
this.rooms.addListener(this.updateRoom);
|
2018-04-24 19:34:03 +00:00
|
|
|
const [members, member] = await Promise.all([this.updateRoomMembers(), this.updateRoomMember()]);
|
|
|
|
this.setState({ ...members, ...member });
|
2018-02-19 21:19:39 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 16:49:51 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
this.rooms.removeAllListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
onPressTouchable = (item) => {
|
|
|
|
if (item.route) {
|
2018-04-10 13:03:54 +00:00
|
|
|
return this.props.navigation.navigate({ key: item.route, routeName: item.route, params: item.params });
|
2018-03-23 16:49:51 +00:00
|
|
|
}
|
|
|
|
if (item.event) {
|
|
|
|
return item.event();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updateRoomMembers = async() => {
|
2018-04-24 19:34:03 +00:00
|
|
|
const { t } = this.state.room;
|
|
|
|
if (t === 'c' || t === 'p') {
|
|
|
|
let onlineMembers = [];
|
|
|
|
let allMembers = [];
|
|
|
|
try {
|
|
|
|
const onlineMembersCall = RocketChat.getRoomMembers(this.state.room.rid, false);
|
|
|
|
const allMembersCall = RocketChat.getRoomMembers(this.state.room.rid, true);
|
|
|
|
const [onlineMembersResult, allMembersResult] = await Promise.all([onlineMembersCall, allMembersCall]);
|
|
|
|
onlineMembers = onlineMembersResult.records;
|
|
|
|
allMembers = allMembersResult.records;
|
|
|
|
return { onlineMembers, allMembers };
|
|
|
|
} catch (error) {
|
|
|
|
return {};
|
|
|
|
}
|
2018-03-23 16:49:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-29 17:55:37 +00:00
|
|
|
updateRoomMember = async() => {
|
2018-05-18 17:55:08 +00:00
|
|
|
if (this.state.room.t !== 'd') {
|
2018-04-24 19:34:03 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const member = await RocketChat.getRoomMember(this.state.room.rid, this.props.user_id);
|
|
|
|
return { member };
|
2018-05-18 17:55:08 +00:00
|
|
|
} catch (e) {
|
|
|
|
log('RoomActions updateRoomMember', e);
|
2018-04-24 19:34:03 +00:00
|
|
|
return {};
|
2018-03-29 17:55:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
updateRoom = () => {
|
|
|
|
this.setState({ room: this.room });
|
|
|
|
}
|
|
|
|
get canAddUser() { // Invite user
|
|
|
|
const {
|
|
|
|
rid, t
|
|
|
|
} = this.room;
|
|
|
|
const { allMembers } = this.state;
|
|
|
|
// TODO: same test joined
|
|
|
|
const userInRoom = !!allMembers.find(m => m.username === this.props.user_username);
|
|
|
|
const permissions = RocketChat.hasPermission(['add-user-to-joined-room', 'add-user-to-any-c-room', 'add-user-to-any-p-room'], rid);
|
|
|
|
|
|
|
|
if (userInRoom && permissions['add-user-to-joined-room']) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (t === 'c' && permissions['add-user-to-any-c-room']) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (t === 'p' && permissions['add-user-to-any-p-room']) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2018-02-19 21:19:39 +00:00
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
get sections() {
|
|
|
|
const {
|
|
|
|
rid, t, blocked, notifications
|
|
|
|
} = this.room;
|
|
|
|
const { onlineMembers } = this.state;
|
2018-02-19 21:19:39 +00:00
|
|
|
|
|
|
|
const sections = [{
|
2018-03-29 17:55:37 +00:00
|
|
|
data: [{
|
|
|
|
icon: 'ios-star',
|
|
|
|
name: 'USER',
|
|
|
|
route: 'RoomInfo',
|
|
|
|
params: { rid }
|
|
|
|
}],
|
2018-02-19 21:19:39 +00:00
|
|
|
renderItem: this.renderRoomInfo
|
|
|
|
}, {
|
|
|
|
data: [
|
2018-03-29 17:55:37 +00:00
|
|
|
{ icon: 'ios-call-outline', name: 'Voice call', disabled: true },
|
|
|
|
{ icon: 'ios-videocam-outline', name: 'Video call', disabled: true }
|
2018-02-19 21:19:39 +00:00
|
|
|
],
|
|
|
|
renderItem: this.renderItem
|
|
|
|
}, {
|
|
|
|
data: [
|
2018-03-23 16:49:51 +00:00
|
|
|
{
|
|
|
|
icon: 'ios-attach',
|
|
|
|
name: 'Files',
|
|
|
|
route: 'RoomFiles',
|
|
|
|
params: { rid }
|
|
|
|
},
|
2018-03-02 15:11:34 +00:00
|
|
|
{
|
|
|
|
icon: 'ios-at-outline',
|
|
|
|
name: 'Mentions',
|
|
|
|
route: 'MentionedMessages',
|
|
|
|
params: { rid }
|
|
|
|
},
|
2018-02-19 21:19:39 +00:00
|
|
|
{
|
|
|
|
icon: 'ios-star-outline',
|
|
|
|
name: 'Starred',
|
|
|
|
route: 'StarredMessages',
|
|
|
|
params: { rid }
|
|
|
|
},
|
2018-04-24 19:34:03 +00:00
|
|
|
{
|
|
|
|
icon: 'ios-search',
|
|
|
|
name: 'Search',
|
|
|
|
route: 'SearchMessages',
|
|
|
|
params: { rid }
|
|
|
|
},
|
2018-03-29 17:55:37 +00:00
|
|
|
{ icon: 'ios-share-outline', name: 'Share', disabled: true },
|
2018-02-19 21:19:39 +00:00
|
|
|
{
|
|
|
|
icon: 'ios-pin',
|
|
|
|
name: 'Pinned',
|
|
|
|
route: 'PinnedMessages',
|
|
|
|
params: { rid }
|
|
|
|
},
|
2018-03-23 16:49:51 +00:00
|
|
|
{
|
|
|
|
icon: 'ios-code',
|
|
|
|
name: 'Snippets',
|
|
|
|
route: 'SnippetedMessages',
|
|
|
|
params: { rid }
|
|
|
|
},
|
2018-04-24 19:34:03 +00:00
|
|
|
{
|
|
|
|
icon: `ios-notifications${ notifications ? '' : '-off' }-outline`,
|
|
|
|
name: `${ notifications ? 'Enable' : 'Disable' } notifications`,
|
|
|
|
event: () => this.toggleNotifications()
|
|
|
|
}
|
2018-02-19 21:19:39 +00:00
|
|
|
],
|
|
|
|
renderItem: this.renderItem
|
|
|
|
}];
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-02-19 21:19:39 +00:00
|
|
|
if (t === 'd') {
|
|
|
|
sections.push({
|
|
|
|
data: [
|
2018-03-23 16:49:51 +00:00
|
|
|
{
|
|
|
|
icon: 'block',
|
|
|
|
name: `${ blocked ? 'Unblock' : 'Block' } user`,
|
|
|
|
type: 'danger',
|
|
|
|
event: () => this.toggleBlockUser()
|
|
|
|
}
|
2018-02-19 21:19:39 +00:00
|
|
|
],
|
|
|
|
renderItem: this.renderItem
|
|
|
|
});
|
|
|
|
} else if (t === 'c' || t === 'p') {
|
2018-04-24 19:34:03 +00:00
|
|
|
const actions = [{
|
|
|
|
icon: 'ios-people',
|
|
|
|
name: 'Members',
|
|
|
|
description: (onlineMembers.length === 1 ? `${ onlineMembers.length } member` : `${ onlineMembers.length } members`),
|
|
|
|
route: 'RoomMembers',
|
|
|
|
params: { rid, members: onlineMembers }
|
|
|
|
}];
|
|
|
|
|
|
|
|
if (this.canAddUser) {
|
|
|
|
actions.push({
|
|
|
|
icon: 'ios-person-add',
|
|
|
|
name: 'Add user',
|
|
|
|
route: 'SelectedUsers',
|
|
|
|
params: {
|
|
|
|
nextAction: async() => {
|
|
|
|
try {
|
|
|
|
this.props.setLoadingInvite(true);
|
|
|
|
await RocketChat.addUsersToRoom(rid);
|
|
|
|
this.props.navigation.goBack();
|
2018-05-18 17:55:08 +00:00
|
|
|
} catch (e) {
|
|
|
|
log('RoomActions Add User', e);
|
2018-04-24 19:34:03 +00:00
|
|
|
} finally {
|
|
|
|
this.props.setLoadingInvite(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-23 16:49:51 +00:00
|
|
|
});
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
sections[2].data = [...actions, ...sections[2].data];
|
2018-02-19 21:19:39 +00:00
|
|
|
sections.push({
|
|
|
|
data: [
|
2018-03-23 16:49:51 +00:00
|
|
|
{
|
|
|
|
icon: 'block',
|
|
|
|
name: 'Leave channel',
|
|
|
|
type: 'danger',
|
|
|
|
event: () => this.leaveChannel()
|
|
|
|
}
|
2018-02-19 21:19:39 +00:00
|
|
|
],
|
|
|
|
renderItem: this.renderItem
|
|
|
|
});
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
return sections;
|
2018-02-19 21:19:39 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 16:49:51 +00:00
|
|
|
toggleBlockUser = () => {
|
|
|
|
const { rid, blocked } = this.state.room;
|
2018-03-29 17:55:37 +00:00
|
|
|
const { member } = this.state;
|
2018-05-18 17:55:08 +00:00
|
|
|
try {
|
|
|
|
RocketChat.toggleBlockUser(rid, member._id, !blocked);
|
|
|
|
} catch (e) {
|
|
|
|
log('toggleBlockUser', e);
|
|
|
|
}
|
2018-03-23 16:49:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
leaveChannel = () => {
|
|
|
|
const { room } = this.state;
|
|
|
|
Alert.alert(
|
|
|
|
'Are you sure?',
|
2018-04-24 19:34:03 +00:00
|
|
|
`Are you sure you want to leave the room ${ getRoomTitle(room) }?`,
|
2018-03-23 16:49:51 +00:00
|
|
|
[
|
|
|
|
{
|
|
|
|
text: 'Cancel',
|
|
|
|
style: 'cancel'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: 'Yes, leave it!',
|
|
|
|
style: 'destructive',
|
2018-03-29 17:55:37 +00:00
|
|
|
onPress: () => this.props.leaveRoom(room.rid)
|
2018-03-23 16:49:51 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
toggleNotifications = () => {
|
|
|
|
const { room } = this.state;
|
2018-05-18 17:55:08 +00:00
|
|
|
try {
|
|
|
|
RocketChat.saveNotificationSettings(room.rid, 'mobilePushNotifications', room.notifications ? 'default' : 'nothing');
|
|
|
|
} catch (e) {
|
|
|
|
log('toggleNotifications', e);
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
|
2018-03-02 15:11:34 +00:00
|
|
|
renderRoomInfo = ({ item }) => {
|
2018-03-29 17:55:37 +00:00
|
|
|
const { room, member } = this.state;
|
2018-03-23 16:49:51 +00:00
|
|
|
const { name, t, topic } = room;
|
2018-03-02 15:11:34 +00:00
|
|
|
return (
|
|
|
|
this.renderTouchableItem([
|
|
|
|
<Avatar
|
|
|
|
key='avatar'
|
|
|
|
text={name}
|
|
|
|
size={50}
|
|
|
|
style={styles.avatar}
|
|
|
|
type={t}
|
2018-03-29 17:55:37 +00:00
|
|
|
>
|
|
|
|
{t === 'd' ? <Status style={sharedStyles.status} id={member._id} /> : null }
|
|
|
|
</Avatar>,
|
2018-03-02 15:11:34 +00:00
|
|
|
<View key='name' style={styles.roomTitleContainer}>
|
2018-04-24 19:34:03 +00:00
|
|
|
<Text style={styles.roomTitle}>{ getRoomTitle(room) }</Text>
|
2018-03-02 15:11:34 +00:00
|
|
|
<Text style={styles.roomDescription} ellipsizeMode='tail' numberOfLines={1}>{t === 'd' ? `@${ name }` : topic}</Text>
|
|
|
|
</View>,
|
2018-03-29 17:55:37 +00:00
|
|
|
<Icon key='icon' name='ios-arrow-forward' size={20} style={styles.sectionItemIcon} color='#ccc' />
|
2018-03-02 15:11:34 +00:00
|
|
|
], item)
|
|
|
|
);
|
|
|
|
}
|
2018-02-19 21:19:39 +00:00
|
|
|
|
|
|
|
renderTouchableItem = (subview, item) => (
|
|
|
|
<Touch
|
2018-03-23 16:49:51 +00:00
|
|
|
onPress={() => this.onPressTouchable(item)}
|
2018-02-19 21:19:39 +00:00
|
|
|
underlayColor='#FFFFFF'
|
|
|
|
activeOpacity={0.5}
|
|
|
|
accessibilityLabel={item.name}
|
|
|
|
accessibilityTraits='button'
|
|
|
|
>
|
2018-03-29 17:55:37 +00:00
|
|
|
<View style={[styles.sectionItem, item.disabled && styles.sectionItemDisabled]}>
|
2018-02-19 21:19:39 +00:00
|
|
|
{subview}
|
|
|
|
</View>
|
|
|
|
</Touch>
|
|
|
|
)
|
|
|
|
|
|
|
|
renderItem = ({ item }) => {
|
2018-04-24 19:34:03 +00:00
|
|
|
const subview = item.type === 'danger' ? [
|
|
|
|
<MaterialIcon key='icon' name={item.icon} size={20} style={[styles.sectionItemIcon, styles.textColorDanger]} />,
|
|
|
|
<Text key='name' style={[styles.sectionItemName, styles.textColorDanger]}>{ item.name }</Text>
|
|
|
|
] : [
|
2018-02-19 21:19:39 +00:00
|
|
|
<Icon key='left-icon' name={item.icon} size={24} style={styles.sectionItemIcon} />,
|
|
|
|
<Text key='name' style={styles.sectionItemName}>{ item.name }</Text>,
|
|
|
|
item.description && <Text key='description' style={styles.sectionItemDescription}>{ item.description }</Text>,
|
2018-03-29 17:55:37 +00:00
|
|
|
<Icon key='right-icon' name='ios-arrow-forward' size={20} style={styles.sectionItemIcon} color='#ccc' />
|
2018-02-19 21:19:39 +00:00
|
|
|
];
|
|
|
|
return this.renderTouchableItem(subview, item);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderSectionSeparator = (data) => {
|
2018-04-24 19:34:03 +00:00
|
|
|
if (data.trailingItem) {
|
|
|
|
return <View style={[styles.sectionSeparator, data.leadingSection && styles.sectionSeparatorBorder]} />;
|
2018-02-19 21:19:39 +00:00
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
if (!data.trailingSection) {
|
|
|
|
return <View style={styles.sectionSeparatorBorder} />;
|
|
|
|
}
|
|
|
|
return null;
|
2018-02-19 21:19:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<SectionList
|
|
|
|
style={styles.container}
|
|
|
|
stickySectionHeadersEnabled={false}
|
2018-04-24 19:34:03 +00:00
|
|
|
sections={this.sections}
|
2018-02-19 21:19:39 +00:00
|
|
|
SectionSeparatorComponent={this.renderSectionSeparator}
|
2018-04-24 19:34:03 +00:00
|
|
|
ItemSeparatorComponent={renderSeparator}
|
|
|
|
keyExtractor={item => item.name}
|
2018-02-19 21:19:39 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|