Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion server/src/controllers/client.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ const controllers = ({ strapi }: StrapiContext) => ({
const result = clientValidator.findAllInHierarchyValidator(config.enabledCollections, ctx.params.relation, ctx.query);
if (isRight(result)) {
return this.getService('common').findAllInHierarchy(
flatInput<clientValidator.FindAllInHierarchyValidatorSchema>(result.right),
flatInput<clientValidator.FindAllInHierarchyValidatorSchema>(
result.right
)
);
}
throw throwError(ctx, unwrapEither(result));
Expand Down
43 changes: 27 additions & 16 deletions server/src/services/common.service.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import { Params } from '@strapi/database/dist/entity-manager/types';
import { UID } from '@strapi/strapi';
import {
first,
get,
isNil,
isObject,
isString,
omit as filterItem,
parseInt,
uniq,
} from 'lodash';
import { first, get, isNil, isObject, isString, omit as filterItem, parseInt, uniq } from 'lodash';
import { isProfane, replaceProfanities } from 'no-profanity';
import { Id, PathTo, PathValue, RelatedEntity, StrapiContext } from '../@types';
import { CommentsPluginConfig } from '../config';
Expand Down Expand Up @@ -56,7 +47,7 @@ const commonService = ({ strapi }: StrapiContext) => ({
return user ? user.id != undefined : true;
},

sanitizeCommentEntity(entity: Comment | CommentWithRelated, blockedAuthors: string[], omitProps: Array<keyof Comment> = [], populate: any = {}): Comment {
sanitizeCommentEntity(entity: (Comment | CommentWithRelated) & { children?: Comment | CommentWithRelated[] }, blockedAuthors: string[], omitProps: Array<keyof Comment> = [], populate: any = {}): Comment {
const fieldsToPopulate = Array.isArray(populate) ? populate : Object.keys(populate || {});
return filterItem({
...buildAuthorModel(
Expand Down Expand Up @@ -119,10 +110,12 @@ const commonService = ({ strapi }: StrapiContext) => ({
where: {
threadOf: _.id,
},
pageSize: Infinity,
});
return {
id: _.id,
itemsInTread: total,
children: results,
firstThreadItemId: first(results)?.id,
};
}),
Expand All @@ -144,6 +137,9 @@ const commonService = ({ strapi }: StrapiContext) => ({
return this.sanitizeCommentEntity(
{
..._,
// @ts-ignore
children: (threadedItem?.children || [])
.map((item) => this.sanitizeCommentEntity(item, doNotPopulateAuthor, omit as Array<keyof Comment>, authorUserPopulate)),
threadOf: primitiveThreadOf || _.threadOf,
gotThread: (threadedItem?.itemsInTread || 0) > 0,
threadFirstItemId: threadedItem?.firstThreadItemId,
Expand Down Expand Up @@ -173,10 +169,25 @@ const commonService = ({ strapi }: StrapiContext) => ({
omit = [],
locale,
limit,
pagination,
}: clientValidator.FindAllInHierarchyValidatorSchema,
relatedEntity?: any,
) {
const entities = await this.findAllFlat({ filters, populate, sort, fields, isAdmin, omit, locale, limit }, relatedEntity);
const entities = await this.findAllFlat({
filters: {
threadOf: { $null: true }, // Only root comments and the next query will be fetch children
...filters,
},
pagination,
populate,
sort,
fields,
isAdmin,
omit,
locale,
limit,
}, relatedEntity);
// console.log('entities', entities);
return buildNestedStructure(
entities?.data,
startingFromId,
Expand Down Expand Up @@ -359,11 +370,11 @@ const commonService = ({ strapi }: StrapiContext) => ({
.updateMany({
where: {
related,
$or: [{ locale }, defaultLocale === locale ? { locale: { $eq: null } } : null].filter(Boolean)
$or: [{ locale }, defaultLocale === locale ? { locale: { $eq: null } } : null].filter(Boolean),
},
data: {
removed: true,
}
},
});
},

Expand All @@ -373,11 +384,11 @@ const commonService = ({ strapi }: StrapiContext) => ({
.updateMany({
where: {
related,
$or: [{ locale }, defaultLocale === locale ? { locale: { $eq: null } } : null].filter(Boolean)
$or: [{ locale }, defaultLocale === locale ? { locale: { $eq: null } } : null].filter(Boolean),
},
data: {
removed: false,
}
},
});
},

Expand Down
23 changes: 11 additions & 12 deletions server/src/services/utils/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ interface StrapiAuthorUser {
username: string;
email: string;
avatar?: string | object;

[key: string]: unknown;
}

export const buildNestedStructure = (
entities: Array<Comment | CommentWithRelated>,
entities: Array<Comment | CommentWithRelated & { children?: Array<Comment> }>,
id: Id | null = null,
field: string = 'threadOf',
dropBlockedThreads = false,
Expand All @@ -35,21 +36,19 @@ export const buildNestedStructure = (
(isObject(entityField) && (entityField as any).id === id)
);
})
.map((entity: Comment) => ({
.map((entity: Comment & { children?: Array<Comment> }) => ({
...entity,
[field]: undefined,
related: undefined,
blockedThread: blockNestedThreads || entity.blockedThread,
children:
entity.blockedThread && dropBlockedThreads
? []
: buildNestedStructure(
entities,
entity.id,
field,
dropBlockedThreads,
entity.blockedThread,
),
children: entity.blockedThread && dropBlockedThreads ? [] : entity.children ? entity.children :
buildNestedStructure(
entities,
entity.id,
field,
dropBlockedThreads,
entity.blockedThread,
),
}));

export const getRelatedGroups = (related: string): Array<string> =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const getBaseFindSchema = (enabledCollections: string[]) => {
blockedThread: true,
approvalStatus: true,
isAdminComment: true,
threadOf: true,
});
return z
.object({
Expand Down Expand Up @@ -117,6 +118,7 @@ export const findAllInHierarchyValidator = (enabledCollections: string[], relati
skip: true,
relation: true,
locale: true,
pagination: true,
})
.merge(z.object({
startingFromId: z.number().optional(),
Expand Down
10 changes: 5 additions & 5 deletions server/src/validators/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const filtersValidator = z.union([
endWithValidators,
containsValidators,
notContainsValidators,
z.object({ $null: z.string().min(1) }),
z.object({ $null: z.boolean() }),
z.object({ $notNull: z.boolean() }),
]);

Expand Down Expand Up @@ -115,11 +115,11 @@ export const queryPaginationSchema = z.object({ pageSize: stringToNumberValidato
getFiltersOperators({
blocked: true,
blockedThread: true,
})
).optional()
})
}),
).optional(),
}),
).optional(),
}))
}));

export const validate = <I, O>(result: z.SafeParseReturnType<I, O>) => {
if (!result.success) {
Expand Down