Update index.js
This commit is contained in:
parent
e7cee49491
commit
b12a909443
1 changed files with 35 additions and 28 deletions
63
index.js
63
index.js
|
@ -4,6 +4,7 @@ const bcrypt = require('bcryptjs');
|
||||||
const jwt = require('jsonwebtoken');
|
const jwt = require('jsonwebtoken');
|
||||||
const { ActivityPubRouter, createActor } = require('activitypub-express');
|
const { ActivityPubRouter, createActor } = require('activitypub-express');
|
||||||
const uuid = require('uuid');
|
const uuid = require('uuid');
|
||||||
|
const db = require('quick.db'); // Import quick.db
|
||||||
const fetch = require('node-fetch');
|
const fetch = require('node-fetch');
|
||||||
|
|
||||||
const app = express();
|
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
|
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
|
// Middleware to parse JSON
|
||||||
app.use(bodyParser.json());
|
app.use(bodyParser.json());
|
||||||
|
|
||||||
|
@ -47,13 +43,16 @@ const authenticate = (req, res, next) => {
|
||||||
app.post('/users', async (req, res) => {
|
app.post('/users', async (req, res) => {
|
||||||
const { username, displayName, password } = req.body;
|
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' });
|
return res.status(400).json({ error: 'User already exists' });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hash the password before saving
|
// Hash the password before saving
|
||||||
const hashedPassword = await bcrypt.hash(password, 10);
|
const hashedPassword = await bcrypt.hash(password, 10);
|
||||||
|
|
||||||
|
// Create the actor and store it in the DB
|
||||||
const actor = createActor({
|
const actor = createActor({
|
||||||
id: actorURL(username),
|
id: actorURL(username),
|
||||||
username,
|
username,
|
||||||
|
@ -64,7 +63,9 @@ app.post('/users', async (req, res) => {
|
||||||
following: `http://localhost:${port}/users/${username}/following`,
|
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);
|
res.status(201).json(actor);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -72,7 +73,8 @@ app.post('/users', async (req, res) => {
|
||||||
app.post('/login', async (req, res) => {
|
app.post('/login', async (req, res) => {
|
||||||
const { username, password } = req.body;
|
const { username, password } = req.body;
|
||||||
|
|
||||||
const user = users[username];
|
// Retrieve user from the database
|
||||||
|
const user = db.get(`user_${username}`);
|
||||||
if (!user) {
|
if (!user) {
|
||||||
return res.status(404).json({ error: 'User not found' });
|
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' });
|
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' });
|
return res.status(404).json({ error: 'User not found' });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create a post and store it in the database
|
||||||
const post = {
|
const post = {
|
||||||
id: `http://localhost:${port}/posts/${uuid.v4()}`,
|
id: `http://localhost:${port}/posts/${uuid.v4()}`,
|
||||||
type: 'Note',
|
type: 'Note',
|
||||||
content,
|
content,
|
||||||
author: users[username],
|
author: user,
|
||||||
published: new Date().toISOString(),
|
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);
|
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' });
|
return res.status(403).json({ error: 'You can only view your own posts' });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!users[username]) {
|
// Retrieve all posts from the database
|
||||||
return res.status(404).json({ error: 'User not found' });
|
const userPosts = Object.values(db.get("posts") || {}).filter(post => post.author.username === username);
|
||||||
}
|
|
||||||
|
|
||||||
const userPosts = Object.values(posts).filter(post => post.author.username === username);
|
|
||||||
res.json(userPosts);
|
res.json(userPosts);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -137,18 +141,19 @@ activityPubRouter.post('/users/:username/inbox', authenticate, async (req, res)
|
||||||
const { username } = req.params;
|
const { username } = req.params;
|
||||||
const activity = req.body;
|
const activity = req.body;
|
||||||
|
|
||||||
if (!users[username]) {
|
if (!db.get(`user_${username}`)) {
|
||||||
return res.status(404).json({ error: 'User not found' });
|
return res.status(404).json({ error: 'User not found' });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (activity.type === 'Follow') {
|
if (activity.type === 'Follow') {
|
||||||
const actor = activity.actor;
|
const actor = activity.actor;
|
||||||
|
|
||||||
// Add follower to the user’s list of followers
|
// Retrieve followers list and add new follower
|
||||||
if (!followers[username]) {
|
const followersList = db.get(`followers_${username}`) || [];
|
||||||
followers[username] = [];
|
followersList.push(actor.id);
|
||||||
}
|
|
||||||
followers[username].push(actor.id);
|
// Store the updated followers list in the database
|
||||||
|
db.set(`followers_${username}`, followersList);
|
||||||
|
|
||||||
res.status(200).json({ status: 'Followed successfully' });
|
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) => {
|
activityPubRouter.get('/users/:username', (req, res) => {
|
||||||
const { username } = req.params;
|
const { username } = req.params;
|
||||||
|
|
||||||
if (!users[username]) {
|
const user = db.get(`user_${username}`);
|
||||||
|
if (!user) {
|
||||||
return res.status(404).json({ error: 'User not found' });
|
return res.status(404).json({ error: 'User not found' });
|
||||||
}
|
}
|
||||||
|
|
||||||
res.json(users[username]);
|
res.json(user);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Outbox endpoint to send posts
|
// Outbox endpoint to send posts
|
||||||
activityPubRouter.get('/users/:username/outbox', (req, res) => {
|
activityPubRouter.get('/users/:username/outbox', (req, res) => {
|
||||||
const { username } = req.params;
|
const { username } = req.params;
|
||||||
|
|
||||||
if (!users[username]) {
|
const user = db.get(`user_${username}`);
|
||||||
|
if (!user) {
|
||||||
return res.status(404).json({ error: 'User not found' });
|
return res.status(404).json({ error: 'User not found' });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch posts from this user’s outbox
|
// Fetch posts from this user’s outbox (from the DB)
|
||||||
const userPosts = Object.values(posts).filter(post => post.author.username === username);
|
const userPosts = Object.values(db.get("posts") || {}).filter(post => post.author.username === username);
|
||||||
res.json(userPosts);
|
res.json(userPosts);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -187,5 +194,5 @@ app.use(activityPubRouter.router);
|
||||||
|
|
||||||
// Start the server
|
// Start the server
|
||||||
app.listen(port, () => {
|
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}`);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue