Skip to content

Commit

Permalink
Update sendNotification function to manage thread seen_by_ids (#148)
Browse files Browse the repository at this point in the history
* Update sendNotification function to manage thread's seen_by_ids

* Reset battery level
  • Loading branch information
cp-radhika-s authored Jan 7, 2025
1 parent f5a787a commit 9861bdb
Showing 1 changed file with 11 additions and 19 deletions.
30 changes: 11 additions & 19 deletions functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,21 @@ exports.sendNotification = onDocumentCreated({
const senderName = senderData.first_name + ' ' + senderData.last_name;
const senderProfile = senderData.profile_image;

var documentSnapshot = await admin.firestore().collection('space_threads').doc(threadId).get();
const threadRef = admin.firestore().collection('space_threads').doc(threadId);
var documentSnapshot = await threadRef.get();

if (!documentSnapshot.exists) {
console.log('Thread does not exist');
return;
}

// update new message, time and seen by
await threadRef.update({
last_message: message,
last_message_at: snap.created_at,
seen_by_ids: [senderId],
});

const documentData = documentSnapshot.data();

const memberIds = documentData.member_ids.filter(memberId => memberId !== senderId);
Expand Down Expand Up @@ -385,6 +394,7 @@ exports.updateUserStateNotification = onDocumentCreated({
const userRef = db.collection('users').doc(userId);
await userRef.update({
state: 1,
battery_pct: 0,
updated_at: admin.firestore.Timestamp.now().toMillis()
});
console.log('User is not in network, updated state to 1:', userId);
Expand Down Expand Up @@ -528,21 +538,3 @@ exports.sendNewPlaceAddedNotification = onDocumentCreated({
console.error("Error sending place notification", error);
}
});

exports.updateLastMessageAndTimeInThread = onDocumentCreated({
document: "space_threads/{threadId}/thread_messages/{messageId}",
region: "asia-south1",
}, async event => {
const snap = event.data.data();
const threadId = event.params.threadId;

const message = snap.message;
const lastMessageAt = snap.created_at;

const threadRef = admin.firestore().collection('space_threads').doc(threadId);

await threadRef.update({
last_message: message,
last_message_at: lastMessageAt,
});
});

0 comments on commit 9861bdb

Please sign in to comment.