Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: translate smart title during fetch #2643

Merged
merged 15 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 157 additions & 0 deletions __tests__/posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6306,6 +6306,87 @@ describe('posts title field', () => {
title: 'P1',
});
});

it('should return smart title translation', async () => {
loggedUser = '1';
isPlus = true;
await con.getRepository(Settings).update(
{ userId: '1' },
{
flags: {
clickbaitShieldEnabled: true,
},
},
);
await con.getRepository(Post).update(
{ id: 'p1' },
{
contentQuality: { is_clickbait_probability: 1.98 },
contentMeta: {
alt_title: {
translations: { en: 'Clickbait title', de: 'Clickbait title DE' },
},
},
translation: {
en: {
smartTitle: 'Smart Title EN',
},
de: {
smartTitle: 'Smart Title DE',
},
},
},
);

const res = await client.query(QUERY, {
headers: {
'content-language': 'de',
},
});
Comment on lines +6341 to +6345
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love that we can do this via headers actually :)

expect(res.errors).toBeFalsy();
expect(res.data.post).toEqual({
title: 'Smart Title DE',
});
});

it('should return english smart title translation when smart title translation does not exist', async () => {
loggedUser = '1';
isPlus = true;
await con.getRepository(Settings).update(
{ userId: '1' },
{
flags: {
clickbaitShieldEnabled: true,
},
},
);
await con.getRepository(Post).update(
{ id: 'p1' },
{
contentQuality: { is_clickbait_probability: 1.98 },
contentMeta: {
alt_title: {
translations: { en: 'Clickbait title EN' },
},
},
translation: {
en: {
smartTitle: 'Smart Title EN',
},
},
},
);

const res = await client.query(QUERY, {
headers: {
'content-language': 'de',
},
});
expect(res.errors).toBeFalsy();
expect(res.data.post).toEqual({
title: 'Smart Title EN',
});
});
});
});

Expand Down Expand Up @@ -6961,6 +7042,82 @@ describe('query fetchSmartTitle', () => {
expect(res.data.fetchSmartTitle.title).toEqual('Alt Title DE');
});

it('should return smart title translation when clickbait shield is enabled and language is set', async () => {
loggedUser = '1';
isPlus = true;

await con.getRepository(Post).update(
{ id: 'p1' },
{
contentMeta: {
alt_title: {
translations: {
en: 'Alt Title',
de: 'Alt Title DE',
},
},
},
translation: {
de: {
title: 'Title DE',
smartTitle: 'Smart Title DE',
},
},
},
);

await con
.getRepository(Settings)
.save({ userId: loggedUser, flags: { clickbaitShieldEnabled: false } });

const res = await client.query<
{ fetchSmartTitle: GQLPostSmartTitle },
{ id: string }
>(QUERY, {
variables: { id: 'p1' },
headers: { 'content-language': 'de' },
});

expect(res.errors).toBeFalsy();
expect(res.data.fetchSmartTitle.title).toEqual('Smart Title DE');
});

it('should return the original title translation when clickbait shield is enabled and language is set', async () => {
loggedUser = '1';
isPlus = true;

await con.getRepository(Post).update(
{ id: 'p1' },
{
contentMeta: {
alt_title: {
translations: {
en: 'Alt Title',
de: 'Alt Title DE',
},
},
},
translation: {
de: {
title: 'Title DE',
smartTitle: 'Smart Title DE',
},
},
},
);

const res = await client.query<
{ fetchSmartTitle: GQLPostSmartTitle },
{ id: string }
>(QUERY, {
variables: { id: 'p1' },
headers: { 'content-language': 'de' },
});

expect(res.errors).toBeFalsy();
expect(res.data.fetchSmartTitle.title).toEqual('Title DE');
});

describe('free user', () => {
it('should be able to get the smart title for trial', async () => {
loggedUser = '2';
Expand Down
47 changes: 47 additions & 0 deletions __tests__/workers/postTranslated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,30 @@ describe('postTranslated', () => {
});
});

it('should update multiple translations', async () => {
expect(
(await con.getRepository(Post).findOneByOrFail({ id: 'p1' })).translation,
).toEqual({});

await expectSuccessfulTypedBackground(worker, {
id: 'p1',
language: ContentLanguage.German,
translations: {
title: 'new title',
smartTitle: `smart title's @re cool#$%`,
},
});

expect(
(await con.getRepository(Post).findOneByOrFail({ id: 'p1' })).translation,
).toEqual({
de: {
title: 'new title',
smartTitle: `smart title's @re cool#$%`,
},
});
});

it('should handle titles with special characters', async () => {
expect(
(await con.getRepository(Post).findOneByOrFail({ id: 'p1' })).translation,
Expand Down Expand Up @@ -140,4 +164,27 @@ describe('postTranslated', () => {
},
});
});

it('should not remove other existing translations', async () => {
await con
.getRepository(Post)
.update({ id: 'p1' }, { translation: { de: { title: 'old title' } } });

await expectSuccessfulTypedBackground(worker, {
id: 'p1',
language: ContentLanguage.German,
translations: {
smartTitle: `smart title's @re cool#$%`,
},
});

expect(
(await con.getRepository(Post).findOneByOrFail({ id: 'p1' })).translation,
).toEqual({
de: {
title: 'old title',
smartTitle: `smart title's @re cool#$%`,
},
});
});
});
112 changes: 112 additions & 0 deletions __tests__/workers/postUpdated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,37 @@ describe('on post create', () => {
]);
});
});

