111 lines
5.5 KiB
JavaScript
111 lines
5.5 KiB
JavaScript
|
|
"use strict";
|
||
|
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
||
|
|
if (k2 === undefined) k2 = k;
|
||
|
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
||
|
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
||
|
|
desc = { enumerable: true, get: function() { return m[k]; } };
|
||
|
|
}
|
||
|
|
Object.defineProperty(o, k2, desc);
|
||
|
|
}) : (function(o, m, k, k2) {
|
||
|
|
if (k2 === undefined) k2 = k;
|
||
|
|
o[k2] = m[k];
|
||
|
|
}));
|
||
|
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
||
|
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
||
|
|
}) : function(o, v) {
|
||
|
|
o["default"] = v;
|
||
|
|
});
|
||
|
|
var __importStar = (this && this.__importStar) || (function () {
|
||
|
|
var ownKeys = function(o) {
|
||
|
|
ownKeys = Object.getOwnPropertyNames || function (o) {
|
||
|
|
var ar = [];
|
||
|
|
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
||
|
|
return ar;
|
||
|
|
};
|
||
|
|
return ownKeys(o);
|
||
|
|
};
|
||
|
|
return function (mod) {
|
||
|
|
if (mod && mod.__esModule) return mod;
|
||
|
|
var result = {};
|
||
|
|
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
||
|
|
__setModuleDefault(result, mod);
|
||
|
|
return result;
|
||
|
|
};
|
||
|
|
})();
|
||
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
|
exports.onDateReflectionRevealed = void 0;
|
||
|
|
const functions = __importStar(require("firebase-functions"));
|
||
|
|
const admin = __importStar(require("firebase-admin"));
|
||
|
|
const quietHours_1 = require("../notifications/quietHours");
|
||
|
|
const pruneTokens_1 = require("../notifications/pruneTokens");
|
||
|
|
/**
|
||
|
|
* Fires when a partner OPENS (reveals) the shared date reflections — their own reflection metadata doc
|
||
|
|
* flips `isRevealed` false → true (`couples/{coupleId}/date_reflections/{dateId}/answers/{userId}`).
|
||
|
|
* Notifies the OTHER partner that their reflection was read, mirroring daily's `onAnswerRevealed`:
|
||
|
|
* generic copy (no decrypted content, no name — the app renders the real name in-app), gated on
|
||
|
|
* `notifPartnerAnswered` + quiet hours (push suppressed in quiet hours, but the in-app record is kept).
|
||
|
|
*/
|
||
|
|
exports.onDateReflectionRevealed = functions.firestore
|
||
|
|
.document('couples/{coupleId}/date_reflections/{dateId}/answers/{userId}')
|
||
|
|
.onUpdate(async (change, context) => {
|
||
|
|
var _a, _b, _c, _d, _e;
|
||
|
|
const { coupleId, dateId, userId } = context.params;
|
||
|
|
const before = change.before.data();
|
||
|
|
const after = change.after.data();
|
||
|
|
// Only on the false → true reveal transition.
|
||
|
|
if (before.isRevealed === true || after.isRevealed !== true)
|
||
|
|
return;
|
||
|
|
const db = admin.firestore();
|
||
|
|
const coupleDoc = await db.collection('couples').doc(coupleId).get();
|
||
|
|
if (!coupleDoc.exists)
|
||
|
|
return;
|
||
|
|
const userIds = ((_b = (_a = coupleDoc.data()) === null || _a === void 0 ? void 0 : _a.userIds) !== null && _b !== void 0 ? _b : []);
|
||
|
|
if (!userIds.includes(userId))
|
||
|
|
return;
|
||
|
|
const partnerId = userIds.find((u) => u !== userId);
|
||
|
|
if (!partnerId)
|
||
|
|
return;
|
||
|
|
const partnerUserDoc = await db.collection('users').doc(partnerId).get();
|
||
|
|
if (((_c = partnerUserDoc.data()) === null || _c === void 0 ? void 0 : _c.notifPartnerAnswered) === false) {
|
||
|
|
console.log(`[onDateReflectionRevealed] ${partnerId} has partner notifications off`);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
const title = 'Your partner opened your reflection ✨';
|
||
|
|
const body = 'Open to see what you each wrote.';
|
||
|
|
const type = 'date_reflection_opened';
|
||
|
|
// In-app record (→ Together feed) — written regardless of quiet hours.
|
||
|
|
await db.collection('users').doc(partnerId).collection('notification_queue').add({
|
||
|
|
type, title, body, read: false, createdAt: admin.firestore.FieldValue.serverTimestamp(),
|
||
|
|
});
|
||
|
|
if ((0, quietHours_1.recipientInQuietHours)(partnerUserDoc.data())) {
|
||
|
|
console.log(`[onDateReflectionRevealed] ${partnerId} in quiet hours — push suppressed (in-app kept)`);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
const senderAvatar = (_d = (await db.collection('users').doc(userId).get()).data()) === null || _d === void 0 ? void 0 : _d.photoUrl;
|
||
|
|
const tokens = [];
|
||
|
|
const legacy = (_e = partnerUserDoc.data()) === null || _e === void 0 ? void 0 : _e.fcmToken;
|
||
|
|
if (typeof legacy === 'string' && legacy.length > 0)
|
||
|
|
tokens.push(legacy);
|
||
|
|
const tokenSnap = await db.collection('users').doc(partnerId).collection('fcmTokens').get();
|
||
|
|
tokenSnap.docs.forEach((d) => {
|
||
|
|
var _a;
|
||
|
|
const t = (_a = d.data()) === null || _a === void 0 ? void 0 : _a.token;
|
||
|
|
if (typeof t === 'string' && t.length > 0 && !tokens.includes(t))
|
||
|
|
tokens.push(t);
|
||
|
|
});
|
||
|
|
if (tokens.length === 0)
|
||
|
|
return;
|
||
|
|
const payload = {
|
||
|
|
notification: { title, body },
|
||
|
|
data: Object.assign({ type, couple_id: coupleId, date_id: dateId }, (typeof senderAvatar === 'string' && senderAvatar.length > 0
|
||
|
|
? { sender_avatar_url: senderAvatar }
|
||
|
|
: {})),
|
||
|
|
};
|
||
|
|
const results = await Promise.allSettled(tokens.map((token) => admin.messaging().send(Object.assign(Object.assign({}, payload), { token, android: { notification: { channelId: 'partner_activity' } } }))));
|
||
|
|
results.forEach((r, i) => {
|
||
|
|
if (r.status === 'rejected')
|
||
|
|
console.warn(`[onDateReflectionRevealed] FCM failed for ${tokens[i]}:`, r.reason);
|
||
|
|
});
|
||
|
|
await (0, pruneTokens_1.pruneDeadTokens)(db, partnerId, tokens, results);
|
||
|
|
});
|
||
|
|
//# sourceMappingURL=onDateReflectionRevealed.js.map
|