aboutsummaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
authorIvan Chen <ivan@tagg.id>2021-04-09 19:55:26 -0400
committerIvan Chen <ivan@tagg.id>2021-04-09 19:55:26 -0400
commit0a480048b41a80e8569ce57064d1b9716c3d25e3 (patch)
tree4f1118560c10dcdfa32e99d2b73c3d7814d7904d /src/utils
parent17de7d8312b10f84af2178f769ff92bf96ab47f5 (diff)
parent9d5ad9bea36c0b2abffd04b25126d18158017137 (diff)
Merge branch 'master' into tma784-style-message-input
# Conflicts: # src/screens/chat/ChatListScreen.tsx # src/screens/chat/ChatScreen.tsx
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/common.ts18
-rw-r--r--src/utils/index.ts1
-rw-r--r--src/utils/messages.ts59
-rw-r--r--src/utils/users.ts2
4 files changed, 60 insertions, 20 deletions
diff --git a/src/utils/common.ts b/src/utils/common.ts
index 0900a084..7ae36dc6 100644
--- a/src/utils/common.ts
+++ b/src/utils/common.ts
@@ -173,21 +173,3 @@ const _crestIcon = (university: UniversityType) => {
return require('../assets/images/bwbadges.png');
}
};
-
-export const createChannel = async (
- loggedInUser: string,
- id: string,
- chatClient: any,
-) => {
- console.log(loggedInUser, id, chatClient);
- try {
- const channel = chatClient.channel('messaging', {
- members: [loggedInUser, id],
- });
- await channel.watch();
- return channel;
- } catch (error) {
- console.log(error);
- throw error;
- }
-};
diff --git a/src/utils/index.ts b/src/utils/index.ts
index 739e6fb8..4ff9afac 100644
--- a/src/utils/index.ts
+++ b/src/utils/index.ts
@@ -4,3 +4,4 @@ export * from './common';
export * from './users';
export * from './friends';
export * from './search';
+export * from './messages';
diff --git a/src/utils/messages.ts b/src/utils/messages.ts
index d63f2b7a..dd29f317 100644
--- a/src/utils/messages.ts
+++ b/src/utils/messages.ts
@@ -1,6 +1,9 @@
+import AsyncStorage from '@react-native-community/async-storage';
import moment from 'moment';
+import {loadChatTokenService} from '../services/ChatService';
import {RootState} from '../store/rootReducer';
import {ChannelGroupedType} from '../types';
+import {StreamChat} from 'stream-chat';
/**
* Finds the difference in time in minutes
@@ -81,3 +84,59 @@ export const getMember = (
: [];
return otherMembers.length === 1 ? otherMembers[0] : undefined;
};
+
+export const connectChatAccount = async (
+ loggedInUserId: string,
+ chatClient: StreamChat,
+) => {
+ try {
+ await getChatToken();
+ const chatToken = await AsyncStorage.getItem('chatToken');
+ if (!chatClient.user && chatToken) {
+ await chatClient.connectUser(
+ {
+ id: loggedInUserId,
+ },
+ chatToken,
+ );
+ return true;
+ } else if (chatClient.user) {
+ return true;
+ } else {
+ console.log('Unable to connect to stream. Empty chat token');
+ return false;
+ }
+ } catch (err) {
+ console.log('Error while connecting user to Stream: ', err);
+ return false;
+ }
+};
+
+export const getChatToken = async () => {
+ try {
+ const currentChatToken = await AsyncStorage.getItem('chatToken');
+ if (currentChatToken === null) {
+ const chatToken = await loadChatTokenService();
+ await AsyncStorage.setItem('chatToken', chatToken);
+ }
+ } catch (err) {
+ console.log(err);
+ }
+};
+
+export const createChannel = async (
+ loggedInUser: string,
+ id: string,
+ chatClient: any,
+) => {
+ try {
+ const channel = chatClient.channel('messaging', {
+ members: [loggedInUser, id],
+ });
+ await channel.watch();
+ return channel;
+ } catch (error) {
+ console.log(error);
+ throw error;
+ }
+};
diff --git a/src/utils/users.ts b/src/utils/users.ts
index 22c1c1f0..7148eb79 100644
--- a/src/utils/users.ts
+++ b/src/utils/users.ts
@@ -12,14 +12,12 @@ import {
logout,
} from '../store/actions';
import {NO_SOCIAL_ACCOUNTS} from '../store/initialStates';
-import {userLoggedIn} from '../store/reducers';
import {loadUserMomentCategories} from './../store/actions/momentCategories';
import {loadUserX} from './../store/actions/userX';
import {AppDispatch} from './../store/configureStore';
import {RootState} from './../store/rootReducer';
import {
ProfilePreviewType,
- CategoryPreviewType,
ProfileInfoType,
ScreenType,
UserType,