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
4 changes: 4 additions & 0 deletions __tests__/highlights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ const MAJOR_HEADLINES_QUERY = `
channel
highlightedAt
headline
significance
post {
id
title
Expand Down Expand Up @@ -390,16 +391,19 @@ describe('query majorHeadlines', () => {
expect.objectContaining({
channel: 'vibes',
headline: 'Newer breaking headline',
significance: 'breaking',
post: { id: 'h1', title: 'Test Post 1' },
}),
expect.objectContaining({
channel: 'vibes',
headline: 'Breaking headline',
significance: 'breaking',
post: { id: 'h2', title: 'Test Post 2' },
}),
expect.objectContaining({
channel: 'agentic',
headline: 'Major headline',
significance: 'major',
post: { id: 'h4', title: 'Test Post 4' },
}),
]);
Expand Down
87 changes: 87 additions & 0 deletions __tests__/posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ import {
WelcomePost,
YouTubePost,
} from '../src/entity';
import {
PostHighlight,
PostHighlightSignificance,
} from '../src/entity/PostHighlight';
import { Roles, SourceMemberRoles, sourceRoleRank } from '../src/roles';
import { sourcesFixture } from './fixture/source';
import {
Expand Down Expand Up @@ -941,6 +945,89 @@ describe('sharedPost field', () => {
});
});

describe('postHighlight field', () => {
const QUERY = `{
post(id: "p1") {
postHighlight {
id
channel
headline
significance
highlightedAt
}
}
}`;

it('returns null when the post has no active highlight', async () => {
const res = await client.query(QUERY);
expect(res.errors).toBeFalsy();
expect(res.data.post.postHighlight).toBeNull();
});

it('returns the active highlight when one exists', async () => {
await con.getRepository(PostHighlight).save({
postId: 'p1',
channel: 'vibes',
highlightedAt: new Date(),
headline: 'Breaking headline',
significance: PostHighlightSignificance.Breaking,
});

const res = await client.query(QUERY);
expect(res.errors).toBeFalsy();
expect(res.data.post.postHighlight).toEqual({
id: expect.any(String),
channel: 'vibes',
headline: 'Breaking headline',
significance: 'breaking',
highlightedAt: expect.any(String),
});
});

it('returns null when the highlight significance is unspecified', async () => {
await con.getRepository(PostHighlight).save({
postId: 'p1',
channel: 'vibes',
highlightedAt: new Date(),
headline: 'Unspecified headline',
significance: PostHighlightSignificance.Unspecified,
});

const res = await client.query(QUERY);
expect(res.errors).toBeFalsy();
expect(res.data.post.postHighlight).toBeNull();
});

it('returns null when the highlight is retired', async () => {
await con.getRepository(PostHighlight).save({
postId: 'p1',
channel: 'vibes',
highlightedAt: new Date(),
headline: 'Retired headline',
significance: PostHighlightSignificance.Breaking,
retiredAt: new Date(),
});

const res = await client.query(QUERY);
expect(res.errors).toBeFalsy();
expect(res.data.post.postHighlight).toBeNull();
});

it('returns null when the highlight is older than the TTL', async () => {
await con.getRepository(PostHighlight).save({
postId: 'p1',
channel: 'vibes',
highlightedAt: new Date(Date.now() - 24 * 60 * 60 * 1000),
headline: 'Stale headline',
significance: PostHighlightSignificance.Breaking,
});

const res = await client.query(QUERY);
expect(res.errors).toBeFalsy();
expect(res.data.post.postHighlight).toBeNull();
});
});

describe('type field', () => {
const QUERY = `{
post(id: "p1") {
Expand Down
44 changes: 44 additions & 0 deletions src/graphorm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
domainOnly,
getSmartTitle,
getTranslationRecord,
ONE_HOUR_IN_SECONDS,
transformDate,
} from '../common';
import { GQLComment } from '../schema/comments';
Expand All @@ -54,6 +55,10 @@ import {
import { whereVordrFilter } from '../common/vordr';
import { MIN_INDEXABLE_REPUTATION } from '../common/users';
import { UserCompany, Post } from '../entity';
import {
PostHighlightSignificance,
toPostHighlightSignificanceLabel,
} from '../entity/PostHighlight';
import {
ContentPreferenceStatus,
ContentPreferenceType,
Expand Down Expand Up @@ -93,6 +98,15 @@ export enum LocationVerificationStatus {
Verified = 'verified',
}

const getPostHighlightTtlSeconds = (): number => {
const DEFAULT_POST_HIGHLIGHT_TTL_SECONDS = 12 * ONE_HOUR_IN_SECONDS;

return (
remoteConfig.vars.postHighlightTtlSeconds ??
DEFAULT_POST_HIGHLIGHT_TTL_SECONDS
);
};

const existsByUserAndPost =
(entity: string, build?: (queryBuilder: QueryBuilder) => QueryBuilder) =>
(ctx: Context, alias: string, qb: QueryBuilder): string => {
Expand Down Expand Up @@ -726,6 +740,25 @@ const obj = new GraphORM({
.andWhere(`${childAlias}."deleted" = false`),
},
},
postHighlight: {
relation: {
isMany: false,
customRelation: (_, parentAlias, childAlias, qb): QueryBuilder =>
qb
.where(`${childAlias}."postId" = ${parentAlias}."id"`)
.andWhere(`${childAlias}."significance" != :unspecified`, {
unspecified: PostHighlightSignificance.Unspecified,
})
.andWhere(`${childAlias}."retiredAt" IS NULL`)
.andWhere(
`${childAlias}."highlightedAt" > now() - (:ttlSeconds || ' seconds')::interval`,
{ ttlSeconds: getPostHighlightTtlSeconds() },
)
.orderBy(`${childAlias}."significance"`, 'ASC')
.addOrderBy(`${childAlias}."highlightedAt"`, 'DESC')
.limit(1),
},
},
liveRoom: {
relation: {
isMany: false,
Expand Down Expand Up @@ -2739,6 +2772,17 @@ const obj = new GraphORM({
updatedAt: { transform: transformDate },
},
},
PostHighlight: {
fields: {
significance: {
transform: (value: PostHighlightSignificance) =>
toPostHighlightSignificanceLabel(value),
},
highlightedAt: { transform: transformDate },
createdAt: { transform: transformDate },
updatedAt: { transform: transformDate },
},
},
});

export default obj;
1 change: 1 addition & 0 deletions src/remoteConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export type RemoteConfigValue = {
newViewLogs: boolean;
verboseGqlLogging: boolean;
engagementAdsEnabled: boolean;
postHighlightTtlSeconds: number;
};

class RemoteConfig {
Expand Down
1 change: 1 addition & 0 deletions src/schema/highlights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export const typeDefs = /* GraphQL */ `
channel: String!
highlightedAt: DateTime!
headline: String!
significance: String
createdAt: DateTime!
updatedAt: DateTime!
}
Expand Down
7 changes: 7 additions & 0 deletions src/schema/posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,13 @@ export const typeDefs = /* GraphQL */ `
"""
sharedPost: Post

"""
Currently-active highlight for this post, across all significance tiers
(breaking, major, notable, routine). Null when no highlight is active or
it has expired.
"""
postHighlight: PostHighlight

"""
Additional information required for analytics purposes
"""
Expand Down
Loading