Skip to content

Commit 5a4ef46

Browse files
feat: support non-strapi-users (#40)
1 parent 305f7cf commit 5a4ef46

File tree

15 files changed

+904
-84
lines changed

15 files changed

+904
-84
lines changed

@types/services.d.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ export interface IServiceAdmin {
2525

2626
export interface IServiceClient {
2727
kinds(): Promise<Array<AnyEntity>>;
28-
list(kind?: string, uid?: UID.ContentType, user?: StrapiUser, documentId?: Data.DocumentID, locale?: string): Promise<Array<AnyEntity>>;
29-
listPerUser(user: StrapiUser, kind?: string, populate?: StrapiQueryParamsParsed): Promise<Array<AnyEntity>>;
30-
create(kind: string, uid: UID.ContentType, user?: StrapiUser, documentId?: Data.DocumentID, locale?: string): Promise<AnyEntity>;
31-
delete(kind: string, uid: UID.ContentType, user?: StrapiUser, documentId?: Data.DocumentID, locale?: string): Promise<boolean>;
32-
toggle(kind: string, uid: UID.ContentType, user?: StrapiUser, documentId?: Data.DocumentID, locale?: string): Promise<AnyEntity | boolean>;
28+
list(kind?: string, uid?: UID.ContentType, user?: StrapiUser, documentId?: Data.DocumentID, locale?: string, authorId?: string): Promise<Array<AnyEntity>>;
29+
listPerUser(user: StrapiUser, userId: string, kind?: string, populate?: StrapiQueryParamsParsed): Promise<Array<AnyEntity>>;
30+
create(kind: string, uid: UID.ContentType, user?: StrapiUser, documentId?: Data.DocumentID, locale?: string, authorId?: string): Promise<AnyEntity>;
31+
delete(kind: string, uid: UID.ContentType, user?: StrapiUser, documentId?: Data.DocumentID, locale?: string, authorId?: string): Promise<boolean>;
32+
toggle(kind: string, uid: UID.ContentType, user?: StrapiUser, documentId?: Data.DocumentID, locale?: string, authorId?: string): Promise<AnyEntity | boolean>;
3333
prefetchConditions(props: PrefetchConditionsProps): Promise<[AnyEntity, AnyEntity]>;
34-
directCreate(uid: UID.ContentType, kind: AnyEntity, related: AnyEntity, user?: StrapiUser, locale?: string): Promise<AnyEntity>;
34+
directCreate(uid: UID.ContentType, kind: AnyEntity, related: AnyEntity, user?: StrapiUser, locale?: string, authorId?: string): Promise<AnyEntity>;
3535
directDelete(reactions: Array<CTReaction>, locale?: string): Promise<boolean>;
3636
}
3737

README.md

Lines changed: 59 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ For any role different than **Super Admin**, to access the **Reactions settings*
216216
"username": "Joe Doe",
217217
"email": "[email protected]",
218218
},
219+
"userId": "32", // Custom User ID provided if X-Reactions-Author header passed
219220
"createdAt": "2023-09-14T20:13:01.649Z",
220221
"updatedAt": "2023-09-14T20:13:01.670Z",
221222
}
@@ -260,7 +261,8 @@ _GraphQL equivalent: [Public GraphQL API -> List all reactions associated with C
260261

261262
Return all reactions associated with provided Collection / Single Type UID and Content Type Document ID with following combinations:
262263
- all - if you're not providing the user context via `Authorization` header
263-
- all related with user - if call is done with user context via `Authorization` header
264+
- all related with Strapi user - if call is done with user context via `Authorization` header
265+
- all related with non-Strapi user - if call is done with user ID via `X-Reactions-Author` header
264266

265267
**Example URL**: `https://localhost:1337/api/reactions/list/single/api::homepage.homepage?locale=en`
266268
**Example URL**: `https://localhost:1337/api/reactions/list/collection/api::post.post/njx99iv4p4txuqp307ye8625?locale=en`
@@ -278,11 +280,12 @@ Return all reactions associated with provided Collection / Single Type UID and C
278280
"slug": "like",
279281
"name": "Like"
280282
},
281-
"user":{ // Added if no user context provided to identify who made such reaction
283+
"user":{ // Added if Strapi user context provided to identify who made such reaction
282284
"documentId": "njx99iv4p4txuqp307ye8625",
283285
"username": "[email protected]",
284286
"email": "[email protected]"
285-
}
287+
},
288+
"userId": 17 // Added if non-Strapi user context provided to identify who made such reaction
286289
},
287290
// ...
288291
]
@@ -297,7 +300,8 @@ _GraphQL equivalent: [Public GraphQL API -> List all reactions associated with p
297300

298301
Return all reactions associated with provided user:
299302
- for logged in user - if call is done with user context via `Authorization` header
300-
- for specific ID - if you're not providing the user context via `Authorization` header
303+
- for specific Strapi user ID - if you're not providing the user context via `Authorization` header and you're providing the user context via `user-id` url param
304+
- for specific non-Strapi user ID - if you're not providing the user context via `Authorization` header and you're providing the user context via `X-Reactions-Author` header
301305

302306
**Example URL**: `https://localhost:1337/api/reactions/list/user`
303307
**Example URL**: `https://localhost:1337/api/reactions/list/user/1`
@@ -334,7 +338,8 @@ _GraphQL equivalent: [Public GraphQL API -> List all reactions of kind / type as
334338

335339
Return all reactions of specific kind / type associated with provided Collection / Single Type UID and Content Type Document ID with following combinations:
336340
- all - if you're not providing the user context via `Authorization` header
337-
- all related with user - if call is done with user context via `Authorization` header
341+
- all related with Strapi user - if call is done with user context via `Authorization` header
342+
- all related with non-Strapi user - if call is done with user ID via `X-Reactions-Author` header
338343

339344
**Example URL**: `https://localhost:1337/api/reactions/list/like/single/api::homepage.homepage?locale=en`
340345
**Example URL**: `https://localhost:1337/api/reactions/list/like/collection/api::post.post/njx99iv4p4txuqp307ye8625?locale=en`
@@ -347,12 +352,13 @@ Return all reactions of specific kind / type associated with provided Collection
347352
"documentId": "njx99iv4p4txuqp307ye8625",
348353
"createdAt": "2023-09-14T20:13:01.649Z",
349354
"updatedAt": "2023-09-14T20:13:01.670Z",
350-
"user":{ // Added if no user context provided to identify who made such reaction
355+
"user":{ // Added if Strapi user context provided to identify who made such reaction
351356
"documentId": "njx99iv4p4txuqp307ye8625",
352357
"username": "[email protected]",
353358
"email": "[email protected]"
354359
}
355360
},
361+
"userId": 17 // Added if non-Strapi user context provided to identify who made such reaction
356362
// ...
357363
]
358364
```
@@ -366,7 +372,8 @@ _GraphQL equivalent: [Public GraphQL API -> List all reactions of kind associate
366372

367373
Return all reactions of specific kind associated with provided user:
368374
- for logged in user - if call is done with user context via `Authorization` header
369-
- for specific ID - if you're not providing the user context via `Authorization` header
375+
- for specific Strapi user ID - if you're not providing the user context via `Authorization` header and you're providing the user context via `user-id` url param
376+
- for specific non-Strapi user ID - if you're not providing the user context via `Authorization` header and you're providing the user context via `X-Reactions-Author` header
370377

371378
**Example URL**: `https://localhost:1337/api/reactions/list/like/user`
372379
**Example URL**: `https://localhost:1337/api/reactions/list/like/user/1`
@@ -403,7 +410,10 @@ _GraphQL equivalent: [Public GraphQL API -> Set reaction for Content Type](#set-
403410

404411
Create reaction of specific kind / type associated with provided Collection / Single Type UID and Content Type Document ID.
405412

406-
`Authorization` header is required
413+
To identify the reaction author, you must provide **one** of the following headers:
414+
415+
- `Authorization` – use this header if you want to set a reaction for a Strapi user.
416+
- `X-Reactions-Author` – use this header if you want to set a reaction for a non-Strapi user. The value can be any identifier that uniquely distinguishes the reaction author in your system.
407417

408418
**Example URL**: `https://localhost:1337/api/reactions/set/like/single/api::homepage.homepage?locale=en`
409419
**Example URL**: `https://localhost:1337/api/reactions/set/like/collection/api::post.post/njx99iv4p4txuqp307ye8625?locale=en`
@@ -428,7 +438,10 @@ _GraphQL equivalent: [Public GraphQL API -> Unset reaction for Content Type](#un
428438

429439
Delete reaction of specific kind / type associated with provided Collection / Single Type UID and Content Type Document ID.
430440

431-
`Authorization` header is required
441+
To identify the reaction author, you must provide **one** of the following headers:
442+
443+
- `Authorization` – use this header if you want to set a reaction for a Strapi user.
444+
- `X-Reactions-Author` – use this header if you want to set a reaction for a non-Strapi user. The value can be any identifier that uniquely distinguishes the reaction author in your system.
432445

433446
**Example URL**: `https://localhost:1337/api/reactions/unset/like/single/api::homepage.homepage?locale=en`
434447
**Example URL**: `https://localhost:1337/api/reactions/unset/like/collection/api::post.post/njx99iv4p4txuqp307ye8625?locale=en`
@@ -448,7 +461,10 @@ _GraphQL equivalent: [Public GraphQL API -> Toggle reaction for Content Type](#t
448461

449462
Toggle reaction of specific kind / type associated with provided Collection / Single Type UID and Content Type Document ID.
450463

451-
`Authorization` header is required
464+
To identify the reaction author, you must provide **one** of the following headers:
465+
466+
- `Authorization` – use this header if you want to set a reaction for a Strapi user.
467+
- `X-Reactions-Author` – use this header if you want to set a reaction for a non-Strapi user. The value can be any identifier that uniquely distinguishes the reaction author in your system.
452468

453469
**Example URL**: `https://localhost:1337/api/reactions/toggle/like/single/api::homepage.homepage?locale=en`
454470
**Example URL**: `https://localhost:1337/api/reactions/toggle/like/collection/api::post.post/njx99iv4p4txuqp307ye8625?locale=en`
@@ -469,9 +485,9 @@ true
469485
```
470486

471487
### Possible scenarios
472-
1. No reaction set yet - after `toggle` reaction is set
473-
2. Single reaction already set - after `toogle` no reaction is set
474-
3. Multiple reactions already set - after `toggle` just specified reaction stays, rest becomes unset
488+
1. No reaction set yet - Calling `toggle` for a reaction sets that reaction.
489+
2. Same reaction already set - Calling `toggle` for a reaction that is already set will **unset** it (result: no reaction).
490+
3. Different reaction already set - If another reaction is set, calling `toggle` for a new reaction will **switch** to that reaction: the specified reaction becomes set, and any previously set reactions of that type are unset.
475491

476492
## 🕸️ Public GraphQL API specification
477493

@@ -522,8 +538,10 @@ query {
522538
_REST API equivalent: [Public REST API -> List all reactions associated with Content Type](#list-all-reactions-associated-with-content-type)_
523539

524540
Return all reactions associated with provided Collection / Single Type UID and Content Type Document ID with following combinations:
525-
- Query `reactionsList` - no `Authorization` header provided (open for public)
526-
- Query `reactionsListPerUser` - an `Authorization` header is mandatory
541+
- Query `reactionsList` – returns all reactions for the specified document. No `Authorization` header is required (public access).
542+
- Query `reactionsListPerUser` – returns reactions added by a specific author. One of the following headers is required:
543+
- `Authorization` – to fetch reactions for a Strapi user.
544+
- `X-Reactions-Author` – to fetch reactions for a non-Strapi user, identified by any custom author ID.
527545

528546
**Example request**
529547

@@ -600,7 +618,9 @@ query {
600618
_REST API equivalent: [Public REST API -> List all reactions associated with particular user](#list-all-reactions-associated-with-particular-user)_
601619

602620
Return all reactions associated with provided user:
603-
- Query `reactionsListAllPerUser` - an `Authorization` header is mandatory or `userId` in args
621+
- Query `reactionsListAllPerUser` – one of the following headers is required:
622+
- `Authorization` – to fetch reactions for a Strapi user.
623+
- `X-Reactions-Author` – to fetch reactions for a non-Strapi user, identified by any custom author ID.
604624

605625
**Example request**
606626

@@ -666,8 +686,11 @@ query {
666686
_REST API equivalent: [Public REST API -> List all reactions of kind / type associated with Content Type](#list-all-reactions-of-kind--type-associated-with-content-type)_
667687

668688
Return all reactions of specific kind / type associated with provided Collection / Single Type UID and Content Type Document ID with following combinations:
669-
- Query `reactionsList` - no `Authorization` header provided (open for public)
670-
- Query `reactionsListPerUser` - an `Authorization` header is mandatory
689+
- Query `reactionsList` – returns all reactions for the specified document. No `Authorization` header is required (public access).
690+
- Query `reactionsListPerUser` – returns reactions added by a specific author. One of the following headers is required:
691+
- `Authorization` – to fetch reactions for a Strapi user.
692+
- `X-Reactions-Author` – to fetch reactions for a non-Strapi user, identified by any custom author ID.
693+
671694

672695
**Example request**
673696

@@ -726,7 +749,9 @@ query {
726749
_REST API equivalent: [Public REST API -> List all reactions of kind associated with particular user](#list-all-reactions-of-kind-associated-with-particular-user)_
727750

728751
Return all reactions of specific kind associated with provided user:
729-
- Query `reactionsListAllPerUser` - an `Authorization` header is mandatory or `userId` in args
752+
- Query `reactionsListAllPerUser` – one of the following headers is required:
753+
- `Authorization` – to fetch reactions for a Strapi user.
754+
- `X-Reactions-Author` – to fetch reactions for a non-Strapi user, identified by any custom author ID.
730755

731756
**Example request**
732757

@@ -784,7 +809,10 @@ _REST API equivalent: [Public REST API -> Set reaction for Content Type](#set-re
784809

785810
Create reaction of specific kind / type associated with provided Collection / Single Type UID and Content Type Document ID.
786811

787-
`Authorization` header is required
812+
To identify the reaction author, you must provide **one** of the following headers:
813+
814+
- `Authorization` – use this header if you want to set a reaction for a Strapi user.
815+
- `X-Reactions-Author` – use this header if you want to set a reaction for a non-Strapi user. The value can be any identifier that uniquely distinguishes the reaction author in your system.
788816

789817
**Example request**
790818

@@ -821,7 +849,10 @@ _REST API equivalent: [Public REST API -> Unset reaction for Content Type](#unse
821849

822850
Delete reaction of specific kind / type associated with provided Collection / Single Type UID and Content Type Document ID.
823851

824-
`Authorization` header is required
852+
To identify the reaction author, you must provide **one** of the following headers:
853+
854+
- `Authorization` – use this header if you want to set a reaction for a Strapi user.
855+
- `X-Reactions-Author` – use this header if you want to set a reaction for a non-Strapi user. The value can be any identifier that uniquely distinguishes the reaction author in your system.
825856

826857
**Example request**
827858

@@ -858,7 +889,10 @@ _REST API equivalent: [Public REST API -> Toggle reaction for Content Type](#tog
858889

859890
Toggle reaction of specific kind / type associated with provided Collection / Single Type UID and Content Type Document ID.
860891

861-
`Authorization` header is required
892+
To identify the reaction author, you must provide **one** of the following headers:
893+
894+
- `Authorization` – use this header if you want to set a reaction for a Strapi user.
895+
- `X-Reactions-Author` – use this header if you want to set a reaction for a non-Strapi user. The value can be any identifier that uniquely distinguishes the reaction author in your system.
862896

863897
**Example request**
864898

@@ -900,9 +934,9 @@ mutation reactionToggle {
900934
```
901935

902936
### Possible scenarios
903-
1. No reaction set yet - after `toggle` reaction is set
904-
2. Single reaction already set - after `toogle` no reaction is set
905-
3. Multiple reactions already set - after `toggle` just specified reaction stays, rest becomes unset
937+
1. No reaction set yet - Calling `toggle` for a reaction sets that reaction.
938+
2. Same reaction already set - Calling `toggle` for a reaction that is already set will **unset** it (result: no reaction).
939+
3. Different reaction already set - If another reaction is set, calling `toggle` for a new reaction will **switch** to that reaction: the specified reaction becomes set, and any previously set reactions of that type are unset.
906940

907941
## 🔌 Enrich service for Strapi extensions
908942

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "strapi-plugin-reactions",
3-
"version": "2.1.10",
3+
"version": "2.2.0",
44
"description": "Strapi - Reactions plugin",
55
"strapi": {
66
"name": "reactions",
@@ -129,5 +129,6 @@
129129
"node": ">=18.0.0 <=22.x.x",
130130
"npm": ">=6.0.0"
131131
},
132-
"license": "MIT"
132+
"license": "MIT",
133+
"packageManager": "[email protected]"
133134
}

server/src/content-types/reaction.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ export default {
3636
target: "plugin::users-permissions.user",
3737
configurable: false,
3838
},
39+
userId: {
40+
type: "string",
41+
configurable: false,
42+
required: false,
43+
},
3944
kind: {
4045
type: 'relation',
4146
relation: "manyToOne",

0 commit comments

Comments
 (0)