Skip to content

Commit 43601c1

Browse files
committed
adds source field to torrent
1 parent db2ff91 commit 43601c1

11 files changed

Lines changed: 118 additions & 39 deletions

File tree

api/src/controllers/torrent.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import slugify from 'slugify'
55
import Torrent from '../schema/torrent'
66
import User from '../schema/user'
77
import Comment from '../schema/comment'
8-
import { tracker } from '../index'
98

109
export const embellishTorrentsWithTrackerScrape = async (tracker, torrents) => {
1110
if (!torrents.length) return []
@@ -46,6 +45,23 @@ export const uploadTorrent = async (req, res) => {
4645
return
4746
}
4847

48+
if (process.env.SQ_TORRENT_CATEGORIES.length) {
49+
const sources =
50+
process.env.SQ_TORRENT_CATEGORIES[
51+
Object.keys(process.env.SQ_TORRENT_CATEGORIES).find(
52+
(cat) => slugify(cat, { lower: true }) === req.body.type
53+
)
54+
]
55+
if (
56+
!sources
57+
.map((source) => slugify(source, { lower: true }))
58+
.includes(req.body.source)
59+
) {
60+
res.status(400).send('Torrent must have a source')
61+
return
62+
}
63+
}
64+
4965
const user = await User.findOne({ _id: req.userId }).lean()
5066

5167
if (
@@ -87,6 +103,7 @@ export const uploadTorrent = async (req, res) => {
87103
name: req.body.name,
88104
description: req.body.description,
89105
type: req.body.type,
106+
source: req.body.source,
90107
infoHash,
91108
binary: req.body.torrent,
92109
uploadedBy: req.userId,
@@ -157,6 +174,7 @@ export const fetchTorrent = (tracker) => async (req, res) => {
157174
name: 1,
158175
description: 1,
159176
type: 1,
177+
source: 1,
160178
infoHash: 1,
161179
uploadedBy: 1,
162180
downloads: 1,
@@ -441,7 +459,7 @@ export const searchTorrents = (tracker) => async (req, res) => {
441459
try {
442460
const torrents = await getTorrentsPage({
443461
skip: page ? parseInt(page) : 0,
444-
query: decodeURIComponent(query),
462+
query: query ? decodeURIComponent(query) : undefined,
445463
category,
446464
tag,
447465
tracker,

api/src/schema/torrent.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const Torrent = new mongoose.Schema({
77
name: String,
88
description: String,
99
type: String,
10+
source: String,
1011
image: String,
1112
downloads: Number,
1213
anonymous: Boolean,

api/src/utils/validateConfig.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,24 @@ const configSchema = yup
2020
SQ_BP_COST_PER_INVITE: yup.number().min(0).required(),
2121
SQ_BP_COST_PER_GB: yup.number().min(0).required(),
2222
SQ_SITE_WIDE_FREELEECH: yup.boolean().required(),
23-
SQ_TORRENT_CATEGORIES: yup
24-
.array()
25-
.of(yup.string())
26-
.min(0)
27-
.test('items-unique', 'Categories must be unique', (value) =>
28-
value.every(
29-
(category) => value.filter((c) => c === category).length === 1
30-
)
31-
)
32-
.required(),
23+
SQ_TORRENT_CATEGORIES: yup.lazy((value) => {
24+
const entries = Object.keys(value).reduce((obj, key) => {
25+
obj[key] = yup
26+
.array()
27+
.of(yup.string())
28+
.min(0)
29+
.test(
30+
`${key}-items-unique`,
31+
`Sources in category "${key}" must be unique`,
32+
(value) =>
33+
value.every(
34+
(source) => value.filter((c) => c === source).length === 1
35+
)
36+
)
37+
return obj
38+
}, {})
39+
return yup.object(entries).required()
40+
}),
3341
SQ_BASE_URL: yup.string().matches(httpRegex).required(),
3442
SQ_API_URL: yup.string().matches(httpRegex).required(),
3543
SQ_MONGO_URL: yup.string().matches(mongoRegex).required(),

client/components/Navigation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ const Navigation = ({ isMobile, menuIsOpen, setMenuIsOpen }) => {
151151
<Home size={24} />
152152
</NavLink>
153153
</Link>
154-
{!!SQ_TORRENT_CATEGORIES.length && (
154+
{!!Object.keys(SQ_TORRENT_CATEGORIES).length && (
155155
<Link href="/categories" passHref>
156156
<NavLink>
157157
<Text>Browse</Text>

client/components/Select.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ const StyledSelect = styled.select(
3131
typography
3232
)
3333

34-
const Select = ({ label, ...rest }) => {
34+
const Select = ({ label, fRef, ...rest }) => {
3535
return (
3636
<WrapLabel label={label}>
3737
<Box position="relative" display="inline-block">
38-
<StyledSelect {...rest} />
38+
<StyledSelect {...rest} ref={fRef} />
3939
<Box
4040
position="absolute"
4141
top="7px"
@@ -50,4 +50,6 @@ const Select = ({ label, ...rest }) => {
5050
)
5151
}
5252

53-
export default Select
53+
export default React.forwardRef((props, ref) => (
54+
<Select {...props} fRef={ref} />
55+
))

client/components/TorrentList.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ const TorrentList = ({ torrents = [], categories, total }) => {
6666
accessor: 'type',
6767
cell: ({ value }) => {
6868
const category =
69-
categories.find((c) => slugify(c, { lower: true }) === value) ||
70-
'None'
69+
Object.keys(categories).find(
70+
(c) => slugify(c, { lower: true }) === value
71+
) || 'None'
7172
return (
7273
<Text icon={ListUl} title={category}>
7374
{category}

client/pages/categories/[category].js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const Category = ({ results }) => {
1818
publicRuntimeConfig: { SQ_TORRENT_CATEGORIES },
1919
} = getConfig()
2020

21-
const category = SQ_TORRENT_CATEGORIES.find(
21+
const category = Object.keys(SQ_TORRENT_CATEGORIES).find(
2222
(c) => slugify(c, { lower: true }) === categorySlug
2323
)
2424

client/pages/categories/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ const Categories = () => {
3535
<Text as="h1" mb={5}>
3636
Categories
3737
</Text>
38-
{SQ_TORRENT_CATEGORIES.length ? (
38+
{Object.keys(SQ_TORRENT_CATEGORIES).length ? (
3939
<Box
4040
as="ul"
4141
display="grid"
4242
gridTemplateColumns={['1fr', 'repeat(4, 1fr)']}
4343
gridGap={4}
4444
_css={{ pl: 0, listStyle: 'none' }}
4545
>
46-
{SQ_TORRENT_CATEGORIES.map((category) => (
46+
{Object.keys(SQ_TORRENT_CATEGORIES).map((category) => (
4747
<CategoryItem key={category}>
4848
<Link
4949
href={`/categories/${slugify(category, { lower: true })}`}

client/pages/requests/[index].js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ const Request = ({ request, token, user }) => {
274274
accessor: 'type',
275275
cell: ({ value }) => (
276276
<Text icon={ListUl}>
277-
{SQ_TORRENT_CATEGORIES.find(
277+
{Object.keys(SQ_TORRENT_CATEGORIES).find(
278278
(c) => slugify(c, { lower: true }) === value
279279
) || 'None'}
280280
</Text>

client/pages/torrent/[infoHash].js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ const Torrent = ({ token, torrent, userId, userRole, uid }) => {
129129
publicRuntimeConfig: {
130130
SQ_SITE_NAME,
131131
SQ_API_URL,
132-
SQ_BASE_URL,
133132
SQ_TORRENT_CATEGORIES,
134133
SQ_SITE_WIDE_FREELEECH,
135134
},
@@ -367,10 +366,14 @@ const Torrent = ({ token, torrent, userId, userRole, uid }) => {
367366
setLoading(false)
368367
}
369368

370-
const category = SQ_TORRENT_CATEGORIES.find(
369+
const category = Object.keys(SQ_TORRENT_CATEGORIES).find(
371370
(c) => slugify(c, { lower: true }) === torrent.type
372371
)
373372

373+
const source = SQ_TORRENT_CATEGORIES[category].find(
374+
(s) => slugify(s, { lower: true }) === torrent.source
375+
)
376+
374377
const parsedFiles = torrent.files
375378
.map(({ path, size }) => ({ path: path.split('/'), size }))
376379
.reduce((children, { path, size }) => insert(children, path, size), [])
@@ -434,6 +437,16 @@ const Torrent = ({ token, torrent, userId, userRole, uid }) => {
434437
<Text as="a">{category}</Text>
435438
</Link>
436439
) : undefined,
440+
Source: source ? (
441+
<Link
442+
href={`/categories/${slugify(category, {
443+
lower: true,
444+
})}?source=${slugify(source, { lower: true })}`}
445+
passHref
446+
>
447+
<Text as="a">{source}</Text>
448+
</Link>
449+
) : undefined,
437450
Date: moment(torrent.created).format('HH:mm Do MMM YYYY'),
438451
'Info hash': (
439452
<Text

0 commit comments

Comments
 (0)