diff --git a/api/resolvers/item.js b/api/resolvers/item.js index c4e79ff6..6a930e37 100644 --- a/api/resolvers/item.js +++ b/api/resolvers/item.js @@ -72,7 +72,8 @@ export default { itemRepetition: async (parent, { parentId }, { me, models }) => { if (!me) return 0 // how many of the parents starting at parentId belong to me - const [{ item_spam: count }] = await models.$queryRaw(`SELECT item_spam($1, $2, '${ITEM_SPAM_INTERVAL}')`, Number(parentId), Number(me.id)) + const [{ item_spam: count }] = await models.$queryRaw(`SELECT item_spam($1, $2, '${ITEM_SPAM_INTERVAL}')`, + Number(parentId), Number(me.id)) return count }, @@ -709,37 +710,12 @@ export default { } return await models.user.findUnique({ where: { id: item.fwdUserId } }) }, - ncomments: async (item, args, { models }) => { - const [{ count }] = await models.$queryRaw` - SELECT count(*) - FROM "Item" - WHERE path <@ text2ltree(${item.path}) AND id != ${Number(item.id)}` - return count || 0 - }, comments: async (item, args, { models }) => { if (item.comments) { return item.comments } return comments(models, item.id, 'hot') }, - sats: async (item, args, { models }) => { - const { sum: { sats } } = await models.itemAct.aggregate({ - sum: { - sats: true - }, - where: { - itemId: Number(item.id), - userId: { - not: Number(item.userId) - }, - act: { - not: 'BOOST' - } - } - }) - - return sats || 0 - }, upvotes: async (item, args, { models }) => { const { sum: { sats } } = await models.itemAct.aggregate({ sum: { @@ -966,7 +942,8 @@ export const SELECT = `SELECT "Item".id, "Item".created_at as "createdAt", "Item".updated_at as "updatedAt", "Item".title, "Item".text, "Item".url, "Item"."userId", "Item"."fwdUserId", "Item"."parentId", "Item"."pinId", "Item"."maxBid", "Item".company, "Item".location, "Item".remote, - "Item"."subName", "Item".status, "Item"."uploadId", "Item"."pollCost", "Item"."paidImgLink", ltree2text("Item"."path") AS "path"` + "Item"."subName", "Item".status, "Item"."uploadId", "Item"."pollCost", "Item"."paidImgLink", + "Item".sats, "Item".ncomments, "Item"."commentSats", "Item"."lastCommentAt", ltree2text("Item"."path") AS "path"` function newTimedOrderByWeightedSats (num) { return ` diff --git a/api/typeDefs/item.js b/api/typeDefs/item.js index 6ba733aa..1c6cb937 100644 --- a/api/typeDefs/item.js +++ b/api/typeDefs/item.js @@ -74,6 +74,8 @@ export default gql` mine: Boolean! boost: Int! sats: Int! + commentSats: Int! + lastCommentAt: String upvotes: Int! meSats: Int! paidImgLink: Boolean diff --git a/components/comments.js b/components/comments.js index ed99b8a2..9f5b6860 100644 --- a/components/comments.js +++ b/components/comments.js @@ -6,7 +6,7 @@ import { Nav, Navbar } from 'react-bootstrap' import { COMMENTS_QUERY } from '../fragments/items' import { COMMENTS } from '../fragments/comments' -export function CommentsHeader ({ handleSort }) { +export function CommentsHeader ({ handleSort, commentSats }) { const [sort, setSort] = useState('hot') const getHandleClick = sort => { @@ -17,44 +17,49 @@ export function CommentsHeader ({ handleSort }) { } return ( - + ) } -export default function Comments ({ parentId, comments, ...props }) { +export default function Comments ({ parentId, commentSats, comments, ...props }) { const client = useApolloClient() useEffect(() => { const hash = window.location.hash @@ -87,7 +92,7 @@ export default function Comments ({ parentId, comments, ...props }) { return ( <> - {comments.length ? getComments({ variables: { id: parentId, sort } })} /> : null} + {comments.length ? getComments({ variables: { id: parentId, sort } })} /> : null} {loading ? : comments.map(item => ( diff --git a/components/item-full.js b/components/item-full.js index 4d6228a2..bf7ce2f3 100644 --- a/components/item-full.js +++ b/components/item-full.js @@ -112,7 +112,7 @@ export default function ItemFull ({ item, bio, ...props }) { )} {item.comments &&
- +
} ) diff --git a/fragments/items.js b/fragments/items.js index 415058e0..c3981a11 100644 --- a/fragments/items.js +++ b/fragments/items.js @@ -22,6 +22,8 @@ export const ITEM_FIELDS = gql` path meSats ncomments + commentSats + lastCommentAt maxBid company location