-
-
Save Daltonic/b91f8b1e7d9359e2a082c7323afaf632 to your computer and use it in GitHub Desktop.
DappBnb Project the Chat file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { CometChat } from '@cometchat-pro/chat' | |
import { getGlobalState } from '../store' | |
const CONSTANTS = { | |
APP_ID: process.env.REACT_APP_COMET_CHAT_APP_ID, | |
REGION: process.env.REACT_APP_COMET_CHAT_REGION, | |
Auth_Key: process.env.REACT_APP_COMET_CHAT_AUTH_KEY, | |
} | |
const initCometChat = async () => { | |
const appID = CONSTANTS.APP_ID | |
const region = CONSTANTS.REGION | |
const appSetting = new CometChat.AppSettingsBuilder() | |
.subscribePresenceForAllUsers() | |
.setRegion(region) | |
.build() | |
await CometChat.init(appID, appSetting) | |
.then(() => console.log('Initialization completed successfully')) | |
.catch((error) => error) | |
} | |
const loginWithCometChat = async () => { | |
const authKey = CONSTANTS.Auth_Key | |
const UID = getGlobalState('connectedAccount') | |
return new Promise(async (resolve, reject) => { | |
await CometChat.login(UID, authKey) | |
.then((user) => resolve(user)) | |
.catch((error) => reject(error)) | |
}) | |
} | |
const signUpWithCometChat = async () => { | |
const authKey = CONSTANTS.Auth_Key | |
const UID = getGlobalState('connectedAccount') | |
const user = new CometChat.User(UID) | |
user.setName(UID) | |
return new Promise(async (resolve, reject) => { | |
await CometChat.createUser(user, authKey) | |
.then((user) => resolve(user)) | |
.catch((error) => reject(error)) | |
}) | |
} | |
const logOutWithCometChat = async () => { | |
return new Promise(async (resolve, reject) => { | |
await CometChat.logout() | |
.then(() => resolve()) | |
.catch(() => reject()) | |
}) | |
} | |
const isUserLoggedIn = async () => { | |
return new Promise(async (resolve, reject) => { | |
await CometChat.getLoggedinUser() | |
.then((user) => resolve(user)) | |
.catch((error) => reject(error)) | |
}) | |
} | |
const getUser = async (UID) => { | |
return new Promise(async (resolve, reject) => { | |
await CometChat.getUser(UID) | |
.then((user) => resolve(user)) | |
.catch((error) => reject(error)) | |
}) | |
} | |
const getMessages = async (UID) => { | |
const limit = 30 | |
const messagesRequest = new CometChat.MessagesRequestBuilder() | |
.setUID(UID) | |
.setLimit(limit) | |
.build() | |
return new Promise(async (resolve, reject) => { | |
await messagesRequest | |
.fetchPrevious() | |
.then((messages) => resolve(messages.filter((msg) => msg.type == 'text'))) | |
.catch((error) => reject(error)) | |
}) | |
} | |
const sendMessage = async (receiverID, messageText) => { | |
const receiverType = CometChat.RECEIVER_TYPE.USER | |
const textMessage = new CometChat.TextMessage( | |
receiverID, | |
messageText, | |
receiverType | |
) | |
return new Promise(async (resolve, reject) => { | |
await CometChat.sendMessage(textMessage) | |
.then((message) => resolve(message)) | |
.catch((error) => reject(error)) | |
}) | |
} | |
const getConversations = async () => { | |
const limit = 30 | |
const conversationsRequest = new CometChat.ConversationsRequestBuilder() | |
.setLimit(limit) | |
.build() | |
return new Promise(async (resolve, reject) => { | |
await conversationsRequest | |
.fetchNext() | |
.then((conversationList) => resolve(conversationList)) | |
.catch((error) => reject(error)) | |
}) | |
} | |
const listenForMessage = async (listenerID) => { | |
return new Promise(async (resolve, reject) => { | |
CometChat.addMessageListener( | |
listenerID, | |
new CometChat.MessageListener({ | |
onTextMessageReceived: (message) => resolve(message), | |
}) | |
); | |
}); | |
}; | |
export { | |
initCometChat, | |
loginWithCometChat, | |
signUpWithCometChat, | |
logOutWithCometChat, | |
getMessages, | |
sendMessage, | |
getConversations, | |
isUserLoggedIn, | |
getUser, | |
listenForMessage, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment