forked from snxraven/LinkUp-P2P-Chat
Up to date fork #1
18
app.js
18
app.js
@ -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,10 +521,11 @@ 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;
|
||||
}
|
||||
|
13
commands.js
13
commands.js
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user