0.12.19 : better notifications
This commit is contained in:
@@ -75,10 +75,74 @@ export default factories.createCoreController(
|
||||
}
|
||||
const message = await strapi
|
||||
.service("api::chat-message.chat-message")
|
||||
.createMessageInConversation(finalConversationId, userId, content, mediaId);
|
||||
.createMessageInConversation(
|
||||
finalConversationId,
|
||||
userId,
|
||||
content,
|
||||
mediaId
|
||||
);
|
||||
|
||||
const currentUser = await strapi.entityService.findOne(
|
||||
"plugin::users-permissions.user",
|
||||
userId,
|
||||
{ populate: { avatar: true } }
|
||||
);
|
||||
|
||||
const conversation = await strapi.entityService.findOne(
|
||||
"api::chat-conversation.chat-conversation",
|
||||
finalConversationId,
|
||||
{ populate: { users: true } }
|
||||
);
|
||||
|
||||
const conversationMembers = (conversation as any)?.users || [];
|
||||
const today = new Date();
|
||||
today.setHours(0, 0, 0, 0);
|
||||
|
||||
for (const member of conversationMembers) {
|
||||
if (member.id === userId) continue;
|
||||
|
||||
const existingNotifications = await strapi.entityService.findMany(
|
||||
"api::notification.notification",
|
||||
{
|
||||
filters: {
|
||||
$and: [
|
||||
{ type: "messageSent" },
|
||||
{ target_user: member.id },
|
||||
{ floodId: finalConversationId },
|
||||
],
|
||||
},
|
||||
sort: { createdAt: "desc" },
|
||||
}
|
||||
);
|
||||
|
||||
const hasNotificationToday = existingNotifications?.some(
|
||||
(notification: any) => {
|
||||
const notificationDate = new Date(notification.createdAt);
|
||||
notificationDate.setHours(0, 0, 0, 0);
|
||||
return notificationDate.getTime() === today.getTime();
|
||||
}
|
||||
);
|
||||
|
||||
if (!hasNotificationToday) {
|
||||
await strapi
|
||||
.service("api::notification.notification")
|
||||
?.createNotification({
|
||||
message: `Tu as reçu un message de ${currentUser?.name}`,
|
||||
type: "messageSent",
|
||||
target_user: member.id,
|
||||
source: "user",
|
||||
floodId: finalConversationId,
|
||||
payload: {
|
||||
avatar: (currentUser as any)?.avatar?.url,
|
||||
url: `/app/chat/${finalConversationId}`,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
ctx.body = {
|
||||
data: message,
|
||||
conversationId: finalConversationId,
|
||||
};
|
||||
} catch (error: any) {
|
||||
return ctx.badRequest(`Failed to create message: ${error.message}`);
|
||||
|
||||
Reference in New Issue
Block a user