mayaspace-2-backend/index.js

199 lines
6 KiB
JavaScript
Raw Normal View History

2024-11-06 13:18:37 -06:00
const express = require('express');
const bodyParser = require('body-parser');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const { ActivityPubRouter, createActor } = require('activitypub-express');
const uuid = require('uuid');
2024-11-06 13:28:15 -06:00
const db = require('quick.db'); // Import quick.db
2024-11-06 13:18:37 -06:00
const fetch = require('node-fetch');
const app = express();
const port = 3000;
const SECRET_KEY = 'your_secret_key'; // Replace with a secure key in a real app
// Middleware to parse JSON
app.use(bodyParser.json());
// Helper function to create an actor URL
const actorURL = (username) => `http://localhost:${port}/users/${username}`;
// Helper function to generate JWT token
const generateToken = (user) => {
return jwt.sign({ id: user.username }, SECRET_KEY, { expiresIn: '1h' });
};
// Middleware to protect routes
const authenticate = (req, res, next) => {
const token = req.headers['authorization']?.split(' ')[1];
if (!token) {
return res.status(401).json({ error: 'Unauthorized' });
}
jwt.verify(token, SECRET_KEY, (err, decoded) => {
if (err) {
return res.status(403).json({ error: 'Invalid or expired token' });
}
req.user = decoded;
next();
});
};
// Route to create a new user (actor with password)
app.post('/users', async (req, res) => {
const { username, displayName, password } = req.body;
2024-11-06 13:28:15 -06:00
// Check if user already exists
const existingUser = db.get(`user_${username}`);
if (existingUser) {
2024-11-06 13:18:37 -06:00
return res.status(400).json({ error: 'User already exists' });
}
// Hash the password before saving
const hashedPassword = await bcrypt.hash(password, 10);
2024-11-06 13:28:15 -06:00
// Create the actor and store it in the DB
2024-11-06 13:18:37 -06:00
const actor = createActor({
id: actorURL(username),
username,
displayName,
inbox: `http://localhost:${port}/users/${username}/inbox`,
outbox: `http://localhost:${port}/users/${username}/outbox`,
followers: `http://localhost:${port}/users/${username}/followers`,
following: `http://localhost:${port}/users/${username}/following`,
});
2024-11-06 13:28:15 -06:00
// Store actor info and password in the database
db.set(`user_${username}`, { ...actor, password: hashedPassword });
2024-11-06 13:18:37 -06:00
res.status(201).json(actor);
});
// Route to log in a user and get a token
app.post('/login', async (req, res) => {
const { username, password } = req.body;
2024-11-06 13:28:15 -06:00
// Retrieve user from the database
const user = db.get(`user_${username}`);
2024-11-06 13:18:37 -06:00
if (!user) {
return res.status(404).json({ error: 'User not found' });
}
// Compare hashed password
const isPasswordValid = await bcrypt.compare(password, user.password);
if (!isPasswordValid) {
return res.status(401).json({ error: 'Invalid password' });
}
// Generate and return JWT token
const token = generateToken(user);
res.json({ token });
});
// Route to create a post (status update) with authentication
app.post('/users/:username/posts', authenticate, (req, res) => {
const { username } = req.params;
const { content } = req.body;
if (req.user.id !== username) {
return res.status(403).json({ error: 'You can only post as yourself' });
}
2024-11-06 13:28:15 -06:00
// Retrieve the user from the database
const user = db.get(`user_${username}`);
if (!user) {
2024-11-06 13:18:37 -06:00
return res.status(404).json({ error: 'User not found' });
}
2024-11-06 13:28:15 -06:00
// Create a post and store it in the database
2024-11-06 13:18:37 -06:00
const post = {
id: `http://localhost:${port}/posts/${uuid.v4()}`,
type: 'Note',
content,
2024-11-06 13:28:15 -06:00
author: user,
2024-11-06 13:18:37 -06:00
published: new Date().toISOString(),
};
2024-11-06 13:28:15 -06:00
// Store the post in the database
db.set(`post_${post.id}`, post);
2024-11-06 13:18:37 -06:00
res.status(201).json(post);
});
// Route to get a users posts
app.get('/users/:username/posts', authenticate, (req, res) => {
const { username } = req.params;
if (req.user.id !== username) {
return res.status(403).json({ error: 'You can only view your own posts' });
}
2024-11-06 13:28:15 -06:00
// Retrieve all posts from the database
const userPosts = Object.values(db.get("posts") || {}).filter(post => post.author.username === username);
2024-11-06 13:18:37 -06:00
res.json(userPosts);
});
// ActivityPub setup
const activityPubRouter = new ActivityPubRouter();
// Handle incoming Follow activity
activityPubRouter.post('/users/:username/inbox', authenticate, async (req, res) => {
const { username } = req.params;
const activity = req.body;
2024-11-06 13:28:15 -06:00
if (!db.get(`user_${username}`)) {
2024-11-06 13:18:37 -06:00
return res.status(404).json({ error: 'User not found' });
}
if (activity.type === 'Follow') {
const actor = activity.actor;
2024-11-06 13:28:15 -06:00
// Retrieve followers list and add new follower
const followersList = db.get(`followers_${username}`) || [];
followersList.push(actor.id);
// Store the updated followers list in the database
db.set(`followers_${username}`, followersList);
2024-11-06 13:18:37 -06:00
res.status(200).json({ status: 'Followed successfully' });
// Optionally, notify the actor that the follow was accepted
await sendFollowResponse(actor, username);
} else {
res.status(400).json({ error: 'Unsupported activity type' });
}
});
// Federated actor info endpoint
activityPubRouter.get('/users/:username', (req, res) => {
const { username } = req.params;
2024-11-06 13:28:15 -06:00
const user = db.get(`user_${username}`);
if (!user) {
2024-11-06 13:18:37 -06:00
return res.status(404).json({ error: 'User not found' });
}
2024-11-06 13:28:15 -06:00
res.json(user);
2024-11-06 13:18:37 -06:00
});
// Outbox endpoint to send posts
activityPubRouter.get('/users/:username/outbox', (req, res) => {
const { username } = req.params;
2024-11-06 13:28:15 -06:00
const user = db.get(`user_${username}`);
if (!user) {
2024-11-06 13:18:37 -06:00
return res.status(404).json({ error: 'User not found' });
}
2024-11-06 13:28:15 -06:00
// Fetch posts from this users outbox (from the DB)
const userPosts = Object.values(db.get("posts") || {}).filter(post => post.author.username === username);
2024-11-06 13:18:37 -06:00
res.json(userPosts);
});
app.use(activityPubRouter.router);
// Start the server
app.listen(port, () => {
2024-11-06 13:28:15 -06:00
console.log(`Federated social network with SQLite (quick.db) running at http://localhost:${port}`);
2024-11-06 13:18:37 -06:00
});