-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: profile reports on mod (#4643)
- Loading branch information
Showing
15 changed files
with
310 additions
and
69 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
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
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
12 changes: 8 additions & 4 deletions
12
apps/api/src/routes/trusted/publications.ts → apps/api/src/routes/gardener/reports.ts
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,79 @@ | ||
import type { Handler } from 'express'; | ||
|
||
import logger from '@hey/lib/logger'; | ||
import parseJwt from '@hey/lib/parseJwt'; | ||
import catchedError from 'src/lib/catchedError'; | ||
import createClickhouseClient from 'src/lib/createClickhouseClient'; | ||
import validateLensAccount from 'src/lib/middlewares/validateLensAccount'; | ||
import { invalidBody, noBody, notAllowed } from 'src/lib/responses'; | ||
import { object, string } from 'zod'; | ||
|
||
type ExtensionRequest = { | ||
id: string; | ||
reason: string; | ||
}; | ||
|
||
const validationSchema = object({ | ||
id: string(), | ||
reason: string() | ||
}); | ||
|
||
export const post: Handler = async (req, res) => { | ||
const { body } = req; | ||
|
||
if (!body) { | ||
return noBody(res); | ||
} | ||
|
||
const accessToken = req.headers['x-access-token'] as string; | ||
const validation = validationSchema.safeParse(body); | ||
|
||
if (!validation.success) { | ||
return invalidBody(res); | ||
} | ||
|
||
if (!(await validateLensAccount(req))) { | ||
return notAllowed(res); | ||
} | ||
|
||
const { id, reason } = body as ExtensionRequest; | ||
|
||
try { | ||
const payload = parseJwt(accessToken); | ||
const actor = payload.id; | ||
|
||
const client = createClickhouseClient(); | ||
|
||
// Do not allow to report the same publication twice | ||
const rows = await client.query({ | ||
format: 'JSONEachRow', | ||
query: ` | ||
SELECT * FROM reports | ||
WHERE publication_id = '${id}' | ||
AND actor = '${actor}' | ||
LIMIT 1; | ||
` | ||
}); | ||
|
||
const reports = await rows.json<Array<any>>(); | ||
|
||
if (reports.length > 0) { | ||
return res.status(200).json({ | ||
message: 'You already reported this publication!', | ||
success: false | ||
}); | ||
} | ||
|
||
const result = await client.insert({ | ||
format: 'JSONEachRow', | ||
table: 'reports', | ||
values: [{ actor, publication_id: id, reason }] | ||
}); | ||
|
||
logger.info(`Reported ${id} by profile: ${actor}`); | ||
|
||
return res.status(200).json({ id: result.query_id, 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
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
Oops, something went wrong.
68ff60b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
web – ./apps/web
web-heyxyz.vercel.app
hey.xyz
heyxyz.vercel.app
web-git-main-heyxyz.vercel.app