Skip to content

Commit

Permalink
Noindex, nofollow spammers
Browse files Browse the repository at this point in the history
  • Loading branch information
IanPhilips committed Feb 2, 2024
1 parent 7f73e48 commit e3bb3b9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
13 changes: 13 additions & 0 deletions common/src/user.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { notification_preferences } from './user-notification-preferences'
import { ENV_CONFIG } from './envs/constants'
import { DAY_MS } from 'common/util/time'
import { run, SupabaseClient } from 'common/supabase/utils'

export type User = {
id: string
Expand Down Expand Up @@ -173,3 +174,15 @@ export const isUserLikelySpammer = (
(hasCreatedDashboard ?? false))
)
}

export const shouldIgnoreUserPage = async (user: User, db: SupabaseClient) => {
// lastBetTime isn't always reliable, so use the contract_bets table to be sure
const { data: bet } = await run(
db.from('contract_bets').select('bet_id').eq('user_id', user.id).limit(1)
)
return (
user.userDeleted ||
user.isBannedFromPosting ||
isUserLikelySpammer(user, bet.length > 0)
)
}
8 changes: 4 additions & 4 deletions politics/app/[username]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import type { Metadata, ResolvingMetadata } from 'next'
import UserPage from 'politics/app/[username]/user-page'
import { filterDefined } from 'common/util/array'
import Custom404 from 'politics/app/404/page'
import { shouldIgnoreUserPage } from 'common/user'
import { db } from 'web/lib/supabase/db'

export const revalidate = 60

Expand All @@ -16,17 +18,15 @@ export async function generateMetadata(
return {
title: `User @${props.params.username} not found`,
}
const ignore = await shouldIgnoreUserPage(user, db)
return {
title: `${user.name} (@${user.username})`,
description: user.bio ?? '',
openGraph: {
images: filterDefined([user.avatarUrl, ...previousImages]),
url: `/${user.username}`,
},
robots:
user.userDeleted || user.isBannedFromPosting
? 'noindex, nofollow'
: undefined,
robots: ignore ? 'noindex, nofollow' : undefined,
}
}
export default async function Page(props: { params: { username: string } }) {
Expand Down
10 changes: 7 additions & 3 deletions web/pages/[username]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import { BackButton } from 'web/components/contract/back-button'
import { useHeaderIsStuck } from 'web/hooks/use-header-is-stuck'
import { DailyLoan } from 'web/components/home/daily-loan'
import { getFullUserByUsername } from 'web/lib/supabase/users'
import { shouldIgnoreUserPage } from 'common/user'

export const getStaticProps = async (props: {
params: {
Expand All @@ -82,14 +83,15 @@ export const getStaticProps = async (props: {

const { count, rating } = (user ? await getUserRating(user.id) : null) ?? {}
const averageRating = user ? await getAverageUserRating(user.id) : undefined

const shouldIgnoreUser = user ? await shouldIgnoreUserPage(user, db) : false
return {
props: removeUndefinedProps({
user,
username,
rating: rating,
reviewCount: count,
averageRating: averageRating,
shouldIgnoreUser,
}),
// revalidate: 60 * 5, // Regenerate after 5 minutes
revalidate: 4,
Expand All @@ -106,6 +108,7 @@ export default function UserPage(props: {
rating?: number
reviewCount?: number
averageRating?: number
shouldIgnoreUser: boolean
}) {
const isAdmin = useAdmin()
const { user, ...profileProps } = props
Expand Down Expand Up @@ -141,8 +144,9 @@ function UserProfile(props: {
rating?: number
reviewCount?: number
averageRating?: number
shouldIgnoreUser: boolean
}) {
const { rating, reviewCount, averageRating } = props
const { rating, shouldIgnoreUser, reviewCount, averageRating } = props
const user = useUserById(props.user.id) ?? props.user
const isMobile = useIsMobile()
const router = useRouter()
Expand Down Expand Up @@ -198,7 +202,7 @@ function UserProfile(props: {
description={user.bio ?? ''}
url={`/${user.username}`}
/>
{(user.isBannedFromPosting || user.userDeleted) && (
{shouldIgnoreUser && (
<Head>
<meta name="robots" content="noindex, nofollow" />
</Head>
Expand Down

0 comments on commit e3bb3b9

Please sign in to comment.