File size: 1,457 Bytes
3bc8a85
 
 
 
ee76c0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import express from 'express';
import axios from 'axios';
import { authenticateApiKey, apiLimiter } from '../middleware/midware.js';
import { Federation } from '../models.js';
const FedsRoutes = express.Router();

FedsRoutes.post("/api/v1/fed/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/v1/fed/subfed", 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 });
  }
});

export { FedsRoutes };