Skip to content

Commit

Permalink
Merge pull request #84 from Yeghro/main
Browse files Browse the repository at this point in the history
modified sendMessage
  • Loading branch information
bitkarrot authored Oct 10, 2024
2 parents ff4027d + 1f6501a commit dacd239
Showing 1 changed file with 82 additions and 12 deletions.
94 changes: 82 additions & 12 deletions public/js/RoomClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -4406,6 +4406,43 @@ class RoomClient {
}
}

wrapLongStrings(message) {
const allowedPrefixes = ['npub', 'nprofile', 'note', 'nevent', 'nrelay', 'naddr',]; // You can add more prefixes here

try {
const words = message.split(' ');
const wrappedWords = words.map(word => {
// Ignore words that start with 'https'
if (word.startsWith('https')) {
return word; // Ignore this word, return it as is
}

// Check if the word starts with any of the allowed prefixes
const hasPrefix = allowedPrefixes.some(prefix => word.startsWith(prefix));

if (hasPrefix && word.length > 30) {
// If it has a prefix and is longer than 30, wrap it in quotes
return `"${word}"`;
} else if (!hasPrefix && word.length > 30) {
// If it doesn't have a prefix and is longer than 30, alert and stop processing
alert(`The entered text "${word}" is too long and cannot be processed for security reasons.`);
throw new Error(`Text "${word}" exceeds allowed length without a valid prefix.`);
}

// Return the word unmodified if none of the above conditions are met
return word;
});

// Join the modified words back into a string and return
return wrappedWords.join(' ');

} catch (error) {
console.error('An error occurred: ', error.message);
return null; // or you can return a different message if needed
}
}


sendMessage() {
// comment out for testing, allow send if no participants
// if (!this.thereAreParticipants() && !isChatGPTOn) {
Expand Down Expand Up @@ -4449,25 +4486,27 @@ class RoomClient {
this.chatMessageTimeLast = currentTime;

chatMessage.value = filterXSS(chatMessage.value.trim());
const peer_msg = this.formatMsg(chatMessage.value);
const wrappedMessage = this.wrapLongStrings(chatMessage.value);
const peer_msg = this.formatMsg(wrappedMessage);
if (!peer_msg) {
return this.cleanMessage();
}
this.peer_name = filterXSS(this.peer_name);

const data = {
room_id: this.room_id,
peer_name: this.peer_name,
peer_id: this.peer_id,
to_peer_id: 'ChatGPT',
to_peer_name: 'ChatGPT',
peer_msg: peer_msg,
peer_info: this.peer_info,
};

if (isChatGPTOn) {
console.log('Send message:', data);
this.socket.emit('message', data);
this.setMsgAvatar('left', this.peer_name);
this.setMsgAvatar('left', this.peer_name, this.peer_info);
this.appendMessage(
'left',
this.leftMsgAvatar,
Expand All @@ -4476,6 +4515,7 @@ class RoomClient {
peer_msg,
data.to_peer_id,
data.to_peer_name,
this.peer_info,
);
this.cleanMessage();

Expand Down Expand Up @@ -4524,6 +4564,7 @@ class RoomClient {
peer_msg,
data.to_peer_id,
data.to_peer_name,
this.peer_info,
);
this.cleanMessage();
}
Expand Down Expand Up @@ -4574,6 +4615,7 @@ class RoomClient {
peer_msg,
to_peer_id,
toPeerName,
data.to_peer_name,
);
if (!this.isChatOpen) this.toggleChat();
}
Expand All @@ -4582,7 +4624,7 @@ class RoomClient {

async showMessage(data) {
if (!this.isChatOpen && this.showChatOnMessage) await this.toggleChat();
this.setMsgAvatar('right', data.peer_name);
this.setMsgAvatar('right', data.peer_name, data.peer_info);
this.appendMessage(
'right',
this.rightMsgAvatar,
Expand All @@ -4591,6 +4633,7 @@ class RoomClient {
data.peer_msg,
data.to_peer_id,
data.to_peer_name,
data.peer_info,
);
if (!this.showChatOnMessage) {
this.userLog('info', `💬 New message from: ${data.peer_name}`, 'top-end');
Expand Down Expand Up @@ -4630,7 +4673,7 @@ class RoomClient {
avatar === 'left' ? (this.leftMsgAvatar = avatarImg) : (this.rightMsgAvatar = avatarImg);
}

appendMessage(side, img, fromName, fromId, msg, toId, toName) {
appendMessage(side, img, fromName, fromId, msg, toId, toName, peerInfo) {
//
const getSide = filterXSS(side);
const getImg = filterXSS(img);
Expand Down Expand Up @@ -4659,9 +4702,11 @@ class RoomClient {
</button>`
: '';

const avatarImg = peerInfo && peerInfo.peer_url ? peerInfo.peer_url : img;

const positionFirst = myMessage
? `<img src="${getImg}" alt="avatar" />${timeAndName}`
: `${timeAndName}<img src="${getImg}" alt="avatar" />`;
? `<img src="${avatarImg}" alt="avatar" />${timeAndName}`
: `${timeAndName}<img src="${avatarImg}" alt="avatar" />`;

const message = getFromName === 'ChatGPT' ? `<pre>${getMsg}</pre>` : getMsg;

Expand Down Expand Up @@ -7295,10 +7340,35 @@ class RoomClient {
if (inputPv && audioConsumerPlayer) {
inputPv.style.display = 'inline';
inputPv.value = 100;
// Not work on Mobile?
inputPv.addEventListener('input', () => {
audioConsumerPlayer.volume = inputPv.value / 100;
});

const updateVolume = () => {
const volume = inputPv.value / 100;
this.setAudioVolume(audioConsumerPlayer, volume);
};

inputPv.addEventListener('input', updateVolume);
inputPv.addEventListener('change', updateVolume);

if (this.isMobileDevice) {
inputPv.addEventListener('touchstart', updateVolume);
inputPv.addEventListener('touchmove', updateVolume);
}
}
}

setAudioVolume(audioConsumerPlayer, volume) {
if (audioConsumerPlayer) {
if (this.isMobileDevice) {
// On mobile, we'll use a different approach
audioConsumerPlayer.muted = volume === 0;
if (!audioConsumerPlayer.muted) {
// We can only set volume to 1 on mobile, so we'll adjust playback rate instead
audioConsumerPlayer.playbackRate = Math.max(0.1, volume);
}
} else {
// On desktop, we can directly set the volume
audioConsumerPlayer.volume = volume;
}
}
}

Expand Down

0 comments on commit dacd239

Please sign in to comment.