Hey there!
Creating a followers/following functionality in Strapi involves setting up relationships between your user model and creating the necessary logic to manage these relationships. Here’s a step-by-step guide to help you implement this:
Step 1: Define User Model
Ensure you have a User model. Strapi provides a default User model if you have the Users & Permissions plugin installed.
Step 2: Add Relationships
You need to add self-referencing many-to-many relationships to the User model for followers and following.
- Navigate to Content-Types Builder:
- Go to Strapi’s admin panel.
- Click on Content-Types Builder.
- Modify the User Model:
- Select the User model.
- Add a new field for
followers
:
- Field Name:
followers
- Field Type:
Relation
- Relation: User → Many-to-Many → User
- Add another new field for
following
:
- Field Name:
following
- Field Type:
Relation
- Relation: User → Many-to-Many → User
- Save and Restart:
- Save your changes and restart Strapi to apply the changes.
Step 3: Create Endpoints
You need to create custom endpoints to handle the follow and unfollow actions.
- Create a Custom Controller:
- In your Strapi project, navigate to
./api/user/controllers
.
- Create a new file named
customUser.js
.
- Implement Follow/Unfollow Logic:
- Add the following code to
customUser.js
:
javascript
Copy code
module.exports = {
async follow(ctx) {
const { userId, followId } = ctx.request.body;
if (!userId || !followId) {
return ctx.badRequest('User ID and Follow ID are required.');
}
const user = await strapi.query('user', 'users-permissions').findOne({ id: userId });
const followUser = await strapi.query('user', 'users-permissions').findOne({ id: followId });
if (!user || !followUser) {
return ctx.notFound('User not found.');
}
await strapi.query('user', 'users-permissions').update({ id: userId }, {
following: [...user.following, followId]
});
await strapi.query('user', 'users-permissions').update({ id: followId }, {
followers: [...followUser.followers, userId]
});
return ctx.send({ message: 'Followed successfully.' });
},
async unfollow(ctx) {
const { userId, followId } = ctx.request.body;
if (!userId || !followId) {
return ctx.badRequest('User ID and Follow ID are required.');
}
const user = await strapi.query('user', 'users-permissions').findOne({ id: userId });
const followUser = await strapi.query('user', 'users-permissions').findOne({ id: followId });
if (!user || !followUser) {
return ctx.notFound('User not found.');
}
await strapi.query('user', 'users-permissions').update({ id: userId }, {
following: user.following.filter(id => id !== followId)
});
await strapi.query('user', 'users-permissions').update({ id: followId }, {
followers: followUser.followers.filter(id => id !== userId)
});
return ctx.send({ message: 'Unfollowed successfully.' });
}
};
Step 4: Create Routes
You need to create routes for the follow and unfollow actions.
- Create a Custom Route:
- In your Strapi project, navigate to
./api/user/config/routes.json
.
- Add Routes for Follow/Unfollow:
- Add the following routes to
routes.json
:
json
Copy code
{
"routes": [
{
"method": "POST",
"path": "/users/follow",
"handler": "customUser.follow",
"config": {
"policies": []
}
},
{
"method": "POST",
"path": "/users/unfollow",
"handler": "customUser.unfollow",
"config": {
"policies": []
}
}
]
}
Step 5: Test Your Endpoints
Now you can test your follow and unfollow endpoints using a tool like Postman or directly in your frontend.
- Follow Endpoint:
- URL:
http://localhost:1337/users/follow
- Method: POST
- Body:
{ "userId": "USER_ID", "followId": "FOLLOW_ID" }
- Unfollow Endpoint:
- URL:
http://localhost:1337/users/unfollow
- Method: POST
- Body:
{ "userId": "USER_ID", "followId": "FOLLOW_ID" }
This should get you started with implementing followers/following functionality in your social media app using Strapi. Let me know if you need any further assistance!