Up to date fork #1

Merged
MiTask merged 33 commits from snxraven/LinkUp-P2P-Chat:main into main 2024-06-14 04:32:25 -04:00
2 changed files with 24 additions and 9 deletions
Showing only changes of commit f84a6cff1b - Show all commits

20
app.js
View File

@ -67,6 +67,13 @@ async function joinRoom(topicStr) {
await joinSwarm(topicBuffer);
}
async function createRoom(alias) {
const topicBuffer = crypto.randomBytes(32);
const topic = b4a.toString(topicBuffer, 'hex');
addRoomToList(topic, alias);
await joinSwarm(topicBuffer);
}
async function initialize() {
try {
servePort = getRandomPort();
@ -381,17 +388,17 @@ async function joinSwarm(topicBuffer) {
}
}
function addRoomToList(topic) {
function addRoomToList(topic, alias) {
const roomList = document.querySelector('#room-list');
const roomItem = document.createElement('li');
roomItem.textContent = truncateHash(topic);
roomItem.textContent = alias || truncateHash(topic);
roomItem.dataset.topic = topic;
roomItem.addEventListener('dblclick', () => enterEditMode(roomItem));
roomItem.addEventListener('click', () => switchRoom(topic));
roomList.appendChild(roomItem);
config.rooms.push({ topic, alias: truncateHash(topic) });
config.rooms.push({ topic, alias: alias || truncateHash(topic) });
writeConfigToFile("./config.json");
}
@ -514,14 +521,15 @@ async function sendMessage(e) {
await handleCommand(message, {
eventEmitter,
currentTopic,
clearMessagesCMD,
clearMessages,
addMessage: (from, message, avatar, topic) => onMessageAdded(from, message, avatar, topic, timestamp),
joinRoom,
leaveRoom
leaveRoom,
createRoom
});
return;
}
console.log('Sending message:', message); // Debugging log
onMessageAdded(config.userName, message, config.userAvatar, topic, timestamp);

View File

@ -12,7 +12,7 @@ if (fs.existsSync(agentAvatarPath)) {
}
export default async function handleCommand(command, context) {
const { eventEmitter, currentTopic, clearMessagesCMD, addMessage, joinRoom, leaveRoom } = context;
const { eventEmitter, currentTopic, clearMessages, addMessage, joinRoom, leaveRoom, createRoom } = context;
const args = command.trim().split(' ');
const cmd = args[0].toLowerCase();
@ -20,13 +20,13 @@ export default async function handleCommand(command, context) {
switch (cmd) {
case '~clear':
clearMessagesCMD();
clearMessages();
break;
case '~ping':
addMessage('LinkUp', 'pong', agentAvatar, currentTopic());
break;
case '~help':
addMessage('LinkUp', 'Available commands: ~clear, ~ping, ~help, ~join [topic], ~leave', agentAvatar, currentTopic());
addMessage('LinkUp', 'Available commands:\n- ~clear\n- ~ping\n- ~help\n- ~join [topic]\n- ~leave\n- ~create [alias]', agentAvatar, currentTopic());
break;
case '~join':
if (restArgs) {
@ -38,6 +38,13 @@ export default async function handleCommand(command, context) {
case '~leave':
leaveRoom(currentTopic());
break;
case '~create':
if (restArgs) {
await createRoom(restArgs);
} else {
addMessage('LinkUp', 'Usage: ~create [alias]', agentAvatar, currentTopic());
}
break;
default:
console.log('Unknown command:', command);
}