Skip to content

Commit 51ec01e

Browse files
feat: use middleware instead of db hooks to remove comments (#339)
* feat: use middleware instead of db hooks to remove comments * chore: use correct lockfile * feat: filter uid without api prefix
1 parent 41d65c7 commit 51ec01e

File tree

5 files changed

+19
-56
lines changed

5 files changed

+19
-56
lines changed

admin/src/components/CommentStatusFilters/index.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@ const getFilter = (filterName: string | undefined) => {
1717
switch (filterName) {
1818
case COMMENT_STATUS.BLOCKED:
1919
return {
20-
$or: [{ blocked: { $eq: true } }, { blockedThread: { $eq: true } }],
20+
$or: [
21+
{ blocked: true },
22+
{ blockedThread: true },
23+
],
2124
};
2225
case COMMENT_STATUS.REMOVED:
2326
return {
24-
$or: [{ blocked: { $eq: true } }, { blockedThread: { $eq: true } }],
27+
removed: true,
2528
};
2629
case COMMENT_STATUS.OPEN:
2730
return {
@@ -31,9 +34,9 @@ const getFilter = (filterName: string | undefined) => {
3134
return {};
3235
default:
3336
return {
34-
approvalStatus: { $eq: filterName },
37+
approvalStatus: filterName,
3538
};
36-
}
39+
};
3740
};
3841

3942
export const CommentsStatusFilters = () => {

server/src/bootstrap.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { StrapiContext } from './@types';
22
import { setupGQL } from './graphql';
33
import permissions from './permissions';
4-
import { getPluginService } from './utils/getPluginService';
54

65
export default async ({ strapi }: StrapiContext) => {
76
if (strapi.plugin('graphql')) {
@@ -54,20 +53,4 @@ export default async ({ strapi }: StrapiContext) => {
5453
];
5554

5655
await strapi.admin.services.permission.actionProvider.registerMany(actions);
57-
58-
const commonService = getPluginService(strapi, 'common');
59-
strapi.db.lifecycles.subscribe({
60-
afterDelete: async (event) => {
61-
const uid = event.model.uid;
62-
const { documentId, locale } = event.result;
63-
const relation = [uid, documentId].join(':');
64-
await commonService.perRemove(relation, locale);
65-
},
66-
afterCreate: async (event) => {
67-
const uid = event.model.uid;
68-
const { documentId, locale } = event.result;
69-
const relation = [uid, documentId].join(':');
70-
await commonService.perRestore(relation, locale);
71-
}
72-
});
7356
};

server/src/register/index.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
import { StrapiContext } from '../@types';
22
import { registerCustomFields } from './custom-fields';
3+
import { getPluginService } from '../utils/getPluginService';
34

45
const register = (context: StrapiContext) => {
56
registerCustomFields(context);
6-
};
77

8+
const commonService = getPluginService(context.strapi, 'common');
9+
context.strapi.documents.use(async (ctx, next) => {
10+
if (ctx.action === 'delete' && ctx.uid.startsWith('api::')) {
11+
const { params: { locale, documentId }, uid } = ctx;
12+
const relation = [uid, documentId].join(":");
13+
await commonService.perRemove(relation, locale);
14+
}
15+
return next();
16+
});
17+
};
818

9-
export default register;
19+
export default register;

server/src/services/__tests__/common.service.test.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -605,23 +605,5 @@ describe('common.service', () => {
605605
expect(result).toEqual({ count: 2});
606606
expect(mockCommentRepository.updateMany).toHaveBeenCalled();
607607
});
608-
609-
it('should resolve comments from deleted if related entry is restore', async () => {
610-
const strapi = getStrapi();
611-
const service = getService(strapi);
612-
const mockComments = [
613-
{ id: 1, related: 'api::test.test:1', locale: 'en' },
614-
{ id: 1, related: 'api::test.test:1', locale: 'en' }
615-
];
616-
const mockRelatedEntities = { uid: 'api::test.test', documentId: '1', locale: 'en', title: 'Test Title 1' };
617-
618-
mockCommentRepository.findMany.mockResolvedValue(mockComments)
619-
mockCommentRepository.updateMany.mockResolvedValue({ count: 2 });
620-
621-
const result = await service.perRestore([mockRelatedEntities.uid, mockRelatedEntities.documentId].join(':'));
622-
623-
expect(result).toEqual({ count: 2});
624-
expect(mockCommentRepository.updateMany).toHaveBeenCalled();
625-
});
626608
});
627609
});

server/src/services/common.service.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -367,21 +367,6 @@ const commonService = ({ strapi }: StrapiContext) => ({
367367
});
368368
},
369369

370-
async perRestore(related: string, locale?: string) {
371-
const defaultLocale = await strapi.plugin('i18n')?.service('locales').getDefaultLocale() || null;
372-
return getCommentRepository(strapi)
373-
.updateMany({
374-
where: {
375-
related,
376-
$or: [{ locale }, defaultLocale === locale ? { locale: { $eq: null } } : null].filter(Boolean)
377-
},
378-
data: {
379-
removed: false,
380-
}
381-
});
382-
},
383-
384-
385370
registerLifecycleHook(/*{ callback, contentTypeName, hookName }*/) {
386371
},
387372

0 commit comments

Comments
 (0)