Skip to content

Commit 760428b

Browse files
authored
Updated getPostsByAccount query to get if a post id liked/reposted by the current user (#488)
Ref https://linear.app/ghost/issue/AP-1024/ Previously, this query (`getPostsByAccount`) was used to fetch only the posts of the current user for the Profile view, so `likedByMe` and `repostedByMe` indicated whether the posts were liked or reposted by that account. Now, this query will also fetch posts from other ghost accounts, so the `likedByMe` and `repostedByMe` fields should indicate whether the current user (the one viewing the profile) has liked or reposted the posts.
1 parent d298940 commit 760428b

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

src/http/api/account.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ export function createGetAccountPostsHandler(
256256
ctx.get('site'),
257257
);
258258
const { results, nextCursor } = await postService.getPostsByAccount(
259+
account.id,
259260
account.id,
260261
params.limit,
261262
params.cursor,

src/post/post.service.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ interface BaseGetProfileDataResultRow {
1717
post_image_url: string | null;
1818
post_published_at: Date;
1919
post_like_count: number;
20-
post_liked_by_user: 0 | 1;
20+
post_liked_by_current_user: 0 | 1;
2121
post_reply_count: number;
2222
post_reading_time_minutes: number;
2323
post_repost_count: number;
24-
post_reposted_by_user: 0 | 1;
24+
post_reposted_by_current_user: 0 | 1;
2525
post_ap_id: string;
2626
post_attachments: {
2727
type: string | null;
@@ -90,7 +90,7 @@ export class PostService {
9090
featureImageUrl: result.post_image_url ?? null,
9191
publishedAt: result.post_published_at,
9292
likeCount: result.post_like_count,
93-
likedByMe: result.post_liked_by_user === 1,
93+
likedByMe: result.post_liked_by_current_user === 1,
9494
replyCount: result.post_reply_count,
9595
readingTimeMinutes: result.post_reading_time_minutes,
9696
attachments: result.post_attachments
@@ -113,7 +113,7 @@ export class PostService {
113113
},
114114
authoredByMe: result.author_id === accountId,
115115
repostCount: result.post_repost_count,
116-
repostedByMe: result.post_reposted_by_user === 1,
116+
repostedByMe: result.post_reposted_by_current_user === 1,
117117
repostedBy: result.reposter_id
118118
? {
119119
id: result.reposter_id.toString(),
@@ -235,6 +235,7 @@ export class PostService {
235235
*/
236236
async getPostsByAccount(
237237
accountId: number,
238+
defaultAccountId: number,
238239
limit: number,
239240
cursor: string | null,
240241
): Promise<GetProfileDataResult> {
@@ -252,19 +253,19 @@ export class PostService {
252253
'posts_with_source.like_count as post_like_count',
253254
this.db.raw(
254255
`CASE
255-
WHEN likes.post_id IS NOT NULL THEN 1
256+
WHEN current_user_likes.post_id IS NOT NULL THEN 1
256257
ELSE 0
257-
END AS post_liked_by_user`,
258+
END AS post_liked_by_current_user`,
258259
),
259260
'posts_with_source.reply_count as post_reply_count',
260261
'posts_with_source.reading_time_minutes as post_reading_time_minutes',
261262
'posts_with_source.attachments as post_attachments',
262263
'posts_with_source.repost_count as post_repost_count',
263264
this.db.raw(
264265
`CASE
265-
WHEN user_reposts.post_id IS NOT NULL THEN 1
266+
WHEN current_user_reposts.post_id IS NOT NULL THEN 1
266267
ELSE 0
267-
END AS post_reposted_by_user`,
268+
END AS post_reposted_by_current_user`,
268269
),
269270
'posts_with_source.ap_id as post_ap_id',
270271
// Author fields (Who originally created the post)
@@ -355,21 +356,24 @@ export class PostService {
355356
'reposter_account.id',
356357
'posts_with_source.reposter_id',
357358
)
358-
.leftJoin('likes', function () {
359-
this.on('likes.post_id', 'posts_with_source.id').andOnVal(
360-
'likes.account_id',
359+
.leftJoin('likes as current_user_likes', function () {
360+
this.on(
361+
'current_user_likes.post_id',
362+
'posts_with_source.id',
363+
).andOnVal(
364+
'current_user_likes.account_id',
361365
'=',
362-
accountId.toString(),
366+
defaultAccountId.toString(),
363367
);
364368
})
365-
.leftJoin('reposts as user_reposts', function () {
369+
.leftJoin('reposts as current_user_reposts', function () {
366370
this.on(
367-
'user_reposts.post_id',
371+
'current_user_reposts.post_id',
368372
'posts_with_source.id',
369373
).andOnVal(
370-
'user_reposts.account_id',
374+
'current_user_reposts.account_id',
371375
'=',
372-
accountId.toString(),
376+
defaultAccountId.toString(),
373377
);
374378
})
375379
.modify((query) => {
@@ -425,7 +429,7 @@ export class PostService {
425429
'posts.image_url as post_image_url',
426430
'posts.published_at as post_published_at',
427431
'posts.like_count as post_like_count',
428-
this.db.raw('1 AS post_liked_by_user'), // Since we are selecting from `likes`, this is always 1
432+
this.db.raw('1 AS post_liked_by_current_user'), // Since we are selecting from `likes`, this is always 1
429433
'posts.reply_count as post_reply_count',
430434
'posts.reading_time_minutes as post_reading_time_minutes',
431435
'posts.attachments as post_attachments',
@@ -434,7 +438,7 @@ export class PostService {
434438
`CASE
435439
WHEN reposts.post_id IS NOT NULL THEN 1
436440
ELSE 0
437-
END AS post_reposted_by_user`,
441+
END AS post_reposted_by_current_user`,
438442
),
439443
'posts.ap_id as post_ap_id',
440444
// Author fields

0 commit comments

Comments
 (0)