Skip to content

Commit

Permalink
Updated query for retrieving thread parents / children
Browse files Browse the repository at this point in the history
refs [AP-579](https://linear.app/ghost/issue/AP-579/investigate-parent-post-visibility-issues-in-replies-to-articles)

Updated the query for retrieving thread parents / children so that it takes into
account the activity type. This is needed because when we query on the object
it is possible for there to be multiple activities with a matching object if
the actvity embeds the object (i.e in the case of a `Like` activity). This is
important when looking up the immediate parent of an activity (as we only expect
there to be 1 parent). Scoping the type of activity should be fine for our
use case of "threads" (a list of comments)
  • Loading branch information
mike182uk committed Nov 12, 2024
1 parent 96673bc commit 89e2ca3
Showing 1 changed file with 34 additions and 25 deletions.
59 changes: 34 additions & 25 deletions src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,22 @@ export async function getActivityChildren(activity: ActivityJsonLd) {
const results = await client
.select('value')
.from('key_value')
// If inReplyTo is a string
.where(
client.raw(
`JSON_EXTRACT(value, "$.object.inReplyTo") = "${objectId}"`,
),
)
// If inReplyTo is an object
.orWhere(
client.raw(
`JSON_EXTRACT(value, "$.object.inReplyTo.id") = "${objectId}"`,
),
);
.where(function () {
// If inReplyTo is a string
this.where(
client.raw(
`JSON_EXTRACT(value, "$.object.inReplyTo") = "${objectId}"`,
),
);

// If inReplyTo is an object
this.orWhere(
client.raw(
`JSON_EXTRACT(value, "$.object.inReplyTo.id") = "${objectId}"`,
),
);
})
.andWhere(client.raw(`JSON_EXTRACT(value, "$.type") = "Create"`));

return results.map((result) => result.value);
}
Expand All @@ -147,18 +151,22 @@ export async function getActivityChildrenCount(activity: ActivityJsonLd) {
const result = await client
.count('* as count')
.from('key_value')
// If inReplyTo is a string
.where(
client.raw(
`JSON_EXTRACT(value, "$.object.inReplyTo") = "${objectId}"`,
),
)
// If inReplyTo is an object
.orWhere(
client.raw(
`JSON_EXTRACT(value, "$.object.inReplyTo.id") = "${objectId}"`,
),
);
.where(function () {
// If inReplyTo is a string
this.where(
client.raw(
`JSON_EXTRACT(value, "$.object.inReplyTo") = "${objectId}"`,
),
);

// If inReplyTo is an object
this.orWhere(
client.raw(
`JSON_EXTRACT(value, "$.object.inReplyTo.id") = "${objectId}"`,
),
);
})
.andWhere(client.raw(`JSON_EXTRACT(value, "$.type") = "Create"`));

return result[0].count;
}
Expand All @@ -174,7 +182,8 @@ export async function getActivityParents(activity: ActivityJsonLd) {
client.raw(
`JSON_EXTRACT(value, "$.object.id") = "${objectId}"`,
),
);
)
.andWhere(client.raw(`JSON_EXTRACT(value, "$.type") = "Create"`));

if (result.length === 1) {
const parent = result[0];
Expand Down

0 comments on commit 89e2ca3

Please sign in to comment.