Add getDiscussions endpoint

This commit is contained in:
Gerzon Z 2021-09-03 15:50:18 -04:00
parent 2b1f78f3ec
commit 7429e4be5c
2 changed files with 44 additions and 26 deletions

View File

@ -798,6 +798,18 @@ const RocketChat = {
prid, pmid, t_name, reply, users, encrypted prid, pmid, t_name, reply, users, encrypted
}); });
}, },
getDiscussions({
roomId, offset, count, text = ''
}) {
const params = {
roomId,
offset,
count,
text
};
// RC 2.4.0
return this.sdk.get('chat.getDiscussions', params);
},
createTeam({ createTeam({
name, users, type, readOnly, broadcast, encrypted name, users, type, readOnly, broadcast, encrypted
}) { }) {

View File

@ -3,13 +3,11 @@ import React, { useEffect, useState } from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { FlatList } from 'react-native'; import { FlatList } from 'react-native';
import { useSelector } from 'react-redux'; import { useSelector } from 'react-redux';
import { Q } from '@nozbe/watermelondb';
import { useSafeAreaInsets } from 'react-native-safe-area-context'; import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { HeaderBackButton } from '@react-navigation/stack'; import { HeaderBackButton } from '@react-navigation/stack';
import ActivityIndicator from '../containers/ActivityIndicator'; import ActivityIndicator from '../containers/ActivityIndicator';
import I18n from '../i18n'; import I18n from '../i18n';
import database from '../lib/database';
import StatusBar from '../containers/StatusBar'; import StatusBar from '../containers/StatusBar';
import log from '../utils/log'; import log from '../utils/log';
import debounce from '../utils/debounce'; import debounce from '../utils/debounce';
@ -23,7 +21,9 @@ import { getHeaderTitlePosition } from '../containers/Header';
import SearchHeader from './ThreadMessagesView/SearchHeader'; import SearchHeader from './ThreadMessagesView/SearchHeader';
import { useTheme } from '../theme'; import { useTheme } from '../theme';
import Message from '../containers/message'; import Message from '../containers/message';
import { sanitizeLikeString } from '../lib/database/utils'; import RocketChat from '../lib/rocketchat';
const API_FETCH_COUNT = 25;
const DiscussionMessagesView = ({ navigation, route }) => { const DiscussionMessagesView = ({ navigation, route }) => {
const rid = route.params?.rid; const rid = route.params?.rid;
@ -38,6 +38,7 @@ const DiscussionMessagesView = ({ navigation, route }) => {
const isMasterDetail = useSelector(state => state.app.isMasterDetail); const isMasterDetail = useSelector(state => state.app.isMasterDetail);
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [discussions, setDiscussions] = useState([]); const [discussions, setDiscussions] = useState([]);
const [search, setSearch] = useState([]);
const [isSearching, setIsSearching] = useState(false); const [isSearching, setIsSearching] = useState(false);
const { theme } = useTheme(); const { theme } = useTheme();
const insets = useSafeAreaInsets(); const insets = useSafeAreaInsets();
@ -48,39 +49,43 @@ const DiscussionMessagesView = ({ navigation, route }) => {
} }
setLoading(true); setLoading(true);
try { try {
const db = database.active; const result = await RocketChat.getDiscussions({
const subCollection = db.get('messages'); roomId: rid,
const subDiscussions = await subCollection.query( offset: discussions.length,
Q.where('rid', Q.eq(rid)), count: API_FETCH_COUNT
Q.where('drid', Q.notEq(null)) });
);
setDiscussions(subDiscussions); if (result.success) {
if (isSearching) {
setSearch(result.messages);
} else {
setDiscussions(result.messages);
}
}
setLoading(false); setLoading(false);
} catch (e) { } catch (e) {
log(e); log(e);
setLoading(false);
} }
}; };
const onSearchChangeText = debounce(async(text) => { const onSearchChangeText = debounce(async(text) => {
setLoading(true);
try { try {
const db = database.active; const result = await RocketChat.getDiscussions({
const whereClause = [ roomId: rid,
Q.where('rid', Q.eq(rid)), offset: search.length,
Q.where('drid', Q.notEq(null)) count: API_FETCH_COUNT,
]; text
});
if (text?.trim()) { if (result.success) {
whereClause.push(Q.where('msg', Q.like(`%${ sanitizeLikeString(text?.trim()) }%`))); setSearch(result.messages);
} }
setLoading(false);
const discussionsMessages = await db
.get('messages')
.query(...whereClause);
setDiscussions(discussionsMessages);
} catch (e) { } catch (e) {
log(e); log(e);
setLoading(false);
} }
}, 300); }, 300);
@ -106,7 +111,7 @@ const DiscussionMessagesView = ({ navigation, route }) => {
/> />
</HeaderButton.Container> </HeaderButton.Container>
), ),
headerTitle: () => <SearchHeader placeholder='Search by first message' onSearchChangeText={onSearchChangeText} />, headerTitle: () => <SearchHeader placeholder='Search Messages' onSearchChangeText={onSearchChangeText} />,
headerTitleContainerStyle: { headerTitleContainerStyle: {
left: headerTitlePosition.left, left: headerTitlePosition.left,
right: headerTitlePosition.right right: headerTitlePosition.right
@ -185,7 +190,7 @@ const DiscussionMessagesView = ({ navigation, route }) => {
<SafeAreaView testID='discussion-messages-view'> <SafeAreaView testID='discussion-messages-view'>
<StatusBar /> <StatusBar />
<FlatList <FlatList
data={discussions} data={isSearching ? search : discussions}
renderItem={renderItem} renderItem={renderItem}
keyExtractor={item => item.msg} keyExtractor={item => item.msg}
style={{ backgroundColor: themes[theme].backgroundColor }} style={{ backgroundColor: themes[theme].backgroundColor }}
@ -194,6 +199,7 @@ const DiscussionMessagesView = ({ navigation, route }) => {
windowSize={10} windowSize={10}
initialNumToRender={7} initialNumToRender={7}
removeClippedSubviews={isIOS} removeClippedSubviews={isIOS}
onEndReached={() => discussions.length > 25 ?? load()}
ItemSeparatorComponent={List.Separator} ItemSeparatorComponent={List.Separator}
ListFooterComponent={loading ? <ActivityIndicator theme={theme} /> : null} ListFooterComponent={loading ? <ActivityIndicator theme={theme} /> : null}
scrollIndicatorInsets={{ right: 1 }} scrollIndicatorInsets={{ right: 1 }}