Created
November 14, 2021 03:33
-
-
Save hieptl/95a8f83bc9329fa4b16c4b038f6b4ca8 to your computer and use it in GitHub Desktop.
Pendings.js - Add CometChat Friend and Send Custom Message - Discord Clone
This file contains hidden or 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
... | |
const sendCustomMessage = ({ message, type, receiverId }) => { | |
const receiverID = receiverId; | |
const customType = type; | |
const receiverType = cometChat.RECEIVER_TYPE.USER; | |
const customData = { | |
message | |
}; | |
const customMessage = new cometChat.CustomMessage( | |
receiverID, | |
receiverType, | |
customType, | |
customData | |
); | |
cometChat.sendCustomMessage(customMessage).then( | |
message => { | |
}, | |
error => { | |
} | |
); | |
}; | |
const addCometChatFriend = async (selectedUser, authenticatedUser) => { | |
if (!selectedUser || !authenticatedUser) { | |
return; | |
} | |
const cometChatAppId = `${process.env.REACT_APP_COMETCHAT_APP_ID}`; | |
const cometChatAppRegion = `${process.env.REACT_APP_COMETCHAT_REGION}`; | |
const cometChatApiKey = `${process.env.REACT_APP_COMETCHAT_API_KEY}`; | |
const url = `https://${cometChatAppId}.api-${cometChatAppRegion}.cometchat.io/v3/users/${authenticatedUser.id}/friends`; | |
const options = { | |
method: "POST", | |
headers: { | |
Accept: "application/json", | |
"Content-Type": "application/json", | |
appId: cometChatAppId, | |
apiKey: cometChatApiKey, | |
}, | |
body: JSON.stringify({ accepted: [selectedUser.id] }), | |
}; | |
const response = await fetch(url, options); | |
if (response) { | |
const customMessage = { | |
message: `${authenticatedUser.fullname} has accepted your friend request`, | |
type: 'friend', | |
receiverId: selectedUser.id | |
}; | |
sendCustomMessage(customMessage); | |
... | |
} | |
... | |
}; | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment