2019-04-17 17:01:03 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import {
|
|
|
|
FlatList, View, Text, InteractionManager
|
|
|
|
} from 'react-native';
|
|
|
|
import { connect } from 'react-redux';
|
2019-10-02 12:18:08 +00:00
|
|
|
import { SafeAreaView } from 'react-navigation';
|
2019-04-17 17:01:03 +00:00
|
|
|
import moment from 'moment';
|
2019-09-16 20:26:32 +00:00
|
|
|
import orderBy from 'lodash/orderBy';
|
|
|
|
import { Q } from '@nozbe/watermelondb';
|
|
|
|
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
2019-04-17 17:01:03 +00:00
|
|
|
|
|
|
|
import styles from './styles';
|
|
|
|
import Message from '../../containers/message';
|
2019-12-04 16:39:53 +00:00
|
|
|
import ActivityIndicator from '../../containers/ActivityIndicator';
|
2019-04-17 17:01:03 +00:00
|
|
|
import I18n from '../../i18n';
|
|
|
|
import RocketChat from '../../lib/rocketchat';
|
2019-09-16 20:26:32 +00:00
|
|
|
import database from '../../lib/database';
|
2019-04-17 17:01:03 +00:00
|
|
|
import StatusBar from '../../containers/StatusBar';
|
|
|
|
import buildMessage from '../../lib/methods/helpers/buildMessage';
|
|
|
|
import log from '../../utils/log';
|
|
|
|
import debounce from '../../utils/debounce';
|
2019-09-16 20:26:32 +00:00
|
|
|
import protectedFunction from '../../lib/methods/helpers/protectedFunction';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { themes } from '../../constants/colors';
|
|
|
|
import { withTheme } from '../../theme';
|
|
|
|
import { themedHeader } from '../../utils/navigation';
|
2019-11-25 20:01:17 +00:00
|
|
|
import ModalNavigation from '../../lib/ModalNavigation';
|
2020-02-11 14:09:14 +00:00
|
|
|
import { getUserSelector } from '../../selectors/login';
|
2019-04-17 17:01:03 +00:00
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
const Separator = React.memo(({ theme }) => <View style={[styles.separator, { backgroundColor: themes[theme].separatorColor }]} />);
|
|
|
|
Separator.propTypes = {
|
|
|
|
theme: PropTypes.string
|
|
|
|
};
|
|
|
|
|
2019-04-24 18:36:29 +00:00
|
|
|
const API_FETCH_COUNT = 50;
|
2019-04-17 17:01:03 +00:00
|
|
|
|
2019-08-07 13:51:34 +00:00
|
|
|
class ThreadMessagesView extends React.Component {
|
2019-12-04 16:39:53 +00:00
|
|
|
static navigationOptions = ({ screenProps }) => ({
|
|
|
|
...themedHeader(screenProps.theme),
|
2019-04-17 17:01:03 +00:00
|
|
|
title: I18n.t('Threads')
|
2019-12-04 16:39:53 +00:00
|
|
|
});
|
2019-04-17 17:01:03 +00:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
user: PropTypes.object,
|
2019-05-20 20:43:50 +00:00
|
|
|
navigation: PropTypes.object,
|
|
|
|
baseUrl: PropTypes.string,
|
2019-09-16 20:26:32 +00:00
|
|
|
useRealName: PropTypes.bool,
|
2019-12-04 16:39:53 +00:00
|
|
|
theme: PropTypes.string,
|
2019-11-25 20:01:17 +00:00
|
|
|
customEmojis: PropTypes.object,
|
|
|
|
screenProps: PropTypes.object
|
2019-04-17 17:01:03 +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;
|
2019-04-17 17:01:03 +00:00
|
|
|
this.rid = props.navigation.getParam('rid');
|
|
|
|
this.t = props.navigation.getParam('t');
|
|
|
|
this.state = {
|
|
|
|
loading: false,
|
|
|
|
end: false,
|
2019-09-16 20:26:32 +00:00
|
|
|
messages: []
|
2019-04-17 17:01:03 +00:00
|
|
|
};
|
2019-09-17 14:43:49 +00:00
|
|
|
this.subscribeData();
|
2019-04-17 17:01:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2019-09-16 20:26:32 +00:00
|
|
|
this.mounted = true;
|
2019-04-24 18:36:29 +00:00
|
|
|
this.mountInteraction = InteractionManager.runAfterInteractions(() => {
|
|
|
|
this.init();
|
|
|
|
});
|
2019-04-17 17:01:03 +00:00
|
|
|
}
|
|
|
|
|
2019-04-24 18:36:29 +00:00
|
|
|
componentWillUnmount() {
|
2020-04-06 19:07:40 +00:00
|
|
|
console.countReset(`${ this.constructor.name }.render calls`);
|
2019-04-24 18:36:29 +00:00
|
|
|
if (this.mountInteraction && this.mountInteraction.cancel) {
|
|
|
|
this.mountInteraction.cancel();
|
2019-04-17 17:01:03 +00:00
|
|
|
}
|
2019-04-24 18:36:29 +00:00
|
|
|
if (this.syncInteraction && this.syncInteraction.cancel) {
|
|
|
|
this.syncInteraction.cancel();
|
2019-04-17 17:01:03 +00:00
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
if (this.subSubscription && this.subSubscription.unsubscribe) {
|
|
|
|
this.subSubscription.unsubscribe();
|
|
|
|
}
|
|
|
|
if (this.messagesSubscription && this.messagesSubscription.unsubscribe) {
|
|
|
|
this.messagesSubscription.unsubscribe();
|
|
|
|
}
|
2019-04-17 17:01:03 +00:00
|
|
|
}
|
|
|
|
|
2019-04-24 18:36:29 +00:00
|
|
|
// eslint-disable-next-line react/sort-comp
|
2020-04-06 19:07:40 +00:00
|
|
|
subscribeData = async() => {
|
2019-09-16 20:26:32 +00:00
|
|
|
try {
|
|
|
|
const db = database.active;
|
2020-04-06 19:07:40 +00:00
|
|
|
const subscription = await db.collections
|
2019-09-16 20:26:32 +00:00
|
|
|
.get('subscriptions')
|
2020-04-06 19:07:40 +00:00
|
|
|
.find(this.rid);
|
|
|
|
const observable = subscription.observe();
|
|
|
|
this.subSubscription = observable
|
2019-09-16 20:26:32 +00:00
|
|
|
.subscribe((data) => {
|
|
|
|
this.subscription = data;
|
|
|
|
});
|
|
|
|
this.messagesObservable = db.collections
|
|
|
|
.get('threads')
|
|
|
|
.query(
|
|
|
|
Q.where('rid', this.rid),
|
|
|
|
Q.where('t', Q.notEq('rm'))
|
|
|
|
)
|
|
|
|
.observeWithColumns(['updated_at']);
|
|
|
|
this.messagesSubscription = this.messagesObservable
|
|
|
|
.subscribe((data) => {
|
|
|
|
const messages = orderBy(data, ['ts'], ['desc']);
|
|
|
|
if (this.mounted) {
|
|
|
|
this.setState({ messages });
|
|
|
|
} else {
|
|
|
|
this.state.messages = messages;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (e) {
|
2020-04-06 19:07:40 +00:00
|
|
|
// Do nothing
|
2019-09-16 20:26:32 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-24 18:36:29 +00:00
|
|
|
|
2019-05-20 20:43:50 +00:00
|
|
|
// eslint-disable-next-line react/sort-comp
|
2019-04-24 18:36:29 +00:00
|
|
|
init = () => {
|
2019-09-16 20:26:32 +00:00
|
|
|
if (!this.subscription) {
|
2020-04-06 19:07:40 +00:00
|
|
|
this.load();
|
2019-06-03 12:53:06 +00:00
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
try {
|
|
|
|
const lastThreadSync = new Date();
|
|
|
|
if (this.subscription.lastThreadSync) {
|
|
|
|
this.sync(this.subscription.lastThreadSync);
|
|
|
|
} else {
|
|
|
|
this.load(lastThreadSync);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
}
|
2019-06-03 12:53:06 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
updateThreads = async({ update, remove, lastThreadSync }) => {
|
2020-04-06 19:07:40 +00:00
|
|
|
// if there's no subscription, manage data on this.state.messages
|
|
|
|
// note: sync will never be called without subscription
|
|
|
|
if (!this.subscription) {
|
|
|
|
this.setState(({ messages }) => ({ messages: [...messages, ...update] }));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
try {
|
|
|
|
const db = database.active;
|
|
|
|
const threadsCollection = db.collections.get('threads');
|
|
|
|
const allThreadsRecords = await this.subscription.threads.fetch();
|
|
|
|
let threadsToCreate = [];
|
|
|
|
let threadsToUpdate = [];
|
|
|
|
let threadsToDelete = [];
|
|
|
|
|
|
|
|
if (update && update.length) {
|
|
|
|
update = update.map(m => buildMessage(m));
|
|
|
|
// filter threads
|
|
|
|
threadsToCreate = update.filter(i1 => !allThreadsRecords.find(i2 => i1._id === i2.id));
|
|
|
|
threadsToUpdate = allThreadsRecords.filter(i1 => update.find(i2 => i1.id === i2._id));
|
|
|
|
threadsToCreate = threadsToCreate.map(thread => threadsCollection.prepareCreate(protectedFunction((t) => {
|
|
|
|
t._raw = sanitizedRaw({ id: thread._id }, threadsCollection.schema);
|
|
|
|
t.subscription.set(this.subscription);
|
|
|
|
Object.assign(t, thread);
|
|
|
|
})));
|
|
|
|
threadsToUpdate = threadsToUpdate.map((thread) => {
|
|
|
|
const newThread = update.find(t => t._id === thread.id);
|
|
|
|
return thread.prepareUpdate(protectedFunction((t) => {
|
|
|
|
Object.assign(t, newThread);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (remove && remove.length) {
|
|
|
|
threadsToDelete = allThreadsRecords.filter(i1 => remove.find(i2 => i1.id === i2._id));
|
|
|
|
threadsToDelete = threadsToDelete.map(t => t.prepareDestroyPermanently());
|
|
|
|
}
|
|
|
|
|
|
|
|
await db.action(async() => {
|
|
|
|
await db.batch(
|
|
|
|
...threadsToCreate,
|
|
|
|
...threadsToUpdate,
|
|
|
|
...threadsToDelete,
|
|
|
|
this.subscription.prepareUpdate((s) => {
|
|
|
|
s.lastThreadSync = lastThreadSync;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2019-04-24 18:36:29 +00:00
|
|
|
}
|
2019-04-17 17:01:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line react/sort-comp
|
2019-09-16 20:26:32 +00:00
|
|
|
load = debounce(async(lastThreadSync) => {
|
|
|
|
const { loading, end, messages } = this.state;
|
2019-04-24 18:36:29 +00:00
|
|
|
if (end || loading || !this.mounted) {
|
2019-04-17 17:01:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({ loading: true });
|
|
|
|
|
|
|
|
try {
|
2019-04-24 18:36:29 +00:00
|
|
|
const result = await RocketChat.getThreadsList({
|
2019-09-16 20:26:32 +00:00
|
|
|
rid: this.rid, count: API_FETCH_COUNT, offset: messages.length
|
2019-04-17 17:01:03 +00:00
|
|
|
});
|
2019-04-24 18:36:29 +00:00
|
|
|
if (result.success) {
|
2020-04-06 19:07:40 +00:00
|
|
|
this.updateThreads({ update: result.threads, lastThreadSync });
|
|
|
|
this.setState({
|
|
|
|
loading: false,
|
|
|
|
end: result.count < API_FETCH_COUNT
|
2019-04-24 18:36:29 +00:00
|
|
|
});
|
|
|
|
}
|
2019-08-23 13:18:47 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2019-04-17 17:01:03 +00:00
|
|
|
this.setState({ loading: false, end: true });
|
|
|
|
}
|
2019-04-24 18:36:29 +00:00
|
|
|
}, 300)
|
|
|
|
|
|
|
|
// eslint-disable-next-line react/sort-comp
|
|
|
|
sync = async(updatedSince) => {
|
|
|
|
this.setState({ loading: true });
|
|
|
|
|
|
|
|
try {
|
|
|
|
const result = await RocketChat.getSyncThreadsList({
|
|
|
|
rid: this.rid, updatedSince: updatedSince.toISOString()
|
|
|
|
});
|
|
|
|
if (result.success && result.threads) {
|
|
|
|
this.syncInteraction = InteractionManager.runAfterInteractions(() => {
|
|
|
|
const { update, remove } = result.threads;
|
2019-09-16 20:26:32 +00:00
|
|
|
this.updateThreads({ update, remove, lastThreadSync: updatedSince });
|
2019-04-24 18:36:29 +00:00
|
|
|
});
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
this.setState({
|
|
|
|
loading: false
|
|
|
|
});
|
2019-08-23 13:18:47 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2019-04-24 18:36:29 +00:00
|
|
|
this.setState({ loading: false });
|
|
|
|
}
|
|
|
|
}
|
2019-04-17 17:01:03 +00:00
|
|
|
|
|
|
|
formatMessage = lm => (
|
|
|
|
lm ? moment(lm).calendar(null, {
|
|
|
|
lastDay: `[${ I18n.t('Yesterday') }]`,
|
|
|
|
sameDay: 'h:mm A',
|
|
|
|
lastWeek: 'dddd',
|
|
|
|
sameElse: 'MMM D'
|
|
|
|
}) : null
|
|
|
|
)
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
getCustomEmoji = (name) => {
|
|
|
|
const { customEmojis } = this.props;
|
|
|
|
const emoji = customEmojis[name];
|
|
|
|
if (emoji) {
|
|
|
|
return emoji;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-01-28 13:41:06 +00:00
|
|
|
showAttachment = (attachment) => {
|
|
|
|
const { navigation } = this.props;
|
|
|
|
navigation.navigate('AttachmentView', { attachment });
|
|
|
|
}
|
|
|
|
|
2019-05-20 20:43:50 +00:00
|
|
|
onThreadPress = debounce((item) => {
|
|
|
|
const { navigation } = this.props;
|
2019-09-16 20:26:32 +00:00
|
|
|
navigation.push('RoomView', {
|
|
|
|
rid: item.subscription.id, tmid: item.id, name: item.msg, t: 'thread'
|
|
|
|
});
|
2019-05-20 20:43:50 +00:00
|
|
|
}, 1000, true)
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
renderSeparator = () => {
|
|
|
|
const { theme } = this.props;
|
|
|
|
return <Separator theme={theme} />;
|
|
|
|
}
|
2019-04-17 17:01:03 +00:00
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
renderEmpty = () => {
|
|
|
|
const { theme } = this.props;
|
|
|
|
return (
|
|
|
|
<View style={[styles.listEmptyContainer, { backgroundColor: themes[theme].backgroundColor }]} testID='thread-messages-view'>
|
|
|
|
<Text style={[styles.noDataFound, { color: themes[theme].titleText }]}>{I18n.t('No_thread_messages')}</Text>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2019-04-17 17:01:03 +00:00
|
|
|
|
2019-11-13 19:04:49 +00:00
|
|
|
navToRoomInfo = (navParam) => {
|
2019-11-25 20:01:17 +00:00
|
|
|
const { navigation, user, screenProps } = this.props;
|
2019-11-13 19:04:49 +00:00
|
|
|
if (navParam.rid === user.id) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-25 20:01:17 +00:00
|
|
|
if (screenProps && screenProps.split) {
|
|
|
|
navigation.navigate('RoomActionsView', { rid: this.rid, t: this.t });
|
|
|
|
ModalNavigation.navigate('RoomInfoView', navParam);
|
|
|
|
} else {
|
|
|
|
navigation.navigate('RoomInfoView', navParam);
|
|
|
|
}
|
2019-11-13 19:04:49 +00:00
|
|
|
}
|
|
|
|
|
2019-04-17 17:01:03 +00:00
|
|
|
renderItem = ({ item }) => {
|
2019-05-20 20:43:50 +00:00
|
|
|
const {
|
|
|
|
user, navigation, baseUrl, useRealName
|
|
|
|
} = this.props;
|
2019-09-16 20:26:32 +00:00
|
|
|
return (
|
|
|
|
<Message
|
|
|
|
key={item.id}
|
|
|
|
item={item}
|
|
|
|
user={user}
|
|
|
|
archived={false}
|
|
|
|
broadcast={false}
|
|
|
|
status={item.status}
|
|
|
|
navigation={navigation}
|
|
|
|
timeFormat='MMM D'
|
|
|
|
customThreadTimeFormat='MMM Do YYYY, h:mm:ss a'
|
|
|
|
onThreadPress={this.onThreadPress}
|
|
|
|
baseUrl={baseUrl}
|
|
|
|
useRealName={useRealName}
|
|
|
|
getCustomEmoji={this.getCustomEmoji}
|
2019-11-13 19:04:49 +00:00
|
|
|
navToRoomInfo={this.navToRoomInfo}
|
2020-01-28 13:41:06 +00:00
|
|
|
showAttachment={this.showAttachment}
|
2019-09-16 20:26:32 +00:00
|
|
|
/>
|
|
|
|
);
|
2019-04-17 17:01:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2020-04-06 19:07:40 +00:00
|
|
|
console.count(`${ this.constructor.name }.render calls`);
|
2019-04-24 18:36:29 +00:00
|
|
|
const { loading, messages } = this.state;
|
2019-12-04 16:39:53 +00:00
|
|
|
const { theme } = this.props;
|
2019-04-17 17:01:03 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
if (!loading && messages.length === 0) {
|
2019-04-17 17:01:03 +00:00
|
|
|
return this.renderEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2019-08-07 13:51:34 +00:00
|
|
|
<SafeAreaView style={styles.list} testID='thread-messages-view' forceInset={{ vertical: 'never' }}>
|
2019-12-04 16:39:53 +00:00
|
|
|
<StatusBar theme={theme} />
|
2019-04-17 17:01:03 +00:00
|
|
|
<FlatList
|
|
|
|
data={messages}
|
2019-04-24 18:36:29 +00:00
|
|
|
extraData={this.state}
|
2019-04-17 17:01:03 +00:00
|
|
|
renderItem={this.renderItem}
|
2019-12-04 16:39:53 +00:00
|
|
|
style={[styles.list, { backgroundColor: themes[theme].backgroundColor }]}
|
2019-04-17 17:01:03 +00:00
|
|
|
contentContainerStyle={styles.contentContainer}
|
2020-04-06 19:07:40 +00:00
|
|
|
keyExtractor={item => item._id}
|
2019-04-17 17:01:03 +00:00
|
|
|
onEndReached={this.load}
|
|
|
|
onEndReachedThreshold={0.5}
|
|
|
|
maxToRenderPerBatch={5}
|
|
|
|
initialNumToRender={1}
|
|
|
|
ItemSeparatorComponent={this.renderSeparator}
|
2019-12-04 16:39:53 +00:00
|
|
|
ListFooterComponent={loading ? <ActivityIndicator theme={theme} /> : null}
|
2019-04-17 17:01:03 +00:00
|
|
|
/>
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 13:51:34 +00:00
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
2020-02-11 14:09:14 +00:00
|
|
|
baseUrl: state.server.server,
|
|
|
|
user: getUserSelector(state),
|
2019-09-16 20:26:32 +00:00
|
|
|
useRealName: state.settings.UI_Use_Real_Name,
|
|
|
|
customEmojis: state.customEmojis
|
2019-08-07 13:51:34 +00:00
|
|
|
});
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
export default connect(mapStateToProps)(withTheme(ThreadMessagesView));
|