Compare commits

...

19 Commits

Author SHA1 Message Date
4fbbc73527 Merge pull request 'Fixed bot crash on file attachment, added 2 more new events and topic ID to file message' (#6) from MiTask/LinkUp-P2P-Chat:main into main
Reviewed-on: #6
2024-06-10 20:09:25 +00:00
MrMasrozYTLIVE
edf61f0462 Started working on proper classes for messages (files and text), user peer class, chatroom class 2024-06-10 21:34:50 +03:00
MrMasrozYTLIVE
92ba56b9fc Added topic to file attachment json 2024-06-10 21:25:41 +03:00
MrMasrozYTLIVE
7275b69948 Added onIcon and onFile events. onMessage is now being called only on messageType == message. Removed debug code from swarm.on('update'). 2024-06-10 21:23:59 +03:00
MrMasrozYTLIVE
56d8a8091e Yeah it actually works :yay: 2024-06-10 21:18:33 +03:00
MrMasrozYTLIVE
4ec0be2774 I think i got it to work lol 2024-06-10 21:17:16 +03:00
MrMasrozYTLIVE
27b3528304 It can have multiple topics :( time to figure a way to do it then ig 2024-06-10 21:13:52 +03:00
MrMasrozYTLIVE
2ca07e46c3 so topics in .map() are array. Is it array that always has only one buffer or can have lots of them??? 2024-06-10 21:11:46 +03:00
MrMasrozYTLIVE
e396bcbb0b b4a doesnt work its time to debug again 2024-06-10 21:10:25 +03:00
MrMasrozYTLIVE
3b7f0fbd1d replaced topic.toString with b4a.toString(topic) 2024-06-10 21:09:33 +03:00
MrMasrozYTLIVE
79bdd04810 Lmfao i did peer.topics 2024-06-10 21:06:09 +03:00
MrMasrozYTLIVE
1dca21fe75 Did an oopsie with array.filter ([array.filter] instead of [array].filter) 2024-06-10 21:03:01 +03:00
MrMasrozYTLIVE
edd3f632cf Now its time to debug peer topics since its empty right now 2024-06-10 21:02:01 +03:00
MrMasrozYTLIVE
340362872f Lol it actually was inverted for some reason 2024-06-10 21:00:22 +03:00
MrMasrozYTLIVE
230cdd1321 Huh? Is it actually inverted?? 2024-06-10 20:58:59 +03:00
MrMasrozYTLIVE
acb5609d03 Its not working 😭 time to debug 2024-06-10 20:57:32 +03:00
MrMasrozYTLIVE
66d684e473 Filter all the peerInfo topics that are undefined and then map all of them to hex string 2024-06-10 20:56:02 +03:00
MrMasrozYTLIVE
03f053723d Merge branch 'main' of https://git.ssh.surf/snxraven/LinkUp-P2P-Chat 2024-06-10 20:49:17 +03:00
MrMasrozYTLIVE
033330fd04 started working on parsing peers & topics they're currently in on swarm update event using 'swarm.peers' 2024-06-10 20:46:59 +03:00
7 changed files with 52 additions and 7 deletions

3
app.js
View File

@ -307,7 +307,7 @@ function sendMessage(e) {
message,
avatar: config.userAvatar,
topic: b4a.toString(currentRoom.topic, 'hex'),
peers: peersPublicKeys,
peers: peersPublicKeys, // Deprecated. To be deleted in future updates
timestamp: Date.now(),
readableTimestamp: new Date().toLocaleString(), // Added human-readable timestamp
});
@ -327,6 +327,7 @@ function sendFileMessage(name, fileUrl, fileType, avatar) {
fileUrl,
fileType,
avatar,
topic: b4a.toString(currentRoom.topic, 'hex'),
timestamp: Date.now(),
});

View File

@ -1,6 +1,6 @@
import fs from 'fs';
import path from 'path';
import Client from './includes/client.js'; // Adjust the import path as necessary
import Client from './includes/Client.js'; // Adjust the import path as necessary
import 'dotenv/config';
// Create a new instance of the chatBot class with a valid botName
@ -39,7 +39,6 @@ loadCommands().then(commands => {
// We use Event Emitter here to handle new messages
bot.on('onMessage', (peer, message) => {
if (message.type == "icon") return;
console.log(message);
console.log(`Message received from ${message.name}@${message.topic} at ${new Date(message.timestamp).toLocaleTimeString()}: ${message.message}`);

View File

@ -3,8 +3,6 @@ class ChatRoom {
this.topic = topic;
this.peers = peers;
}
}
export default ChatRoom;

View File

@ -1,5 +1,6 @@
import Hyperswarm from 'hyperswarm';
import EventEmitter from 'node:events'
import b4a from "b4a";
class Client extends EventEmitter {
constructor(botName) {
@ -12,7 +13,12 @@ class Client extends EventEmitter {
setupSwarm() {
this.swarm.on('connection', (peer) => {
peer.on('data', message => this.emit('onMessage', peer, JSON.parse(message.toString())));
peer.on('data', message => {
if(message.type === "message") this.emit('onMessage', peer, JSON.parse(message.toString()));
if(message.type === "icon") this.emit('onIcon', peer, JSON.parse(message.toString()));
if(message.type === "file") this.emit('onFile', peer, JSON.parse(message.toString()));
});
peer.on('error', e => {
this.emit('onError', e);
console.error(`Connection error: ${e}`)
@ -20,7 +26,16 @@ class Client extends EventEmitter {
});
this.swarm.on('update', () => {
console.log(`Peers count: ${this.swarm.connections.size}`);
console.log(`Connections count: ${this.swarm.connections.size} / Peers count: ${this.swarm.peers.size}`);
this.swarm.peers.forEach((peerInfo, peerId) => {
// Please do not try to understand what is going on here. I have no idea anyway. But it surprisingly works
const peer = [peerId];
const peerTopics = [peerInfo.topics]
.filter(topics => topics)
.map(topics => topics.map(topic => b4a.toString(topic, 'hex')));
});
});
}

View File

@ -0,0 +1,12 @@
class FileMessage {
public FileMessage(chatRoom, userPeer, fileName, fileUrl, fileType, timestamp) {
this.chatRoom = chatRoom;
this.userPeer = userPeer;
this.fileName = fileName;
this.fileUrl = fileUrl;
this.fileType = fileType;
this.timestamp = timestamp;
}
}
export default FileMessage;

View File

@ -0,0 +1,10 @@
class TextMessage {
public TextMessage(chatRoom, userPeer, message, timestamp) {
this.chatRoom = chatRoom;
this.userPeer = userPeer;
this.message = message;
this.timestamp = timestamp;
}
}
export default TextMessage;

View File

@ -0,0 +1,10 @@
class UserPeer {
public UserPeer(peer, topics, username, avatar) {
this.peer = peer;
this.topics = topics;
this.username = username;
this.avatar = avatar;
}
}
export default UserPeer;