forked from snxraven/LinkUp-P2P-Chat
Up to date fork #1
25
app.js
25
app.js
@ -6,13 +6,22 @@ import Hyperdrive from 'hyperdrive';
|
|||||||
import Corestore from 'corestore';
|
import Corestore from 'corestore';
|
||||||
import { EventEmitter } from 'events';
|
import { EventEmitter } from 'events';
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
|
import handleCommand from './commands.js';
|
||||||
|
|
||||||
|
const agentAvatarPath = './assets/agent.png';
|
||||||
|
let agentAvatar = '';
|
||||||
const storagePath = `./storage/`;
|
const storagePath = `./storage/`;
|
||||||
const store = new Corestore(storagePath);
|
const store = new Corestore(storagePath);
|
||||||
const drive = new Hyperdrive(store);
|
const drive = new Hyperdrive(store);
|
||||||
|
|
||||||
await drive.ready();
|
await drive.ready();
|
||||||
|
|
||||||
|
// Load the agent avatar once when the app starts
|
||||||
|
if (fs.existsSync(agentAvatarPath)) {
|
||||||
|
const avatarBuffer = fs.readFileSync(agentAvatarPath);
|
||||||
|
agentAvatar = `data:image/png;base64,${b4a.toString(avatarBuffer, 'base64')}`;
|
||||||
|
}
|
||||||
|
|
||||||
let activeRooms = [];
|
let activeRooms = [];
|
||||||
const eventEmitter = new EventEmitter();
|
const eventEmitter = new EventEmitter();
|
||||||
|
|
||||||
@ -484,7 +493,6 @@ function leaveRoom(topic) {
|
|||||||
document.querySelector('#setup').classList.remove('hidden');
|
document.querySelector('#setup').classList.remove('hidden');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendMessage(e) {
|
function sendMessage(e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
const message = document.querySelector('#message').value;
|
const message = document.querySelector('#message').value;
|
||||||
@ -493,6 +501,17 @@ function sendMessage(e) {
|
|||||||
const topic = currentTopic();
|
const topic = currentTopic();
|
||||||
const timestamp = Date.now();
|
const timestamp = Date.now();
|
||||||
|
|
||||||
|
if (message.startsWith('~')) {
|
||||||
|
// Handle command
|
||||||
|
handleCommand(message, {
|
||||||
|
eventEmitter,
|
||||||
|
currentTopic,
|
||||||
|
clearMessages,
|
||||||
|
addMessage: (from, message, avatar, topic) => onMessageAdded(from, message, avatar, topic, timestamp)
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
console.log('Sending message:', message); // Debugging log
|
console.log('Sending message:', message); // Debugging log
|
||||||
|
|
||||||
onMessageAdded(config.userName, message, config.userAvatar, topic, timestamp);
|
onMessageAdded(config.userName, message, config.userAvatar, topic, timestamp);
|
||||||
@ -715,6 +734,10 @@ function clearMessages() {
|
|||||||
while (messagesContainer.firstChild) {
|
while (messagesContainer.firstChild) {
|
||||||
messagesContainer.removeChild(messagesContainer.firstChild);
|
messagesContainer.removeChild(messagesContainer.firstChild);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clear the messages from the store for the current room
|
||||||
|
const topic = currentTopic();
|
||||||
|
messagesStore[topic] = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
function toggleSetupView() {
|
function toggleSetupView() {
|
||||||
|
BIN
assets/agent.png
Normal file
BIN
assets/agent.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 20 KiB |
27
commands.js
Normal file
27
commands.js
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
// commands.js
|
||||||
|
import b4a from 'b4a';
|
||||||
|
import fs from 'fs';
|
||||||
|
|
||||||
|
const agentAvatarPath = './assets/agent.png';
|
||||||
|
let agentAvatar = '';
|
||||||
|
|
||||||
|
// Load the agent avatar once when the module is imported
|
||||||
|
if (fs.existsSync(agentAvatarPath)) {
|
||||||
|
const avatarBuffer = fs.readFileSync(agentAvatarPath);
|
||||||
|
agentAvatar = `data:image/png;base64,${b4a.toString(avatarBuffer, 'base64')}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function handleCommand(command, context) {
|
||||||
|
const { eventEmitter, currentTopic, clearMessages, addMessage } = context;
|
||||||
|
|
||||||
|
switch (command.trim().toLowerCase()) {
|
||||||
|
case '~clear':
|
||||||
|
clearMessages();
|
||||||
|
break;
|
||||||
|
case '~ping':
|
||||||
|
addMessage('LinkUp', 'pong', agentAvatar, currentTopic());
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
console.log('Unknown command:', command);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user