Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
55 changes: 55 additions & 0 deletions __tests__/notifications/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1490,6 +1490,61 @@ describe('storeNotificationBundle', () => {
]);
});

it('should truncate attachment title for SocialTwitter posts exceeding 140 characters', () => {
const type = NotificationType.SourcePostAdded;
const longTitle = 'a'.repeat(200);
const post = {
...postsFixture[0],
title: longTitle,
type: PostType.SocialTwitter,
} as Reference<Post>;
const ctx: NotificationSourceContext & NotificationPostContext = {
userIds: [userId],
source: sourcesFixture[0] as Reference<Source>,
post,
};
const actual = generateNotificationV2(type, ctx);

expect(actual.attachments![0].title).toEqual(`${'a'.repeat(137)}...`);
expect(actual.attachments![0].title!.length).toEqual(140);
});

it('should not truncate attachment title for SocialTwitter posts within 140 characters', () => {
const type = NotificationType.SourcePostAdded;
const shortTitle = 'a'.repeat(140);
const post = {
...postsFixture[0],
title: shortTitle,
type: PostType.SocialTwitter,
} as Reference<Post>;
const ctx: NotificationSourceContext & NotificationPostContext = {
userIds: [userId],
source: sourcesFixture[0] as Reference<Source>,
post,
};
const actual = generateNotificationV2(type, ctx);

expect(actual.attachments![0].title).toEqual(shortTitle);
});

it('should not truncate attachment title for non-SocialTwitter posts', () => {
const type = NotificationType.SourcePostAdded;
const longTitle = 'a'.repeat(200);
const post = {
...postsFixture[0],
title: longTitle,
type: PostType.Article,
} as Reference<Post>;
const ctx: NotificationSourceContext & NotificationPostContext = {
userIds: [userId],
source: sourcesFixture[0] as Reference<Source>,
post,
};
const actual = generateNotificationV2(type, ctx);

expect(actual.attachments![0].title).toEqual(longTitle);
});

it('should generate user_post_added notification', () => {
const type = NotificationType.UserPostAdded;
const ctx: NotificationUserContext & NotificationPostContext = {
Expand Down
10 changes: 9 additions & 1 deletion src/notifications/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,18 @@ export class NotificationBuilder {
post.type as keyof typeof postTypeToAttachmentType
] ?? NotificationAttachmentType.Post;

const MAX_TWITTER_TITLE_LENGTH = 140;
const title = post.title ?? '';
const truncatedTitle =
post.type === PostType.SocialTwitter &&
title.length > MAX_TWITTER_TITLE_LENGTH
? `${title.substring(0, MAX_TWITTER_TITLE_LENGTH - 3)}...`
: title;

this.attachments.push({
type,
image: (post as ArticlePost)?.image || pickImageUrl(post),
title: post.title ?? '',
title: truncatedTitle,
referenceId: post.id,
});
return this;
Expand Down
Loading