Skip to content

Commit 3353275

Browse files
committed
added ui feature
1 parent 9fd1737 commit 3353275

File tree

10 files changed

+79
-3
lines changed

10 files changed

+79
-3
lines changed

public/openapi/components/schemas/PostObject.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ PostObject:
1818
For posts received via ActivityPub, it is the url of the original piece of content.
1919
content:
2020
type: string
21+
isEnglish:
22+
type: boolean
23+
translatedContent:
24+
type: string
2125
sourceContent:
2226
type: string
2327
nullable: true
@@ -170,6 +174,10 @@ PostDataObject:
170174
description: A topic identifier
171175
content:
172176
type: string
177+
isEnglish:
178+
type: boolean
179+
translatedContent:
180+
type: string
173181
timestamp:
174182
type: number
175183
votes:

public/openapi/read/categories.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ get:
8282
type: number
8383
content:
8484
type: string
85+
isEnglish:
86+
type: boolean
87+
translatedContent:
88+
type: string
8589
timestampISO:
8690
type: string
8791
description: An ISO 8601 formatted date string (complementing `timestamp`)
@@ -142,6 +146,10 @@ get:
142146
type: number
143147
content:
144148
type: string
149+
isEnglish:
150+
type: boolean
151+
translatedContent:
152+
type: string
145153
sourceContent:
146154
type: string
147155
nullable: true

public/openapi/read/index.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ get:
7777
type: number
7878
content:
7979
type: string
80+
isEnglish:
81+
type: boolean
82+
translatedContent:
83+
type: string
8084
timestampISO:
8185
type: string
8286
description: An ISO 8601 formatted date string (complementing `timestamp`)
@@ -144,6 +148,10 @@ get:
144148
type: number
145149
content:
146150
type: string
151+
isEnglish:
152+
type: boolean
153+
translatedContent:
154+
type: string
147155
sourceContent:
148156
type: string
149157
nullable: true

public/openapi/write/posts/pid.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ get:
3535
description: A topic identifier
3636
content:
3737
type: string
38+
isEnglish:
39+
type: boolean
40+
translatedContent:
41+
type: string
3842
timestamp:
3943
type: number
4044
flagId:

public/src/client/topic.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,28 @@ define('forum/topic', [
7373
handleThumbs();
7474

7575
$(window).on('scroll', utils.debounce(updateTopicTitle, 250));
76+
configurePostToggle();
7677

7778
handleTopicSearch();
7879

7980
hooks.fire('action:topic.loaded', ajaxify.data);
8081
};
8182

83+
function configurePostToggle() {
84+
$('.topic').on('click', '.view-translated-btn', function () {
85+
// Toggle the visibility of the next .translated-content div
86+
$(this).closest('.sensitive-content-message').next('.translated-content').toggle();
87+
// Optionally, change the button text based on visibility
88+
var isVisible = $(this).closest('.sensitive-content-message').next('.translated-content').is(':visible');
89+
if (isVisible) {
90+
$(this).text('Hide the translated message.');
91+
} else {
92+
$(this).text('Click here to view the translated message.');
93+
}
94+
});
95+
}
96+
97+
8298
function handleTopicSearch() {
8399
require(['mousetrap'], (mousetrap) => {
84100
if (config.topicSearchEnabled) {

src/posts/create.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const groups = require('../groups');
1010
const privileges = require('../privileges');
1111
const activitypub = require('../activitypub');
1212
const utils = require('../utils');
13+
const translate = require('../translate');
1314

1415
module.exports = function (Posts) {
1516
Posts.create = async function (data) {
@@ -18,6 +19,7 @@ module.exports = function (Posts) {
1819
const content = data.content.toString();
1920
const timestamp = data.timestamp || Date.now();
2021
const isMain = data.isMain || false;
22+
const [isEnglish, translatedContent] = await translate.translate(data);
2123

2224
if (!uid && parseInt(uid, 10) !== 0) {
2325
throw new Error('[[error:invalid-uid]]');
@@ -28,7 +30,7 @@ module.exports = function (Posts) {
2830
}
2931

3032
const pid = data.pid || await db.incrObjectField('global', 'nextPid');
31-
let postData = { pid, uid, tid, content, sourceContent, timestamp };
33+
let postData = { pid, uid, tid, content, sourceContent, timestamp, isEnglish, translatedContent };
3234

3335
if (data.toPid) {
3436
postData.toPid = data.toPid;

src/posts/data.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,11 @@ function modifyPost(post, fields) {
7070
if (!fields.length || fields.includes('attachments')) {
7171
post.attachments = (post.attachments || '').split(',').filter(Boolean);
7272
}
73+
// Mark post as "English" if decided by translator service or if it has no info
74+
post.isEnglish = post.isEnglish == 'true' || post.isEnglish === undefined;
75+
// If translatedContent is undefined, default to empty string (no translation needed for English posts)
76+
if (post.translatedContent === undefined) {
77+
post.translatedContent = '';
78+
}
7379
}
7480
}

src/posts/summary.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module.exports = function (Posts) {
2222
options.escape = options.hasOwnProperty('escape') ? options.escape : false;
2323
options.extraFields = options.hasOwnProperty('extraFields') ? options.extraFields : [];
2424

25-
const fields = ['pid', 'tid', 'toPid', 'url', 'content', 'sourceContent', 'uid', 'timestamp', 'deleted', 'upvotes', 'downvotes', 'replies', 'handle'].concat(options.extraFields);
25+
const fields = ['pid', 'tid', 'toPid', 'url', 'content', 'sourceContent', 'uid', 'timestamp', 'deleted', 'upvotes', 'downvotes', 'replies', 'handle', 'isEnglish', 'translatedContent'].concat(options.extraFields);
2626

2727
let posts = await Posts.getPostsFields(pids, fields);
2828
posts = posts.filter(Boolean);

src/translate/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* eslint-disable strict */
2+
//var request = require('request');
3+
4+
const translatorApi = module.exports;
5+
6+
translatorApi.translate = function (postData) {
7+
return ['is_english',postData];
8+
};
9+
10+
// translatorApi.translate = async function (postData) {
11+
// Edit the translator URL below
12+
// const TRANSLATOR_API = "TODO"
13+
// const response = await fetch(TRANSLATOR_API+'/?content='+postData.content);
14+
// const data = await response.json();
15+
// return ['is_english','translated_content'];
16+
// };

vendor/nodebb-theme-harmony-2.1.15/templates/partials/topic/post.tpl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,15 @@
8181
</div>
8282

8383
<div class="content text-break" component="post/content" itemprop="text">
84-
{posts.content}
84+
{posts.content}
85+
{{{if !posts.isEnglish }}}
86+
<div class="sensitive-content-message">
87+
<a class="btn btn-sm btn-primary view-translated-btn">Click here to view the translated message.</a>
88+
</div>
89+
<div class="translated-content" style="display:none;">
90+
{posts.translatedContent}
91+
</div>
92+
{{{end}}}
8593
</div>
8694

8795
<!-- IMPORT partials/topic/post-poll.tpl -->

0 commit comments

Comments
 (0)