@@ -5,6 +5,8 @@ import Torrent from '../schema/torrent'
55import Progress from '../schema/progress'
66import { getUserRatio } from '../utils/ratio'
77
8+ const BYTES_GB = 1.074e9
9+
810export const binaryToHex = ( b ) => Buffer . from ( b , 'binary' ) . toString ( 'hex' )
911export const hexToBinary = ( h ) => Buffer . from ( h , 'hex' ) . toString ( 'binary' )
1012
@@ -71,6 +73,44 @@ const handleAnnounce = async (req, res, next) => {
7173 return
7274 }
7375
76+ /*
77+ Bonus points: determine how much the user has uploaded, and whether or not this announce will take their total
78+ uploaded amount into the next whole GiB. If so, increment the users bonus points by the configured amount.
79+ */
80+
81+ const [ sumUploaded ] = await Progress . aggregate ( [
82+ {
83+ $match : {
84+ userId : user . _id ,
85+ } ,
86+ } ,
87+ {
88+ $group : {
89+ _id : 'uploaded' ,
90+ bytes : { $sum : '$uploaded' } ,
91+ } ,
92+ } ,
93+ ] )
94+
95+ const { bytes } = sumUploaded ?? { bytes : 0 }
96+ const nextGb = Math . max ( Math . ceil ( bytes / BYTES_GB ) , 1 )
97+
98+ const prevProgressRecord = await Progress . findOne ( {
99+ userId : user . _id ,
100+ infoHash,
101+ } ) . lean ( )
102+ const alreadyUploaded = prevProgressRecord ?. uploaded ?? 0
103+ const uploadDelta = params . uploaded - alreadyUploaded
104+
105+ if ( ( bytes + uploadDelta ) / BYTES_GB >= nextGb ) {
106+ await User . findOneAndUpdate (
107+ { _id : user . _id } ,
108+ { $inc : { bonusPoints : process . env . SQ_BP_PER_GB } }
109+ )
110+ }
111+
112+ // update the progress report for this user/torrent pair
113+
74114 await Progress . findOneAndUpdate (
75115 { userId : user . _id , infoHash } ,
76116 {
@@ -80,7 +120,7 @@ const handleAnnounce = async (req, res, next) => {
80120 uploaded : params . uploaded ,
81121 downloaded :
82122 torrent . freeleech || process . env . SQ_SITE_WIDE_FREELEECH === true
83- ? 0
123+ ? prevProgressRecord ?. downloaded ?? 0
84124 : params . downloaded ,
85125 left : params . left ,
86126 } ,
0 commit comments