Spaces:
Paused
Paused
import express from 'express'; | |
import axios from 'axios'; | |
import { authenticateApiKey, apiLimiter } from '../middleware/midware.js'; | |
import { Federation } from '../models.js'; | |
const FedsRoutes = express.Router(); | |
/** | |
* @swagger | |
* /api/v2/federation/newfed: | |
* post: | |
* summary: Create a new federation | |
* tags: [Federation] | |
* description: Creates a new federation with a unique name and owner. | |
* security: | |
* - apiKeyAuth: [] | |
* requestBody: | |
* required: true | |
* content: | |
* application/json: | |
* schema: | |
* type: object | |
* properties: | |
* name: | |
* type: string | |
* description: The unique name of the federation. | |
* owner: | |
* type: integer | |
* description: The user ID of the federation owner. | |
* parameters: | |
* - in: header | |
* name: x-api-key | |
* required: true | |
* description: API key for authentication. | |
* schema: | |
* type: string | |
* responses: | |
* 200: | |
* description: Federation created successfully. | |
* content: | |
* application/json: | |
* schema: | |
* type: object | |
* properties: | |
* message: | |
* type: string | |
* example: Federation created successfully | |
* federation: | |
* type: object | |
* properties: | |
* name: | |
* type: string | |
* owner: | |
* type: integer | |
* banned_users: | |
* type: array | |
* items: | |
* type: integer | |
* sub_federations: | |
* type: array | |
* items: | |
* type: string | |
* 400: | |
* description: Federation already exists or missing parameters. | |
* 500: | |
* description: Internal server error. | |
*/ | |
FedsRoutes.post("/api/v2/federation/newfed", authenticateApiKey, apiLimiter, async (req, res) => { | |
try { | |
const { name, owner } = req.body; | |
const existing = await Federation.findOne({ name }); | |
if (existing) return res.status(400).json({ error: "Federation already exists" }); | |
const federation = new Federation({ name, owner, banned_users: [], sub_federations: [] }); | |
await federation.save(); | |
res.json({ message: "Federation created successfully", federation }); | |
} catch (err) { | |
res.status(500).json({ error: err.message }); | |
} | |
}); | |
FedsRoutes.post("/api/v2/federation/subfed", authenticateApiKey, apiLimiter, async (req, res) => { | |
try { | |
const { parent_uuid, child_uuid } = req.body; | |
const parent = await Federation.findOne({ uuid: parent_uuid }); | |
const child = await Federation.findOne({ uuid: child_uuid }); | |
if (!parent || !child) return res.status(404).json({ error: "Federation not found" }); | |
if (!parent.sub_federations.includes(child.uuid)) { | |
parent.sub_federations.push(child.uuid); | |
await parent.save(); | |
} | |
res.json({ message: `Federation ${child.name} is now a sub-federation of ${parent.name}` }); | |
} catch (err) { | |
res.status(500).json({ error: err.message }); | |
} | |
}); | |
FedsRoutes.get("/api/v1/federation/getfed/:uuid", authenticateApiKey, apiLimiter, async (req, res) => { | |
try { | |
const federation = await Federation.findOne({ uuid: req.params.uuid }); | |
if (!federation) return res.status(404).json({ error: "Federation not found" }); | |
const subFeds = await Federation.find({ uuid: { $in: federation.sub_federations } }); | |
res.json({ federation, sub_federations: subFeds }); | |
} catch (err) { | |
res.status(500).json({ error: err.message }); | |
} | |
}); | |
FedsRoutes.post('/api/v2/federation/unban', authenticateApiKey, async (req, res) => { | |
try { | |
const { name, user_id } = req.body; | |
if (!name || !user_id || isNaN(user_id)) { | |
return res.status(400).json({ error: "Federation name and valid user ID required" }); | |
} | |
const federation = await Federation.findOne({ name }); | |
if (!federation) { | |
return res.status(404).json({ error: "Federation not found." }); | |
} | |
federation.banned_users = federation.banned_users.filter(id => Number(id) !== Number(user_id)); | |
await federation.save(); | |
await Federation.updateMany( | |
{ uuid: { $in: federation.sub_federations } }, | |
{ $pull: { banned_users: Number(user_id) } } | |
); | |
res.json({ success: true, message: `User ${user_id} unbanned from ${name} and its sub-federations.` }); | |
} catch (error) { | |
res.status(500).json({ error: `Failed to unban user: ${error.message}` }); | |
} | |
}); | |
FedsRoutes.post("/api/v2/federation/ban", authenticateApiKey, apiLimiter, async (req, res) => { | |
try { | |
const { federation_uuid, user_id } = req.body; | |
if (!federation_uuid || !user_id || isNaN(user_id)) { | |
return res.status(400).json({ error: "Federation UUID and valid user ID required" }); | |
} | |
const federation = await Federation.findOne({ uuid: federation_uuid }); | |
if (!federation) return res.status(404).json({ error: "Federation not found" }); | |
if (!federation.banned_users.includes(Number(user_id))) { | |
federation.banned_users.push(Number(user_id)); | |
await federation.save(); | |
} | |
await Federation.updateMany( | |
{ uuid: { $in: federation.sub_federations } }, | |
{ $addToSet: { banned_users: Number(user_id) } } | |
); | |
res.json({ message: `User ${user_id} banned in ${federation.name} and all its sub-federations.` }); | |
} catch (err) { | |
res.status(500).json({ error: err.message }); | |
} | |
}); | |
export { FedsRoutes }; |