Skip to content

Commit e15c196

Browse files
authored
Merge pull request #55 from CMU-17313Q/add_socket_event
Add socket event to avoid manual page refresh
2 parents 98ea8f3 + 211b194 commit e15c196

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

src/posts/create.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const privileges = require('../privileges');
1111
const activitypub = require('../activitypub');
1212
const utils = require('../utils');
1313
const translate = require('../translate');
14+
const websockets = require('../socket.io');
1415

1516
module.exports = function (Posts) {
1617
Posts.create = async function (data) {
@@ -36,14 +37,21 @@ module.exports = function (Posts) {
3637
let postData = { pid, uid, tid, content, sourceContent, timestamp, isEnglish, translatedContent };
3738

3839
// Start translation in background (non-blocking)
39-
translate.translate(data).then(([detected, translated]) => {
40+
translate.translate(data).then(async ([detected, translated]) => {
4041
// Update post asynchronously when translation completes
41-
Posts.setPostFields(pid, {
42+
await Posts.setPostFields(pid, {
4243
isEnglish: detected,
4344
translatedContent: translated,
44-
}).catch((err) => {
45-
// Silent fail - post already created successfully
46-
console.error('[translator] Failed to update translation:', err.message);
45+
});
46+
47+
// Emit socket event to notify clients of translation update
48+
websockets.in(`topic_${tid}`).emit('event:post_edited', {
49+
post: {
50+
pid: pid,
51+
isEnglish: detected,
52+
translatedContent: translated,
53+
},
54+
topic: { tid: tid },
4755
});
4856
}).catch((err) => {
4957
// Translation failed - post already created with English defaults

0 commit comments

Comments
 (0)