All files / src/routes translate.js

84% Statements 63/75
77.41% Branches 48/62
75% Functions 6/8
86.76% Lines 59/68

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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171              2x 2x 2x   2x   2x 2x       14x 12x 10x 9x         2x 2x 2x 2x 2x         2x 2x         8x           8x             8x 8x             2x 14x 14x 14x   14x 14x     9x     9x         9x 2x 2x       9x 8x 8x 8x   8x 8x 8x 8x                 8x 8x     8x 2x                       8x 8x                         8x           1x 1x         2x 2x 2x 2x 2x 2x 2x   2x   2x                           2x  
/**
 * Translation routes — machine translation via LibreTranslate with caching and quotas.
 *
 * POST /api/translate       → Translate text (cached per message doc)
 * GET  /api/translate/quota → Check daily translation quota
 */
 
const express = require('express');
const { db, FieldValue } = require('../utils/firebase');
const log = require('../utils/log');
 
const router = express.Router();
 
const LIBRETRANSLATE_URL = process.env.LIBRETRANSLATE_URL || 'http://localhost:5000';
const FREE_DAILY_LIMIT = 50;
 
/** Validate translate request inputs. Returns error string or null. */
function validateTranslateInput(text, targetLang) {
  if (!text || !targetLang) return 'text and targetLang required';
  if (!/^[a-z]{2,3}(-[A-Za-z]{2,4})?$/.test(targetLang)) return 'Invalid language code';
  if (text.length > 5000) return 'Text too long (max 5000 characters)';
  return null;
}
 
/** Verify the user is a participant of the parent conversation/room. */
async function verifyParticipant(messagePath, uniqueId) {
  const parentPath = messagePath.split('/').slice(0, 2).join('/');
  const parentSnap = await db.doc(parentPath).get();
  Iif (!parentSnap.exists) return false;
  const participantIds = parentSnap.data().participantIds || [];
  return participantIds.includes(uniqueId);
}
 
/** Check translation cache on the message doc. */
async function checkTranslationCache(messagePath, targetLang) {
  const msgSnap = await db.doc(messagePath).get();
  return msgSnap.data()?.translations?.[targetLang] || null;
}
 
/** Call LibreTranslate API. Returns { translatedText, detectedSourceLang } or null on error. */
async function callLibreTranslate(text, targetLang, res) {
  const ltResp = await fetch(`${LIBRETRANSLATE_URL}/translate`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ q: text, source: 'auto', target: targetLang }),
  });
 
  Iif (!ltResp.ok) {
    const err = await ltResp.text();
    log.error('translate', 'LibreTranslate request failed', { status: ltResp.status, error: err });
    res.status(502).json({ error: 'Translation service unavailable' });
    return null;
  }
 
  const ltData = await ltResp.json();
  return {
    translatedText: ltData.translatedText,
    detectedSourceLang: ltData.detectedLanguage?.language || 'unknown',
  };
}
 
// POST /api/translate
router.post('/translate', async (req, res) => {
  try {
    const { text, targetLang, messagePath } = req.body;
    const uniqueId = req.auth.uniqueId;
 
    const inputError = validateTranslateInput(text, targetLang);
    if (inputError) return res.status(400).json({ error: inputError });
 
    const validMessagePath =
      messagePath &&
      /^(conversations|rooms)\/[a-zA-Z0-9_-]+\/messages\/[a-zA-Z0-9_-]+$/.test(messagePath);
 
    const participantVerified = validMessagePath
      ? await verifyParticipant(messagePath, uniqueId)
      : false;
 
    // Check cache
    if (validMessagePath && participantVerified) {
      const cached = await checkTranslationCache(messagePath, targetLang);
      Iif (cached) return res.json({ translatedText: cached, cached: true });
    }
 
    // Check quota for non-SuperShy users
    const userSnap = await db.doc(`users/${uniqueId}`).get();
    const userData = userSnap.data() || {};
    const isSuperShy = userData.isSuperShy === true;
    const today = new Date().toISOString().slice(0, 10);
 
    Eif (!isSuperShy) {
      const translationDate = userData.translationDate || '';
      const translationsToday = translationDate === today ? userData.translationsToday || 0 : 0;
      Iif (translationsToday >= FREE_DAILY_LIMIT) {
        return res.status(429).json({
          error: 'Daily translation limit reached',
          limit: FREE_DAILY_LIMIT,
          upgradePrompt: true,
        });
      }
    }
 
    const result = await callLibreTranslate(text, targetLang, res);
    Iif (!result) return; // error response already sent
 
    // Cache translation on message doc (only if participant verified)
    if (validMessagePath && participantVerified) {
      db.doc(messagePath)
        .update({ [`translations.${targetLang}`]: result.translatedText })
        .catch((err) =>
          log.error('translate', 'Failed to cache translation', {
            messagePath,
            targetLang,
            error: err.message,
          }),
        );
    }
 
    // Increment daily counter (non-SuperShy only)
    Eif (!isSuperShy) {
      db.doc(`users/${uniqueId}`)
        .update({
          translationsToday: userData.translationDate === today ? FieldValue.increment(1) : 1,
          translationDate: today,
        })
        .catch((err) =>
          log.error('translate', 'Failed to update translation quota', {
            userId: uniqueId,
            error: err.message,
          }),
        );
    }
 
    res.json({
      translatedText: result.translatedText,
      detectedSourceLang: result.detectedSourceLang,
      cached: false,
    });
  } catch (err) {
    log.error('translate', 'Translation request failed', { error: err.message });
    res.status(500).json({ error: 'Translation failed' });
  }
});
 
// GET /api/translate/quota
router.get('/translate/quota', async (req, res) => {
  try {
    const uniqueId = req.auth.uniqueId;
    const userSnap = await db.doc(`users/${uniqueId}`).get();
    const userData = userSnap.data() || {};
    const isSuperShy = userData.isSuperShy === true;
    const today = new Date().toISOString().slice(0, 10);
    const translationsToday =
      userData.translationDate === today ? userData.translationsToday || 0 : 0;
 
    res.json({
      used: translationsToday,
      limit: isSuperShy ? -1 : FREE_DAILY_LIMIT,
      unlimited: isSuperShy,
    });
  } catch (err) {
    log.error('translate', 'Failed to check translation quota', {
      userId: req.auth.uniqueId,
      error: err.message,
    });
    res.status(500).json({ error: 'Failed to check quota' });
  }
});
 
module.exports = router;