mediasfu-reactnative
Version:
MediaSFU Prebuilt React Native SDK
91 lines • 3.81 kB
JavaScript
/**
* Sends a message to the specified room.
*
* @param {Object} options - The options for sending the message.
* @param {string} options.member - The member sending the message.
* @param {string} options.islevel - The level of the member.
* @param {Function} options.showAlert - Function to show alert messages.
* @param {Array} options.coHostResponsibility - List of co-host responsibilities.
* @param {string} options.coHost - The co-host of the room.
* @param {boolean} options.chatSetting - Chat setting for the room.
* @param {string} options.message - The message to be sent.
* @param {string} options.roomName - The name of the room.
* @param {number} options.messagesLength - The current number of messages in the room.
* @param {Array} options.receivers - List of receivers for the message.
* @param {boolean} options.group - Indicates if the message is for a group.
* @param {string} options.sender - The sender of the message.
* @param {Object} options.socket - The socket instance for communication.
*
* @returns {Promise<void>} A promise that resolves when the message is sent.
*
* @throws Will throw an error if the message count limit is exceeded.
* @throws Will throw an error if the message, sender, or receivers are not valid.
* @throws Will throw an error if the user is not allowed to send a message in the event room.
*
* @example
* ```typescript
* launchMessages({
* updateIsMessagesModalVisible: (visible) => console.log("Modal visibility:", visible),
* isMessagesModalVisible: false,
* });
* ```
*/
export const sendMessage = async ({ member, islevel, showAlert, coHostResponsibility, coHost, chatSetting, message, roomName, messagesLength, receivers, group, sender, socket, }) => {
var _a, _b;
let chatValue = false;
// Check message count limit based on the room type
if ((messagesLength > 100 && roomName.startsWith('d'))
|| (messagesLength > 500 && roomName.startsWith('s'))
|| (messagesLength > 100000 && roomName.startsWith('p'))) {
showAlert === null || showAlert === void 0 ? void 0 : showAlert({
message: 'You have reached the maximum number of messages allowed.',
type: 'danger',
duration: 3000,
});
return;
}
// Validate message, sender, and receivers
if (!message || !receivers || (!member && !sender)) {
showAlert === null || showAlert === void 0 ? void 0 : showAlert({
message: 'Message is not valid.',
type: 'danger',
duration: 3000,
});
return;
}
// Create the message object
const messageObject = {
sender: sender || member,
receivers,
message,
timestamp: new Date().toLocaleTimeString(),
group: group !== undefined && group !== null ? group : false,
};
try {
// Check co-host responsibility for chat
chatValue = (_b = (_a = coHostResponsibility.find((item) => item.name === 'chat')) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : false;
}
catch (error) {
console.error(error);
}
if (islevel === '2' || (coHost === member && chatValue === true)) {
// Allow sending message
}
else {
// Check if user is allowed to send a message in the event room
if (!chatSetting) {
showAlert === null || showAlert === void 0 ? void 0 : showAlert({
message: 'You are not allowed to send a message in this event room',
type: 'danger',
duration: 3000,
});
return;
}
}
// Send the message to the server
socket.emit('sendMessage', {
messageObject,
roomName,
});
};
//# sourceMappingURL=sendMessage.js.map