diff --git a/index.js b/index.js index 7039eb5..8462f52 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,7 @@ const bcrypt = require('bcryptjs'); const jwt = require('jsonwebtoken'); const { ActivityPubRouter, createActor } = require('activitypub-express'); const uuid = require('uuid'); +const db = require('quick.db'); // Import quick.db const fetch = require('node-fetch'); const app = express(); @@ -11,11 +12,6 @@ const port = 3000; const SECRET_KEY = 'your_secret_key'; // Replace with a secure key in a real app -// In-memory "database" for users and posts -const users = {}; -const posts = {}; -const followers = {}; - // Middleware to parse JSON app.use(bodyParser.json()); @@ -47,13 +43,16 @@ const authenticate = (req, res, next) => { app.post('/users', async (req, res) => { const { username, displayName, password } = req.body; - if (users[username]) { + // Check if user already exists + const existingUser = db.get(`user_${username}`); + if (existingUser) { return res.status(400).json({ error: 'User already exists' }); } // Hash the password before saving const hashedPassword = await bcrypt.hash(password, 10); + // Create the actor and store it in the DB const actor = createActor({ id: actorURL(username), username, @@ -64,7 +63,9 @@ app.post('/users', async (req, res) => { following: `http://localhost:${port}/users/${username}/following`, }); - users[username] = { ...actor, password: hashedPassword }; + // Store actor info and password in the database + db.set(`user_${username}`, { ...actor, password: hashedPassword }); + res.status(201).json(actor); }); @@ -72,7 +73,8 @@ app.post('/users', async (req, res) => { app.post('/login', async (req, res) => { const { username, password } = req.body; - const user = users[username]; + // Retrieve user from the database + const user = db.get(`user_${username}`); if (!user) { return res.status(404).json({ error: 'User not found' }); } @@ -97,19 +99,24 @@ app.post('/users/:username/posts', authenticate, (req, res) => { return res.status(403).json({ error: 'You can only post as yourself' }); } - if (!users[username]) { + // Retrieve the user from the database + const user = db.get(`user_${username}`); + if (!user) { return res.status(404).json({ error: 'User not found' }); } + // Create a post and store it in the database const post = { id: `http://localhost:${port}/posts/${uuid.v4()}`, type: 'Note', content, - author: users[username], + author: user, published: new Date().toISOString(), }; - posts[post.id] = post; + // Store the post in the database + db.set(`post_${post.id}`, post); + res.status(201).json(post); }); @@ -121,11 +128,8 @@ app.get('/users/:username/posts', authenticate, (req, res) => { return res.status(403).json({ error: 'You can only view your own posts' }); } - if (!users[username]) { - return res.status(404).json({ error: 'User not found' }); - } - - const userPosts = Object.values(posts).filter(post => post.author.username === username); + // Retrieve all posts from the database + const userPosts = Object.values(db.get("posts") || {}).filter(post => post.author.username === username); res.json(userPosts); }); @@ -137,18 +141,19 @@ activityPubRouter.post('/users/:username/inbox', authenticate, async (req, res) const { username } = req.params; const activity = req.body; - if (!users[username]) { + if (!db.get(`user_${username}`)) { return res.status(404).json({ error: 'User not found' }); } if (activity.type === 'Follow') { const actor = activity.actor; - // Add follower to the user’s list of followers - if (!followers[username]) { - followers[username] = []; - } - followers[username].push(actor.id); + // 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); res.status(200).json({ status: 'Followed successfully' }); @@ -163,23 +168,25 @@ activityPubRouter.post('/users/:username/inbox', authenticate, async (req, res) activityPubRouter.get('/users/:username', (req, res) => { const { username } = req.params; - if (!users[username]) { + const user = db.get(`user_${username}`); + if (!user) { return res.status(404).json({ error: 'User not found' }); } - res.json(users[username]); + res.json(user); }); // Outbox endpoint to send posts activityPubRouter.get('/users/:username/outbox', (req, res) => { const { username } = req.params; - if (!users[username]) { + const user = db.get(`user_${username}`); + if (!user) { return res.status(404).json({ error: 'User not found' }); } - // Fetch posts from this user’s outbox - const userPosts = Object.values(posts).filter(post => post.author.username === username); + // Fetch posts from this user’s outbox (from the DB) + const userPosts = Object.values(db.get("posts") || {}).filter(post => post.author.username === username); res.json(userPosts); }); @@ -187,5 +194,5 @@ app.use(activityPubRouter.router); // Start the server app.listen(port, () => { - console.log(`Federated social network with authentication running at http://localhost:${port}`); + console.log(`Federated social network with SQLite (quick.db) running at http://localhost:${port}`); });