Skip to content

Commit

Permalink
feat: update profile reports feed
Browse files Browse the repository at this point in the history
  • Loading branch information
bigint committed Feb 14, 2024
1 parent 926518d commit 496bcec
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 127 deletions.
9 changes: 7 additions & 2 deletions apps/api/src/lib/feeds/providers/hey/algorithms/heyReports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@ import { Errors } from '@hey/data/errors';
import logger from '@hey/lib/logger';
import createClickhouseClient from 'src/lib/createClickhouseClient';

const heyReports = async (limit: number, offset: number): Promise<any[]> => {
const heyReports = async (
limit: number,
offset: number,
isTrusted = false
): Promise<any[]> => {
if (limit > 500) {
throw new Error(Errors.Limit500);
}

try {
const client = createClickhouseClient();
const table = isTrusted ? 'trusted_reports' : 'reports';
const rows = await client.query({
format: 'JSONEachRow',
query: `
SELECT
publication_id AS id,
count(*) as count
FROM reports
FROM ${table}
WHERE resolved = 0
GROUP BY publication_id
ORDER BY count DESC
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import { boolean, object, string } from 'zod';
type ExtensionRequest = {
id: string;
looksGood: boolean;
trusted: boolean;
};

const validationSchema = object({
id: string(),
looksGood: boolean()
looksGood: boolean(),
trusted: boolean()
});

export const post: Handler = async (req, res) => {
Expand All @@ -34,25 +36,24 @@ export const post: Handler = async (req, res) => {
return notAllowed(res);
}

const { id, looksGood } = body as ExtensionRequest;
const { id, looksGood, trusted } = body as ExtensionRequest;
const table = trusted ? 'trusted_reports' : 'reports';

try {
const client = createClickhouseClient();

if (looksGood) {
await client.command({
query: `DELETE FROM trusted_reports WHERE publication_id = '${id}';`
query: `DELETE FROM ${table} WHERE publication_id = '${id}';`
});
logger.info('Deleted report from trusted reports');

logger.info('Deleted report from reports');
} else {
await client.command({
query: `
ALTER TABLE trusted_reports
UPDATE resolved = 1
WHERE publication_id = '${id}';
`
query: `ALTER TABLE ${table} UPDATE resolved = 1 WHERE publication_id = '${id}';`
});
logger.info('Marked trusted report as resolved');

logger.info('Marked report as resolved');
}

return res.status(200).json({ success: true });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,29 @@ import createClickhouseClient from 'src/lib/createClickhouseClient';
import { noBody } from 'src/lib/responses';

export const get: Handler = async (req, res) => {
const { id } = req.query;
const { id, trusted } = req.query;

if (!id) {
return noBody(res);
}

const table = trusted === 'true' ? 'trusted_reports' : 'reports';

try {
const client = createClickhouseClient();
const rows = await client.query({
format: 'JSONEachRow',
query: `
SELECT
reason,
count(*) as count
FROM trusted_reports
SELECT reason, count(*) as count
FROM ${table}
WHERE publication_id = '${id}'
GROUP BY reason
ORDER BY count DESC;
`
});

const result = await rows.json<
Array<{
count: string;
reason: string;
}>
>();
logger.info(`Trusted report details fetched for ${id}`);
const result = await rows.json<Array<{ count: string; reason: string }>>();
logger.info(`Report details fetched for ${id}`);

return res.status(200).json({
result: result.map((row) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ import heyReports from 'src/lib/feeds/providers/hey/algorithms/heyReports';
export const get: Handler = async (req, res) => {
const limit = (parseInt(req.query?.limit as string) || 50) as number;
const offset = (parseInt(req.query?.offset as string) || 0) as number;
const trusted = req.query?.trusted as string;

const isTrusted = trusted === 'true';

try {
return res
.status(200)
.json({ ids: await heyReports(limit, offset), success: true });
return res.status(200).json({
ids: await heyReports(limit, offset, isTrusted),
success: true
});
} catch (error) {
return catchedError(res, error);
}
Expand Down
17 changes: 0 additions & 17 deletions apps/api/src/routes/trusted/publications.ts

This file was deleted.

24 changes: 14 additions & 10 deletions apps/web/src/components/Mod/Actions.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import GardenerActions from '@components/Publication/Actions/GardenerActions';
import TrustedProfilesActions from '@components/Publication/Actions/TrustedProfilesActions';
import { ModFeedType } from '@hey/data/enums';
import { type FC, useState } from 'react';
import { useFeatureFlagsStore } from 'src/store/persisted/useFeatureFlagsStore';

import TrustedReportDetails from './TrustedReportDetails';
import ReportDetails from './ReportDetails';

interface ActionsProps {
hideTrustedReport?: boolean;
publicationId: string;
type?: ModFeedType.REPORTS | ModFeedType.TRUSTED_REPORTS;
}

const Actions: FC<ActionsProps> = ({
hideTrustedReport = false,
publicationId
}) => {
const Actions: FC<ActionsProps> = ({ publicationId, type }) => {
const [expanded, setExpanded] = useState(true);
const trusted = useFeatureFlagsStore((state) => state.trusted);
const gardenerMode = useFeatureFlagsStore((state) => state.gardenerMode);

const isTrustedReport = type === ModFeedType.TRUSTED_REPORTS;
const isNormalReport = type === ModFeedType.REPORTS;

if (!expanded) {
return null;
}
Expand All @@ -30,18 +31,21 @@ const Actions: FC<ActionsProps> = ({
<div className="m-5 space-y-2">
<b>Gardener actions</b>
<GardenerActions
ableToRemoveReport={hideTrustedReport}
className="mt-3 max-w-md"
publicationId={publicationId}
setExpanded={setExpanded}
type={type}
/>
</div>
{hideTrustedReport && (
<TrustedReportDetails publicationId={publicationId} />
{(isTrustedReport || isNormalReport) && (
<ReportDetails
isTrustedReport={isTrustedReport}
publicationId={publicationId}
/>
)}
</>
)}
{trusted && !hideTrustedReport && (
{trusted && !isTrustedReport && !isNormalReport && (
<>
<div className="divider" />
<div className="m-5">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ import { HEY_API_URL } from '@hey/data/constants';
import { useQuery } from '@tanstack/react-query';
import axios from 'axios';

interface TrustedReportDetailsProps {
interface ReportDetailsProps {
isTrustedReport: boolean;
publicationId: string;
}

const TrustedReportDetails: FC<TrustedReportDetailsProps> = ({
const ReportDetails: FC<ReportDetailsProps> = ({
isTrustedReport,
publicationId
}) => {
const fetchTrustedReportDetails = async (): Promise<
{
count: number;
reason: string;
}[]
{ count: number; reason: string }[]
> => {
try {
const response = await axios.get(`${HEY_API_URL}/trusted/reportDetails`, {
params: { id: publicationId }
});
const response = await axios.get(
`${HEY_API_URL}/gardener/reportDetails`,
{ params: { id: publicationId, trusted: isTrustedReport } }
);

return response.data.result;
} catch {
Expand Down Expand Up @@ -51,4 +51,4 @@ const TrustedReportDetails: FC<TrustedReportDetailsProps> = ({
);
};

export default TrustedReportDetails;
export default ReportDetails;
14 changes: 6 additions & 8 deletions apps/web/src/components/Mod/ReportsFeed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import SinglePublication from '@components/Publication/SinglePublication';
import PublicationsShimmer from '@components/Shared/Shimmer/PublicationsShimmer';
import { SparklesIcon } from '@heroicons/react/24/outline';
import { HEY_API_URL } from '@hey/data/constants';
import { ModFeedType } from '@hey/data/enums';
import { LimitType, usePublicationsQuery } from '@hey/lens';
import { Card, EmptyState, ErrorMessage } from '@hey/ui';
import { useQuery } from '@tanstack/react-query';
Expand All @@ -20,12 +21,9 @@ const ReportsFeed: FC = () => {
const limit = LimitType.TwentyFive;
const offset = displayedPublications.length;

const getTrustedReportFeed = async (
limit: null | number,
offset: null | number
) => {
const getReportFeed = async (limit: null | number, offset: null | number) => {
try {
const response = await axios.get(`${HEY_API_URL}/mod/reports`, {
const response = await axios.get(`${HEY_API_URL}/gardener/reports`, {
params: { limit, offset }
});

Expand All @@ -40,8 +38,8 @@ const ReportsFeed: FC = () => {
error: algoError,
isLoading: algoLoading
} = useQuery({
queryFn: async () => await getTrustedReportFeed(25, offset),
queryKey: ['getTrustedReportFeed', 25, offset]
queryFn: async () => await getReportFeed(25, offset),
queryKey: ['getReportFeed', 25, offset]
});

const request: PublicationsRequest = {
Expand Down Expand Up @@ -100,7 +98,7 @@ const ReportsFeed: FC = () => {
showActions={false}
showThread={false}
/>
<Actions hideTrustedReport publicationId={publication.id} />
<Actions publicationId={publication.id} type={ModFeedType.REPORTS} />
</Card>
))}
<span ref={observe} />
Expand Down
10 changes: 7 additions & 3 deletions apps/web/src/components/Mod/TrustedReportsFeed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import SinglePublication from '@components/Publication/SinglePublication';
import PublicationsShimmer from '@components/Shared/Shimmer/PublicationsShimmer';
import { SparklesIcon } from '@heroicons/react/24/outline';
import { HEY_API_URL } from '@hey/data/constants';
import { ModFeedType } from '@hey/data/enums';
import { LimitType, usePublicationsQuery } from '@hey/lens';
import { Card, EmptyState, ErrorMessage } from '@hey/ui';
import { useQuery } from '@tanstack/react-query';
Expand All @@ -25,8 +26,8 @@ const TrustedReportsFeed: FC = () => {
offset: null | number
) => {
try {
const response = await axios.get(`${HEY_API_URL}/trusted/publications`, {
params: { limit, offset }
const response = await axios.get(`${HEY_API_URL}/gardener/reports`, {
params: { limit, offset, trusted: true }
});

return response.data.success ? response.data.ids : [];
Expand Down Expand Up @@ -100,7 +101,10 @@ const TrustedReportsFeed: FC = () => {
showActions={false}
showThread={false}
/>
<Actions hideTrustedReport publicationId={publication.id} />
<Actions
publicationId={publication.id}
type={ModFeedType.TRUSTED_REPORTS}
/>
</Card>
))}
<span ref={observe} />
Expand Down
Loading

0 comments on commit 496bcec

Please sign in to comment.