Skip to content

Commit

Permalink
feat: add trusted profile status badge (heyxyz#4555)
Browse files Browse the repository at this point in the history
  • Loading branch information
bigint authored Jan 23, 2024
2 parents 5add31e + 8edde79 commit e8615cd
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 4 deletions.
51 changes: 51 additions & 0 deletions apps/api/src/routes/trusted/status.ts
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);
}
};
2 changes: 2 additions & 0 deletions apps/web/src/components/Profile/Details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import MutualFollowersList from './MutualFollowers/List';
import ProBadge from './ProBadge';
import ScamWarning from './ScamWarning';
import TbaBadge from './TbaBadge';
import TrustedBadge from './TrustedBadge';

interface DetailsProps {
profile: Profile;
Expand Down Expand Up @@ -111,6 +112,7 @@ const Details: FC<DetailsProps> = ({ profile }) => {
) : null}
<TbaBadge address={profile.ownedBy.address} />
<ProBadge id={profile.id} />
<TrustedBadge id={profile.id} />
{hasMisused(profile.id) ? (
<Tooltip content={misuseDetails?.type}>
<ExclamationCircleIcon className="size-6 text-red-500" />
Expand Down
59 changes: 59 additions & 0 deletions apps/web/src/components/Profile/TrustedBadge.tsx
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;
5 changes: 1 addition & 4 deletions packages/lib/api/getProStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ const getProStatus = async (
});
const { data } = response;

return {
isBeliever: data.isBeliever,
isPro: data.isPro
};
return data;
};

export default getProStatus;

0 comments on commit e8615cd

Please sign in to comment.