Skip to content

Commit 9437ef6

Browse files
committed
accepting of candidates on request
1 parent ee5cecc commit 9437ef6

5 files changed

Lines changed: 317 additions & 6 deletions

File tree

api/src/controllers/request.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Request from '../schema/request'
22
import Comment from '../schema/comment'
3+
import Torrent from '../schema/torrent'
34

45
export const createRequest = async (req, res) => {
56
if (req.body.title && req.body.body) {
@@ -148,13 +149,38 @@ export const fetchRequest = async (req, res) => {
148149
],
149150
},
150151
},
152+
{
153+
$lookup: {
154+
from: 'torrents',
155+
as: 'candidates',
156+
let: { torrentIds: '$candidates' },
157+
pipeline: [
158+
{ $match: { $expr: { $in: ['$_id', '$$torrentIds'] } } },
159+
{
160+
$project: {
161+
infoHash: 1,
162+
name: 1,
163+
type: 1,
164+
created: 1,
165+
},
166+
},
167+
{
168+
$unwind: {
169+
path: '$candidates',
170+
preserveNullAndEmptyArrays: true,
171+
},
172+
},
173+
],
174+
},
175+
},
151176
])
152177
if (!request) {
153178
res.status(404).send('Request could not be found')
154179
return
155180
}
156181
res.send(request)
157182
} catch (e) {
183+
console.error(e)
158184
res.status(500).send(e.message)
159185
}
160186
}
@@ -206,3 +232,84 @@ export const addComment = async (req, res) => {
206232
res.status(400).send('Request must include comment')
207233
}
208234
}
235+
236+
export const addCandidate = async (req, res) => {
237+
if (req.body.infoHash) {
238+
try {
239+
const request = await Request.findOne({
240+
_id: req.params.requestId,
241+
}).lean()
242+
243+
const torrent = await Torrent.findOne(
244+
{
245+
infoHash: req.body.infoHash,
246+
},
247+
{ infoHash: 1, name: 1, type: 1, created: 1 }
248+
).lean()
249+
250+
if (!torrent) {
251+
res.status(404).send('Torrent does not exist')
252+
return
253+
}
254+
255+
if (request.candidates.includes(torrent._id)) {
256+
res.status(409).send('Torrent has already been suggested')
257+
return
258+
}
259+
260+
await Request.findOneAndUpdate(
261+
{ _id: req.params.requestId },
262+
{ $addToSet: { candidates: torrent._id } }
263+
)
264+
265+
res.status(200).send({ torrent })
266+
} catch (err) {
267+
res.status(500).send(err.message)
268+
}
269+
} else {
270+
res.status(400).send('Request must include infoHash')
271+
}
272+
}
273+
274+
export const acceptCandidate = async (req, res) => {
275+
if (req.body.infoHash) {
276+
try {
277+
const request = await Request.findOne({
278+
_id: req.params.requestId,
279+
}).lean()
280+
281+
if (req.userId.toString() !== request.createdBy.toString()) {
282+
res
283+
.status(401)
284+
.send('You do not have permission to accept that suggestion')
285+
return
286+
}
287+
288+
const torrent = await Torrent.findOne({
289+
infoHash: req.body.infoHash,
290+
}).lean()
291+
292+
if (
293+
!request.candidates.some(
294+
(candidate) => candidate.toString() === torrent._id.toString()
295+
)
296+
) {
297+
res
298+
.status(403)
299+
.send('Cannot accept a torrent that has not been suggested')
300+
return
301+
}
302+
303+
await Request.findOneAndUpdate(
304+
{ _id: req.params.requestId },
305+
{ $set: { fulfilledBy: torrent._id } }
306+
)
307+
308+
res.status(200).send({ torrent: torrent._id })
309+
} catch (e) {
310+
res.status(500).send(e.message)
311+
}
312+
} else {
313+
res.status(400).send('Request must include infoHash')
314+
}
315+
}

api/src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ import {
6161
fetchRequest,
6262
deleteRequest,
6363
addComment as addCommentRequest,
64+
addCandidate,
65+
acceptCandidate,
6466
} from './controllers/request'
6567
import validateConfig from './utils/validateConfig'
6668
import createAdminUser from './setup/createAdminUser'
@@ -202,6 +204,8 @@ app.get('/requests/page/:page', getRequests)
202204
app.get('/requests/:index', fetchRequest)
203205
app.delete('/requests/:index', deleteRequest)
204206
app.post('/requests/comment/:requestId', addCommentRequest)
207+
app.post('/requests/suggest/:requestId', addCandidate)
208+
app.post('/requests/accept/:requestId', acceptCandidate)
205209

206210
const port = process.env.SQ_PORT || 3001
207211
app.listen(port, () => {

client/components/Button.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import css from '@styled-system/css'
44
import { darken, lighten, getLuminance } from 'polished'
55

66
const StyledButton = styled.button(
7-
({ theme }) =>
7+
({ theme, small }) =>
88
css({
99
appearance: 'none',
1010
bg: 'primary',
@@ -15,7 +15,7 @@ const StyledButton = styled.button(
1515
fontFamily: 'body',
1616
fontSize: 2,
1717
px: 4,
18-
py: 3,
18+
py: !small ? 3 : 2,
1919
cursor: 'pointer',
2020
whiteSpace: 'nowrap',
2121
'&:hover': {

client/components/List.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,17 @@ const List = ({ data = [], columns = [], ...rest }) => {
8585
]}
8686
gridGap={[2, 4]}
8787
alignItems="center"
88-
p={4}
88+
minHeight="50px"
89+
px={4}
8990
>
9091
{columns.map((col, j) => (
9192
<Box
9293
key={`list-row-${i}-col-${j}`}
94+
width="100%"
95+
display="flex"
96+
alignItems="center"
9397
textAlign={col.rightAlign ? ['left', 'right'] : 'left'}
94-
_css={{ whiteSpace: 'nowrap' }}
98+
_css={{ whiteSpace: 'nowrap', '> *': { width: '100%' } }}
9599
>
96100
{col.cell({
97101
value: col.accessor ? getIn(row, col.accessor) : null,

0 commit comments

Comments
 (0)