like & follow notifs
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -9,6 +9,7 @@ exports.refList = {
|
|||||||
playlists: admin.firestore().collection("playlists"),
|
playlists: admin.firestore().collection("playlists"),
|
||||||
tasks: admin.firestore().collection("tasks"),
|
tasks: admin.firestore().collection("tasks"),
|
||||||
notifications: admin.firestore().collection("notifications"),
|
notifications: admin.firestore().collection("notifications"),
|
||||||
|
users: admin.firestore().collection("users"),
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.ALERT_TYPE = {
|
exports.ALERT_TYPE = {
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
const { onDocumentCreated } = require("firebase-functions/v2/firestore");
|
const {
|
||||||
|
onDocumentCreated,
|
||||||
|
onDocumentWritten,
|
||||||
|
} = require("firebase-functions/v2/firestore");
|
||||||
const admin = require("firebase-admin");
|
const admin = require("firebase-admin");
|
||||||
const { refList, ALERT_TYPE } = require("../index");
|
const { refList, ALERT_TYPE } = require("../index");
|
||||||
const { Expo } = require("expo-server-sdk");
|
const { Expo } = require("expo-server-sdk");
|
||||||
@@ -315,3 +318,157 @@ exports.createProjectCommentNotification = onDocumentCreated(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
exports.createProjectLikeNotification = onDocumentWritten(
|
||||||
|
{
|
||||||
|
region: "europe-west1",
|
||||||
|
document: "projects/{projectId}",
|
||||||
|
},
|
||||||
|
async (event) => {
|
||||||
|
try {
|
||||||
|
const { projectId } = event.params || {};
|
||||||
|
const before = event?.data?.before?.data() || {};
|
||||||
|
const after = event?.data?.after?.data() || {};
|
||||||
|
|
||||||
|
if (!projectId || !after) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const ownerId = after.userId || null;
|
||||||
|
if (!ownerId) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const beforeLikes = Array.isArray(before?.likedBy) ? before.likedBy : [];
|
||||||
|
const afterLikes = Array.isArray(after?.likedBy) ? after.likedBy : [];
|
||||||
|
|
||||||
|
if (afterLikes.length <= beforeLikes.length) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const beforeSet = new Set(beforeLikes);
|
||||||
|
const newLikers = afterLikes.filter((uid) => !beforeSet.has(uid));
|
||||||
|
|
||||||
|
if (!newLikers.length) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const projectTitle =
|
||||||
|
typeof after.title === "string" && after.title.trim()
|
||||||
|
? after.title.trim()
|
||||||
|
: "ton projet";
|
||||||
|
|
||||||
|
await Promise.all(
|
||||||
|
newLikers.map(async (likerId) => {
|
||||||
|
if (!likerId || likerId === ownerId) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const likerSnap = await refList.users.doc(likerId).get();
|
||||||
|
const liker = likerSnap?.data() || {};
|
||||||
|
const likerName =
|
||||||
|
typeof liker?.userName === "string" && liker.userName.trim()
|
||||||
|
? liker.userName.trim()
|
||||||
|
: "Un utilisateur";
|
||||||
|
|
||||||
|
const message = `${likerName} a aimé ton projet "${projectTitle}"`;
|
||||||
|
|
||||||
|
await sendNotification({
|
||||||
|
sender: likerId,
|
||||||
|
receiver: ownerId,
|
||||||
|
receiverCollection: "users",
|
||||||
|
title: "Nouveau like",
|
||||||
|
message,
|
||||||
|
data: {
|
||||||
|
type: ALERT_TYPE?.NEW_LIKE,
|
||||||
|
projectId,
|
||||||
|
likerId,
|
||||||
|
likerName,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return null;
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
return null;
|
||||||
|
} catch (error) {
|
||||||
|
console.log("[createProjectLikeNotification] error:", error);
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
exports.createNewFollowerNotification = onDocumentWritten(
|
||||||
|
{
|
||||||
|
region: "europe-west1",
|
||||||
|
document: "users/{userId}",
|
||||||
|
},
|
||||||
|
async (event) => {
|
||||||
|
try {
|
||||||
|
const { userId } = event.params || {};
|
||||||
|
const before = event?.data?.before?.data() || {};
|
||||||
|
const after = event?.data?.after?.data() || {};
|
||||||
|
|
||||||
|
if (!userId || !after) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const beforeFollowers = Array.isArray(before?.followedBy)
|
||||||
|
? before.followedBy
|
||||||
|
: [];
|
||||||
|
const afterFollowers = Array.isArray(after?.followedBy)
|
||||||
|
? after.followedBy
|
||||||
|
: [];
|
||||||
|
|
||||||
|
if (afterFollowers.length <= beforeFollowers.length) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const previousSet = new Set(beforeFollowers);
|
||||||
|
const newFollowers = afterFollowers.filter((uid) => !previousSet.has(uid));
|
||||||
|
|
||||||
|
if (!newFollowers.length) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
await Promise.all(
|
||||||
|
newFollowers.map(async (followerId) => {
|
||||||
|
if (!followerId || followerId === userId) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const followerSnap = await refList.users.doc(followerId).get();
|
||||||
|
const follower = followerSnap?.data() || {};
|
||||||
|
const followerName =
|
||||||
|
typeof follower?.userName === "string" && follower.userName.trim()
|
||||||
|
? follower.userName.trim()
|
||||||
|
: "Un utilisateur";
|
||||||
|
|
||||||
|
const message = `${followerName} te suit maintenant`;
|
||||||
|
|
||||||
|
await sendNotification({
|
||||||
|
sender: followerId,
|
||||||
|
receiver: userId,
|
||||||
|
receiverCollection: "users",
|
||||||
|
title: "Nouvel abonné",
|
||||||
|
message,
|
||||||
|
data: {
|
||||||
|
type: ALERT_TYPE?.NEW_FOLLOWER,
|
||||||
|
followerId,
|
||||||
|
followerName,
|
||||||
|
followerProfilePicture: follower?.profilePicture || "",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return null;
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
return null;
|
||||||
|
} catch (error) {
|
||||||
|
console.log("[createNewFollowerNotification] error:", error);
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user