display earned sats in comments

This commit is contained in:
keyan 2022-09-01 16:06:11 -05:00
parent 6aeeb40623
commit 4be5cfa5c7
5 changed files with 44 additions and 58 deletions

View File

@ -72,7 +72,8 @@ export default {
itemRepetition: async (parent, { parentId }, { me, models }) => { itemRepetition: async (parent, { parentId }, { me, models }) => {
if (!me) return 0 if (!me) return 0
// how many of the parents starting at parentId belong to me // 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 return count
}, },
@ -709,37 +710,12 @@ export default {
} }
return await models.user.findUnique({ where: { id: item.fwdUserId } }) 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 }) => { comments: async (item, args, { models }) => {
if (item.comments) { if (item.comments) {
return item.comments return item.comments
} }
return comments(models, item.id, 'hot') 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 }) => { upvotes: async (item, args, { models }) => {
const { sum: { sats } } = await models.itemAct.aggregate({ const { sum: { sats } } = await models.itemAct.aggregate({
sum: { sum: {
@ -966,7 +942,8 @@ export const SELECT =
`SELECT "Item".id, "Item".created_at as "createdAt", "Item".updated_at as "updatedAt", "Item".title, `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".text, "Item".url, "Item"."userId", "Item"."fwdUserId", "Item"."parentId", "Item"."pinId", "Item"."maxBid",
"Item".company, "Item".location, "Item".remote, "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) { function newTimedOrderByWeightedSats (num) {
return ` return `

View File

@ -74,6 +74,8 @@ export default gql`
mine: Boolean! mine: Boolean!
boost: Int! boost: Int!
sats: Int! sats: Int!
commentSats: Int!
lastCommentAt: String
upvotes: Int! upvotes: Int!
meSats: Int! meSats: Int!
paidImgLink: Boolean paidImgLink: Boolean

View File

@ -6,7 +6,7 @@ import { Nav, Navbar } from 'react-bootstrap'
import { COMMENTS_QUERY } from '../fragments/items' import { COMMENTS_QUERY } from '../fragments/items'
import { COMMENTS } from '../fragments/comments' import { COMMENTS } from '../fragments/comments'
export function CommentsHeader ({ handleSort }) { export function CommentsHeader ({ handleSort, commentSats }) {
const [sort, setSort] = useState('hot') const [sort, setSort] = useState('hot')
const getHandleClick = sort => { const getHandleClick = sort => {
@ -17,44 +17,49 @@ export function CommentsHeader ({ handleSort }) {
} }
return ( return (
<Navbar className='py-1'> <Navbar className='pt-1 pb-0'>
<Nav <Nav
className={styles.navbarNav} className={styles.navbarNav}
activeKey={sort} activeKey={sort}
> >
<Nav.Item> <Nav.Item className='text-muted'>
<Nav.Link {commentSats} sats
eventKey='hot'
className={styles.navLink}
onClick={getHandleClick('hot')}
>
hot
</Nav.Link>
</Nav.Item>
<Nav.Item>
<Nav.Link
eventKey='recent'
className={styles.navLink}
onClick={getHandleClick('recent')}
>
recent
</Nav.Link>
</Nav.Item>
<Nav.Item>
<Nav.Link
eventKey='top'
className={styles.navLink}
onClick={getHandleClick('top')}
>
top
</Nav.Link>
</Nav.Item> </Nav.Item>
<div className='ml-auto d-flex'>
<Nav.Item>
<Nav.Link
eventKey='hot'
className={styles.navLink}
onClick={getHandleClick('hot')}
>
hot
</Nav.Link>
</Nav.Item>
<Nav.Item>
<Nav.Link
eventKey='recent'
className={styles.navLink}
onClick={getHandleClick('recent')}
>
recent
</Nav.Link>
</Nav.Item>
<Nav.Item>
<Nav.Link
eventKey='top'
className={styles.navLink}
onClick={getHandleClick('top')}
>
top
</Nav.Link>
</Nav.Item>
</div>
</Nav> </Nav>
</Navbar> </Navbar>
) )
} }
export default function Comments ({ parentId, comments, ...props }) { export default function Comments ({ parentId, commentSats, comments, ...props }) {
const client = useApolloClient() const client = useApolloClient()
useEffect(() => { useEffect(() => {
const hash = window.location.hash const hash = window.location.hash
@ -87,7 +92,7 @@ export default function Comments ({ parentId, comments, ...props }) {
return ( return (
<> <>
{comments.length ? <CommentsHeader handleSort={sort => getComments({ variables: { id: parentId, sort } })} /> : null} {comments.length ? <CommentsHeader commentSats={commentSats} handleSort={sort => getComments({ variables: { id: parentId, sort } })} /> : null}
{loading {loading
? <CommentsSkeleton /> ? <CommentsSkeleton />
: comments.map(item => ( : comments.map(item => (

View File

@ -112,7 +112,7 @@ export default function ItemFull ({ item, bio, ...props }) {
</div>)} </div>)}
{item.comments && {item.comments &&
<div className={styles.comments}> <div className={styles.comments}>
<Comments parentId={item.id} comments={item.comments} /> <Comments parentId={item.id} commentSats={item.commentSats} comments={item.comments} />
</div>} </div>}
</> </>
) )

View File

@ -22,6 +22,8 @@ export const ITEM_FIELDS = gql`
path path
meSats meSats
ncomments ncomments
commentSats
lastCommentAt
maxBid maxBid
company company
location location