Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated query for retrieving thread parents / children #163

Merged
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
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