Skip to content

Commit 73dda4c

Browse files
committed
SQ_ALLOW_UNREGISTERED_VIEW allow public access to category/tag pages, and wiki pages if opted in
1 parent 55606bb commit 73dda4c

9 files changed

Lines changed: 92 additions & 40 deletions

File tree

api/src/controllers/wiki.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export const createWiki = async (req, res, next) => {
4747
title: req.body.title,
4848
body: req.body.body,
4949
createdBy: req.userId,
50+
public: !!req.body.public,
5051
created: Date.now(),
5152
});
5253

@@ -64,7 +65,7 @@ export const getWiki = async (req, res, next) => {
6465
try {
6566
const slug = req.params[0];
6667

67-
const [page] = await Wiki.aggregate([
68+
let [page] = await Wiki.aggregate([
6869
{
6970
$match: { slug },
7071
},
@@ -98,7 +99,17 @@ export const getWiki = async (req, res, next) => {
9899
return;
99100
}
100101

101-
const allPages = await Wiki.find({}, { slug: 1, title: 1 }).lean();
102+
if (process.env.SQ_ALLOW_UNREGISTERED_VIEW && !req.userId && !page.public) {
103+
page = null;
104+
}
105+
106+
const query = {};
107+
108+
if (process.env.SQ_ALLOW_UNREGISTERED_VIEW && !req.userId) {
109+
query.public = true;
110+
}
111+
112+
const allPages = await Wiki.find(query, { slug: 1, title: 1 }).lean();
102113

103114
res.json({ page, allPages });
104115
} catch (e) {
@@ -174,6 +185,7 @@ export const updateWiki = async (req, res, next) => {
174185
slug,
175186
title: req.body.title,
176187
body: req.body.body,
188+
public: !!req.body.public,
177189
updated: Date.now(),
178190
},
179191
}

api/src/schema/wiki.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const Wiki = new mongoose.Schema({
55
slug: String,
66
body: String,
77
createdBy: mongoose.Schema.ObjectId,
8+
public: Boolean,
89
created: Number,
910
updated: Number,
1011
});

client/components/Navigation.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ const Navigation = ({ isMobile, menuIsOpen, setMenuIsOpen }) => {
7373
SQ_API_URL,
7474
SQ_ALLOW_REGISTER,
7575
SQ_VERSION,
76+
SQ_ALLOW_UNREGISTERED_VIEW,
7677
},
7778
} = getConfig();
7879

@@ -248,6 +249,22 @@ const Navigation = ({ isMobile, menuIsOpen, setMenuIsOpen }) => {
248249
</NavLink>
249250
</Link>
250251
)}
252+
{SQ_ALLOW_UNREGISTERED_VIEW && (
253+
<>
254+
<Link href="/categories" passHref>
255+
<NavLink>
256+
<Text>Browse</Text>
257+
<ListUl size={24} />
258+
</NavLink>
259+
</Link>
260+
<Link href="/wiki" passHref>
261+
<NavLink>
262+
<Text>Wiki</Text>
263+
<BookOpen size={24} />
264+
</NavLink>
265+
</Link>
266+
</>
267+
)}
251268
</Box>
252269
)}
253270
</Box>

client/pages/categories/[category].js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ export const getServerSideProps = withAuthServerSideProps(
5050
async ({
5151
token,
5252
fetchHeaders,
53+
isPublicAccess,
5354
query: { category, source, page: pageParam },
5455
}) => {
55-
if (!token) return { props: {} };
56+
if (!token && !isPublicAccess) return { props: {} };
5657

5758
const {
5859
publicRuntimeConfig: { SQ_API_URL },
@@ -84,7 +85,8 @@ export const getServerSideProps = withAuthServerSideProps(
8485
if (e === "banned") throw "banned";
8586
return { props: {} };
8687
}
87-
}
88+
},
89+
true
8890
);
8991

9092
export default Category;

client/pages/categories/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ const Categories = ({ tags }) => {
9797
};
9898

9999
export const getServerSideProps = withAuthServerSideProps(
100-
async ({ token, fetchHeaders }) => {
101-
if (!token) return { props: {} };
100+
async ({ token, fetchHeaders, isPublicAccess }) => {
101+
if (!token && !isPublicAccess) return { props: {} };
102102

103103
const {
104104
publicRuntimeConfig: { SQ_API_URL },
@@ -120,7 +120,8 @@ export const getServerSideProps = withAuthServerSideProps(
120120
if (e === "banned") throw "banned";
121121
return { props: {} };
122122
}
123-
}
123+
},
124+
true
124125
);
125126

126127
export default Categories;

client/pages/tags/[tag].js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,13 @@ const Tag = ({ results, token }) => {
4242
};
4343

4444
export const getServerSideProps = withAuthServerSideProps(
45-
async ({ token, fetchHeaders, query: { tag, page: pageParam } }) => {
46-
if (!token) return { props: {} };
45+
async ({
46+
token,
47+
fetchHeaders,
48+
isPublicAccess,
49+
query: { tag, page: pageParam },
50+
}) => {
51+
if (!token && !isPublicAccess) return { props: {} };
4752

4853
const {
4954
publicRuntimeConfig: { SQ_API_URL },
@@ -74,7 +79,8 @@ export const getServerSideProps = withAuthServerSideProps(
7479
if (e === "banned") throw "banned";
7580
return { props: {} };
7681
}
77-
}
82+
},
83+
true
7884
);
7985

8086
export default Tag;

client/pages/wiki/[[...slug]].js

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ const Wiki = ({ page, allPages, token, userRole, slug }) => {
8787
slug: page.slug === "/" ? "/" : form.get("slug"),
8888
title: form.get("title"),
8989
body: form.get("body"),
90+
public: !!form.get("public"),
9091
}),
9192
}
9293
);
@@ -169,15 +170,35 @@ const Wiki = ({ page, allPages, token, userRole, slug }) => {
169170
{!editing ? (
170171
<Box
171172
display="grid"
172-
gridTemplateColumns={["1fr", "200px auto"]}
173+
gridTemplateColumns={["1fr", "auto 200px"]}
173174
gridGap={5}
175+
alignItems="start"
174176
>
177+
<MarkdownBody>
178+
<ReactMarkdown
179+
remarkPlugins={[remarkGfm]}
180+
components={{
181+
a({ href, ...props }) {
182+
return href.startsWith("http") ? (
183+
<a href={href} target="_blank" {...props} />
184+
) : (
185+
<Link href={href} passHref>
186+
<a {...props} />
187+
</Link>
188+
);
189+
},
190+
}}
191+
>
192+
{page.body}
193+
</ReactMarkdown>
194+
</MarkdownBody>
175195
<Box
176196
bg="sidebar"
177197
border="1px solid"
178198
borderColor="border"
179199
borderRadius={1}
180-
p={3}
200+
p={4}
201+
_css={{ order: [-1, "unset"] }}
181202
>
182203
<Text
183204
fontWeight={600}
@@ -188,35 +209,13 @@ const Wiki = ({ page, allPages, token, userRole, slug }) => {
188209
Pages
189210
</Text>
190211
{allPages.sort(sortSlug).map((p) => (
191-
<Link
192-
key={`page-${p.slug}`}
193-
href={`/wiki/${p.slug}`}
194-
passHref
195-
>
212+
<Link key={`page-${p.slug}`} href={`/wiki${p.slug}`} passHref>
196213
<Text as="a" display="block">
197214
{p.title}
198215
</Text>
199216
</Link>
200217
))}
201218
</Box>
202-
<MarkdownBody>
203-
<ReactMarkdown
204-
remarkPlugins={[remarkGfm]}
205-
components={{
206-
a({ href, ...props }) {
207-
return href.startsWith("http") ? (
208-
<a href={href} target="_blank" {...props} />
209-
) : (
210-
<Link href={href} passHref>
211-
<a {...props} />
212-
</Link>
213-
);
214-
},
215-
}}
216-
>
217-
{page.body}
218-
</ReactMarkdown>
219-
</MarkdownBody>
220219
</Box>
221220
) : (
222221
<form onSubmit={handleEdit}>
@@ -263,8 +262,8 @@ const Wiki = ({ page, allPages, token, userRole, slug }) => {
263262
};
264263

265264
export const getServerSideProps = withAuthServerSideProps(
266-
async ({ token, fetchHeaders, query: { slug } }) => {
267-
if (!token) return { props: {} };
265+
async ({ token, fetchHeaders, isPublicAccess, query: { slug } }) => {
266+
if (!token && !isPublicAccess) return { props: {} };
268267

269268
const parsedSlug = slug?.length ? slug.join("/") : "";
270269

@@ -273,7 +272,7 @@ export const getServerSideProps = withAuthServerSideProps(
273272
serverRuntimeConfig: { SQ_JWT_SECRET },
274273
} = getConfig();
275274

276-
const { role } = jwt.verify(token, SQ_JWT_SECRET);
275+
const { role } = token ? jwt.verify(token, SQ_JWT_SECRET) : { role: null };
277276

278277
try {
279278
const wikiRes = await fetch(`${SQ_API_URL}/wiki/${parsedSlug}`, {
@@ -293,7 +292,8 @@ export const getServerSideProps = withAuthServerSideProps(
293292
if (e === "banned") throw "banned";
294293
return { props: { token, userRole: role, slug: parsedSlug } };
295294
}
296-
}
295+
},
296+
true
297297
);
298298

299299
export default Wiki;

client/pages/wiki/new.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ import MarkdownInput from "../../components/MarkdownInput";
1111
import { withAuthServerSideProps } from "../../utils/withAuth";
1212
import { NotificationContext } from "../../components/Notifications";
1313
import LoadingContext from "../../utils/LoadingContext";
14+
import Checkbox from "../../components/Checkbox";
1415

1516
export const WikiFields = ({ values }) => {
1617
const [slugValue, setSlugValue] = useState(values?.slug);
1718

1819
const {
19-
publicRuntimeConfig: { SQ_BASE_URL },
20+
publicRuntimeConfig: { SQ_BASE_URL, SQ_ALLOW_UNREGISTERED_VIEW },
2021
} = getConfig();
2122

23+
console.log(values);
24+
2225
return (
2326
<>
2427
<Input
@@ -59,6 +62,14 @@ export const WikiFields = ({ values }) => {
5962
mb={4}
6063
required
6164
/>
65+
{SQ_ALLOW_UNREGISTERED_VIEW && (
66+
<Checkbox
67+
name="public"
68+
label="Allow unregistered view"
69+
inputProps={{ defaultChecked: values?.public }}
70+
mb={4}
71+
/>
72+
)}
6273
</>
6374
);
6475
};
@@ -93,6 +104,7 @@ const NewWiki = ({ token, userRole }) => {
93104
slug: form.get("slug"),
94105
title: form.get("title"),
95106
body: form.get("body"),
107+
public: !!form.get("public"),
96108
}),
97109
});
98110

config.example.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ module.exports = {
6262

6363
// Whether torrent pages can be viewed by unregistered users.
6464
// If true, only logged-in users will be able to download/interact, but anyone (search engines included) will be able to view/read torrent info.
65+
// Non-logged-in users will also be able to browse category/tag pages and wiki pages that have been set to public.
6566
// Enable if you want torrents to be indexed to help search traffic.
6667
SQ_ALLOW_UNREGISTERED_VIEW: false,
6768

0 commit comments

Comments
 (0)