randydev commited on
Commit
398b28a
·
verified ·
1 Parent(s): 60d1a19

Update plugins/federations.js

Browse files
Files changed (1) hide show
  1. plugins/federations.js +30 -21
plugins/federations.js CHANGED
@@ -68,39 +68,48 @@ FedsRoutes.post('/api/v2/federation/unban', authenticateApiKey, async (req, res)
68
  return res.status(404).json({ error: "Federation not found." });
69
  }
70
 
71
- federation.banned_users = federation.banned_users.filter(id => id !== user_id);
72
  await federation.save();
73
 
74
- res.json({ success: true, message: `User ${user_id} unbanned from federation ${name}.` });
 
 
 
 
 
75
  } catch (error) {
76
  res.status(500).json({ error: `Failed to unban user: ${error.message}` });
77
  }
78
  });
79
 
 
80
  FedsRoutes.post("/api/v2/federation/ban", authenticateApiKey, apiLimiter, async (req, res) => {
81
- try {
82
- const { federation_uuid, user_id } = req.body;
83
 
84
- const federation = await Federation.findOne({ uuid: federation_uuid });
85
- if (!federation) return res.status(404).json({ error: "Federation not found" });
 
86
 
87
- if (!federation.banned_users.includes(user_id)) {
88
- federation.banned_users.push(user_id);
89
- await federation.save();
90
- }
91
 
92
- const subFeds = await Federation.find({ uuid: { $in: federation.sub_federations } });
93
- for (const subFed of subFeds) {
94
- if (!subFed.banned_users.includes(user_id)) {
95
- subFed.banned_users.push(user_id);
96
- await subFed.save();
97
- }
98
- }
99
 
100
- res.json({ message: `User ${user_id} banned in ${federation.name} and all its sub-federations` });
101
- } catch (err) {
102
- res.status(500).json({ error: err.message });
103
- }
 
 
 
 
 
 
 
 
 
 
104
  });
105
 
 
106
  export { FedsRoutes };
 
68
  return res.status(404).json({ error: "Federation not found." });
69
  }
70
 
71
+ federation.banned_users = federation.banned_users.filter(id => Number(id) !== Number(user_id));
72
  await federation.save();
73
 
74
+ await Federation.updateMany(
75
+ { uuid: { $in: federation.sub_federations } },
76
+ { $pull: { banned_users: Number(user_id) } }
77
+ );
78
+
79
+ res.json({ success: true, message: `User ${user_id} unbanned from ${name} and its sub-federations.` });
80
  } catch (error) {
81
  res.status(500).json({ error: `Failed to unban user: ${error.message}` });
82
  }
83
  });
84
 
85
+
86
  FedsRoutes.post("/api/v2/federation/ban", authenticateApiKey, apiLimiter, async (req, res) => {
87
+ try {
88
+ const { federation_uuid, user_id } = req.body;
89
 
90
+ if (!federation_uuid || !user_id || isNaN(user_id)) {
91
+ return res.status(400).json({ error: "Federation UUID and valid user ID required" });
92
+ }
93
 
94
+ const federation = await Federation.findOne({ uuid: federation_uuid });
 
 
 
95
 
96
+ if (!federation) return res.status(404).json({ error: "Federation not found" });
 
 
 
 
 
 
97
 
98
+ if (!federation.banned_users.includes(Number(user_id))) {
99
+ federation.banned_users.push(Number(user_id));
100
+ await federation.save();
101
+ }
102
+
103
+ await Federation.updateMany(
104
+ { uuid: { $in: federation.sub_federations } },
105
+ { $addToSet: { banned_users: Number(user_id) } }
106
+ );
107
+
108
+ res.json({ message: `User ${user_id} banned in ${federation.name} and all its sub-federations.` });
109
+ } catch (err) {
110
+ res.status(500).json({ error: err.message });
111
+ }
112
  });
113
 
114
+
115
  export { FedsRoutes };