Skip to content
Merged
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
51 changes: 25 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ In our minimum support we're following [official Node.js releases timelines](htt
> This plugin is designed for **Strapi v5**. To get support for other Strapi versions, please follow the [versions](#-versions) section.

**Plugin dependencies**
- `@strapi/plugin-graphql` - required to run GraphQL handled by this plugin

- `@strapi/plugin-graphql` - required to run GraphQL handled by this plugin

**We recommend always using the latest version of Strapi to start your new projects**.

Expand All @@ -134,7 +135,7 @@ On the dedicated page, you will be able to set up all crucial properties which d
To setup amend default plugin configuration we recommend to put following snippet as part of `config/plugins.{js|ts}` or `config/<env>/plugins.{js|ts}` file. If the file does not exist yet, you have to create it manually. If you've got already configurations for other plugins stores by this way, use just the `comments` part within exising `plugins` item.

```ts
module.exports = ({ env }) => ({
module.exports = ({ env }) => ({
//...
comments: {
enabled: true,
Expand Down Expand Up @@ -289,6 +290,7 @@ Return a hierarchical tree structure of comments for specified instance of Conte

- [field selection](https://docs.strapi.io/dev-docs/api/rest/populate-select#field-selection)
- [sorting](https://docs.strapi.io/dev-docs/api/rest/sort-pagination#sorting)
- [pagination](https://docs.strapi.io/dev-docs/api/rest/sort-pagination#pagination)

### Get Comments (flat structure)

Expand Down Expand Up @@ -666,7 +668,7 @@ query {
"id": "123456",
"name": "Joe Doe"
}
},
}
// ...
]
}
Expand Down Expand Up @@ -937,34 +939,32 @@ Lifecycle hooks can be register either in `register()` or `bootstrap()` methods

Listeners can by sync and `async`.

>Be aware that lifecycle hooks registered in `register()` may be fired by plugin's bootstrapping. If you want listen to events triggered after server's startup use `bootstrap()`.
> Be aware that lifecycle hooks registered in `register()` may be fired by plugin's bootstrapping. If you want listen to events triggered after server's startup use `bootstrap()`.

Example:

```ts
const commentsCommonService = strapi
.plugin("comments")
.service("common");
const commentsCommonService = strapi.plugin("comments").service("common");

commentsCommonService.registerLifecycleHook({
callback: async ({ action, result }) => {
const saveResult = await logIntoSystem(action, result);
commentsCommonService.registerLifecycleHook({
callback: async ({ action, result }) => {
const saveResult = await logIntoSystem(action, result);

console.log(saveResult);
},
contentTypeName: "comment",
hookName: "afterCreate",
});
console.log(saveResult);
},
contentTypeName: "comment",
hookName: "afterCreate",
});

commentsCommonService.registerLifecycleHook({
callback: async ({ action, result }) => {
const saveResult = await logIntoSystem(action, result);
commentsCommonService.registerLifecycleHook({
callback: async ({ action, result }) => {
const saveResult = await logIntoSystem(action, result);

console.log(saveResult);
},
contentTypeName: "report",
hookName: "afterCreate",
});
console.log(saveResult);
},
contentTypeName: "report",
hookName: "afterCreate",
});
```

## 💬 FAQ
Expand All @@ -977,14 +977,13 @@ Example:

```ts
module.exports = {
'comments': { enabled: true },
'graphql': { enabled: true },
comments: { enabled: true },
graphql: { enabled: true },
};
```

If you already got it, make sure that `comments` plugin is inserted before `graphql`. That should do the job.


## 🤝 Contributing to the plugin

Feel free to fork and make a Pull Request to this plugin project. All the input is warmly welcome!
Expand Down
59 changes: 37 additions & 22 deletions server/src/services/__tests__/common.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,17 +284,21 @@ describe('common.service', () => {
const strapi = getStrapi();
const service = getService(strapi);
const mockComments = [
{ id: 1, content: 'Parent 1', threadOf: null },
{ id: 2, content: 'Child 1', threadOf: 1 },
{ id: 3, content: 'Child 2', threadOf: 1 },
{ id: 4, content: 'Grandchild 1', threadOf: 2 },
{ id: 1, content: "Parent 1", threadOf: null },
{ id: 2, content: "Child 1", threadOf: "1" },
{ id: 3, content: "Child 2", threadOf: "1" },
{ id: 4, content: "Grandchild 1", threadOf: "2" },
];

mockCommentRepository.findMany.mockResolvedValue(mockComments);
caster<jest.Mock>(getOrderBy).mockReturnValue(['createdAt', 'desc']);
mockCommentRepository.findWithCount.mockResolvedValue({
results: mockComments,
pagination: { total: 4 },
mockCommentRepository.findWithCount.mockImplementation(async (args) => {
const threadOf = args?.where?.threadOf?.$eq ?? null;
const filtered = mockComments.filter((c) => c.threadOf === threadOf);
return {
results: filtered,
pagination: { total: filtered.length },
};
});
mockStoreRepository.getConfig.mockResolvedValue([]);

Expand Down Expand Up @@ -334,18 +338,25 @@ describe('common.service', () => {
const strapi = getStrapi();
const service = getService(strapi);
const mockComments = [
{ id: 2, content: 'Child 1', threadOf: 1 },
{ id: 3, content: 'Child 2', threadOf: 1 },
{ id: 4, content: 'Grandchild 1', threadOf: 2 },
{ id: 5, content: 'Grandchild 2', threadOf: 2 },
{ id: 6, content: 'Grandchild 3', threadOf: 4 },
{ id: 2, content: "Child 1", threadOf: "1" },
{ id: 3, content: "Child 2", threadOf: "1" },
{ id: 4, content: "Grandchild 1", threadOf: "2" },
{ id: 5, content: "Grandchild 2", threadOf: "2" },
{ id: 6, content: "Grandchild 3", threadOf: "4" },
];

mockCommentRepository.findMany.mockResolvedValue(mockComments);
caster<jest.Mock>(getOrderBy).mockReturnValue(['createdAt', 'desc']);
mockCommentRepository.findWithCount.mockResolvedValue({
results: mockComments,
pagination: { total: 3 },
mockCommentRepository.findWithCount.mockImplementation(async (args) => {
const threadOf =
args?.where?.threadOf?.$eq ??
args?.where?.threadOf.toString() ??
null;
const filtered = mockComments.filter((c) => c.threadOf === threadOf);
return {
results: filtered,
pagination: { total: filtered.length },
};
});
mockStoreRepository.getConfig.mockResolvedValue([]);

Expand All @@ -365,16 +376,20 @@ describe('common.service', () => {
const service = getService(strapi);
const mockComments = [
{ id: 1, content: 'Parent 1', threadOf: null, dropBlockedThreads: true, blockedThread: true },
{ id: 2, content: 'Child 1', threadOf: 1, dropBlockedThreads: false },
{ id: 3, content: 'Child 2', threadOf: 1, dropBlockedThreads: false },
{ id: 4, content: 'Grandchild 1', threadOf: 2, dropBlockedThreads: false },
{ id: 2, content: 'Child 1', threadOf: '1', dropBlockedThreads: false },
{ id: 3, content: 'Child 2', threadOf: '1', dropBlockedThreads: false },
{ id: 4, content: 'Grandchild 1', threadOf: '2', dropBlockedThreads: false },
];

mockCommentRepository.findMany.mockResolvedValue(mockComments);
caster<jest.Mock>(getOrderBy).mockReturnValue(['createdAt', 'desc']);
mockCommentRepository.findWithCount.mockResolvedValue({
results: mockComments,
pagination: { total: 4 },
mockCommentRepository.findWithCount.mockImplementation(async (args) => {
const threadOf = args?.where?.threadOf?.$eq ?? null;
const filtered = mockComments.filter((c) => c.threadOf === threadOf);
return {
results: filtered,
pagination: { total: filtered.length },
};
});
mockStoreRepository.getConfig.mockResolvedValue([]);

Expand Down Expand Up @@ -606,4 +621,4 @@ describe('common.service', () => {
expect(mockCommentRepository.updateMany).toHaveBeenCalled();
});
});
});
});
Loading