make freebie comments visible case by case

This commit is contained in:
keyan 2023-06-03 20:01:50 -05:00
parent 56f08e0144
commit 369bd82a84
4 changed files with 38 additions and 12 deletions

View File

@ -17,17 +17,17 @@ async function comments (me, models, id, sort) {
let orderBy
switch (sort) {
case 'top':
orderBy = `ORDER BY ${await orderByNumerator(me, models)} DESC, "Item".msats DESC, "Item".id DESC`
orderBy = `ORDER BY ${await orderByNumerator(me, models)} DESC, "Item".msats DESC, ("Item".freebie IS FALSE) DESC, "Item".id DESC`
break
case 'recent':
orderBy = 'ORDER BY "Item".created_at DESC, "Item".msats DESC, "Item".id DESC'
orderBy = 'ORDER BY "Item".created_at DESC, "Item".msats DESC, ("Item".freebie IS FALSE) DESC, "Item".id DESC'
break
default:
orderBy = `ORDER BY ${await orderByNumerator(me, models)}/POWER(GREATEST(3, EXTRACT(EPOCH FROM (now_utc() - "Item".created_at))/3600), 1.3) DESC NULLS LAST, "Item".msats DESC, "Item".id DESC`
orderBy = `ORDER BY ${await orderByNumerator(me, models)}/POWER(GREATEST(3, EXTRACT(EPOCH FROM (now_utc() - "Item".created_at))/3600), 1.3) DESC NULLS LAST, "Item".msats DESC, ("Item".freebie IS FALSE) DESC, "Item".id DESC`
break
}
const filter = await filterClause(me, models)
const filter = await commentFilterClause(me, models)
if (me) {
const [{ item_comments_with_me: comments }] = await models.$queryRaw(
'SELECT item_comments_with_me($1, $2, $3, $4, $5)', Number(id), Number(me.id), COMMENT_DEPTH_LIMIT, filter, orderBy)
@ -110,6 +110,25 @@ export async function joinSatRankView (me, models) {
return 'JOIN sat_rank_tender_view ON "Item".id = sat_rank_tender_view.id'
}
export async function commentFilterClause (me, models) {
let clause = ` AND ("Item"."weightedVotes" - "Item"."weightedDownVotes" > -${ITEM_FILTER_THRESHOLD}`
if (me) {
const user = await models.user.findUnique({ where: { id: me.id } })
// wild west mode has everything
if (user.wildWestMode) {
return ''
}
// always include if it's mine
clause += ` OR "Item"."userId" = ${me.id}`
}
// close the clause
clause += ')'
return clause
}
export async function filterClause (me, models) {
// by default don't include freebies unless they have upvotes
let clause = ' AND (NOT "Item".freebie OR "Item"."weightedVotes" - "Item"."weightedDownVotes" > 0'

View File

@ -20,6 +20,7 @@ import Share from './share'
import ItemInfo from './item-info'
import { Badge } from 'react-bootstrap'
import { RootProvider, useRoot } from './root'
import { useMe } from './me'
function Parent ({ item, rootText }) {
const root = useRoot()
@ -87,7 +88,12 @@ export default function Comment ({
rootText, noComments, noReply, truncate, depth
}) {
const [edit, setEdit] = useState()
const [collapse, setCollapse] = useState(false)
const me = useMe()
const [collapse, setCollapse] = useState(
!me?.wildWestMode && !me?.greeterMode &&
!item.mine && item.freebie && item.wvotes <= 0
? 'yep'
: 'nope')
const ref = useRef(null)
const router = useRouter()
const root = useRoot()
@ -101,7 +107,7 @@ export default function Comment ({
query: { id: router.query.id }
}, undefined, { scroll: false })
}
setCollapse(localStorage.getItem(`commentCollapse:${item.id}`))
setCollapse(localStorage.getItem(`commentCollapse:${item.id}`) || collapse)
}, [item])
const bottomedOut = depth === COMMENT_DEPTH_LIMIT
@ -110,7 +116,7 @@ export default function Comment ({
return (
<div
ref={ref} className={includeParent ? '' : `${styles.comment} ${collapse ? styles.collapsed : ''}`}
ref={ref} className={includeParent ? '' : `${styles.comment} ${collapse === 'yep' ? styles.collapsed : ''}`}
>
<div className={`${itemStyles.item} ${styles.item}`}>
{item.meDontLike
@ -135,16 +141,16 @@ export default function Comment ({
onEdit={e => { setEdit(!edit) }}
editText={edit ? 'cancel' : 'edit'}
/>
{!includeParent && (collapse
{!includeParent && (collapse === 'yep'
? <Eye
className={styles.collapser} height={10} width={10} onClick={() => {
setCollapse(false)
localStorage.removeItem(`commentCollapse:${item.id}`)
setCollapse('nope')
localStorage.setItem(`commentCollapse:${item.id}`, 'nope')
}}
/>
: <EyeClose
className={styles.collapser} height={10} width={10} onClick={() => {
setCollapse(true)
setCollapse('yep')
localStorage.setItem(`commentCollapse:${item.id}`, 'yep')
}}
/>)}

View File

@ -69,7 +69,7 @@ export default function Comments ({ parentId, pinned, commentSats, comments, ...
document.querySelector(hash).scrollIntoView({ behavior: 'smooth' })
} catch {}
}
}, [])
}, [typeof window !== 'undefined' && window.location.hash])
const [loading, setLoading] = useState()
const [getComments] = useLazyQuery(COMMENTS_QUERY, {
fetchPolicy: 'network-only',

View File

@ -15,6 +15,7 @@ export const COMMENT_FIELDS = gql`
}
sats
upvotes
wvotes
boost
meSats
meDontLike