Skip to content

Commit 426a6f3

Browse files
committed
awards bp for filled requests
1 parent 17f16ae commit 426a6f3

4 files changed

Lines changed: 42 additions & 10 deletions

File tree

api/src/controllers/request.js

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Request from "../schema/request";
22
import Comment from "../schema/comment";
33
import Torrent from "../schema/torrent";
4+
import User from "../schema/user";
45

56
export const createRequest = async (req, res, next) => {
67
if (req.body.title && req.body.body) {
@@ -153,7 +154,7 @@ export const fetchRequest = async (req, res, next) => {
153154
$lookup: {
154155
from: "torrents",
155156
as: "candidates",
156-
let: { torrentIds: "$candidates" },
157+
let: { torrentIds: "$candidates.torrent" },
157158
pipeline: [
158159
{ $match: { $expr: { $in: ["$_id", "$$torrentIds"] } } },
159160
{
@@ -254,7 +255,7 @@ export const addCandidate = async (req, res, next) => {
254255

255256
if (
256257
request.candidates
257-
.map((c) => c.toString())
258+
.map((c) => c.torrent.toString())
258259
.includes(torrent._id.toString())
259260
) {
260261
res.status(409).send("Torrent has already been suggested");
@@ -263,7 +264,11 @@ export const addCandidate = async (req, res, next) => {
263264

264265
await Request.findOneAndUpdate(
265266
{ _id: req.params.requestId },
266-
{ $addToSet: { candidates: torrent._id } }
267+
{
268+
$addToSet: {
269+
candidates: { torrent: torrent._id, suggestedBy: req.userId },
270+
},
271+
}
267272
);
268273

269274
res.status(200).send({ torrent });
@@ -293,11 +298,11 @@ export const acceptCandidate = async (req, res, next) => {
293298
infoHash: req.body.infoHash,
294299
}).lean();
295300

296-
if (
297-
!request.candidates.some(
298-
(candidate) => candidate.toString() === torrent._id.toString()
299-
)
300-
) {
301+
const candidate = request.candidates.find(
302+
(c) => c.torrent.toString() === torrent._id.toString()
303+
);
304+
305+
if (!candidate) {
301306
res
302307
.status(403)
303308
.send("Cannot accept a torrent that has not been suggested");
@@ -309,6 +314,20 @@ export const acceptCandidate = async (req, res, next) => {
309314
{ $set: { fulfilledBy: torrent._id } }
310315
);
311316

317+
await User.findOneAndUpdate(
318+
{ _id: candidate.suggestedBy },
319+
{
320+
$inc: {
321+
bonusPoints:
322+
process.env.SQ_BP_EARNED_PER_FILLED_REQUEST *
323+
(torrent.uploadedBy.toString() ===
324+
candidate.suggestedBy.toString()
325+
? 2
326+
: 1),
327+
},
328+
}
329+
);
330+
312331
res.status(200).send({ torrent: torrent._id });
313332
} catch (e) {
314333
next(e);

api/src/schema/request.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,16 @@ const Request = new mongoose.Schema({
66
body: String,
77
createdBy: mongoose.Schema.ObjectId,
88
created: Number,
9-
candidates: Array,
10-
fulfilledBy: mongoose.Schema.ObjectId,
9+
candidates: [
10+
{
11+
torrent: mongoose.Schema.ObjectId,
12+
suggestedBy: mongoose.Schema.ObjectId,
13+
},
14+
],
15+
fulfilledBy: {
16+
torrent: mongoose.Schema.ObjectId,
17+
suggestedBy: mongoose.Schema.ObjectId,
18+
},
1119
});
1220

1321
export default mongoose.model("request", Request);

api/src/utils/validateConfig.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const configSchema = yup
1717
SQ_ALLOW_ANONYMOUS_UPLOADS: yup.boolean().required(),
1818
SQ_MINIMUM_RATIO: yup.number().min(0).required(),
1919
SQ_BP_EARNED_PER_GB: yup.number().min(0).required(),
20+
SQ_BP_EARNED_PER_FILLED_REQUEST: yup.number().min(0).required(),
2021
SQ_BP_COST_PER_INVITE: yup.number().min(0).required(),
2122
SQ_BP_COST_PER_GB: yup.number().min(0).required(),
2223
SQ_SITE_WIDE_FREELEECH: yup.boolean().required(),

config.example.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ module.exports = {
4747
// Number of bonus points awarded to a user for each GB they upload. Minimum 0.
4848
SQ_BP_EARNED_PER_GB: 1,
4949

50+
// Number of bonus points awarded to a user if they suggest a torrent to fill a request and it gets accepted
51+
// They get double if they also the uploader of the accepted torrent
52+
SQ_BP_EARNED_PER_FILLED_REQUEST: 1,
53+
5054
// Number of bonus it costs a user to buy 1 invite (set to 0 to disable buying invites).
5155
SQ_BP_COST_PER_INVITE: 3,
5256

0 commit comments

Comments
 (0)