File size: 3,723 Bytes
3bc8a85
 
 
 
ee76c0c
 
60d1a19
ee76c0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60d1a19
ee76c0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60d1a19
14b551c
 
 
 
 
 
 
 
 
 
 
 
 
60d1a19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14b551c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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/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 => id !== user_id);
        await federation.save();

        res.json({ success: true, message: `User ${user_id} unbanned from federation ${name}.` });
    } 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;

    const federation = await Federation.findOne({ uuid: federation_uuid });
    if (!federation) return res.status(404).json({ error: "Federation not found" });

    if (!federation.banned_users.includes(user_id)) {
      federation.banned_users.push(user_id);
      await federation.save();
    }

    const subFeds = await Federation.find({ uuid: { $in: federation.sub_federations } });
    for (const subFed of subFeds) {
      if (!subFed.banned_users.includes(user_id)) {
        subFed.banned_users.push(user_id);
        await subFed.save();
      }
    }

    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 };