Skip to content

Fix/wiki#63 #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion api/src/controllers/wiki.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,51 @@ export const getWiki = async (req, res, next) => {
next(e);
}
};

export const getWikis = async (req, res, next) => {
try {
const result = await Wiki.aggregate([
{
$lookup: {
from: "users",
as: "createdBy",
let: { userId: "$createdBy" },
pipeline: [
{
$match: { $expr: { $eq: ["$_id", "$$userId"] } },
},
{
$project: {
username: 1,
},
},
],
},
},
{
$unwind: {
path: "$createdBy",
preserveNullAndEmptyArrays: true,
},
},
]);
if (!result || result.length === 0) {
res.status(404).send("No wikis found");
return;
}
let page = result[0];
if (process.env.SQ_ALLOW_UNREGISTERED_VIEW && !req.userId && !page.public) {
page = null;
}
const query = {};
if (process.env.SQ_ALLOW_UNREGISTERED_VIEW && !req.userId) {
query.public = true;
}
const allPages = await Wiki.find(query, { slug: 1, title: 1 }).lean();
res.json({ page, allPages });
} catch (e) {
next(e);
}
};
export const deleteWiki = async (req, res, next) => {
try {
if (req.userRole !== "admin") {
Expand Down
2 changes: 2 additions & 0 deletions api/src/routes/wiki.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import express from "express";
import {
createWiki,
getWiki,
getWikis,
deleteWiki,
updateWiki,
} from "../controllers/wiki";
Expand All @@ -11,6 +12,7 @@ const router = express.Router();
export default () => {
router.post("/new", createWiki);
router.post("/update/:wikiId", updateWiki);
router.get("/", getWikis);
router.get("*", getWiki);
router.delete("*", deleteWiki);
return router;
Expand Down
49 changes: 33 additions & 16 deletions client/pages/wiki/[[...slug]].js
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,18 @@ const Wiki = ({ page, allPages, token, userRole, slug }) => {
</form>
)}
</>
) : allPages.length > 0 ? (
<>
{allPages.map((p) => (
<Link key={`page-${p.slug}`} href={`/wiki${p.slug}`} passHref>
<Text as="a" display="block">
{p.title}
</Text>
</Link>
))}
</>
) : (
<Text color="grey">{getLocaleString("wikiThereNothingHereYet")}</Text>
<Text>{getLocaleString("wikiThereNothingHereYet")}</Text>
)}
{showDeleteModal && (
<Modal close={() => setShowDeleteModal(false)}>
Expand All @@ -272,7 +282,6 @@ const Wiki = ({ page, allPages, token, userRole, slug }) => {
export const getServerSideProps = withAuthServerSideProps(
async ({ token, fetchHeaders, isPublicAccess, query: { slug } }) => {
if (!token && !isPublicAccess) return { props: {} };

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

const {
Expand All @@ -281,21 +290,29 @@ export const getServerSideProps = withAuthServerSideProps(
} = getConfig();

const { role } = token ? jwt.verify(token, SQ_JWT_SECRET) : { role: null };

try {
const wikiRes = await fetch(`${SQ_API_URL}/wiki/${parsedSlug}`, {
headers: fetchHeaders,
});
if (
wikiRes.status === 403 &&
(await wikiRes.text()) === "User is banned"
) {
throw "banned";
}
const { page, allPages } = await wikiRes.json();
return {
props: { page, allPages, token, userRole: role, slug: parsedSlug },
};
let page, allPages;
if (parsedSlug.length === 0) {
const wikiRes = await fetch(`${SQ_API_URL}/wiki`, {
headers: fetchHeaders,
});
({ allPages } = await wikiRes.json());

return {
props: {allPages, token, userRole: role, slug: parsedSlug, },
};
} else {
const wikiRes = await fetch(`${SQ_API_URL}/wiki/${parsedSlug}`, {
headers: fetchHeaders,
});
if (wikiRes.status === 403 && (await wikiRes.text()) === "User is banned") {
throw "banned";
}
({ page, allPages } = await wikiRes.json());
return {
props: { page, allPages, token, userRole: role, slug: parsedSlug },
};
}
} catch (e) {
if (e === "banned") throw "banned";
return { props: { token, userRole: role, slug: parsedSlug } };
Expand Down