forked from heyxyz/hey
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add trusted profile status badge (heyxyz#4555)
- Loading branch information
Showing
4 changed files
with
113 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import type { Handler } from 'express'; | ||
|
||
import logger from '@hey/lib/logger'; | ||
import catchedError from '@utils/catchedError'; | ||
import { TRUSTED_PROFILE_FEATURE_ID } from '@utils/constants'; | ||
import createClickhouseClient from '@utils/createClickhouseClient'; | ||
import prisma from '@utils/prisma'; | ||
import { noBody } from '@utils/responses'; | ||
|
||
export const get: Handler = async (req, res) => { | ||
const { id } = req.query; | ||
|
||
if (!id) { | ||
return noBody(res); | ||
} | ||
|
||
try { | ||
const prismaQuery = prisma.profileFeature.findFirst({ | ||
where: { | ||
enabled: true, | ||
featureId: TRUSTED_PROFILE_FEATURE_ID, | ||
profileId: id as string | ||
} | ||
}); | ||
|
||
const client = createClickhouseClient(); | ||
const clickhouseQuery = client | ||
.query({ | ||
format: 'JSONEachRow', | ||
query: ` | ||
SELECT count(*) as count | ||
FROM trusted_reports | ||
WHERE actor = '${id}' | ||
AND resolved = 1; | ||
` | ||
}) | ||
.then((rows) => rows.json<Array<{ count: string }>>()); | ||
|
||
const [data, result] = await Promise.all([prismaQuery, clickhouseQuery]); | ||
|
||
logger.info(`Trusted profile status fetched: ${id}`); | ||
|
||
return res.status(200).json({ | ||
isTrusted: data?.profileId === id, | ||
resolvedCount: Number(result[0]?.count) || 0, | ||
success: true | ||
}); | ||
} catch (error) { | ||
return catchedError(res, error); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import type { FC } from 'react'; | ||
|
||
import { ShieldCheckIcon } from '@heroicons/react/24/solid'; | ||
import { HEY_API_URL } from '@hey/data/constants'; | ||
import humanize from '@hey/lib/humanize'; | ||
import { Tooltip } from '@hey/ui'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import axios from 'axios'; | ||
|
||
interface TrustedBadgeProps { | ||
id: string; | ||
} | ||
|
||
const TrustedBadge: FC<TrustedBadgeProps> = ({ id }) => { | ||
const getTrustedProfileStatus = async (): Promise<{ | ||
isTrusted: boolean; | ||
resolvedCount: number; | ||
}> => { | ||
if (!id) { | ||
return { isTrusted: false, resolvedCount: 0 }; | ||
} | ||
|
||
const response = await axios.get(`${HEY_API_URL}/trusted/status`, { | ||
params: { id } | ||
}); | ||
const { data } = response; | ||
|
||
return data; | ||
}; | ||
|
||
const { data } = useQuery({ | ||
queryFn: getTrustedProfileStatus, | ||
queryKey: ['getTrustedProfileStatus', id] | ||
}); | ||
|
||
if (!data) { | ||
return null; | ||
} | ||
|
||
const { isTrusted, resolvedCount } = data; | ||
|
||
if (!isTrusted) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Tooltip | ||
content={ | ||
<span> | ||
Trusted profile: <b>{humanize(resolvedCount)} reports resolved</b> | ||
</span> | ||
} | ||
> | ||
<ShieldCheckIcon className="size-6 text-blue-500" /> | ||
</Tooltip> | ||
); | ||
}; | ||
|
||
export default TrustedBadge; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters