Skip to content

Commit 101ca27

Browse files
committed
adds list of tags on browse page
1 parent c5ac6a5 commit 101ca27

3 files changed

Lines changed: 105 additions & 20 deletions

File tree

api/src/controllers/torrent.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,3 +852,24 @@ export const toggleBookmark = async (req, res, next) => {
852852
next(e);
853853
}
854854
};
855+
856+
export const listTags = async (req, res, next) => {
857+
try {
858+
const torrents = await Torrent.find(
859+
{ tags: { $exists: true, $not: { $size: 0 } } },
860+
{ tags: 1 }
861+
).lean();
862+
863+
const uniqueTags = new Set();
864+
865+
for (const { tags } of torrents) {
866+
for (const tag of tags) {
867+
if (tag !== "") uniqueTags.add(tag);
868+
}
869+
}
870+
871+
res.json(Array.from(uniqueTags));
872+
} catch (e) {
873+
next(e);
874+
}
875+
};

api/src/routes/torrent.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
uploadTorrent,
1313
editTorrent,
1414
toggleBookmark,
15+
listTags,
1516
} from "../controllers/torrent";
1617
import { createReport } from "../controllers/moderation";
1718

@@ -31,5 +32,6 @@ export default (tracker) => {
3132
router.get("/latest", listLatest(tracker));
3233
router.get("/all", listAll);
3334
router.get("/search", searchTorrents(tracker));
35+
router.get("/tags", listTags);
3436
return router;
3537
};

client/pages/categories/index.js

Lines changed: 82 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import Link from "next/link";
44
import styled from "styled-components";
55
import css from "@styled-system/css";
66
import slugify from "slugify";
7-
import { withAuth } from "../../utils/withAuth";
7+
import { withAuth, withAuthServerSideProps } from "../../utils/withAuth";
88
import SEO from "../../components/SEO";
99
import Box from "../../components/Box";
1010
import Text from "../../components/Text";
11+
import qs from "qs";
1112

1213
const CategoryItem = styled.li(() =>
1314
css({
@@ -24,7 +25,7 @@ const CategoryItem = styled.li(() =>
2425
})
2526
);
2627

27-
const Categories = () => {
28+
const Categories = ({ tags }) => {
2829
const {
2930
publicRuntimeConfig: { SQ_TORRENT_CATEGORIES },
3031
} = getConfig();
@@ -35,30 +36,91 @@ const Categories = () => {
3536
<Text as="h1" mb={5}>
3637
Categories
3738
</Text>
38-
{Object.keys(SQ_TORRENT_CATEGORIES).length ? (
39-
<Box
40-
as="ul"
41-
display="grid"
42-
gridTemplateColumns={["1fr", "repeat(4, 1fr)"]}
43-
gridGap={4}
44-
_css={{ pl: 0, listStyle: "none" }}
45-
>
46-
{Object.keys(SQ_TORRENT_CATEGORIES).map((category) => (
47-
<CategoryItem key={category}>
48-
<Link
49-
href={`/categories/${slugify(category, { lower: true })}`}
50-
passHref
51-
>
52-
<a>{category}</a>
39+
<Box mb={5}>
40+
{Object.keys(SQ_TORRENT_CATEGORIES).length ? (
41+
<Box
42+
as="ul"
43+
display="grid"
44+
gridTemplateColumns={["1fr", "repeat(4, 1fr)"]}
45+
gridGap={4}
46+
_css={{ pl: 0, listStyle: "none" }}
47+
>
48+
{Object.keys(SQ_TORRENT_CATEGORIES).map((category) => (
49+
<CategoryItem key={category}>
50+
<Link
51+
href={`/categories/${slugify(category, { lower: true })}`}
52+
passHref
53+
>
54+
<a>{category}</a>
55+
</Link>
56+
</CategoryItem>
57+
))}
58+
</Box>
59+
) : (
60+
<Text color="grey">No categories have been defined.</Text>
61+
)}
62+
</Box>
63+
<Text as="h1" mb={5}>
64+
Tags
65+
</Text>
66+
{tags.length ? (
67+
<Box display="flex" flexWrap="wrap" ml={-1} mt={-1}>
68+
{tags.map((tag) => (
69+
<Box
70+
key={`tag-${tag}`}
71+
bg="sidebar"
72+
border="1px solid"
73+
borderColor="border"
74+
borderRadius={1}
75+
m={1}
76+
>
77+
<Link href={`/tags/${tag}`} passHref>
78+
<Text
79+
as="a"
80+
display="block"
81+
color="text"
82+
_css={{ "&:visited": { color: "text" } }}
83+
px={3}
84+
py={1}
85+
>
86+
{tag}
87+
</Text>
5388
</Link>
54-
</CategoryItem>
89+
</Box>
5590
))}
5691
</Box>
5792
) : (
58-
<Text color="grey">No categories have been defined.</Text>
93+
<Text color="grey">No tags have been defined.</Text>
5994
)}
6095
</>
6196
);
6297
};
6398

64-
export default withAuth(Categories);
99+
export const getServerSideProps = withAuthServerSideProps(
100+
async ({ token, fetchHeaders }) => {
101+
if (!token) return { props: {} };
102+
103+
const {
104+
publicRuntimeConfig: { SQ_API_URL },
105+
} = getConfig();
106+
107+
try {
108+
const tagsRes = await fetch(`${SQ_API_URL}/torrent/tags`, {
109+
headers: fetchHeaders,
110+
});
111+
if (
112+
tagsRes.status === 403 &&
113+
(await tagsRes.text()) === "User is banned"
114+
) {
115+
throw "banned";
116+
}
117+
const tags = await tagsRes.json();
118+
return { props: { tags } };
119+
} catch (e) {
120+
if (e === "banned") throw "banned";
121+
return { props: {} };
122+
}
123+
}
124+
);
125+
126+
export default Categories;

0 commit comments

Comments
 (0)