feat: add reviews, notifications, notes and account hardening
This commit is contained in:
@@ -22,6 +22,7 @@ const commentSchema = z.object({
|
||||
});
|
||||
const commentParamsSchema = z.object({ commentId: z.string().uuid() });
|
||||
const replySchema = z.object({ text: z.string().trim().min(1).max(4000) });
|
||||
const noteSchema = z.object({ body: z.string().max(20_000) });
|
||||
const communityQuerySchema = z.object({
|
||||
limit: z.coerce.number().int().min(1).max(100).default(50),
|
||||
offset: z.coerce.number().int().min(0).default(0),
|
||||
@@ -42,6 +43,54 @@ const canViewCourseComments = async (courseId: string, user: { id: string; role:
|
||||
};
|
||||
|
||||
export const learningRoutes: FastifyPluginAsync = async (app) => {
|
||||
const createNotification = async (input: { userId: string; kind: string; title: string; body: string; href: string }) => {
|
||||
await pool.query(
|
||||
'insert into notifications (user_id, kind, title, body, href) values ($1, $2, $3, $4, $5)',
|
||||
[input.userId, input.kind, input.title, input.body, input.href],
|
||||
);
|
||||
};
|
||||
|
||||
const lessonAccessible = async (lessonId: string, user: { id: string; role: string }) => {
|
||||
const result = await pool.query(
|
||||
`select l.id
|
||||
from lessons l
|
||||
join courses c on c.id = l.course_id
|
||||
where l.id = $1 and (c.status = 'published' or c.instructor_id = $2 or $3 = 'admin')`,
|
||||
[lessonId, user.id, user.role],
|
||||
);
|
||||
return result.rowCount === 1;
|
||||
};
|
||||
|
||||
app.get('/lessons/:lessonId/note', { preHandler: app.authenticate }, async (request, reply) => {
|
||||
const { lessonId } = lessonParamsSchema.parse(request.params);
|
||||
if (!(await lessonAccessible(lessonId, request.user))) return reply.code(404).send({ error: 'Aula não encontrada.' });
|
||||
const result = await pool.query<{ body: string; updatedAt: string | null }>(
|
||||
'select body, updated_at as "updatedAt" from lesson_notes where user_id = $1 and lesson_id = $2',
|
||||
[request.user.id, lessonId],
|
||||
);
|
||||
return { data: result.rows[0] || { body: '', updatedAt: null } };
|
||||
});
|
||||
|
||||
app.put('/lessons/:lessonId/note', { preHandler: app.authenticate }, async (request, reply) => {
|
||||
const { lessonId } = lessonParamsSchema.parse(request.params);
|
||||
const input = noteSchema.parse(request.body);
|
||||
if (!(await lessonAccessible(lessonId, request.user))) return reply.code(404).send({ error: 'Aula não encontrada.' });
|
||||
const result = await pool.query<{ body: string; updatedAt: string }>(
|
||||
`insert into lesson_notes (user_id, lesson_id, body)
|
||||
values ($1, $2, $3)
|
||||
on conflict (user_id, lesson_id) do update set body = excluded.body, updated_at = now()
|
||||
returning body, updated_at as "updatedAt"`,
|
||||
[request.user.id, lessonId, input.body],
|
||||
);
|
||||
return { data: result.rows[0] };
|
||||
});
|
||||
|
||||
app.delete('/lessons/:lessonId/note', { preHandler: app.authenticate }, async (request, reply) => {
|
||||
const { lessonId } = lessonParamsSchema.parse(request.params);
|
||||
await pool.query('delete from lesson_notes where user_id = $1 and lesson_id = $2', [request.user.id, lessonId]);
|
||||
return reply.code(204).send();
|
||||
});
|
||||
|
||||
// Public, paginated feed used by the community screen. Individual course
|
||||
// threads remain authenticated because they are part of the lesson area.
|
||||
app.get('/community/comments', async (request) => {
|
||||
@@ -165,6 +214,26 @@ export const learningRoutes: FastifyPluginAsync = async (app) => {
|
||||
join users u on u.id = $3`,
|
||||
[courseId, input.lessonId || null, request.user.id, input.text],
|
||||
);
|
||||
|
||||
const course = await pool.query<{ instructorId: string; title: string }>(
|
||||
'select instructor_id as "instructorId", title from courses where id = $1',
|
||||
[courseId],
|
||||
);
|
||||
const owner = course.rows[0];
|
||||
if (owner && owner.instructorId !== request.user.id) {
|
||||
try {
|
||||
await createNotification({
|
||||
userId: owner.instructorId,
|
||||
kind: 'question',
|
||||
title: `Nova pergunta em ${owner.title}`,
|
||||
body: `${request.user.name}: ${input.text.slice(0, 180)}`,
|
||||
href: input.lessonId ? `/cursos/${courseId}/aulas/${input.lessonId}` : '/comunidade',
|
||||
});
|
||||
} catch (error) {
|
||||
request.log.error(error, 'Could not create question notification');
|
||||
}
|
||||
}
|
||||
|
||||
return reply.code(201).send({ data: result.rows[0] });
|
||||
});
|
||||
|
||||
@@ -178,6 +247,28 @@ export const learningRoutes: FastifyPluginAsync = async (app) => {
|
||||
on conflict (comment_id) do update set author_id = excluded.author_id, body = excluded.body`,
|
||||
[commentId, request.user.id, input.text],
|
||||
);
|
||||
const target = await pool.query<{ authorId: string; courseId: string; lessonId: string | null; courseTitle: string }>(
|
||||
`select c.author_id as "authorId", c.course_id as "courseId", c.lesson_id as "lessonId", co.title as "courseTitle"
|
||||
from comments c
|
||||
join courses co on co.id = c.course_id
|
||||
where c.id = $1`,
|
||||
[commentId],
|
||||
);
|
||||
const comment = target.rows[0];
|
||||
if (comment && comment.authorId !== request.user.id) {
|
||||
try {
|
||||
await createNotification({
|
||||
userId: comment.authorId,
|
||||
kind: 'reply',
|
||||
title: 'Seu comentário recebeu uma resposta',
|
||||
body: `${request.user.name}: ${input.text.slice(0, 180)}`,
|
||||
href: comment.lessonId ? `/cursos/${comment.courseId}/aulas/${comment.lessonId}` : '/comunidade',
|
||||
});
|
||||
} catch (error) {
|
||||
request.log.error(error, 'Could not create reply notification');
|
||||
}
|
||||
}
|
||||
|
||||
await recordAudit({ actorId: request.user.id, action: 'comment.replied', subjectType: 'comment', subjectId: commentId, ipAddress: request.ip });
|
||||
return { data: { id: commentId } };
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user