diff --git a/api/resolvers/sub.js b/api/resolvers/sub.js index a272da5a..d32ced1c 100644 --- a/api/resolvers/sub.js +++ b/api/resolvers/sub.js @@ -79,19 +79,23 @@ export default { }) }, subs: async (parent, args, { models, me }) => { + if (me) { + return await models.$queryRaw` + SELECT "Sub".*, COALESCE(json_agg("MuteSub".*) FILTER (WHERE "MuteSub"."userId" IS NOT NULL), '[]') AS "MuteSub" + FROM "Sub" + LEFT JOIN "MuteSub" ON "Sub".name = "MuteSub"."subName" AND "MuteSub"."userId" = ${me.id}::INTEGER + WHERE status <> 'STOPPED' + GROUP BY "Sub".name, "MuteSub"."userId" + ORDER BY "MuteSub"."userId" NULLS FIRST, "Sub".name ASC + ` + } + return await models.sub.findMany({ where: { status: { not: 'STOPPED' } }, - include: { - MuteSub: { - where: { - userId: Number(me.id) - } - } - }, orderBy: { name: 'asc' } @@ -187,7 +191,7 @@ export default { return await models.user.findUnique({ where: { id: sub.userId } }) }, meMuteSub: async (sub, args, { models }) => { - return sub.MuteSub.length > 0 + return sub.MuteSub?.length > 0 } } } diff --git a/components/sub-select.js b/components/sub-select.js index 7cf78625..f6f2a98b 100644 --- a/components/sub-select.js +++ b/components/sub-select.js @@ -30,7 +30,15 @@ export function useSubs ({ prependSubs = [], sub, filterSubs = () => true, appen ...appendSubs.filter(s => s !== sub)]) useEffect(() => { if (!data) return - setSubs([...prependSubs, ...data.subs.filter(filterSubs).map(s => s.name), ...appendSubs]) + console.log(data) + const joined = data.subs.filter(filterSubs).filter(s => !s.meMuteSub).map(s => s.name) + const muted = data.subs.filter(filterSubs).filter(s => s.meMuteSub).map(s => s.name) + const mutedSection = muted.length ? [{ label: 'muted', items: muted }] : [] + setSubs([ + ...prependSubs, + ...joined, + ...mutedSection, + ...appendSubs]) }, [data]) return subs