visbility filter and slug resolution to id
This commit is contained in:
@@ -752,13 +752,18 @@ export default router => {
|
||||
}, 200);
|
||||
});
|
||||
|
||||
group.get(/\/item\/(?<id>[0-9]+)$/, async (req, res) => {
|
||||
const id = +req.params.id;
|
||||
group.get(/\/item\/(?<id>[a-zA-Z0-9_-]{11}|\d+)$/, async (req, res) => {
|
||||
const rawIdOrSlug = req.params.id;
|
||||
const isNumeric = /^\d+$/.test(rawIdOrSlug);
|
||||
const id = isNumeric ? +rawIdOrSlug : (await db`SELECT id FROM items WHERE slug = ${rawIdOrSlug} LIMIT 1`)[0]?.id;
|
||||
if (!id) {
|
||||
return res.json({ success: false, msg: 'no items found' });
|
||||
}
|
||||
|
||||
const item = await db`
|
||||
select *
|
||||
from "items"
|
||||
where id = ${+id} and active = true
|
||||
where id = ${id} and active = true
|
||||
limit 1
|
||||
`;
|
||||
|
||||
@@ -811,7 +816,7 @@ export default router => {
|
||||
if (effMode === 2) {
|
||||
modeCondition = db`and not exists (select 1 from tags_assign where item_id = items.id)`;
|
||||
} else if (effMode === 0) {
|
||||
modeCondition = db`and id in (select item_id from tags_assign where tag_id = 1)`;
|
||||
modeCondition = db`and (id in (select item_id from tags_assign where tag_id = 1) or not exists (select 1 from tags_assign where item_id = items.id))`;
|
||||
} else if (effMode === 1) {
|
||||
modeCondition = db`and id in (select item_id from tags_assign where tag_id = 2)`;
|
||||
} else if (effMode === 4) {
|
||||
@@ -820,17 +825,23 @@ export default router => {
|
||||
modeCondition = db`and ${db.unsafe(lib.getMultiRatingMode(ratingsArr))}`;
|
||||
}
|
||||
|
||||
const visibilityFilter = isOwnerOrAdmin
|
||||
? db``
|
||||
: (session && session.user
|
||||
? db`and (coalesce(visibility, 0) = 0 or lower(username) = ${session.user.toLowerCase()})`
|
||||
: db`and coalesce(visibility, 0) = 0`);
|
||||
|
||||
const next = await db`
|
||||
select id
|
||||
from "items"
|
||||
where id > ${+id} and active = true and coalesce(visibility, 0) = 0 ${guestTagFilter} ${modeCondition}
|
||||
where id > ${id} and active = true ${visibilityFilter} ${guestTagFilter} ${modeCondition}
|
||||
order by id
|
||||
limit 1
|
||||
`;
|
||||
const prev = await db`
|
||||
select id
|
||||
from "items"
|
||||
where id < ${+id} and active = true and coalesce(visibility, 0) = 0 ${guestTagFilter} ${modeCondition}
|
||||
where id < ${id} and active = true ${visibilityFilter} ${guestTagFilter} ${modeCondition}
|
||||
order by id desc
|
||||
limit 1
|
||||
`;
|
||||
|
||||
Reference in New Issue
Block a user