From 0999004646f072d9797c030c4c516e9aaac18b10 Mon Sep 17 00:00:00 2001 From: keyan Date: Sat, 30 Dec 2023 19:41:16 -0600 Subject: [PATCH] mute territories in overflow --- api/resolvers/item.js | 10 ++++----- api/resolvers/sub.js | 4 ++-- components/item-info.js | 6 +++++ components/territory-header.js | 40 ++++++++++++++++++++++++++++++++-- fragments/comments.js | 6 +++++ fragments/items.js | 2 ++ 6 files changed, 58 insertions(+), 10 deletions(-) diff --git a/api/resolvers/item.js b/api/resolvers/item.js index 48014c45..279d2c26 100644 --- a/api/resolvers/item.js +++ b/api/resolvers/item.js @@ -125,7 +125,7 @@ async function itemQueryWithMeta ({ me, models, query, orderBy = '' }, ...args) COALESCE("ItemAct"."meMsats", 0) as "meMsats", COALESCE("ItemAct"."meDontLikeMsats", 0) as "meDontLikeMsats", b."itemId" IS NOT NULL AS "meBookmark", "ThreadSubscription"."itemId" IS NOT NULL AS "meSubscription", "ItemForward"."itemId" IS NOT NULL AS "meForward", - to_jsonb("Sub".*) as sub + to_jsonb("Sub".*) || jsonb_build_object('meMuteSub', "MuteSub"."userId" IS NOT NULL) as sub FROM ( ${query} ) "Item" @@ -135,6 +135,7 @@ async function itemQueryWithMeta ({ me, models, query, orderBy = '' }, ...args) LEFT JOIN "ThreadSubscription" ON "ThreadSubscription"."itemId" = "Item".id AND "ThreadSubscription"."userId" = ${me.id} LEFT JOIN "ItemForward" ON "ItemForward"."itemId" = "Item".id AND "ItemForward"."userId" = ${me.id} LEFT JOIN "Sub" ON "Sub"."name" = "Item"."subName" + LEFT JOIN "MuteSub" ON "Sub"."name" = "MuteSub"."subName" AND "MuteSub"."userId" = ${me.id} LEFT JOIN LATERAL ( SELECT "itemId", sum("ItemAct".msats) FILTER (WHERE act = 'FEE' OR act = 'TIP') AS "meMsats", sum("ItemAct".msats) FILTER (WHERE act = 'DONT_LIKE_THIS') AS "meDontLikeMsats" @@ -1034,17 +1035,14 @@ export default { mine: async (item, args, { me, models }) => { return me?.id === item.userId }, - root: async (item, args, { models }) => { + root: async (item, args, { models, me }) => { if (!item.rootId) { return null } if (item.root) { return item.root } - return await models.item.findUnique({ - where: { id: item.rootId }, - include: { sub: true } - }) + return await getItem(item, { id: item.rootId }, { me, models }) }, parent: async (item, args, { models }) => { if (!item.parentId) { diff --git a/api/resolvers/sub.js b/api/resolvers/sub.js index d32ced1c..b2a09bba 100644 --- a/api/resolvers/sub.js +++ b/api/resolvers/sub.js @@ -86,7 +86,7 @@ export default { 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 + ORDER BY "Sub".name ASC ` } @@ -191,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.meMuteSub || sub.MuteSub?.length > 0 } } } diff --git a/components/item-info.js b/components/item-info.js index eaeea9fb..2ed16411 100644 --- a/components/item-info.js +++ b/components/item-info.js @@ -20,6 +20,7 @@ import ActionDropdown from './action-dropdown' import MuteDropdownItem from './mute' import { DropdownItemUpVote } from './upvote' import { useRoot } from './root' +import { MuteSubDropdownItem } from './territory-header' export default function ItemInfo ({ item, full, commentsText = 'comments', @@ -168,6 +169,11 @@ export default function ItemInfo ({
} + {me && !item.mine && sub && Number(me.id) !== Number(sub.userId) && + <> +
+ + } {me && !item.mine && <>
diff --git a/components/territory-header.js b/components/territory-header.js index 9a64b71e..6374b95d 100644 --- a/components/territory-header.js +++ b/components/territory-header.js @@ -1,4 +1,4 @@ -import { Badge, Button, CardFooter } from 'react-bootstrap' +import { Badge, Button, CardFooter, Dropdown } from 'react-bootstrap' import { AccordianCard } from './accordian-item' import TerritoryPaymentDue, { TerritoryBillingLine } from './territory-payment-due' import Link from 'next/link' @@ -24,7 +24,7 @@ export default function TerritoryHeader ({ sub }) { cache.modify({ id: `Sub:{"name":"${sub.name}"}`, fields: { - meMuteSub: () => toggleMuteSub.meMuteSub + meMuteSub: () => toggleMuteSub } }) } @@ -90,3 +90,39 @@ export default function TerritoryHeader ({ sub }) { ) } + +export function MuteSubDropdownItem ({ item, sub }) { + const toaster = useToast() + + const [toggleMuteSub] = useMutation( + gql` + mutation toggleMuteSub($name: String!) { + toggleMuteSub(name: $name) + }`, { + update (cache, { data: { toggleMuteSub } }) { + console.log(sub, toggleMuteSub) + cache.modify({ + id: `Sub:{"name":"${sub.name}"}`, + fields: { + meMuteSub: () => toggleMuteSub + } + }) + } + } + ) + + return ( + { + try { + await toggleMuteSub({ variables: { name: sub.name } }) + } catch { + toaster.danger(`failed to ${sub.meMuteSub ? 'join' : 'mute'} territory`) + return + } + toaster.success(`${sub.meMuteSub ? 'joined' : 'muted'} territory`) + }} + >{sub.meMuteSub ? 'unmute' : 'mute'} ~{sub.name} + + ) +} diff --git a/fragments/comments.js b/fragments/comments.js index 3cc20455..fc912077 100644 --- a/fragments/comments.js +++ b/fragments/comments.js @@ -44,6 +44,12 @@ export const COMMENTS_ITEM_EXT_FIELDS = gql` bounty bountyPaidTo subName + sub { + name + userId + moderated + meMuteSub + } user { name optional { diff --git a/fragments/items.js b/fragments/items.js index 2e3d3c93..2e3b8e30 100644 --- a/fragments/items.js +++ b/fragments/items.js @@ -21,6 +21,7 @@ export const ITEM_FIELDS = gql` name userId moderated + meMuteSub } otsHash position @@ -78,6 +79,7 @@ export const ITEM_FULL_FIELDS = gql` name userId moderated + meMuteSub } } forwards {