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

18
app.js
View File

@ -67,6 +67,13 @@ async function joinRoom(topicStr) {
await joinSwarm(topicBuffer); 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() { async function initialize() {
try { try {
servePort = getRandomPort(); servePort = getRandomPort();
@ -381,17 +388,17 @@ async function joinSwarm(topicBuffer) {
} }
} }
function addRoomToList(topic) { function addRoomToList(topic, alias) {
const roomList = document.querySelector('#room-list'); const roomList = document.querySelector('#room-list');
const roomItem = document.createElement('li'); const roomItem = document.createElement('li');
roomItem.textContent = truncateHash(topic); roomItem.textContent = alias || truncateHash(topic);
roomItem.dataset.topic = topic; roomItem.dataset.topic = topic;
roomItem.addEventListener('dblclick', () => enterEditMode(roomItem)); roomItem.addEventListener('dblclick', () => enterEditMode(roomItem));
roomItem.addEventListener('click', () => switchRoom(topic)); roomItem.addEventListener('click', () => switchRoom(topic));
roomList.appendChild(roomItem); roomList.appendChild(roomItem);
config.rooms.push({ topic, alias: truncateHash(topic) }); config.rooms.push({ topic, alias: alias || truncateHash(topic) });
writeConfigToFile("./config.json"); writeConfigToFile("./config.json");
} }
@ -514,10 +521,11 @@ async function sendMessage(e) {
await handleCommand(message, { await handleCommand(message, {
eventEmitter, eventEmitter,
currentTopic, currentTopic,
clearMessagesCMD, clearMessages,
addMessage: (from, message, avatar, topic) => onMessageAdded(from, message, avatar, topic, timestamp), addMessage: (from, message, avatar, topic) => onMessageAdded(from, message, avatar, topic, timestamp),
joinRoom, joinRoom,
leaveRoom leaveRoom,
createRoom
}); });
return; return;
} }

View File

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