forked from tdjsnelling/sqtracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoderation.js
More file actions
232 lines (209 loc) · 5.63 KB
/
Copy pathmoderation.js
File metadata and controls
232 lines (209 loc) · 5.63 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import Report from "../schema/report";
import Torrent from "../schema/torrent";
import User from "../schema/user";
import Progress from "../schema/progress";
import Invite from "../schema/invite";
import Request from "../schema/request";
import Comment from "../schema/comment";
export const createReport = async (req, res, next) => {
if (req.body.reason) {
try {
const torrent = await Torrent.findOne({
infoHash: req.params.infoHash,
}).lean();
if (!torrent) {
res.status(404).send("Torrent with that info hash does not exist");
return;
}
const report = new Report({
torrent: torrent._id,
reportedBy: req.userId,
reason: req.body.reason,
solved: false,
created: Date.now(),
});
await report.save();
res.sendStatus(200);
} catch (e) {
next(e);
}
} else {
res.status(400).send("Request must include reason");
}
};
export const fetchReport = async (req, res, next) => {
try {
if (req.userRole !== "admin") {
res.status(401).send("You do not have permission to view a report");
return;
}
const report = await Report.findOne({ _id: req.params.reportId }).lean();
if (!report) {
res.status(404).send("Report could not be found");
return;
}
report.reportedBy = await User.findOne({ _id: report.reportedBy }).select(
"username created"
);
report.torrent = await Torrent.findOne({ _id: report.torrent }).select(
"name description infoHash created"
);
res.json(report);
} catch (e) {
next(e);
}
};
export const getReports = async (req, res, next) => {
const pageSize = 25;
try {
if (req.userRole !== "admin") {
res.status(401).send("You do not have permission to view reports");
return;
}
let { page } = req.query;
page = parseInt(page) || 0;
const reports = await Report.aggregate([
{
$match: { solved: false },
},
{
$sort: { created: -1 },
},
{
$skip: page * pageSize,
},
{
$limit: pageSize,
},
{
$lookup: {
from: "users",
as: "reportedBy",
let: { userId: "$reportedBy" },
pipeline: [
{
$match: { $expr: { $eq: ["$_id", "$$userId"] } },
},
{
$project: {
username: 1,
},
},
],
},
},
{
$lookup: {
from: "torrents",
as: "torrent",
let: { torrentId: "$torrent" },
pipeline: [
{
$match: { $expr: { $eq: ["$_id", "$$torrentId"] } },
},
{
$project: {
name: 1,
},
},
],
},
},
{
$unwind: {
path: "$reportedBy",
preserveNullAndEmptyArrays: true,
},
},
{
$unwind: {
path: "$torrent",
preserveNullAndEmptyArrays: true,
},
},
]);
res.json(reports);
} catch (e) {
next(e);
}
};
export const setReportResolved = async (req, res, next) => {
try {
if (req.userRole !== "admin") {
res.status(401).send("You do not have permission to resolve a report");
return;
}
await Report.findOneAndUpdate(
{ _id: req.params.reportId },
{ $set: { solved: true } }
);
res.sendStatus(200);
} catch (e) {
next(e);
}
};
export const getStats = (tracker) => async (req, res, next) => {
try {
if (req.userRole !== "admin") {
res.status(401).send("You do not have permission to view tracker stats");
return;
}
const registeredUsers = await User.countDocuments();
const bannedUsers = await User.countDocuments({ banned: true });
const uploadedTorrents = await Torrent.countDocuments();
const completedDownloads = await Progress.countDocuments({ left: 0 });
const totalInvitesSent = await Invite.countDocuments();
const invitesAccepted = await Invite.countDocuments({ claimed: true });
const totalRequests = await Request.countDocuments({});
const filledRequests = await Request.countDocuments({
fulfilledBy: { $exists: true },
});
const totalComments = await Comment.countDocuments();
const allPeers = {};
let activeTorrents = 0;
Object.keys(tracker.torrents).forEach((infoHash) => {
const { peers } = tracker.torrents[infoHash];
const keys = peers.keys;
if (keys.length > 0) activeTorrents++;
keys.forEach((peerId) => {
// Don't mark the peer as most recently used for stats
const peer = peers.peek(peerId);
if (peer == null) return; // peers.peek() can evict the peer
if (!allPeers[peerId]) {
allPeers[peerId] = {
seeder: false,
leecher: false,
};
}
if (peer.complete) {
allPeers[peerId].seeder = true;
} else {
allPeers[peerId].leecher = true;
}
allPeers[peerId].peerId = peer.peerId;
});
});
res.json({
registeredUsers,
bannedUsers,
uploadedTorrents,
completedDownloads,
totalInvitesSent,
invitesAccepted,
totalRequests,
filledRequests,
totalComments,
activeTorrents,
peers: Object.keys(allPeers).length,
seeders: Object.values(allPeers).filter(
(peer) => peer.seeder && !peer.leecher
).length,
leechers: Object.values(allPeers).filter(
(peer) => peer.leecher && !peer.seeder
).length,
});
} catch (e) {
console.error(e);
next(e);
}
};