describe('post alt_title translation', () => {
it('should create post with alt_title translation', async () => {
const uuid = randomUUID();

const postBefore = await con.getRepository(Post).findOneBy({
yggdrasilId: uuid,
});
expect(postBefore).toBeNull();

await expectSuccessfulBackground(worker, {
id: uuid,
content_type: PostType.Article,
alt_title: 'Alt title',
source_id: 'a',
});

const createdPost = await con.getRepository(ArticlePost).findOneBy({
yggdrasilId: uuid,
});

console.log({ uuid });

expect(createdPost).not.toBeNull();
expect(createdPost?.translation).toEqual({
en: {
smartTitle: 'Alt title',
},
});
});
});
});

describe('on post update', () => {
Expand Down Expand Up @@ -1953,6 +1984,87 @@ describe('on post update', () => {
]);
});
});

describe('post alt_title translation', () => {
it('should update post with alt_title English translation', async () => {
const postId = await generateShortId();
const uuid = randomUUID();

const existingPost = await con.getRepository(ArticlePost).save({
id: postId,
shortId: postId,
type: PostType.Article,
yggdrasilId: uuid,
sourceId: 'a',
});

expect(existingPost).not.toBeNull();

await expectSuccessfulBackground(worker, {
id: uuid,
post_id: postId,
content_type: PostType.Article,
alt_title: `Alt title's`,
});

const updatedPost = await con.getRepository(ArticlePost).findOneBy({
id: postId,
});

expect(updatedPost).not.toBeNull();
expect(updatedPost?.translation).toEqual({
en: {
smartTitle: `Alt title's`,
},
});
});
it('should not replace existing translations when updating post with alt_title', async () => {
const postId = await generateShortId();
const uuid = randomUUID();

const existingPost = await con.getRepository(ArticlePost).save({
id: postId,
shortId: postId,
type: PostType.Article,
yggdrasilId: uuid,
sourceId: 'a',
translation: {
en: {
title: 'Post title',
content: 'Post content',
},
de: {
title: 'Post title DE',
},
},
});

expect(existingPost).not.toBeNull();

await expectSuccessfulBackground(worker, {
id: uuid,
post_id: postId,
content_type: PostType.Article,
alt_title: 'Alt title',
});

const updatedPost = await con.getRepository(ArticlePost).findOneBy({
id: postId,
});

expect(updatedPost).not.toBeNull();
expect(updatedPost?.translation).toEqual({
en: {
title: 'Post title',
content: 'Post content',
smartTitle: 'Alt title',
},
de: {
title: 'Post title DE',
},
});
});
});
});

describe('on youtube post', () => {
Expand Down
2 changes: 1 addition & 1 deletion seeds/Post.json

Large diffs are not rendered by default.

20 changes: 14 additions & 6 deletions src/common/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -705,25 +705,33 @@ export const getPostTranslatedTitle = (

export const getSmartTitle = (
contentLanguage: ContentLanguage | null,
translations?: I18nRecord,
legacyTranslations?: I18nRecord, // TODO AS-912 remove when we migrate data to translation column
translations?: Post['translation'],
): string | undefined => {
const fallbackSmartTitle =
translations?.[ContentLanguage.English]?.smartTitle ||
legacyTranslations?.[ContentLanguage.English];

// We will always return the English smart title if the content language is not set
if (!contentLanguage) {
return translations?.[ContentLanguage.English];
return fallbackSmartTitle;
}

return (
translations?.[contentLanguage] ?? translations?.[ContentLanguage.English]
);
const smartTitle =
translations?.[contentLanguage]?.smartTitle ||
legacyTranslations?.[contentLanguage];

return smartTitle ?? fallbackSmartTitle;
};

export const getPostSmartTitle = (
post: Partial<Pick<Post, 'title' | 'contentMeta'>>,
post: Partial<Pick<Post, 'title' | 'contentMeta' | 'translation'>>,
contentLanguage: ContentLanguage | null,
) =>
getSmartTitle(
contentLanguage,
(post.contentMeta as PostContentMeta)?.alt_title?.translations,
post.translation,
) || getPostTranslatedTitle(post, contentLanguage);

export const getModerationItemsAsAdminForSource = async (
Expand Down
Loading