All files / src/routes conversation-message-edits.js

86.53% Statements 45/52
75% Branches 18/24
100% Functions 6/6
87.23% Lines 41/47

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109                  1x 1x 1x 1x 1x 1x   11x             11x 11x       11x     1x 7x 7x 7x 7x 6x 7x 7x             7x 6x 6x   7x   6x 5x                   5x                     1x 4x 4x 4x 4x 4x 5x 5x           5x     4x 4x   5x   4x 3x         3x             1x  
/**
 * SHY-0614 — editing and recalling a private or group message. The phone
 * used to write both itself; now the server reads the stored message inside
 * a transaction, so history and recall records hold what was really said.
 *
 * PATCH /api/conversations/:id/messages/:messageId         { text } -> edit
 * POST  /api/conversations/:id/messages/:messageId/recall           -> recall
 */
 
const router = require('express').Router();
const { db, FieldValue } = require('../utils/firebase');
const { now } = require('../utils/helpers');
const log = require('../utils/log');
const { decideEdit, decideRecall, historyLogFields } = require('../utils/message-history');
const { gateConversationWrite } = require('../utils/conversation-write-gate');
 
const refsFor = (conversationId, messageId) => ({
  conv: db.doc(`conversations/${conversationId}`),
  msg: db.doc(`conversations/${conversationId}/messages/${messageId}`),
});
 
/** The send route's refusals (participant, age gate, cohort); true when answered. */
async function gate(req, res, convRef, ageGate) {
  const snap = await convRef.get();
  Iif (!snap.exists) {
    res.status(404).json({ error: 'Conversation not found' });
    return true;
  }
  return gateConversationWrite(req, res, snap.data(), { ageGate });
}
 
router.patch('/conversations/:id/messages/:messageId', async (req, res) => {
  try {
    const { id, messageId } = req.params;
    const { conv, msg } = refsFor(id, messageId);
    if (await gate(req, res, conv, true)) return;
    const verdict = await db.runTransaction(async (tx) => {
      const [c, m] = await Promise.all([tx.get(conv), tx.get(msg)]);
      const v = decideEdit({
        conversation: c.exists ? c.data() : null,
        message: m.exists ? m.data() : null,
        callerId: req.auth.uniqueId,
        text: req.body?.text,
        now: now(),
      });
      if (v.ok && v.changed) {
        tx.set(msg.collection('edits').doc(), v.editEntry);
        tx.update(msg, { ...v.messageUpdate, editCount: FieldValue.increment(1) });
      }
      return v;
    });
    if (!verdict.ok) return res.status(verdict.status).json({ error: verdict.error });
    log.info(
      'conversations',
      'Message edited',
      historyLogFields({
        conversationId: id,
        messageId,
        callerId: req.auth.uniqueId,
        changed: verdict.changed,
      }),
    );
    return res.json({
      success: true,
      changed: verdict.changed,
      editedAt: verdict.messageUpdate?.editedAt,
    });
  } catch (err) {
    log.error('conversations', 'PATCH message failed', { error: err.message });
    return res.status(500).json({ error: 'Internal server error' });
  }
});
 
router.post('/conversations/:id/messages/:messageId/recall', async (req, res) => {
  try {
    const { id, messageId } = req.params;
    const { conv, msg } = refsFor(id, messageId);
    Iif (await gate(req, res, conv, false)) return;
    const verdict = await db.runTransaction(async (tx) => {
      const [c, m] = await Promise.all([tx.get(conv), tx.get(msg)]);
      const v = decideRecall({
        conversation: c.exists ? c.data() : null,
        message: m.exists ? m.data() : null,
        callerId: req.auth.uniqueId,
        now: now(),
      });
      if (v.ok) {
        // create, not set: a record that already exists fails the commit, so
        // two racing recalls can never both write one.
        tx.create(msg.collection('recall').doc('record'), v.record);
        tx.update(msg, v.messageUpdate);
      }
      return v;
    });
    if (!verdict.ok) return res.status(verdict.status).json({ error: verdict.error });
    log.info(
      'conversations',
      'Message recalled',
      historyLogFields({ conversationId: id, messageId, callerId: req.auth.uniqueId }),
    );
    return res.json({ success: true });
  } catch (err) {
    log.error('conversations', 'POST recall failed', { error: err.message });
    return res.status(500).json({ error: 'Internal server error' });
  }
});
 
module.exports = router;