2022-02-02 18:02:02 +00:00
|
|
|
import { Q } from '@nozbe/watermelondb';
|
|
|
|
import { StackNavigationOptions } from '@react-navigation/stack';
|
|
|
|
import { dequal } from 'dequal';
|
2018-08-31 18:13:30 +00:00
|
|
|
import React from 'react';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { FlatList, StyleSheet, Text, View } from 'react-native';
|
2019-01-31 16:08:38 +00:00
|
|
|
import { connect } from 'react-redux';
|
2021-10-05 13:59:40 +00:00
|
|
|
|
2022-02-02 18:02:02 +00:00
|
|
|
import { createChannelRequest } from '../actions/createChannel';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { themes } from '../lib/constants';
|
2022-02-02 18:02:02 +00:00
|
|
|
import * as HeaderButton from '../containers/HeaderButton';
|
2021-12-02 13:20:19 +00:00
|
|
|
import * as List from '../containers/List';
|
2022-02-02 18:02:02 +00:00
|
|
|
import SafeAreaView from '../containers/SafeAreaView';
|
|
|
|
import SearchBox from '../containers/SearchBox';
|
|
|
|
import StatusBar from '../containers/StatusBar';
|
2022-03-08 14:24:21 +00:00
|
|
|
import { IApplicationState, IBaseScreen, ISearch, TSubscriptionModel } from '../definitions';
|
2022-02-02 18:02:02 +00:00
|
|
|
import I18n from '../i18n';
|
2019-09-16 20:26:32 +00:00
|
|
|
import database from '../lib/database';
|
2022-05-02 19:21:15 +00:00
|
|
|
import { CustomIcon, TIconsName } from '../containers/CustomIcon';
|
2022-04-07 13:22:19 +00:00
|
|
|
import Navigation from '../lib/navigation/appNavigation';
|
2022-04-09 02:43:31 +00:00
|
|
|
import UserItem from '../containers/UserItem';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { withTheme } from '../theme';
|
2022-06-06 14:17:51 +00:00
|
|
|
import { goRoom, TGoRoomItem } from '../lib/methods/helpers/goRoom';
|
|
|
|
import log, { events, logEvent } from '../lib/methods/helpers/log';
|
|
|
|
import Touch from '../lib/methods/helpers/touch';
|
2021-09-13 20:41:05 +00:00
|
|
|
import sharedStyles from './Styles';
|
2022-05-03 02:43:30 +00:00
|
|
|
import { NewMessageStackParamList } from '../stacks/types';
|
2022-06-06 14:17:51 +00:00
|
|
|
import { search } from '../lib/methods';
|
|
|
|
import { hasPermission, compareServerVersion } from '../lib/methods/helpers';
|
2018-08-31 18:13:30 +00:00
|
|
|
|
2021-02-26 16:49:21 +00:00
|
|
|
const QUERY_SIZE = 50;
|
|
|
|
|
2018-08-31 18:13:30 +00:00
|
|
|
const styles = StyleSheet.create({
|
2020-04-01 12:28:54 +00:00
|
|
|
button: {
|
2019-12-04 16:39:53 +00:00
|
|
|
height: 46,
|
2018-08-31 18:13:30 +00:00
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center'
|
|
|
|
},
|
2020-04-01 12:28:54 +00:00
|
|
|
buttonIcon: {
|
2019-06-10 16:22:35 +00:00
|
|
|
marginLeft: 18,
|
2020-03-30 19:50:27 +00:00
|
|
|
marginRight: 16
|
2018-08-31 18:13:30 +00:00
|
|
|
},
|
2020-04-01 12:28:54 +00:00
|
|
|
buttonText: {
|
2019-03-29 19:36:07 +00:00
|
|
|
fontSize: 17,
|
|
|
|
...sharedStyles.textRegular
|
2020-04-01 12:28:54 +00:00
|
|
|
},
|
|
|
|
buttonContainer: {
|
2022-06-06 13:53:02 +00:00
|
|
|
paddingBottom: 16
|
2018-08-31 18:13:30 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-12-02 13:20:19 +00:00
|
|
|
interface IButton {
|
|
|
|
onPress: () => void;
|
|
|
|
testID: string;
|
|
|
|
title: string;
|
2022-05-02 19:21:15 +00:00
|
|
|
icon: TIconsName;
|
2021-12-02 13:20:19 +00:00
|
|
|
first?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface INewMessageViewState {
|
2022-02-01 13:39:09 +00:00
|
|
|
search: (ISearch | TSubscriptionModel)[];
|
|
|
|
chats: TSubscriptionModel[];
|
2021-12-02 13:20:19 +00:00
|
|
|
permissions: boolean[];
|
|
|
|
}
|
|
|
|
|
2022-05-03 02:43:30 +00:00
|
|
|
interface INewMessageViewProps extends IBaseScreen<NewMessageStackParamList, 'NewMessageView'> {
|
2021-12-02 13:20:19 +00:00
|
|
|
maxUsers: number;
|
|
|
|
isMasterDetail: boolean;
|
|
|
|
serverVersion: string;
|
2022-05-03 02:43:30 +00:00
|
|
|
createTeamPermission?: string[];
|
|
|
|
createDirectMessagePermission?: string[];
|
|
|
|
createPublicChannelPermission?: string[];
|
|
|
|
createPrivateChannelPermission?: string[];
|
|
|
|
createDiscussionPermission?: string[];
|
2021-12-02 13:20:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class NewMessageView extends React.Component<INewMessageViewProps, INewMessageViewState> {
|
|
|
|
static navigationOptions = ({ navigation }: INewMessageViewProps): StackNavigationOptions => ({
|
2020-10-30 16:15:58 +00:00
|
|
|
headerLeft: () => <HeaderButton.CloseModal navigation={navigation} testID='new-message-view-close' />,
|
2019-03-12 16:23:06 +00:00
|
|
|
title: I18n.t('New_Message')
|
2021-09-13 20:41:05 +00:00
|
|
|
});
|
2018-08-31 18:13:30 +00:00
|
|
|
|
2021-12-02 13:20:19 +00:00
|
|
|
constructor(props: INewMessageViewProps) {
|
2019-05-28 13:03:08 +00:00
|
|
|
super(props);
|
2019-09-16 20:26:32 +00:00
|
|
|
this.init();
|
2018-08-31 18:13:30 +00:00
|
|
|
this.state = {
|
2019-09-16 20:26:32 +00:00
|
|
|
search: [],
|
2021-10-05 13:59:40 +00:00
|
|
|
chats: [],
|
|
|
|
permissions: []
|
2018-08-31 18:13:30 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
// eslint-disable-next-line react/sort-comp
|
2021-09-13 20:41:05 +00:00
|
|
|
init = async () => {
|
2019-09-16 20:26:32 +00:00
|
|
|
try {
|
|
|
|
const db = database.active;
|
2022-02-01 13:39:09 +00:00
|
|
|
const chats = await db
|
2019-09-16 20:26:32 +00:00
|
|
|
.get('subscriptions')
|
2021-09-13 20:41:05 +00:00
|
|
|
.query(Q.where('t', 'd'), Q.experimentalTake(QUERY_SIZE), Q.experimentalSortBy('room_updated_at', Q.desc))
|
2021-02-26 16:49:21 +00:00
|
|
|
.fetch();
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2021-02-26 16:49:21 +00:00
|
|
|
this.setState({ chats });
|
2019-09-16 20:26:32 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2018-08-31 18:13:30 +00:00
|
|
|
|
2021-10-05 13:59:40 +00:00
|
|
|
componentDidMount() {
|
|
|
|
this.handleHasPermission();
|
|
|
|
}
|
|
|
|
|
2021-12-02 13:20:19 +00:00
|
|
|
componentDidUpdate(prevProps: INewMessageViewProps) {
|
2021-10-05 13:59:40 +00:00
|
|
|
const {
|
|
|
|
createTeamPermission,
|
|
|
|
createPublicChannelPermission,
|
|
|
|
createPrivateChannelPermission,
|
|
|
|
createDirectMessagePermission,
|
|
|
|
createDiscussionPermission
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
if (
|
|
|
|
!dequal(createTeamPermission, prevProps.createTeamPermission) ||
|
|
|
|
!dequal(createPublicChannelPermission, prevProps.createPublicChannelPermission) ||
|
|
|
|
!dequal(createPrivateChannelPermission, prevProps.createPrivateChannelPermission) ||
|
|
|
|
!dequal(createDirectMessagePermission, prevProps.createDirectMessagePermission) ||
|
|
|
|
!dequal(createDiscussionPermission, prevProps.createDiscussionPermission)
|
|
|
|
) {
|
|
|
|
this.handleHasPermission();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-28 20:37:25 +00:00
|
|
|
handleSearch = async (text: string) => {
|
|
|
|
const result = (await search({ text, filterRooms: false })) as ISearch[];
|
|
|
|
this.setState({
|
|
|
|
search: result
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-12-02 13:20:19 +00:00
|
|
|
onSearchChangeText(text: string) {
|
2022-04-28 20:37:25 +00:00
|
|
|
this.handleSearch(text);
|
2018-08-31 18:13:30 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 21:39:48 +00:00
|
|
|
dismiss = () => {
|
2019-03-12 16:23:06 +00:00
|
|
|
const { navigation } = this.props;
|
|
|
|
return navigation.pop();
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2018-10-23 21:39:48 +00:00
|
|
|
|
2018-08-31 18:13:30 +00:00
|
|
|
createChannel = () => {
|
2020-07-30 13:26:17 +00:00
|
|
|
logEvent(events.NEW_MSG_CREATE_CHANNEL);
|
2019-03-12 16:23:06 +00:00
|
|
|
const { navigation } = this.props;
|
2020-04-01 12:28:54 +00:00
|
|
|
navigation.navigate('SelectedUsersViewCreateChannel', { nextAction: () => navigation.navigate('CreateChannelView') });
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-04-01 12:28:54 +00:00
|
|
|
|
2021-05-12 19:01:29 +00:00
|
|
|
createTeam = () => {
|
|
|
|
logEvent(events.NEW_MSG_CREATE_TEAM);
|
|
|
|
const { navigation } = this.props;
|
2021-09-13 20:41:05 +00:00
|
|
|
navigation.navigate('SelectedUsersViewCreateChannel', {
|
|
|
|
nextAction: () => navigation.navigate('CreateChannelView', { isTeam: true })
|
|
|
|
});
|
|
|
|
};
|
2021-05-12 19:01:29 +00:00
|
|
|
|
2020-04-01 12:28:54 +00:00
|
|
|
createGroupChat = () => {
|
2020-07-30 13:26:17 +00:00
|
|
|
logEvent(events.NEW_MSG_CREATE_GROUP_CHAT);
|
2022-02-02 18:02:02 +00:00
|
|
|
const { dispatch, maxUsers, navigation } = this.props;
|
2020-04-01 12:28:54 +00:00
|
|
|
navigation.navigate('SelectedUsersViewCreateChannel', {
|
2022-02-02 18:02:02 +00:00
|
|
|
nextAction: () => dispatch(createChannelRequest({ group: true })),
|
2020-04-01 12:28:54 +00:00
|
|
|
buttonText: I18n.t('Create'),
|
|
|
|
maxUsers
|
|
|
|
});
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-04-01 12:28:54 +00:00
|
|
|
|
2022-05-03 02:43:30 +00:00
|
|
|
goRoom = (item: TGoRoomItem) => {
|
2020-07-30 13:26:17 +00:00
|
|
|
logEvent(events.NEW_MSG_CHAT_WITH_USER);
|
2020-06-15 14:00:46 +00:00
|
|
|
const { isMasterDetail, navigation } = this.props;
|
|
|
|
if (isMasterDetail) {
|
|
|
|
navigation.pop();
|
|
|
|
}
|
|
|
|
goRoom({ item, isMasterDetail });
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-06-15 14:00:46 +00:00
|
|
|
|
2021-12-02 13:20:19 +00:00
|
|
|
renderButton = ({ onPress, testID, title, icon, first }: IButton) => {
|
2020-04-01 12:28:54 +00:00
|
|
|
const { theme } = this.props;
|
|
|
|
return (
|
2021-09-13 20:41:05 +00:00
|
|
|
<Touch onPress={onPress} style={{ backgroundColor: themes[theme].backgroundColor }} testID={testID} theme={theme}>
|
|
|
|
<View
|
|
|
|
style={[
|
|
|
|
first ? sharedStyles.separatorVertical : sharedStyles.separatorBottom,
|
|
|
|
styles.button,
|
|
|
|
{ borderColor: themes[theme].separatorColor }
|
|
|
|
]}>
|
2022-05-02 19:21:15 +00:00
|
|
|
<CustomIcon name={icon} size={24} color={themes[theme].tintColor} style={styles.buttonIcon} />
|
2020-04-01 12:28:54 +00:00
|
|
|
<Text style={[styles.buttonText, { color: themes[theme].tintColor }]}>{title}</Text>
|
|
|
|
</View>
|
|
|
|
</Touch>
|
|
|
|
);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2018-08-31 18:13:30 +00:00
|
|
|
|
2020-03-30 19:50:27 +00:00
|
|
|
createDiscussion = () => {
|
2020-07-30 13:26:17 +00:00
|
|
|
logEvent(events.NEW_MSG_CREATE_DISCUSSION);
|
2020-03-30 19:50:27 +00:00
|
|
|
Navigation.navigate('CreateDiscussionView');
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-03-30 19:50:27 +00:00
|
|
|
|
2021-10-05 13:59:40 +00:00
|
|
|
handleHasPermission = async () => {
|
|
|
|
const {
|
|
|
|
createTeamPermission,
|
|
|
|
createDirectMessagePermission,
|
|
|
|
createPublicChannelPermission,
|
|
|
|
createPrivateChannelPermission,
|
|
|
|
createDiscussionPermission
|
|
|
|
} = this.props;
|
|
|
|
const permissions = [
|
|
|
|
createPublicChannelPermission,
|
|
|
|
createPrivateChannelPermission,
|
|
|
|
createTeamPermission,
|
|
|
|
createDirectMessagePermission,
|
|
|
|
createDiscussionPermission
|
|
|
|
];
|
2022-04-28 20:37:25 +00:00
|
|
|
const permissionsToCreate = await hasPermission(permissions);
|
2021-10-05 13:59:40 +00:00
|
|
|
this.setState({ permissions: permissionsToCreate });
|
|
|
|
};
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
renderHeader = () => {
|
2021-05-28 19:06:20 +00:00
|
|
|
const { maxUsers, theme, serverVersion } = this.props;
|
2021-10-05 13:59:40 +00:00
|
|
|
const { permissions } = this.state;
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
return (
|
|
|
|
<View style={{ backgroundColor: themes[theme].auxiliaryBackground }}>
|
2021-12-02 13:20:19 +00:00
|
|
|
<SearchBox onChangeText={(text: string) => this.onSearchChangeText(text)} testID='new-message-view-search' />
|
2020-04-01 12:28:54 +00:00
|
|
|
<View style={styles.buttonContainer}>
|
2021-10-05 13:59:40 +00:00
|
|
|
{permissions[0] || permissions[1]
|
|
|
|
? this.renderButton({
|
|
|
|
onPress: this.createChannel,
|
|
|
|
title: I18n.t('Create_Channel'),
|
|
|
|
icon: 'channel-public',
|
|
|
|
testID: 'new-message-view-create-channel',
|
|
|
|
first: true
|
|
|
|
})
|
|
|
|
: null}
|
2022-02-07 18:44:04 +00:00
|
|
|
{compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '3.13.0') && permissions[2]
|
2021-09-13 20:41:05 +00:00
|
|
|
? this.renderButton({
|
|
|
|
onPress: this.createTeam,
|
|
|
|
title: I18n.t('Create_Team'),
|
|
|
|
icon: 'teams',
|
|
|
|
testID: 'new-message-view-create-team'
|
|
|
|
})
|
|
|
|
: null}
|
2021-10-05 13:59:40 +00:00
|
|
|
{maxUsers > 2 && permissions[3]
|
2021-09-13 20:41:05 +00:00
|
|
|
? this.renderButton({
|
|
|
|
onPress: this.createGroupChat,
|
|
|
|
title: I18n.t('Create_Direct_Messages'),
|
|
|
|
icon: 'message',
|
|
|
|
testID: 'new-message-view-create-direct-message'
|
|
|
|
})
|
|
|
|
: null}
|
2021-10-05 13:59:40 +00:00
|
|
|
{permissions[4]
|
|
|
|
? this.renderButton({
|
|
|
|
onPress: this.createDiscussion,
|
|
|
|
title: I18n.t('Create_Discussion'),
|
|
|
|
icon: 'discussions',
|
|
|
|
testID: 'new-message-view-create-discussion'
|
|
|
|
})
|
|
|
|
: null}
|
2020-04-01 12:28:54 +00:00
|
|
|
</View>
|
2019-12-04 16:39:53 +00:00
|
|
|
</View>
|
|
|
|
);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2018-08-31 18:13:30 +00:00
|
|
|
|
2022-02-01 13:39:09 +00:00
|
|
|
renderItem = ({ item, index }: { item: ISearch | TSubscriptionModel; index: number }) => {
|
2019-09-16 20:26:32 +00:00
|
|
|
const { search, chats } = this.state;
|
2021-12-02 13:20:19 +00:00
|
|
|
const { theme } = this.props;
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
let style = { borderColor: themes[theme].separatorColor };
|
2018-08-31 18:13:30 +00:00
|
|
|
if (index === 0) {
|
2019-12-04 16:39:53 +00:00
|
|
|
style = { ...style, ...sharedStyles.separatorTop };
|
2018-08-31 18:13:30 +00:00
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
if (search.length > 0 && index === search.length - 1) {
|
2018-08-31 18:13:30 +00:00
|
|
|
style = { ...style, ...sharedStyles.separatorBottom };
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
if (search.length === 0 && index === chats.length - 1) {
|
2018-08-31 18:13:30 +00:00
|
|
|
style = { ...style, ...sharedStyles.separatorBottom };
|
|
|
|
}
|
2022-02-01 13:39:09 +00:00
|
|
|
|
|
|
|
const itemSearch = item as ISearch;
|
|
|
|
const itemModel = item as TSubscriptionModel;
|
|
|
|
|
2018-08-31 18:13:30 +00:00
|
|
|
return (
|
|
|
|
<UserItem
|
2022-02-01 13:39:09 +00:00
|
|
|
name={itemSearch.search ? itemSearch.name : itemModel.fname || ''}
|
|
|
|
username={itemSearch.search ? itemSearch.username : itemModel.name}
|
2022-05-03 02:43:30 +00:00
|
|
|
onPress={() => this.goRoom(itemModel)}
|
2021-09-13 20:41:05 +00:00
|
|
|
testID={`new-message-view-item-${item.name}`}
|
2018-08-31 18:13:30 +00:00
|
|
|
style={style}
|
2019-12-04 16:39:53 +00:00
|
|
|
theme={theme}
|
2018-08-31 18:13:30 +00:00
|
|
|
/>
|
|
|
|
);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2018-08-31 18:13:30 +00:00
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
renderList = () => {
|
2019-09-16 20:26:32 +00:00
|
|
|
const { search, chats } = this.state;
|
2019-12-04 16:39:53 +00:00
|
|
|
const { theme } = this.props;
|
2018-09-25 19:28:42 +00:00
|
|
|
return (
|
|
|
|
<FlatList
|
2019-09-16 20:26:32 +00:00
|
|
|
data={search.length > 0 ? search : chats}
|
2018-09-25 19:28:42 +00:00
|
|
|
extraData={this.state}
|
2022-03-08 14:24:21 +00:00
|
|
|
keyExtractor={item => item._id || item.rid}
|
2018-09-25 19:28:42 +00:00
|
|
|
ListHeaderComponent={this.renderHeader}
|
|
|
|
renderItem={this.renderItem}
|
2021-02-26 16:27:04 +00:00
|
|
|
ItemSeparatorComponent={List.Separator}
|
2019-12-04 16:39:53 +00:00
|
|
|
contentContainerStyle={{ backgroundColor: themes[theme].backgroundColor }}
|
2018-09-25 19:28:42 +00:00
|
|
|
keyboardShouldPersistTaps='always'
|
|
|
|
/>
|
|
|
|
);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2018-08-31 18:13:30 +00:00
|
|
|
|
2020-10-30 13:59:44 +00:00
|
|
|
render() {
|
2019-12-04 16:39:53 +00:00
|
|
|
return (
|
2020-10-30 13:59:44 +00:00
|
|
|
<SafeAreaView testID='new-message-view'>
|
|
|
|
<StatusBar />
|
2019-12-04 16:39:53 +00:00
|
|
|
{this.renderList()}
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
}
|
2018-08-31 18:13:30 +00:00
|
|
|
}
|
2019-08-07 13:51:34 +00:00
|
|
|
|
2022-02-02 18:02:02 +00:00
|
|
|
const mapStateToProps = (state: IApplicationState) => ({
|
|
|
|
serverVersion: state.server.version as string,
|
2020-06-15 14:00:46 +00:00
|
|
|
isMasterDetail: state.app.isMasterDetail,
|
2022-02-02 18:02:02 +00:00
|
|
|
maxUsers: (state.settings.DirectMesssage_maxUsers as number) || 1,
|
2021-10-05 13:59:40 +00:00
|
|
|
createTeamPermission: state.permissions['create-team'],
|
|
|
|
createDirectMessagePermission: state.permissions['create-d'],
|
|
|
|
createPublicChannelPermission: state.permissions['create-c'],
|
|
|
|
createPrivateChannelPermission: state.permissions['create-p'],
|
|
|
|
createDiscussionPermission: state.permissions['start-discussion']
|
2019-08-07 13:51:34 +00:00
|
|
|
});
|
|
|
|
|
2022-02-02 18:02:02 +00:00
|
|
|
export default connect(mapStateToProps)(withTheme(NewMessageView));
|