directus
183 строки · 4.1 Кб
1import { Action } from '@directus/constants';
2import { isDirectusError } from '@directus/errors';
3import express from 'express';
4import Joi from 'joi';
5import { ErrorCode, ForbiddenError, InvalidPayloadError } from '@directus/errors';
6import { respond } from '../middleware/respond.js';
7import useCollection from '../middleware/use-collection.js';
8import { validateBatch } from '../middleware/validate-batch.js';
9import { ActivityService } from '../services/activity.js';
10import { MetaService } from '../services/meta.js';
11import asyncHandler from '../utils/async-handler.js';
12import { getIPFromReq } from '../utils/get-ip-from-req.js';
13
14const router = express.Router();
15
16router.use(useCollection('directus_activity'));
17
18const readHandler = asyncHandler(async (req, res, next) => {
19const service = new ActivityService({
20accountability: req.accountability,
21schema: req.schema,
22});
23
24const metaService = new MetaService({
25accountability: req.accountability,
26schema: req.schema,
27});
28
29let result;
30
31if (req.singleton) {
32result = await service.readSingleton(req.sanitizedQuery);
33} else if (req.body.keys) {
34result = await service.readMany(req.body.keys, req.sanitizedQuery);
35} else {
36result = await service.readByQuery(req.sanitizedQuery);
37}
38
39const meta = await metaService.getMetaForQuery('directus_activity', req.sanitizedQuery);
40
41res.locals['payload'] = {
42data: result,
43meta,
44};
45
46return next();
47});
48
49router.search('/', validateBatch('read'), readHandler, respond);
50router.get('/', readHandler, respond);
51
52router.get(
53'/:pk',
54asyncHandler(async (req, res, next) => {
55const service = new ActivityService({
56accountability: req.accountability,
57schema: req.schema,
58});
59
60const record = await service.readOne(req.params['pk']!, req.sanitizedQuery);
61
62res.locals['payload'] = {
63data: record || null,
64};
65
66return next();
67}),
68respond,
69);
70
71const createCommentSchema = Joi.object({
72comment: Joi.string().required(),
73collection: Joi.string().required(),
74item: [Joi.number().required(), Joi.string().required()],
75});
76
77router.post(
78'/comment',
79asyncHandler(async (req, res, next) => {
80const service = new ActivityService({
81accountability: req.accountability,
82schema: req.schema,
83});
84
85const { error } = createCommentSchema.validate(req.body);
86
87if (error) {
88throw new InvalidPayloadError({ reason: error.message });
89}
90
91const primaryKey = await service.createOne({
92...req.body,
93action: Action.COMMENT,
94user: req.accountability?.user,
95ip: getIPFromReq(req),
96user_agent: req.accountability?.userAgent,
97origin: req.get('origin'),
98});
99
100try {
101const record = await service.readOne(primaryKey, req.sanitizedQuery);
102
103res.locals['payload'] = {
104data: record || null,
105};
106} catch (error: any) {
107if (isDirectusError(error, ErrorCode.Forbidden)) {
108return next();
109}
110
111throw error;
112}
113
114return next();
115}),
116respond,
117);
118
119const updateCommentSchema = Joi.object({
120comment: Joi.string().required(),
121});
122
123router.patch(
124'/comment/:pk',
125asyncHandler(async (req, res, next) => {
126const service = new ActivityService({
127accountability: req.accountability,
128schema: req.schema,
129});
130
131const { error } = updateCommentSchema.validate(req.body);
132
133if (error) {
134throw new InvalidPayloadError({ reason: error.message });
135}
136
137const primaryKey = await service.updateOne(req.params['pk']!, req.body);
138
139try {
140const record = await service.readOne(primaryKey, req.sanitizedQuery);
141
142res.locals['payload'] = {
143data: record || null,
144};
145} catch (error: any) {
146if (isDirectusError(error, ErrorCode.Forbidden)) {
147return next();
148}
149
150throw error;
151}
152
153return next();
154}),
155respond,
156);
157
158router.delete(
159'/comment/:pk',
160asyncHandler(async (req, _res, next) => {
161const service = new ActivityService({
162accountability: req.accountability,
163schema: req.schema,
164});
165
166const adminService = new ActivityService({
167schema: req.schema,
168});
169
170const item = await adminService.readOne(req.params['pk']!, { fields: ['action'] });
171
172if (!item || item['action'] !== Action.COMMENT) {
173throw new ForbiddenError();
174}
175
176await service.deleteOne(req.params['pk']!);
177
178return next();
179}),
180respond,
181);
182
183export default router;
184