2020-06-26 20:22:56 +00:00
|
|
|
import React, { Component } from 'react';
|
2019-07-18 17:44:02 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2020-06-26 20:22:56 +00:00
|
|
|
import { View, Text, NativeModules } from 'react-native';
|
2019-07-18 17:44:02 +00:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import ShareExtension from 'rn-extensions-share';
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
import { themes } from '../../constants/colors';
|
2019-07-18 17:44:02 +00:00
|
|
|
import I18n from '../../i18n';
|
|
|
|
import styles from './styles';
|
2020-06-26 20:22:56 +00:00
|
|
|
import Loading from '../../containers/Loading';
|
2020-10-30 16:15:58 +00:00
|
|
|
import * as HeaderButton from '../../containers/HeaderButton';
|
2020-04-13 13:56:30 +00:00
|
|
|
import { isBlocked } from '../../utils/room';
|
|
|
|
import { isReadOnly } from '../../utils/isReadOnly';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { withTheme } from '../../theme';
|
2020-06-26 20:22:56 +00:00
|
|
|
import Header from './Header';
|
|
|
|
import RocketChat from '../../lib/rocketchat';
|
|
|
|
import TextInput from '../../containers/TextInput';
|
|
|
|
import Preview from './Preview';
|
|
|
|
import Thumbs from './Thumbs';
|
|
|
|
import MessageBox from '../../containers/MessageBox';
|
|
|
|
import SafeAreaView from '../../containers/SafeAreaView';
|
|
|
|
import { getUserSelector } from '../../selectors/login';
|
|
|
|
import StatusBar from '../../containers/StatusBar';
|
|
|
|
import database from '../../lib/database';
|
|
|
|
import { canUploadFile } from '../../utils/media';
|
2021-02-01 17:18:55 +00:00
|
|
|
import { isAndroid } from '../../utils/deviceInfo';
|
2019-07-18 17:44:02 +00:00
|
|
|
|
2020-06-26 20:22:56 +00:00
|
|
|
class ShareView extends Component {
|
2019-07-18 17:44:02 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2020-06-26 20:22:56 +00:00
|
|
|
this.messagebox = React.createRef();
|
|
|
|
this.files = props.route.params?.attachments ?? [];
|
|
|
|
this.isShareExtension = props.route.params?.isShareExtension;
|
|
|
|
this.serverInfo = props.route.params?.serverInfo ?? {};
|
2019-07-18 17:44:02 +00:00
|
|
|
|
|
|
|
this.state = {
|
2020-06-26 20:22:56 +00:00
|
|
|
selected: {},
|
2019-07-18 17:44:02 +00:00
|
|
|
loading: false,
|
2020-04-13 12:51:16 +00:00
|
|
|
readOnly: false,
|
2020-06-26 20:22:56 +00:00
|
|
|
attachments: [],
|
|
|
|
text: props.route.params?.text ?? '',
|
|
|
|
room: props.route.params?.room ?? {},
|
|
|
|
thread: props.route.params?.thread ?? {},
|
|
|
|
maxFileSize: this.isShareExtension ? this.serverInfo?.FileUpload_MaxFileSize : props.FileUpload_MaxFileSize,
|
|
|
|
mediaAllowList: this.isShareExtension ? this.serverInfo?.FileUpload_MediaTypeWhiteList : props.FileUpload_MediaTypeWhiteList
|
2019-07-18 17:44:02 +00:00
|
|
|
};
|
2020-06-26 20:22:56 +00:00
|
|
|
this.getServerInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount = async() => {
|
|
|
|
const readOnly = await this.getReadOnly();
|
|
|
|
const { attachments, selected } = await this.getAttachments();
|
|
|
|
this.setState({ readOnly, attachments, selected }, () => this.setHeader());
|
|
|
|
}
|
2020-04-13 12:51:16 +00:00
|
|
|
|
2020-06-26 20:22:56 +00:00
|
|
|
componentWillUnmount = () => {
|
|
|
|
console.countReset(`${ this.constructor.name }.render calls`);
|
2019-07-18 17:44:02 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 14:00:46 +00:00
|
|
|
setHeader = () => {
|
2020-06-26 20:22:56 +00:00
|
|
|
const {
|
|
|
|
room, thread, readOnly, attachments
|
|
|
|
} = this.state;
|
|
|
|
const { navigation, theme } = this.props;
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
headerTitle: () => <Header room={room} thread={thread} />,
|
|
|
|
headerTitleAlign: 'left',
|
|
|
|
headerTintColor: themes[theme].previewTintColor
|
|
|
|
};
|
|
|
|
|
|
|
|
// if is share extension show default back button
|
|
|
|
if (!this.isShareExtension) {
|
2020-10-30 16:15:58 +00:00
|
|
|
options.headerLeft = () => <HeaderButton.CloseModal navigation={navigation} buttonStyle={{ color: themes[theme].previewTintColor }} />;
|
2020-06-26 20:22:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!attachments.length && !readOnly) {
|
|
|
|
options.headerRight = () => (
|
2020-10-30 16:15:58 +00:00
|
|
|
<HeaderButton.Container>
|
|
|
|
<HeaderButton.Item
|
2020-06-26 20:22:56 +00:00
|
|
|
title={I18n.t('Send')}
|
|
|
|
onPress={this.send}
|
|
|
|
buttonStyle={[styles.send, { color: themes[theme].previewTintColor }]}
|
|
|
|
/>
|
2020-10-30 16:15:58 +00:00
|
|
|
</HeaderButton.Container>
|
2020-06-26 20:22:56 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
options.headerBackground = () => <View style={[styles.container, { backgroundColor: themes[theme].previewBackground }]} />;
|
|
|
|
|
|
|
|
navigation.setOptions(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetch server info
|
|
|
|
getServerInfo = async() => {
|
|
|
|
const { server } = this.props;
|
|
|
|
const serversDB = database.servers;
|
|
|
|
const serversCollection = serversDB.collections.get('servers');
|
|
|
|
try {
|
|
|
|
this.serverInfo = await serversCollection.find(server);
|
|
|
|
} catch (error) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
2020-04-13 12:51:16 +00:00
|
|
|
}
|
|
|
|
|
2020-06-26 20:22:56 +00:00
|
|
|
getReadOnly = async() => {
|
2019-07-18 17:44:02 +00:00
|
|
|
const { room } = this.state;
|
2020-06-15 14:00:46 +00:00
|
|
|
const { user } = this.props;
|
2020-06-26 20:22:56 +00:00
|
|
|
const readOnly = await isReadOnly(room, user);
|
|
|
|
return readOnly;
|
2019-07-18 17:44:02 +00:00
|
|
|
}
|
|
|
|
|
2020-06-26 20:22:56 +00:00
|
|
|
getAttachments = async() => {
|
|
|
|
const { mediaAllowList, maxFileSize } = this.state;
|
|
|
|
const items = await Promise.all(this.files.map(async(item) => {
|
|
|
|
// Check server settings
|
|
|
|
const { success: canUpload, error } = canUploadFile(item, mediaAllowList, maxFileSize);
|
|
|
|
item.canUpload = canUpload;
|
|
|
|
item.error = error;
|
|
|
|
|
|
|
|
// get video thumbnails
|
2021-02-01 17:18:55 +00:00
|
|
|
if (isAndroid && this.files.length > 1 && item.mime?.match?.(/video/)) {
|
2020-06-26 20:22:56 +00:00
|
|
|
try {
|
2021-02-01 17:18:55 +00:00
|
|
|
const VideoThumbnails = require('expo-video-thumbnails');
|
2020-06-26 20:22:56 +00:00
|
|
|
const { uri } = await VideoThumbnails.getThumbnailAsync(item.path);
|
|
|
|
item.uri = uri;
|
|
|
|
} catch {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set a filename, if there isn't any
|
|
|
|
if (!item.filename) {
|
|
|
|
item.filename = new Date().toISOString();
|
|
|
|
}
|
|
|
|
return item;
|
|
|
|
}));
|
|
|
|
return {
|
|
|
|
attachments: items,
|
|
|
|
selected: items[0]
|
|
|
|
};
|
|
|
|
}
|
2019-07-18 17:44:02 +00:00
|
|
|
|
2020-06-26 20:22:56 +00:00
|
|
|
send = async() => {
|
|
|
|
const { loading, selected } = this.state;
|
2019-12-18 19:31:41 +00:00
|
|
|
if (loading) {
|
|
|
|
return;
|
|
|
|
}
|
2019-07-18 17:44:02 +00:00
|
|
|
|
2020-06-26 20:22:56 +00:00
|
|
|
// update state
|
|
|
|
await this.selectFile(selected);
|
|
|
|
|
|
|
|
const {
|
|
|
|
attachments, room, text, thread
|
|
|
|
} = this.state;
|
|
|
|
const { navigation, server, user } = this.props;
|
|
|
|
|
|
|
|
// if it's share extension this should show loading
|
|
|
|
if (this.isShareExtension) {
|
|
|
|
this.setState({ loading: true });
|
|
|
|
|
|
|
|
// if it's not share extension this can close
|
2019-07-18 17:44:02 +00:00
|
|
|
} else {
|
2020-06-26 20:22:56 +00:00
|
|
|
navigation.pop();
|
2019-07-18 17:44:02 +00:00
|
|
|
}
|
|
|
|
|
2020-06-26 20:22:56 +00:00
|
|
|
try {
|
|
|
|
// Send attachment
|
|
|
|
if (attachments.length) {
|
|
|
|
await Promise.all(attachments.map(({
|
|
|
|
filename: name,
|
|
|
|
mime: type,
|
|
|
|
description,
|
|
|
|
size,
|
|
|
|
path,
|
|
|
|
canUpload
|
|
|
|
}) => {
|
|
|
|
if (canUpload) {
|
|
|
|
return RocketChat.sendFileMessage(
|
|
|
|
room.rid,
|
|
|
|
{
|
|
|
|
name,
|
|
|
|
description,
|
|
|
|
size,
|
|
|
|
type,
|
|
|
|
path,
|
|
|
|
store: 'Uploads'
|
|
|
|
},
|
2020-07-03 14:07:29 +00:00
|
|
|
thread?.id,
|
2020-06-26 20:22:56 +00:00
|
|
|
server,
|
|
|
|
{ id: user.id, token: user.token }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return Promise.resolve();
|
|
|
|
}));
|
2019-07-18 17:44:02 +00:00
|
|
|
|
2020-06-26 20:22:56 +00:00
|
|
|
// Send text message
|
|
|
|
} else if (text.length) {
|
2020-07-03 14:07:29 +00:00
|
|
|
await RocketChat.sendMessage(room.rid, text, thread?.id, { id: user.id, token: user.token });
|
2019-07-18 17:44:02 +00:00
|
|
|
}
|
2020-06-26 20:22:56 +00:00
|
|
|
} catch {
|
|
|
|
// Do nothing
|
2019-07-18 17:44:02 +00:00
|
|
|
}
|
|
|
|
|
2020-06-26 20:22:56 +00:00
|
|
|
// if it's share extension this should close
|
|
|
|
if (this.isShareExtension) {
|
|
|
|
ShareExtension.close();
|
2019-07-18 17:44:02 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-06-26 20:22:56 +00:00
|
|
|
selectFile = (item) => {
|
|
|
|
const { attachments, selected } = this.state;
|
|
|
|
if (attachments.length > 0) {
|
|
|
|
const { text } = this.messagebox.current;
|
|
|
|
const newAttachments = attachments.map((att) => {
|
|
|
|
if (att.path === selected.path) {
|
|
|
|
att.description = text;
|
|
|
|
}
|
|
|
|
return att;
|
|
|
|
});
|
|
|
|
return this.setState({ attachments: newAttachments, selected: item });
|
|
|
|
}
|
|
|
|
}
|
2019-07-18 17:44:02 +00:00
|
|
|
|
2020-06-26 20:22:56 +00:00
|
|
|
removeFile = (item) => {
|
|
|
|
const { selected, attachments } = this.state;
|
|
|
|
let newSelected;
|
|
|
|
if (item.path === selected.path) {
|
|
|
|
const selectedIndex = attachments.findIndex(att => att.path === selected.path);
|
|
|
|
// Selects the next one, if available
|
|
|
|
if (attachments[selectedIndex + 1]?.path) {
|
|
|
|
newSelected = attachments[selectedIndex + 1];
|
|
|
|
// If it's the last thumb, selects the previous one
|
|
|
|
} else {
|
|
|
|
newSelected = attachments[selectedIndex - 1] || {};
|
|
|
|
}
|
|
|
|
}
|
2021-02-26 16:01:45 +00:00
|
|
|
this.setState({ attachments: attachments.filter(att => att.path !== item.path), selected: newSelected ?? selected }, () => {
|
|
|
|
this.messagebox?.current?.forceUpdate?.();
|
|
|
|
});
|
2020-06-26 20:22:56 +00:00
|
|
|
}
|
2019-07-18 17:44:02 +00:00
|
|
|
|
2020-06-26 20:22:56 +00:00
|
|
|
onChangeText = (text) => {
|
|
|
|
this.setState({ text });
|
|
|
|
}
|
2019-07-18 17:44:02 +00:00
|
|
|
|
2020-06-26 20:22:56 +00:00
|
|
|
renderContent = () => {
|
|
|
|
const {
|
|
|
|
attachments, selected, room, text
|
|
|
|
} = this.state;
|
|
|
|
const { theme, navigation } = this.props;
|
|
|
|
|
|
|
|
if (attachments.length) {
|
|
|
|
return (
|
|
|
|
<View style={styles.container}>
|
|
|
|
<Preview
|
|
|
|
// using key just to reset zoom/move after change selected
|
|
|
|
key={selected?.path}
|
|
|
|
item={selected}
|
|
|
|
length={attachments.length}
|
2019-12-04 16:39:53 +00:00
|
|
|
theme={theme}
|
2020-06-26 20:22:56 +00:00
|
|
|
isShareExtension={this.isShareExtension}
|
2019-07-18 17:44:02 +00:00
|
|
|
/>
|
2020-06-26 20:22:56 +00:00
|
|
|
<MessageBox
|
|
|
|
showSend
|
|
|
|
sharing
|
|
|
|
ref={this.messagebox}
|
|
|
|
rid={room.rid}
|
|
|
|
roomType={room.t}
|
2019-12-04 16:39:53 +00:00
|
|
|
theme={theme}
|
2020-06-26 20:22:56 +00:00
|
|
|
onSubmit={this.send}
|
|
|
|
message={{ msg: selected?.description ?? '' }}
|
|
|
|
navigation={navigation}
|
|
|
|
isFocused={navigation.isFocused}
|
|
|
|
iOSScrollBehavior={NativeModules.KeyboardTrackingViewManager?.KeyboardTrackingScrollBehaviorNone}
|
|
|
|
isActionsEnabled={false}
|
|
|
|
>
|
|
|
|
<Thumbs
|
|
|
|
attachments={attachments}
|
|
|
|
theme={theme}
|
|
|
|
isShareExtension={this.isShareExtension}
|
|
|
|
onPress={this.selectFile}
|
|
|
|
onRemove={this.removeFile}
|
|
|
|
/>
|
|
|
|
</MessageBox>
|
2019-07-18 17:44:02 +00:00
|
|
|
</View>
|
2020-06-26 20:22:56 +00:00
|
|
|
);
|
|
|
|
}
|
2019-07-18 17:44:02 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<TextInput
|
2020-06-26 20:22:56 +00:00
|
|
|
containerStyle={styles.inputContainer}
|
2019-12-04 16:39:53 +00:00
|
|
|
inputStyle={[
|
|
|
|
styles.input,
|
|
|
|
styles.textInput,
|
2020-06-26 20:22:56 +00:00
|
|
|
{ backgroundColor: themes[theme].focusedBackground }
|
2019-12-04 16:39:53 +00:00
|
|
|
]}
|
2019-07-18 17:44:02 +00:00
|
|
|
placeholder=''
|
2020-06-26 20:22:56 +00:00
|
|
|
onChangeText={this.onChangeText}
|
|
|
|
defaultValue=''
|
2019-07-18 17:44:02 +00:00
|
|
|
multiline
|
|
|
|
textAlignVertical='top'
|
|
|
|
autoFocus
|
2019-12-04 16:39:53 +00:00
|
|
|
theme={theme}
|
2020-06-26 20:22:56 +00:00
|
|
|
value={text}
|
2019-07-18 17:44:02 +00:00
|
|
|
/>
|
|
|
|
);
|
2020-06-26 20:22:56 +00:00
|
|
|
};
|
2019-07-18 17:44:02 +00:00
|
|
|
|
|
|
|
render() {
|
2020-06-26 20:22:56 +00:00
|
|
|
console.count(`${ this.constructor.name }.render calls`);
|
|
|
|
const { readOnly, room, loading } = this.state;
|
2020-04-13 12:51:16 +00:00
|
|
|
const { theme } = this.props;
|
|
|
|
if (readOnly || isBlocked(room)) {
|
2020-06-26 20:22:56 +00:00
|
|
|
return (
|
|
|
|
<View style={[styles.container, styles.centered, { backgroundColor: themes[theme].backgroundColor }]}>
|
|
|
|
<Text style={[styles.title, { color: themes[theme].titleText }]}>
|
|
|
|
{isBlocked(room) ? I18n.t('This_room_is_blocked') : I18n.t('This_room_is_read_only')}
|
2019-07-18 17:44:02 +00:00
|
|
|
</Text>
|
|
|
|
</View>
|
2020-06-26 20:22:56 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<SafeAreaView
|
|
|
|
style={{ backgroundColor: themes[theme].backgroundColor }}
|
|
|
|
>
|
|
|
|
<StatusBar barStyle='light-content' backgroundColor={themes[theme].previewBackground} />
|
|
|
|
{this.renderContent()}
|
|
|
|
<Loading visible={loading} />
|
|
|
|
</SafeAreaView>
|
2019-07-18 17:44:02 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 13:51:34 +00:00
|
|
|
|
2020-06-26 20:22:56 +00:00
|
|
|
ShareView.propTypes = {
|
|
|
|
navigation: PropTypes.object,
|
|
|
|
route: PropTypes.object,
|
|
|
|
theme: PropTypes.string,
|
|
|
|
user: PropTypes.shape({
|
|
|
|
id: PropTypes.string.isRequired,
|
|
|
|
username: PropTypes.string.isRequired,
|
|
|
|
token: PropTypes.string.isRequired
|
|
|
|
}),
|
|
|
|
server: PropTypes.string,
|
|
|
|
FileUpload_MediaTypeWhiteList: PropTypes.string,
|
|
|
|
FileUpload_MaxFileSize: PropTypes.string
|
|
|
|
};
|
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
|
|
|
user: getUserSelector(state),
|
2020-11-04 16:53:44 +00:00
|
|
|
server: state.share.server.server || state.server.server,
|
2020-06-26 20:22:56 +00:00
|
|
|
FileUpload_MediaTypeWhiteList: state.settings.FileUpload_MediaTypeWhiteList,
|
|
|
|
FileUpload_MaxFileSize: state.settings.FileUpload_MaxFileSize
|
|
|
|
});
|
2019-08-07 13:51:34 +00:00
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
export default connect(mapStateToProps)(withTheme(ShareView));
|