Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e8dd905f27 | |||
| c5fd09d71d | |||
| 989fdce883 | |||
| fd82643477 | |||
| 93d5b6b5f3 |
@@ -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) => {
|
DB.getRecentChatMessages = async (limit = 100) => {
|
||||||
const safeLimit = Math.min(Math.max(parseInt(limit, 10) || 100, 1), 200);
|
const safeLimit = Math.min(Math.max(parseInt(limit, 10) || 100, 1), 200);
|
||||||
const messages = await DB.chatMessagesCol.find({})
|
const messages = await DB.chatMessagesCol.find({})
|
||||||
|
|||||||
@@ -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) => {
|
DB.newReaction = (postid, profileid, reaction) => {
|
||||||
if(!DB.ObjectID.isValid(postid)) return false;
|
if(!DB.ObjectID.isValid(postid)) return false;
|
||||||
const id = DB.ObjectID(postid);
|
const id = DB.ObjectID(postid);
|
||||||
|
|||||||
@@ -489,6 +489,26 @@ const Notifications = {
|
|||||||
// sendWebNotification(requesterProfile.webSubscription, notifBody);
|
// sendWebNotification(requesterProfile.webSubscription, notifBody);
|
||||||
DB.addNotification(requesterProfile, notifBody, null, null, groupProfile._id);
|
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' });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-1
@@ -148,7 +148,8 @@ DB.getDB.then((DB) => {
|
|||||||
router.get("/chapters/:chapterId", async (req, res) => {
|
router.get("/chapters/:chapterId", async (req, res) => {
|
||||||
const chapterId = req.params.chapterId;
|
const chapterId = req.params.chapterId;
|
||||||
const bibleId = req.query.bibleId || defaultBibleId;
|
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);
|
return res.json(bibles);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ var express = require('express');
|
|||||||
var router = express.Router();
|
var router = express.Router();
|
||||||
|
|
||||||
const DB = require("../mongoDB.js");
|
const DB = require("../mongoDB.js");
|
||||||
|
const Notifications = require("../notifications.js");
|
||||||
const { getUserId, getProfileId } = require("../utils/sessionUtils.js");
|
const { getUserId, getProfileId } = require("../utils/sessionUtils.js");
|
||||||
const { normalizeLanguageCode, translateText } = require("../utils/chatTranslation.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" });
|
return res.status(500).json({ status: "Could not save message" });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Notifications.youGotANewChatMessage(profileId, text);
|
||||||
|
|
||||||
activeUsers.set(profileId + "", {
|
activeUsers.set(profileId + "", {
|
||||||
profileId: profileId + "",
|
profileId: profileId + "",
|
||||||
userId: userId + "",
|
userId: userId + "",
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ var router = express.Router();
|
|||||||
const DB = require("./../mongoDB.js");
|
const DB = require("./../mongoDB.js");
|
||||||
const Post = require("./../def/post.js");
|
const Post = require("./../def/post.js");
|
||||||
const Notifications = require("./../notifications.js");
|
const Notifications = require("./../notifications.js");
|
||||||
|
const { translateText, normalizeLanguageCode } = require("../utils/chatTranslation.js");
|
||||||
|
|
||||||
DB.getDB.then((DB) => {
|
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
|
* @swagger
|
||||||
* /post/react:
|
* /post/react:
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ const translateText = async ({ text, sourceLang, targetLang }) => {
|
|||||||
content: [
|
content: [
|
||||||
{
|
{
|
||||||
type: "input_text",
|
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.",
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user