Update plugins/federations.js
Browse files- plugins/federations.js +39 -1
plugins/federations.js
CHANGED
@@ -2,4 +2,42 @@ import express from 'express';
|
|
2 |
import axios from 'axios';
|
3 |
import { authenticateApiKey, apiLimiter } from '../middleware/midware.js';
|
4 |
import { Federation } from '../models.js';
|
5 |
-
const FedsRoutes = express.Router();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import axios from 'axios';
|
3 |
import { authenticateApiKey, apiLimiter } from '../middleware/midware.js';
|
4 |
import { Federation } from '../models.js';
|
5 |
+
const FedsRoutes = express.Router();
|
6 |
+
|
7 |
+
FedsRoutes.post("/api/v1/fed/newfed", authenticateApiKey, apiLimiter, async (req, res) => {
|
8 |
+
try {
|
9 |
+
const { name, owner } = req.body;
|
10 |
+
|
11 |
+
const existing = await Federation.findOne({ name });
|
12 |
+
if (existing) return res.status(400).json({ error: "Federation already exists" });
|
13 |
+
|
14 |
+
const federation = new Federation({ name, owner, banned_users: [], sub_federations: [] });
|
15 |
+
await federation.save();
|
16 |
+
|
17 |
+
res.json({ message: "Federation created successfully", federation });
|
18 |
+
} catch (err) {
|
19 |
+
res.status(500).json({ error: err.message });
|
20 |
+
}
|
21 |
+
});
|
22 |
+
|
23 |
+
FedsRoutes.post("/api/v1/fed/subfed", async (req, res) => {
|
24 |
+
try {
|
25 |
+
const { parent_uuid, child_uuid } = req.body;
|
26 |
+
|
27 |
+
const parent = await Federation.findOne({ uuid: parent_uuid });
|
28 |
+
const child = await Federation.findOne({ uuid: child_uuid });
|
29 |
+
|
30 |
+
if (!parent || !child) return res.status(404).json({ error: "Federation not found" });
|
31 |
+
|
32 |
+
if (!parent.sub_federations.includes(child.uuid)) {
|
33 |
+
parent.sub_federations.push(child.uuid);
|
34 |
+
await parent.save();
|
35 |
+
}
|
36 |
+
|
37 |
+
res.json({ message: `Federation ${child.name} is now a sub-federation of ${parent.name}` });
|
38 |
+
} catch (err) {
|
39 |
+
res.status(500).json({ error: err.message });
|
40 |
+
}
|
41 |
+
});
|
42 |
+
|
43 |
+
export { FedsRoutes };
|