Skip to content

Commit 5bc3959

Browse files
authored
Extracted helper to map feed results to post DTOs (#1385)
ref https://linear.app/ghost/issue/BER-2908/add-dummy-api-endpoint-to-start-vertical-integration - in the context of introducing the global feed, extracted the mapping from a feed result to a post DTO, so that it can reused
1 parent 38232a4 commit 5bc3959

File tree

1 file changed

+45
-37
lines changed

1 file changed

+45
-37
lines changed

src/http/api/feed.controller.ts

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import * as Sentry from '@sentry/node';
22

3+
import type { Account } from '@/account/account.entity';
34
import { getAccountHandle } from '@/account/utils';
45
import type { AppContext } from '@/app';
5-
import type { FeedService, FeedType } from '@/feed/feed.service';
6-
import type { PostDTO } from '@/http/api/types';
6+
import { parseURL } from '@/core/url';
7+
import type {
8+
FeedService,
9+
FeedType,
10+
GetFeedDataResultRow,
11+
} from '@/feed/feed.service';
712
import { APIRoute, RequireRoles } from '@/http/decorators/route.decorator';
813
import { GhostRole } from '@/http/middleware/role-guard';
914
import type { PostInteractionCountsService } from '@/post/post-interaction-counts.service';
@@ -63,7 +68,41 @@ export class FeedController {
6368
cursor,
6469
});
6570

66-
const posts: PostDTO[] = results.map((result) => {
71+
const posts = this.mapFeedResultsToPostDTO(results, account);
72+
73+
// Request an update of the interaction counts for the posts in the
74+
// feed - We do not await this as we do not want to increase the
75+
// response time of the request
76+
this.postInteractionCountsService
77+
.requestUpdate(
78+
ctx.get('site').host,
79+
results.map((post) => post.post_id),
80+
)
81+
.catch((error) => {
82+
Sentry.captureException(error);
83+
84+
ctx.get('logger').error(
85+
'Error requesting update of interaction counts for posts {error}',
86+
{ error },
87+
);
88+
});
89+
90+
return new Response(
91+
JSON.stringify({
92+
posts,
93+
next: nextCursor,
94+
}),
95+
{
96+
status: 200,
97+
},
98+
);
99+
}
100+
101+
private mapFeedResultsToPostDTO(
102+
results: GetFeedDataResultRow[],
103+
myAccount: Account,
104+
) {
105+
return results.map((result) => {
67106
return {
68107
id: result.post_ap_id,
69108
type: result.post_type,
@@ -89,26 +128,22 @@ export class FeedController {
89128
author: {
90129
id: result.author_id.toString(),
91130
handle: getAccountHandle(
92-
result.author_url
93-
? new URL(result.author_url).host
94-
: '',
131+
parseURL(result.author_url)?.host ?? '',
95132
result.author_username,
96133
),
97134
name: result.author_name ?? '',
98135
url: result.author_url ?? '',
99136
avatarUrl: result.author_avatar_url ?? '',
100137
followedByMe: result.author_followed_by_user === 1,
101138
},
102-
authoredByMe: result.author_id === account.id,
139+
authoredByMe: result.author_id === myAccount.id,
103140
repostCount: result.post_repost_count,
104141
repostedByMe: result.post_reposted_by_user === 1,
105142
repostedBy: result.reposter_id
106143
? {
107144
id: result.reposter_id.toString(),
108145
handle: getAccountHandle(
109-
result.reposter_url
110-
? new URL(result.reposter_url).host
111-
: '',
146+
parseURL(result.reposter_url)?.host ?? '',
112147
result.reposter_username,
113148
),
114149
name: result.reposter_name ?? '',
@@ -119,32 +154,5 @@ export class FeedController {
119154
: null,
120155
};
121156
});
122-
123-
// Request an update of the interaction counts for the posts in the
124-
// feed - We do not await this as we do not want to increase the
125-
// response time of the request
126-
this.postInteractionCountsService
127-
.requestUpdate(
128-
ctx.get('site').host,
129-
results.map((post) => post.post_id),
130-
)
131-
.catch((error) => {
132-
Sentry.captureException(error);
133-
134-
ctx.get('logger').error(
135-
'Error requesting update of interaction counts for posts {error}',
136-
{ error },
137-
);
138-
});
139-
140-
return new Response(
141-
JSON.stringify({
142-
posts,
143-
next: nextCursor,
144-
}),
145-
{
146-
status: 200,
147-
},
148-
);
149157
}
150158
}

0 commit comments

Comments
 (0)