9 Commits

15 changed files with 396 additions and 21 deletions

View File

@@ -3,7 +3,7 @@ const { client_logger } = require('../utils/analyticsLogger');
const bcrypt = require('bcrypt');
const crypto = require('crypto');
const { getSessionId, getUserId, getProfileId } = require('../utils/sessionUtils.js');
const { cookiesOptions } = require('../config/cookiesOptions');
const { getCookiesOptions } = require('../config/cookiesOptions');
const Notifications = require("../notifications");
// Object Definitions
@@ -19,6 +19,7 @@ const createPasswordTokenHash = (rawToken) =>
const createSessionFromUser = async ({ DB, user, req, res }) => {
const sessionObj = await DB.newSession(user._id);
const cookiesOptions = getCookiesOptions(req);
res.cookie('user_sid', user._id, cookiesOptions);
res.cookie('session_id', sessionObj.insertedId, cookiesOptions);
const latestUpdatedProfile = await DB.latestProfile(user._id);
@@ -143,8 +144,9 @@ const logout = async function (req, res) {
const session_id = getSessionId(req);
const user_sid = getUserId(req);
if (session_id && user_sid) {
res.clearCookie('session_id');
res.clearCookie('user_sid');
const cookiesOptions = getCookiesOptions(req);
res.clearCookie('session_id', cookiesOptions);
res.clearCookie('user_sid', cookiesOptions);
//remove from DB
const DB = await MongoDB.getDB;
DB.removeSession(session_id);

View File

@@ -1,12 +1,49 @@
const isProduction = process.env.NODE_ENV === "production";
const forceSecureCookie = process.env.COOKIE_SECURE === "true";
const secure = forceSecureCookie || isProduction;
const cookiesOptions = {
maxAge: 1000 * 60 * 60 * 24 * 90, // would expire after 90 days
httpOnly: true, // The cookie only accessible by the web server
sameSite: secure ? 'none' : 'lax',
secure,
const COOKIE_MAX_AGE_MS = 1000 * 60 * 60 * 24 * 90; // 90 days
const LOCAL_ORIGIN_REGEX = /^http:\/\/(localhost|127\.0\.0\.1|aeropi\.local)(:\d+)?$/i;
const LOCAL_HOST_REGEX = /^(localhost|127\.0\.0\.1|aeropi\.local)(:\d+)?$/i;
const getHeaderValue = (req, key) => {
if (!req || !req.headers) return "";
const raw = req.headers[key];
if (Array.isArray(raw)) return raw[0] || "";
return raw || "";
};
module.exports = { cookiesOptions };
const isLocalRequest = (req) => {
const origin = getHeaderValue(req, "origin");
const host = getHeaderValue(req, "host");
return LOCAL_ORIGIN_REGEX.test(origin) || LOCAL_HOST_REGEX.test(host);
};
const isHttpsRequest = (req) => {
if (!req) return false;
const forwardedProto = String(getHeaderValue(req, "x-forwarded-proto")).split(",")[0].trim().toLowerCase();
const reqProtocol = String(req.protocol || "").toLowerCase();
const origin = String(getHeaderValue(req, "origin") || "").toLowerCase();
if (forwardedProto === "https" || reqProtocol === "https") return true;
return origin.startsWith("https://");
};
const shouldUseSecureCookie = (req) => {
if (forceSecureCookie) return true;
if (isLocalRequest(req)) return false;
if (isHttpsRequest(req)) return true;
return isProduction;
};
const getCookiesOptions = (req) => {
const secure = shouldUseSecureCookie(req);
return {
maxAge: COOKIE_MAX_AGE_MS,
httpOnly: true,
sameSite: secure ? "none" : "lax",
secure,
};
};
const cookiesOptions = getCookiesOptions();
module.exports = { cookiesOptions, getCookiesOptions };

View File

@@ -7,6 +7,7 @@ var corsOptions = {
'http://127.0.0.1:8081',
'http://localhost:3000',
"https://social.emmint.com",
"https://www.social.emmint.com",
"https://fellowship.emmint.com",
"https://aeropi.local",
],

View File

@@ -27,6 +27,13 @@ const chatDB = (DB) => {
};
};
DB.getChatParticipants = async () => {
return DB.chatMessagesCol.distinct("senderProfileId").catch((err) => {
console.log(err);
return [];
});
};
DB.getRecentChatMessages = async (limit = 100) => {
const safeLimit = Math.min(Math.max(parseInt(limit, 10) || 100, 1), 200);
const messages = await DB.chatMessagesCol.find({})

View File

@@ -38,6 +38,20 @@ postDB = (DB)=>{
});
}
DB.addTranslation = (postid, lang, translatedText) => {
if(!DB.ObjectID.isValid(postid)) return false;
const id = DB.ObjectID(postid);
let update = {
$set:{
["translations." + lang]: translatedText
}
}
return DB.postCols.updateOne({_id: id}, update).catch((err)=>{
console.log(err);
return false;
});
}
DB.newReaction = (postid, profileid, reaction) => {
if(!DB.ObjectID.isValid(postid)) return false;
const id = DB.ObjectID(postid);

View File

@@ -184,6 +184,7 @@ const songsRoute = require('./routes/songs.js');
const paymentsRoute = require('./routes/payments.js');
const bibleRoute = require('./routes/bible.js');
const chatRoute = require('./routes/chat.js');
const liveCaptionsRoute = require('./routes/liveCaptions.js');
const sessionChecker = require('./middleware/sessionChecker');
// -- Private Routes
app.use('/user', sessionChecker, profileRoute);
@@ -192,6 +193,7 @@ app.use('/payments', paymentsRoute);
app.use('/bible', sessionChecker, bibleRoute);
app.use('/songs', sessionChecker, songsRoute);
app.use('/chat', sessionChecker, chatRoute);
app.use('/live-captions', liveCaptionsRoute);
// -- Public Routes
const subsplashRoute = require('./routes/subsplash.js');
app.use('/subsplash', subsplashRoute);
@@ -240,7 +242,7 @@ const webPushEmail = process.env.WEB_PUSH_EMAIL;
webPush.setVapidDetails('mailto:' + webPushEmail, publicVapidKey, privateVapidKey);
const { cookiesOptions } = require('./config/cookiesOptions');
const { getCookiesOptions } = require('./config/cookiesOptions');
const { client_logger } = require('./utils/analyticsLogger.js');
const { getSessionId, getUserId, getProfileId } = require('./utils/sessionUtils.js');
@@ -410,7 +412,7 @@ DB.getDB.then((DB) => {
return res.status(403).json({ status: "Profile does not belong to the logged-in user" });
}
// Update active profile cookie
res.cookie('profile_id', profile._id, cookiesOptions);
res.cookie('profile_id', profile._id, getCookiesOptions(req));
return res.json({ status: "ok", profile });
} catch (error) {
console.error("Error changing profile:", error);

View File

@@ -1,9 +1,22 @@
const { getSessionId, getUserId, getProfileId } = require('../utils/sessionUtils');
const { client_logger } = require('../utils/analyticsLogger');
const { cookiesOptions } = require('../config/cookiesOptions');
const { getCookiesOptions } = require('../config/cookiesOptions');
const MongoDB = require("../mongoDB.js");
const { ObjectId } = require("mongodb");
const shouldReturnJson = (req) => {
const accept = String(req?.headers?.accept || "").toLowerCase();
const contentType = String(req?.headers?.["content-type"] || "").toLowerCase();
return !!req?.headers?.origin || accept.includes("application/json") || contentType.includes("application/json");
};
const rejectUnauthorized = (req, res) => {
if (shouldReturnJson(req)) {
return res.status(401).json({ status: "Unauthorized" });
}
return res.redirect('/login');
};
const sessionChecker = async (req, res, next) => {
try {
const session_id = getSessionId(req);
@@ -11,10 +24,10 @@ const sessionChecker = async (req, res, next) => {
let profile_id = getProfileId(req);
if (!session_id || !user_sid) {
return res.redirect('/login');
return rejectUnauthorized(req, res);
}
if (!ObjectId.isValid(session_id) || !ObjectId.isValid(user_sid)) {
return res.redirect('/login');
return rejectUnauthorized(req, res);
}
const DB = await MongoDB.getDB;
@@ -24,15 +37,15 @@ const sessionChecker = async (req, res, next) => {
if (!await DB.getProfileCache(profile_id)) {
const latestProfile = await DB.latestProfile(user_sid);
if (!latestProfile || !latestProfile._id) {
return res.redirect('/login');
return rejectUnauthorized(req, res);
}
res.cookie('profile_id', latestProfile._id, cookiesOptions);
res.cookie('profile_id', latestProfile._id, getCookiesOptions(req));
profile_id = latestProfile._id;
}
req.profileInfo = { _id: profile_id };
if (!userInfo) return res.redirect('/login');
if (!userInfo) return rejectUnauthorized(req, res);
client_logger.capture({
distinctId: user_sid,
@@ -42,7 +55,7 @@ const sessionChecker = async (req, res, next) => {
next();
} catch (error) {
console.error("Session checker error", error);
return res.redirect('/login');
return rejectUnauthorized(req, res);
}
};

View File

@@ -489,6 +489,26 @@ const Notifications = {
// sendWebNotification(requesterProfile.webSubscription, notifBody);
DB.addNotification(requesterProfile, notifBody, null, null, groupProfile._id);
},
async youGotANewChatMessage(senderProfileId, messageText) {
const DB = await DBGetter.getDB;
const participants = await DB.getChatParticipants();
const senderProfile = await DB.getProfileCache(senderProfileId);
const tokens = [];
for (const participantProfileId of participants) {
if (participantProfileId.toString() === senderProfileId.toString()) continue;
const participantProfile = await DB.getProfileCache(participantProfileId);
if (participantProfile && Array.isArray(participantProfile.token)) {
tokens.push(...participantProfile.token);
}
}
if (tokens.length > 0) {
const notifBody = `${senderProfile.profile.firstName}: ${messageText.substring(0, 100)}${messageText.length > 100 ? '...' : ''}`;
sendPushNotification(tokens, notifBody, { type: 'chat' });
}
},
}

View File

@@ -6,6 +6,7 @@
"scripts": {
"test": "npx mocha test/auth.test.js",
"start": "node index.js",
"live-captions:test-sender": "node scripts/liveCaptionsTestSender.js",
"docker": "docker compose up -d",
"docker_restore": "docker-compose exec mongo mongorestore --db EMI_SOCIAL /dump/EMI_SOCIAL/",
"docker_dump": "docker-compose exec mongo mongodump --uri ${MONGO_URL} --out /dump"

View File

@@ -148,7 +148,8 @@ DB.getDB.then((DB) => {
router.get("/chapters/:chapterId", async (req, res) => {
const chapterId = req.params.chapterId;
const bibleId = req.query.bibleId || defaultBibleId;
const bibles = await fetchAPI('bibles/' + bibleId + "/chapters/" + chapterId);
const contentType = req.query['content-type'] ? `?content-type=${req.query['content-type']}` : '';
const bibles = await fetchAPI('bibles/' + bibleId + "/chapters/" + chapterId + contentType);
return res.json(bibles);
});

View File

@@ -2,6 +2,7 @@ var express = require('express');
var router = express.Router();
const DB = require("../mongoDB.js");
const Notifications = require("../notifications.js");
const { getUserId, getProfileId } = require("../utils/sessionUtils.js");
const { normalizeLanguageCode, translateText } = require("../utils/chatTranslation.js");
@@ -190,6 +191,8 @@ DB.getDB.then((DB) => {
return res.status(500).json({ status: "Could not save message" });
}
Notifications.youGotANewChatMessage(profileId, text);
activeUsers.set(profileId + "", {
profileId: profileId + "",
userId: userId + "",

158
routes/liveCaptions.js Normal file
View File

@@ -0,0 +1,158 @@
var express = require('express');
var router = express.Router();
const sessionChecker = require("../middleware/sessionChecker.js");
const MAX_BUFFER_SIZE = 300;
const DEFAULT_INITIAL_LIMIT = 40;
const MAX_INITIAL_LIMIT = 120;
const CAPTION_META_KEYS = new Set(["sequence", "createdAt", "original"]);
const liveCaptionState = {
startedAt: Date.now(),
latestSequence: 0,
captions: [],
};
const normalizeLang = (lang = "") => {
const value = String(lang || "").trim().toLowerCase();
if (!value) return "";
const base = value.split(",")[0].split("-")[0].trim();
return base || value;
};
const normalizeTranslations = (translations) => {
if (!translations || typeof translations !== "object" || Array.isArray(translations)) return {};
const normalized = {};
for (const [langKey, translatedText] of Object.entries(translations)) {
const lang = normalizeLang(langKey);
const text = typeof translatedText === "string" ? translatedText.trim() : "";
if (!lang || !text) continue;
normalized[lang] = text;
}
return normalized;
};
const buildTranslationsFromFlatPayload = (payload) => {
const ignoredKeys = new Set(["original", "sourceLang", "translations"]);
const normalized = {};
for (const [key, value] of Object.entries(payload || {})) {
if (ignoredKeys.has(key)) continue;
const lang = normalizeLang(key);
const text = typeof value === "string" ? value.trim() : "";
if (!lang || !text) continue;
normalized[lang] = text;
}
return normalized;
};
const inferSourceLangFromTranslations = (original, translations) => {
const normalizedOriginal = String(original || "").trim();
if (!normalizedOriginal) return "original";
for (const [lang, text] of Object.entries(translations || {})) {
if (String(text || "").trim() === normalizedOriginal) return normalizeLang(lang);
}
return "original";
};
const getAvailableLanguages = () => {
const langs = new Set();
for (const caption of liveCaptionState.captions) {
Object.keys(caption || {}).forEach((key) => {
if (CAPTION_META_KEYS.has(key)) return;
const lang = normalizeLang(key);
if (lang) langs.add(lang);
});
}
return Array.from(langs).filter(Boolean).sort();
};
router.get("/stream", sessionChecker, async (req, res) => {
try {
const sinceSequence = Number.parseInt(req.query?.sinceSequence, 10);
const requestedLimit = Number.parseInt(req.query?.limit, 10);
const initialLimit = Number.isFinite(requestedLimit)
? Math.max(1, Math.min(requestedLimit, MAX_INITIAL_LIMIT))
: DEFAULT_INITIAL_LIMIT;
let captions = [];
if (Number.isFinite(sinceSequence) && sinceSequence >= 0) {
captions = liveCaptionState.captions.filter((item) => item.sequence > sinceSequence);
} else {
captions = liveCaptionState.captions.slice(-initialLimit);
}
return res.json({
status: "ok",
latestSequence: liveCaptionState.latestSequence,
startedAt: new Date(liveCaptionState.startedAt).toISOString(),
availableLanguages: getAvailableLanguages(),
captions,
});
} catch (error) {
console.error("Error getting live captions stream", error);
return res.status(500).json({
status: "Internal server error",
latestSequence: liveCaptionState.latestSequence,
captions: [],
availableLanguages: [],
});
}
});
router.post("/ingest", async (req, res) => {
try {
// TODO: Add basic auth/API key validation before production roll-out.
const original = typeof req.body?.original === "string" ? req.body.original.trim() : "";
const mapFromNested = normalizeTranslations(req.body?.translations);
const mapFromFlat = buildTranslationsFromFlatPayload(req.body);
const translations = { ...mapFromNested, ...mapFromFlat };
const sourceLang = normalizeLang(req.body?.sourceLang || inferSourceLangFromTranslations(original, translations));
if (!original) {
return res.status(400).json({ status: "Original text is required" });
}
if (sourceLang && sourceLang !== "original" && !translations[sourceLang]) {
translations[sourceLang] = original;
}
const sequence = liveCaptionState.latestSequence + 1;
const caption = {
sequence,
createdAt: new Date().toISOString(),
original,
...translations,
};
liveCaptionState.latestSequence = sequence;
liveCaptionState.captions.push(caption);
if (liveCaptionState.captions.length > MAX_BUFFER_SIZE) {
liveCaptionState.captions.splice(0, liveCaptionState.captions.length - MAX_BUFFER_SIZE);
}
return res.json({
status: "ok",
caption,
latestSequence: liveCaptionState.latestSequence,
availableLanguages: getAvailableLanguages(),
});
} catch (error) {
console.error("Error ingesting live captions", error);
return res.status(500).json({ status: "Internal server error" });
}
});
router.post("/reset", async (_, res) => {
try {
// TODO: Add admin authorization before exposing this endpoint.
liveCaptionState.startedAt = Date.now();
liveCaptionState.latestSequence = 0;
liveCaptionState.captions = [];
return res.json({ status: "ok" });
} catch (error) {
console.error("Error resetting live captions state", error);
return res.status(500).json({ status: "Internal server error" });
}
});
module.exports = router;

View File

@@ -4,6 +4,7 @@ var router = express.Router();
const DB = require("./../mongoDB.js");
const Post = require("./../def/post.js");
const Notifications = require("./../notifications.js");
const { translateText, normalizeLanguageCode } = require("../utils/chatTranslation.js");
DB.getDB.then((DB) => {
@@ -481,6 +482,47 @@ DB.getDB.then((DB) => {
})
});
router.post("/translate", async (req, res) => {
let postid = req.body.postid;
let targetLang = normalizeLanguageCode(req.body.targetLang);
// Return ack immediately
res.json({ status: "ok", message: "Translation queued" });
if (!postid || !targetLang) return;
try {
// Get post
const posts = await DB.getPostsByTag('', null); // No good way to get one post by ID directly exposed?
// Let's use dbCols directly if needed or find it. Wait, how do we get a single post?
// I'll assume DB.getPost exists, let me check that later. Actually I will use DB.postCols directly.
const post = await DB.postCols.findOne({ _id: DB.ObjectID(postid) });
if (!post || !post.content) return;
// Strip inline tags and bible tags before translating to reduce token usage and confusion,
// or just translate the raw content and let the AI handle it? The chat translator prompt says:
// "You translate chat messages. Keep meaning, tone, emojis, names, and references. Return only the translated text."
// So it can handle tags.
// To avoid huge translations or mostly-media posts
if (post.content.length > 1000) return;
if (post.translations && post.translations[targetLang]) return;
const translation = await translateText({
text: post.content,
sourceLang: "auto",
targetLang: targetLang
});
if (translation && translation.translatedText) {
await DB.addTranslation(postid, targetLang, translation.translatedText);
}
} catch (error) {
console.error("Error in background post translation", error);
}
});
/**
* @swagger
* /post/react:

View File

@@ -0,0 +1,74 @@
#!/usr/bin/env node
require("dotenv").config();
const axios = require("axios");
const baseUrl = (process.env.CAPTION_TEST_BASE_URL || process.env.BASE_URL || "http://localhost:3000").replace(/\/+$/, "");
const ingestUrl = `${baseUrl}/live-captions/ingest`;
const intervalMs = 5000;
const samples = [
{
original: "Bienvenidos a nuestro servicio de adoracion.",
es: "Bienvenidos a nuestro servicio de adoracion.",
en: "Welcome to our worship service.",
fr: "Bienvenue a notre service de louange.",
},
{
original: "Leamos juntos en el Salmo 23.",
es: "Leamos juntos en el Salmo 23.",
en: "Let us read together in Psalm 23.",
fr: "Lisons ensemble le Psaume 23.",
},
{
original: "Dios es fiel en todo tiempo.",
es: "Dios es fiel en todo tiempo.",
en: "God is faithful at all times.",
fr: "Dieu est fidele en tout temps.",
},
{
original: "Tomemos un momento para orar.",
es: "Tomemos un momento para orar.",
en: "Let us take a moment to pray.",
fr: "Prenons un moment pour prier.",
},
];
let sampleIndex = 0;
let timer = null;
const sendNextSample = async () => {
const payload = samples[sampleIndex];
sampleIndex = (sampleIndex + 1) % samples.length;
try {
const response = await axios.post(ingestUrl, payload, {
headers: { "Content-Type": "application/json" },
timeout: 10000,
});
const seq = response?.data?.caption?.sequence || response?.data?.latestSequence || "?";
console.log(`[live-captions:test-sender] sent sequence=${seq} original="${payload.original}"`);
} catch (error) {
const status = error?.response?.status;
const body = error?.response?.data;
const message = error?.message || "request failed";
console.error("[live-captions:test-sender] send failed", { status, body, message });
}
};
const start = async () => {
console.log(`[live-captions:test-sender] posting to ${ingestUrl} every ${intervalMs / 1000}s`);
await sendNextSample();
timer = setInterval(sendNextSample, intervalMs);
};
const shutdown = () => {
if (timer) clearInterval(timer);
console.log("[live-captions:test-sender] stopped");
process.exit(0);
};
process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);
start();

View File

@@ -54,7 +54,7 @@ const translateText = async ({ text, sourceLang, targetLang }) => {
content: [
{
type: "input_text",
text: "You translate chat messages. Keep meaning, tone, emojis, names, and references. Return only the translated text.",
text: "You translate chat messages and posts. Keep meaning, tone, emojis, names, and references. Do not translate structural tags starting with @ (e.g. @image:..., @youtube:..., @bible:...). Leave them exactly as they are or omit them if they do not fit the text flow. Return only the translated text.",
},
],
},