Merge pull request 'Fixed app.js not sending user ID itself and only peers it knows' (#5) from MiTask/LinkUp-P2P-Chat:main into main

Reviewed-on: snxraven/LinkUp-P2P-Chat#5
This commit is contained in:
snxraven 2024-06-10 15:26:59 +00:00
commit b88337c398
7 changed files with 31 additions and 8 deletions

7
app.js
View File

@ -296,13 +296,18 @@ function sendMessage(e) {
onMessageAdded(config.userName, message, config.userAvatar); onMessageAdded(config.userName, message, config.userAvatar);
let peersPublicKeys = [];
peersPublicKeys.push([...swarm.connections].map(peer => peer.remotePublicKey.toString('hex')));
peersPublicKeys = peersPublicKeys.flat(1);
peersPublicKeys.push(swarm.keyPair.publicKey.toString('hex'));
const messageObj = JSON.stringify({ const messageObj = JSON.stringify({
type: 'message', type: 'message',
name: config.userName, name: config.userName,
message, message,
avatar: config.userAvatar, avatar: config.userAvatar,
topic: b4a.toString(currentRoom.topic, 'hex'), topic: b4a.toString(currentRoom.topic, 'hex'),
peers: [...swarm.connections].map(peer => peer.remotePublicKey.toString('hex')), peers: peersPublicKeys,
timestamp: Date.now(), timestamp: Date.now(),
readableTimestamp: new Date().toLocaleString(), // Added human-readable timestamp readableTimestamp: new Date().toLocaleString(), // Added human-readable timestamp
}); });

View File

@ -42,7 +42,7 @@ loadCommands().then(commands => {
if (message.type == "icon") return; if (message.type == "icon") return;
console.log(message); console.log(message);
console.log(`Message received from ${message.name}@${peer} at ${new Date(message.timestamp).toLocaleTimeString()}: ${message.message}`); console.log(`Message received from ${message.name}@${message.topic} at ${new Date(message.timestamp).toLocaleTimeString()}: ${message.message}`);
// Check if the message starts with a command prefix // Check if the message starts with a command prefix
if (message.message.startsWith('!')) { if (message.message.startsWith('!')) {
@ -61,7 +61,7 @@ loadCommands().then(commands => {
bot.on('onBotJoinRoom', () => { bot.on('onBotJoinRoom', () => {
console.log("Bot is ready!"); console.log("Bot is ready!");
bot.sendMessage(process.env.ON_READY_MESSAGE); bot.sendMessageToAll(process.env.ON_READY_MESSAGE);
}); });
bot.joinChatRoom(process.env.LINKUP_ROOM_ID); bot.joinChatRoom(process.env.LINKUP_ROOM_ID);

View File

@ -4,6 +4,6 @@ export default {
handler: function(bot, args, message) { handler: function(bot, args, message) {
const responses = ['It is certain.', 'It is decidedly so.', 'Without a doubt.', 'Yes - definitely.', 'You may rely on it.', 'As I see it, yes.', 'Most likely.', 'Outlook good.', 'Yes.', 'Signs point to yes.', 'Reply hazy, try again.', 'Ask again later.', 'Better not tell you now.', 'Cannot predict now.', 'Concentrate and ask again.', 'Don\'t count on it.', 'My reply is no.', 'My sources say no.', 'Outlook not so good.', 'Very doubtful.']; const responses = ['It is certain.', 'It is decidedly so.', 'Without a doubt.', 'Yes - definitely.', 'You may rely on it.', 'As I see it, yes.', 'Most likely.', 'Outlook good.', 'Yes.', 'Signs point to yes.', 'Reply hazy, try again.', 'Ask again later.', 'Better not tell you now.', 'Cannot predict now.', 'Concentrate and ask again.', 'Don\'t count on it.', 'My reply is no.', 'My sources say no.', 'Outlook not so good.', 'Very doubtful.'];
const randomIndex = Math.floor(Math.random() * responses.length); const randomIndex = Math.floor(Math.random() * responses.length);
bot.sendMessage(responses[randomIndex]); bot.sendMessage(message.peers, responses[randomIndex]);
} }
}; };

View File

@ -3,6 +3,6 @@ export default {
description: 'Greet the user', description: 'Greet the user',
handler: function(bot, args, message) { handler: function(bot, args, message) {
const userName = message.name; const userName = message.name;
bot.sendMessage(`Hello, ${userName}! How can I assist you today?`); bot.sendMessage(message.peers, `Hello, ${userName}! How can I assist you today?`);
} }
}; };

View File

@ -2,6 +2,6 @@
export default { export default {
handler: function(bot, args, message) { handler: function(bot, args, message) {
bot.sendMessage('Pong!'); bot.sendMessage(message.peers, 'Pong!');
} }
}; };

View File

@ -0,0 +1,10 @@
class ChatRoom {
public ChatRoom(topic, peers) {
this.topic = topic;
this.peers = peers;
}
}
export default ChatRoom;

View File

@ -32,10 +32,18 @@ class Client extends EventEmitter {
}); });
} }
sendMessage(message) { sendMessage(roomPeers, message) {
console.log('Bot name:', this.botName); console.log('Bot name:', this.botName);
const timestamp = Date.now(); // Generate timestamp const timestamp = Date.now(); // Generate timestamp
const peers = [...this.swarm.connections]; const peers = [...this.swarm.connections].filter(peer => roomPeers.includes(peer.remotePublicKey.toString('hex'))); // We remove all the peers that arent included in a room
const data = JSON.stringify({name: this.botName, message, timestamp}); // Include timestamp
for (const peer of peers) peer.write(data);
}
sendMessageToAll(message) {
console.log('Bot name:', this.botName);
const timestamp = Date.now(); // Generate timestamp
const peers = [...this.swarm.connections]
const data = JSON.stringify({name: this.botName, message, timestamp}); // Include timestamp const data = JSON.stringify({name: this.botName, message, timestamp}); // Include timestamp
for (const peer of peers) peer.write(data); for (const peer of peers) peer.write(data);
} }