I am implementing a like
feature, the user can like and unlike a thread.
The thread model is like this:
const ThreadSchema = new mongoose.Schema({
author: {
id: { type: ObjectID, required: true, ref: 'User' },
screen_name: { type: String, required: true },
picture: { type: String, required: true },
},
theme: { type: String },
text: { type: String, required: true },
comments: {
count: { type: Number, default: 0 },
},
likes: {
count: { type: Number, default: 0 },
users: [{
_id: false,
id: { type: ObjectID, required: true, ref: 'User' },
screen_name: { type: String, required: true },
picture: { type: String, required: true },
}],
},
}, {
timestamps: true,
});
Something tricky is that when the user sends the request to get the thread information, I need to tell the client whether this user has liked this thread before or not.
Does that mean that every time when the client sends a request to get the thread, I need to loop through thread.likes.users
to find whether his ID is there? This check seems pretty heavy especially when the user tries to retrieve a list...
Any better solution for this?
The requirement is simple, just before the user even clicks the like
button, the state should already there to indicate if this thread has been liked by this user or not.