113 lines
5.8 KiB
JavaScript
113 lines
5.8 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.onAnswerRevealed = void 0;
|
|
const admin = __importStar(require("firebase-admin"));
|
|
const firestore_1 = require("firebase-functions/v2/firestore");
|
|
const quietHours_1 = require("../notifications/quietHours");
|
|
const push_1 = require("../notifications/push");
|
|
const idempotency_1 = require("../notifications/idempotency");
|
|
const log_1 = require("../log");
|
|
/**
|
|
* Firestore trigger: when one partner OPENS (reveals) the shared answers — i.e. their own
|
|
* daily answer doc flips isRevealed false → true — notify the other partner that they've
|
|
* looked, so the pair stays in sync ("[Name] just opened your answers").
|
|
*
|
|
* Path: couples/{coupleId}/daily_question/{date}/answers/{userId}
|
|
*/
|
|
exports.onAnswerRevealed = (0, firestore_1.onDocumentUpdated)('couples/{coupleId}/daily_question/{date}/answers/{userId}', async (event) => {
|
|
var _a, _b, _c, _d, _e, _f, _g;
|
|
const { coupleId, date, userId } = event.params;
|
|
const before = ((_b = (_a = event.data) === null || _a === void 0 ? void 0 : _a.before.data()) !== null && _b !== void 0 ? _b : {});
|
|
const after = ((_d = (_c = event.data) === null || _c === void 0 ? void 0 : _c.after.data()) !== null && _d !== void 0 ? _d : {});
|
|
// Only fire 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) {
|
|
log_1.logger.warn(`[onAnswerRevealed] couple ${coupleId} not found`);
|
|
return;
|
|
}
|
|
const userIds = ((_f = (_e = coupleDoc.data()) === null || _e === void 0 ? void 0 : _e.userIds) !== null && _f !== void 0 ? _f : []);
|
|
if (!userIds.includes(userId)) {
|
|
log_1.logger.warn(`[onAnswerRevealed] revealer ${userId} not a member of ${coupleId}`);
|
|
return;
|
|
}
|
|
const partnerId = userIds.find((uid) => uid !== userId);
|
|
if (!partnerId) {
|
|
log_1.logger.warn(`[onAnswerRevealed] no partner for couple ${coupleId}`);
|
|
return;
|
|
}
|
|
const partnerUserDoc = await db.collection('users').doc(partnerId).get();
|
|
const partnerData = partnerUserDoc.data();
|
|
// Respect the same partner-activity opt-out as the answered ping.
|
|
if ((partnerData === null || partnerData === void 0 ? void 0 : partnerData.notifPartnerAnswered) === false) {
|
|
log_1.logger.log(`[onAnswerRevealed] partner ${partnerId} has partner-activity notifications off`);
|
|
return;
|
|
}
|
|
// M-001: honor the recipient's quiet-hours window ("no notifications" promise). Fail-open.
|
|
if ((0, quietHours_1.recipientInQuietHours)(partnerData)) {
|
|
log_1.logger.log(`[onAnswerRevealed] partner ${partnerId} is in quiet hours — suppressing`);
|
|
return;
|
|
}
|
|
// Dedupe redelivery of this reveal (at-least-once). The false→true guard above already stops
|
|
// the marker write from re-triggering this handler; the claim stops a redelivered event.
|
|
if (!(await (0, idempotency_1.claimOnce)((0, idempotency_1.notifMark)(db, coupleId, `reveal-${date}-${userId}`)))) {
|
|
log_1.logger.log(`[onAnswerRevealed] already notified for ${userId}'s reveal on ${date}; skipping`);
|
|
return;
|
|
}
|
|
const questionId = typeof after.questionId === 'string' ? after.questionId : '';
|
|
// displayName is E2EE in users/{uid} → generic title; the app shows the real name in-app. Avatar
|
|
// (photoUrl) stays plaintext, so it's still sent.
|
|
const revealerDoc = await db.collection('users').doc(userId).get();
|
|
const revealerAvatar = (_g = revealerDoc.data()) === null || _g === void 0 ? void 0 : _g.photoUrl;
|
|
const res = await (0, push_1.sendPushToUser)(db, admin.messaging(), partnerId, {
|
|
notification: {
|
|
title: 'Your partner opened your answers',
|
|
body: 'Open to see what you each said.',
|
|
},
|
|
data: Object.assign({ type: 'partner_opened_answer', couple_id: coupleId, question_id: questionId, date }, (typeof revealerAvatar === 'string' && revealerAvatar.length > 0
|
|
? { sender_avatar_url: revealerAvatar }
|
|
: {})),
|
|
android: { notification: { channelId: 'partner_activity' } },
|
|
}, partnerData);
|
|
if (res.sent === 0 && res.failed === 0) {
|
|
log_1.logger.log(`[onAnswerRevealed] no FCM tokens for partner ${partnerId}`);
|
|
return;
|
|
}
|
|
log_1.logger.log(`[onAnswerRevealed] notified ${partnerId} that ${userId} opened couple ${coupleId}`);
|
|
});
|
|
//# sourceMappingURL=onAnswerRevealed.js.map
|