Skip to content

Commit b0c9023

Browse files
committed
Address feedV2 review comments
1 parent 049cd14 commit b0c9023

7 files changed

Lines changed: 386 additions & 369 deletions

File tree

__tests__/feeds.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,7 +1218,7 @@ describe('query feedV2', () => {
12181218
});
12191219
nock('http://localhost:6000')
12201220
.post('/feed.json', (body) => {
1221-
expect(body.allowed_post_types).toEqual(['article', 'highlight']);
1221+
expect(body.allowed_post_types).toEqual(['article']);
12221222
expect(body.highlights_limit).toEqual(4);
12231223
return true;
12241224
})
@@ -1274,7 +1274,6 @@ describe('query feedV2', () => {
12741274
{ post_id: 'p1', metadata: { p: 'post' } },
12751275
{
12761276
type: 'highlight',
1277-
post_id: '',
12781277
highlight_ids: [
12791278
'3c75fab6-e28b-431d-ab54-a927708de085',
12801279
'c2e332bf-83ac-4651-8a05-8e19fbefc5ac',

__tests__/integrations/feed.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ describe('FeedClient', () => {
184184
{ type: 'post', id: '1', feedMeta: '{"p":"a"}' },
185185
{
186186
type: 'highlight',
187-
postId: null,
188187
highlightIds: ['h1', 'h2'],
189188
feedMeta: '{"p":"highlight"}',
190189
},

src/integrations/feed/clients.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ export class FeedClient implements IFeedClient, IGarmrClient {
8181
if (type === 'highlight') {
8282
return {
8383
type: 'highlight',
84-
postId: post_id || null,
8584
highlightIds: highlight_ids || [],
8685
feedMeta,
8786
};

src/integrations/feed/types.ts

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ import type { FeedFlags } from '../../entity';
33
import { GenericMetadata } from '../lofn';
44

55
export type FeedResponsePostItem = {
6-
type: 'post';
6+
type?: 'post';
77
id: string;
88
feedMeta: string | null;
99
};
1010

1111
export type FeedResponseHighlightItem = {
1212
type: 'highlight';
13-
postId: string | null;
1413
highlightIds: string[];
1514
feedMeta: string | null;
1615
};
@@ -23,27 +22,16 @@ export type FeedResponse = {
2322
staleCursor?: boolean; // True when feed cache was regenerated and cursor became stale
2423
};
2524

26-
export const isFeedResponsePostItem = (
27-
item: FeedResponseItem,
28-
): item is FeedResponsePostItem => item.type === 'post';
29-
3025
export const isFeedResponseHighlightItem = (
3126
item: FeedResponseItem,
3227
): item is FeedResponseHighlightItem => item.type === 'highlight';
3328

34-
export const getFeedResponsePostItems = (
35-
response: Pick<FeedResponse, 'data'>,
36-
): FeedResponsePostItem[] => response.data.filter(isFeedResponsePostItem);
37-
3829
export const getFeedResponsePostIds = (
3930
response: Pick<FeedResponse, 'data'>,
40-
): string[] => getFeedResponsePostItems(response).map(({ id }) => id);
41-
42-
export const findFeedResponsePostItem = (
43-
response: Pick<FeedResponse, 'data'>,
44-
id: string,
45-
): FeedResponsePostItem | undefined =>
46-
getFeedResponsePostItems(response).find((item) => item.id === id);
31+
): string[] =>
32+
response.data.flatMap((item) =>
33+
isFeedResponseHighlightItem(item) ? [] : [item.id],
34+
);
4735

4836
export enum FeedConfigName {
4937
Personalise = 'personalise',

src/schema/common.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@ import {
2323

2424
import { BaseContext, Context } from '../Context';
2525
import type { MeiliPagination } from '../integrations/meilisearch';
26-
import {
27-
FeedResponse,
28-
findFeedResponsePostItem,
29-
getFeedResponsePostItems,
30-
} from '../integrations/feed/types';
26+
import { FeedResponse } from '../integrations/feed/types';
3127

3228
export interface GQLEmptyResponse {
3329
_: boolean;
@@ -279,18 +275,20 @@ export const fixedIdsPageGenerator = <
279275
nodeToCursor: (page, args, node, i, queryParams): string =>
280276
offsetToCursor(
281277
page.offset +
282-
getFeedResponsePostItems(queryParams!).findIndex(
283-
(item) => item.id === node.id,
278+
queryParams!.data.findIndex(
279+
(item) => item.type !== 'highlight' && item.id === node.id,
284280
),
285281
),
286282
hasNextPage: (page, nodesSize, total, queryParams): boolean =>
287-
getFeedResponsePostItems(queryParams!).length >= page.limit,
283+
queryParams!.data.length >= page.limit,
288284
hasPreviousPage: (page): boolean => page.offset > 0,
289285
transformNodes: (page, nodes, queryParams) => {
290286
// Add the metadata object
291287
return nodes.slice(0, page.limit - 1).map((node) => ({
292288
...node,
293-
feedMeta: findFeedResponsePostItem(queryParams!, node.id)?.feedMeta,
289+
feedMeta: queryParams!.data.find(
290+
(item) => item.type !== 'highlight' && item.id === node.id,
291+
)?.feedMeta,
294292
}));
295293
},
296294
});
@@ -311,13 +309,15 @@ export const feedCursorPageGenerator = <
311309
nodeToCursor: (page, args, node, i, queryParams): string =>
312310
queryParams!.cursor as string,
313311
hasNextPage: (page, nodesSize, total, queryParams): boolean =>
314-
getFeedResponsePostItems(queryParams!).length >= page.limit,
312+
queryParams!.data.length >= page.limit,
315313
hasPreviousPage: (page): boolean => !!page.cursor,
316314
transformNodes: (page, nodes, queryParams) => {
317315
// Add the metadata object
318316
return nodes.map((node) => ({
319317
...node,
320-
feedMeta: findFeedResponsePostItem(queryParams!, node.id)?.feedMeta,
318+
feedMeta: queryParams!.data.find(
319+
(item) => item.type !== 'highlight' && item.id === node.id,
320+
)?.feedMeta,
321321
}));
322322
},
323323
});

0 commit comments

Comments
 (0)