Skip to content

Commit 9205010

Browse files
authored
Merge pull request #57 from CMU-313/feature/llm-ui
Add post translation fields with API docs and topic UI
2 parents 8af6d2e + 2e59c4d commit 9205010

13 files changed

Lines changed: 110 additions & 9 deletions

File tree

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/components/schemas/TopicObject.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ TopicObject:
109109
sourceContent:
110110
type: string
111111
nullable: true
112+
translatedContent:
113+
type: string
114+
nullable: true
112115
timestampISO:
113116
type: string
114117
description: An ISO 8601 formatted date string (complementing `timestamp`)
@@ -146,6 +149,8 @@ TopicObject:
146149
example: "#f44336"
147150
index:
148151
type: number
152+
isEnglish:
153+
type: boolean
149154
nullable: true
150155
tags:
151156
type: array
@@ -295,4 +300,4 @@ TopicObjectSlim:
295300
type: number
296301
description: The number of thumbnails associated with this topic
297302
required:
298-
- tid
303+
- tid

public/openapi/read/categories.yaml

Lines changed: 9 additions & 1 deletion
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
@@ -253,4 +261,4 @@ get:
253261
type: string
254262
- $ref: ../components/schemas/Pagination.yaml#/Pagination
255263
- $ref: ../components/schemas/Breadcrumbs.yaml#/Breadcrumbs
256-
- $ref: ../components/schemas/CommonProps.yaml#/CommonProps
264+
- $ref: ../components/schemas/CommonProps.yaml#/CommonProps

public/openapi/read/index.yaml

Lines changed: 9 additions & 1 deletion
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
@@ -254,4 +262,4 @@ get:
254262
imageClass:
255263
type: string
256264
- $ref: ../components/schemas/Pagination.yaml#/Pagination
257-
- $ref: ../components/schemas/CommonProps.yaml#/CommonProps
265+
- $ref: ../components/schemas/CommonProps.yaml#/CommonProps

public/openapi/read/unread.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ get:
143143
sourceContent:
144144
type: string
145145
nullable: true
146+
translatedContent:
147+
type: string
148+
nullable: true
146149
timestampISO:
147150
type: string
148151
description: An ISO 8601 formatted date string (complementing `timestamp`)
@@ -180,6 +183,8 @@ get:
180183
example: "#f44336"
181184
index:
182185
type: number
186+
isEnglish:
187+
type: boolean
183188
tags:
184189
type: array
185190
items:
@@ -270,4 +275,4 @@ get:
270275
type: string
271276
- $ref: ../components/schemas/Pagination.yaml#/Pagination
272277
- $ref: ../components/schemas/Breadcrumbs.yaml#/Breadcrumbs
273-
- $ref: ../components/schemas/CommonProps.yaml#/CommonProps
278+
- $ref: ../components/schemas/CommonProps.yaml#/CommonProps

public/openapi/write/posts/pid.yaml

Lines changed: 5 additions & 1 deletion
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:
@@ -155,4 +159,4 @@ delete:
155159
$ref: ../../components/schemas/Status.yaml#/Status
156160
response:
157161
type: object
158-
properties: {}
162+
properties: {}

public/src/client/topic.js

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

7474
$(window).on('scroll', utils.debounce(updateTopicTitle, 250));
75+
configurePostToggle();
7576

7677
handleTopicSearch();
7778

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

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

src/posts/create.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const categories = require('../categories');
99
const groups = require('../groups');
1010
const activitypub = require('../activitypub');
1111
const utils = require('../utils');
12+
const translate = require('../translate');
1213

1314
module.exports = function (Posts) {
1415
Posts.create = async function (data) {
@@ -18,6 +19,8 @@ module.exports = function (Posts) {
1819
const timestamp = data.timestamp || Date.now();
1920
const isMain = data.isMain || false;
2021
let hasAttachment = false;
22+
const [isEnglish, translatedContent] = await translate.translate(data);
23+
2124

2225
if (!uid && parseInt(uid, 10) !== 0) {
2326
throw new Error('[[error:invalid-uid]]');
@@ -28,7 +31,7 @@ module.exports = function (Posts) {
2831
}
2932

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

3336
if (data.toPid) {
3437
postData.toPid = data.toPid;

src/posts/data.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,11 @@ function modifyPost(post, fields) {
7878
post.uploads = [];
7979
}
8080
}
81+
// Mark post as "English" if decided by translator service or if it has no info
82+
post.isEnglish = post.isEnglish == 'true' || post.isEnglish === undefined;
83+
// If translatedContent is undefined, default to empty string (no translation needed for English posts)
84+
if (post.translatedContent === undefined) {
85+
post.translatedContent = '';
86+
}
8187
}
8288
}

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);

0 commit comments

Comments
 (0)