@jant/core 0.8.1 → 0.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{app--uxnaIRV.js → app-B3P2N2vM.js} +1346 -310
- package/dist/client/.vite/manifest.json +2 -2
- package/dist/client/_assets/{client-auth-begiT1tq.js → client-auth-CeGBe0qG.js} +1 -1
- package/dist/client/_assets/{client-BT6kpcuG.js → client-nByuF0Ct.js} +1 -1
- package/dist/index.js +1 -1
- package/dist/node.js +2 -2
- package/package.json +1 -1
- package/src/__tests__/helpers/cursor-walk.ts +37 -7
- package/src/app.tsx +4 -0
- package/src/lib/__tests__/discover.test.ts +24 -0
- package/src/lib/__tests__/thread-fold.test.ts +43 -0
- package/src/lib/api-posts.ts +110 -2
- package/src/lib/api-public-posts.ts +263 -0
- package/src/lib/api-threads.ts +207 -0
- package/src/lib/discover.ts +34 -0
- package/src/lib/thread-fold.ts +83 -41
- package/src/lib/thread-query.ts +143 -0
- package/src/middleware/cors.ts +8 -1
- package/src/middleware/deprecation.ts +48 -0
- package/src/routes/api/__tests__/mcp.test.ts +143 -0
- package/src/routes/api/__tests__/posts.test.ts +78 -0
- package/src/routes/api/__tests__/public-access.test.ts +5 -0
- package/src/routes/api/__tests__/search.test.ts +104 -0
- package/src/routes/api/__tests__/threads.test.ts +115 -0
- package/src/routes/api/posts.ts +15 -104
- package/src/routes/api/public/__tests__/threads.test.ts +363 -0
- package/src/routes/api/public/archive.ts +82 -81
- package/src/routes/api/public/posts.ts +97 -298
- package/src/routes/api/public/threads.ts +141 -0
- package/src/routes/api/search.ts +31 -2
- package/src/routes/api/threads.ts +128 -0
- package/src/routes/auth/__tests__/setup-discover.test.ts +29 -1
- package/src/routes/auth/__tests__/setup-page.test.tsx +59 -1
- package/src/routes/auth/setup.tsx +48 -9
- package/src/routes/pages/__tests__/search.test.ts +58 -0
- package/src/routes/pages/search.tsx +3 -1
- package/src/services/__tests__/search.test.ts +211 -0
- package/src/services/__tests__/thread.test.ts +495 -0
- package/src/services/index.ts +4 -0
- package/src/services/mcp.ts +217 -57
- package/src/services/post.ts +222 -43
- package/src/services/search.ts +77 -66
- package/src/services/thread.ts +392 -0
- package/src/types/constants.ts +25 -0
- package/src/ui/feed/ThreadPreview.tsx +9 -14
- package/src/ui/feed/__tests__/thread-preview.test.ts +18 -22
- package/src/ui/feed/thread-preview-state.ts +0 -35
package/src/services/post.ts
CHANGED
|
@@ -275,6 +275,13 @@ export interface ThreadRootPageOptions {
|
|
|
275
275
|
rootFormat?: Format;
|
|
276
276
|
/** Restrict to Threads with at least one rated published post. */
|
|
277
277
|
hasRating?: true;
|
|
278
|
+
/**
|
|
279
|
+
* Restrict to Threads whose root matches these filters, the way a list of
|
|
280
|
+
* roots would (`excludeReplies` is implied). Lets a Thread-grouped order,
|
|
281
|
+
* such as a collection's, take the same filter dimensions `list()` does.
|
|
282
|
+
* Sorting and paging fields are ignored.
|
|
283
|
+
*/
|
|
284
|
+
rootFilters?: PostFilters;
|
|
278
285
|
limit?: number;
|
|
279
286
|
offset?: number;
|
|
280
287
|
}
|
|
@@ -304,7 +311,13 @@ interface ListSort {
|
|
|
304
311
|
pinned: boolean;
|
|
305
312
|
}
|
|
306
313
|
|
|
307
|
-
/**
|
|
314
|
+
/**
|
|
315
|
+
* The sort mode a cursor into one Thread's Posts records. Distinct from every
|
|
316
|
+
* `list()` and collection mode, so a position in one never resumes another.
|
|
317
|
+
*/
|
|
318
|
+
const THREAD_POSTS_SORT_MODE = "thread";
|
|
319
|
+
|
|
320
|
+
/** One term of a paged ORDER BY: `list()`'s, a collection's, or a Thread's. */
|
|
308
321
|
interface ListSortKey {
|
|
309
322
|
/** Selectable as a column, so a page's cursor is read from SQL. */
|
|
310
323
|
expr: SQL;
|
|
@@ -587,6 +600,44 @@ export interface PostService {
|
|
|
587
600
|
* then replies by creation time, then ID.
|
|
588
601
|
*/
|
|
589
602
|
getThread(rootId: string): Promise<Post[]>;
|
|
603
|
+
/**
|
|
604
|
+
* One page of a Thread's Posts in Thread order, and an opaque cursor for the
|
|
605
|
+
* next.
|
|
606
|
+
*
|
|
607
|
+
* Pages like {@link listPage}. A new reply joins the end of the Thread, so a
|
|
608
|
+
* walk that is still going when one is published reaches it rather than
|
|
609
|
+
* skipping it. As well as a `nextCursor`, the cursor may be the ID of a Post
|
|
610
|
+
* in this Thread with the requested status: the page then starts right after
|
|
611
|
+
* it, which is how a reader opens the run a fold hides.
|
|
612
|
+
*
|
|
613
|
+
* @param threadId - The Thread root's ID
|
|
614
|
+
* @param options - Which Posts to include; `status` defaults to `published`
|
|
615
|
+
* @param page - The previous page's `nextCursor` or a Post ID, and the page
|
|
616
|
+
* size
|
|
617
|
+
* @returns The page's Posts and the cursor after them, `null` on the last page
|
|
618
|
+
* @throws {ValidationError} When the cursor can't be read, was taken in
|
|
619
|
+
* another order, or names a Post this request doesn't list
|
|
620
|
+
* @example
|
|
621
|
+
* ```ts
|
|
622
|
+
* const first = await posts.listThreadPostsPage(root.id, {}, { limit: 100 });
|
|
623
|
+
* ```
|
|
624
|
+
*/
|
|
625
|
+
listThreadPostsPage(
|
|
626
|
+
threadId: string,
|
|
627
|
+
options: { status?: Status },
|
|
628
|
+
page: PostListPageOptions,
|
|
629
|
+
): Promise<PostListPage>;
|
|
630
|
+
/**
|
|
631
|
+
* How many published Posts each Thread holds, its root included.
|
|
632
|
+
*
|
|
633
|
+
* @param threadIds - Thread root IDs; duplicates are fine
|
|
634
|
+
* @returns Map of Thread root ID to its published Post count; a Thread with
|
|
635
|
+
* nothing published is absent
|
|
636
|
+
* @example
|
|
637
|
+
* const counts = await posts.countThreadPosts([root.id]);
|
|
638
|
+
* counts.get(root.id); // 12
|
|
639
|
+
*/
|
|
640
|
+
countThreadPosts(threadIds: string[]): Promise<Map<string, number>>;
|
|
590
641
|
/**
|
|
591
642
|
* 1-based position of a Post in the reply chain running from its Thread root
|
|
592
643
|
* down to it: a root is 1, a reply to it is 2, a reply to that is 3.
|
|
@@ -1116,14 +1167,33 @@ export function createPostService(
|
|
|
1116
1167
|
* sql`ROW_NUMBER() OVER (ORDER BY ${sql.join(threadOrder(), sql`, `)})`;
|
|
1117
1168
|
*/
|
|
1118
1169
|
function threadOrder(direction: "asc" | "desc" = "asc"): SQL[] {
|
|
1119
|
-
const terms =
|
|
1120
|
-
sql`CASE WHEN ${posts.replyToId} IS NULL THEN 0 ELSE 1 END`,
|
|
1121
|
-
sql`${posts.createdAt}`,
|
|
1122
|
-
sql`${posts.id}`,
|
|
1123
|
-
];
|
|
1170
|
+
const terms = threadSortKeys().map((key) => key.expr);
|
|
1124
1171
|
return direction === "asc" ? terms : terms.map((term) => desc(term));
|
|
1125
1172
|
}
|
|
1126
1173
|
|
|
1174
|
+
/**
|
|
1175
|
+
* {@link threadOrder} as paged sort keys, for walking one Thread with a
|
|
1176
|
+
* cursor. The one definition both read, so a page of a Thread and the
|
|
1177
|
+
* Thread page itself cannot order its posts differently.
|
|
1178
|
+
*/
|
|
1179
|
+
function threadSortKeys(): ListSortKey[] {
|
|
1180
|
+
return [
|
|
1181
|
+
{
|
|
1182
|
+
expr: sql<number>`CASE WHEN ${posts.replyToId} IS NULL THEN 0 ELSE 1 END`.mapWith(
|
|
1183
|
+
Number,
|
|
1184
|
+
),
|
|
1185
|
+
direction: "asc",
|
|
1186
|
+
kind: "number",
|
|
1187
|
+
},
|
|
1188
|
+
{
|
|
1189
|
+
expr: sql<number>`${posts.createdAt}`.mapWith(Number),
|
|
1190
|
+
direction: "asc",
|
|
1191
|
+
kind: "number",
|
|
1192
|
+
},
|
|
1193
|
+
{ expr: sql<string>`${posts.id}`, direction: "asc", kind: "id" },
|
|
1194
|
+
];
|
|
1195
|
+
}
|
|
1196
|
+
|
|
1127
1197
|
function buildYearMonthExpr(column: SQLWrapper): SQL<string> {
|
|
1128
1198
|
return databaseDialect === "pg"
|
|
1129
1199
|
? sql<string>`to_char(timezone('UTC', to_timestamp(${column})), 'YYYY-MM')`
|
|
@@ -1957,6 +2027,26 @@ export function createPostService(
|
|
|
1957
2027
|
if (options?.threadIds !== undefined) {
|
|
1958
2028
|
conditions.push(inArray(posts.threadId, options.threadIds));
|
|
1959
2029
|
}
|
|
2030
|
+
if (options?.rootFilters) {
|
|
2031
|
+
// Uncorrelated: the inner `post` shadows the outer one, so every column
|
|
2032
|
+
// the filters name reads the root row being matched.
|
|
2033
|
+
conditions.push(
|
|
2034
|
+
inArray(
|
|
2035
|
+
posts.threadId,
|
|
2036
|
+
db
|
|
2037
|
+
.select({ id: posts.id })
|
|
2038
|
+
.from(posts)
|
|
2039
|
+
.where(
|
|
2040
|
+
and(
|
|
2041
|
+
...buildFilterConditions({
|
|
2042
|
+
...options.rootFilters,
|
|
2043
|
+
excludeReplies: true,
|
|
2044
|
+
}),
|
|
2045
|
+
),
|
|
2046
|
+
),
|
|
2047
|
+
),
|
|
2048
|
+
);
|
|
2049
|
+
}
|
|
1960
2050
|
|
|
1961
2051
|
return conditions;
|
|
1962
2052
|
}
|
|
@@ -3918,6 +4008,87 @@ export function createPostService(
|
|
|
3918
4008
|
return hydratePosts(rows);
|
|
3919
4009
|
},
|
|
3920
4010
|
|
|
4011
|
+
async listThreadPostsPage(threadId, options, { cursor, limit }) {
|
|
4012
|
+
const keys = threadSortKeys();
|
|
4013
|
+
const conditions: SQL[] = [
|
|
4014
|
+
eq(posts.siteId, siteId),
|
|
4015
|
+
eq(posts.threadId, threadId),
|
|
4016
|
+
eq(posts.status, options.status ?? "published"),
|
|
4017
|
+
];
|
|
4018
|
+
|
|
4019
|
+
if (cursor) {
|
|
4020
|
+
let values: PostListCursorValue[];
|
|
4021
|
+
if (isLegacyPostListCursor(cursor)) {
|
|
4022
|
+
// A Post ID names a position only inside the Thread being listed,
|
|
4023
|
+
// and only among the Posts this request would return, so it can't
|
|
4024
|
+
// be used to learn anything about a Post elsewhere.
|
|
4025
|
+
const rows = await db
|
|
4026
|
+
.select(listSortKeyFields(keys))
|
|
4027
|
+
.from(posts)
|
|
4028
|
+
.where(and(...conditions, eq(posts.id, cursor)))
|
|
4029
|
+
.limit(1);
|
|
4030
|
+
const row = rows[0];
|
|
4031
|
+
if (!row) {
|
|
4032
|
+
throw new ValidationError(MISSING_LEGACY_CURSOR_MESSAGE);
|
|
4033
|
+
}
|
|
4034
|
+
values = readListSortKeyValues(keys, row);
|
|
4035
|
+
} else {
|
|
4036
|
+
values = decodePostListCursor(cursor, {
|
|
4037
|
+
mode: THREAD_POSTS_SORT_MODE,
|
|
4038
|
+
kinds: keys.map((key) => key.kind),
|
|
4039
|
+
});
|
|
4040
|
+
}
|
|
4041
|
+
conditions.push(buildListKeysetCondition(keys, values));
|
|
4042
|
+
}
|
|
4043
|
+
|
|
4044
|
+
const rows = await db
|
|
4045
|
+
.select({ post: posts, sortKey: listSortKeyFields(keys) })
|
|
4046
|
+
.from(posts)
|
|
4047
|
+
.where(and(...conditions))
|
|
4048
|
+
.orderBy(...listOrderBy(keys))
|
|
4049
|
+
.limit(limit + 1);
|
|
4050
|
+
|
|
4051
|
+
const pageRows = rows.slice(0, limit);
|
|
4052
|
+
const lastRow = pageRows[pageRows.length - 1];
|
|
4053
|
+
return {
|
|
4054
|
+
posts: await hydratePosts(pageRows.map((row) => row.post)),
|
|
4055
|
+
nextCursor:
|
|
4056
|
+
rows.length > limit && lastRow
|
|
4057
|
+
? encodePostListCursor(
|
|
4058
|
+
THREAD_POSTS_SORT_MODE,
|
|
4059
|
+
readListSortKeyValues(keys, lastRow.sortKey),
|
|
4060
|
+
)
|
|
4061
|
+
: null,
|
|
4062
|
+
};
|
|
4063
|
+
},
|
|
4064
|
+
|
|
4065
|
+
async countThreadPosts(threadIds) {
|
|
4066
|
+
const counts = new Map<string, number>();
|
|
4067
|
+
if (threadIds.length === 0) return counts;
|
|
4068
|
+
|
|
4069
|
+
const rows = await batchQueryRows([...new Set(threadIds)], (chunk) =>
|
|
4070
|
+
db
|
|
4071
|
+
.select({
|
|
4072
|
+
threadId: posts.threadId,
|
|
4073
|
+
count: sql<number>`CAST(count(*) AS INTEGER)`.as("count"),
|
|
4074
|
+
})
|
|
4075
|
+
.from(posts)
|
|
4076
|
+
.where(
|
|
4077
|
+
and(
|
|
4078
|
+
eq(posts.siteId, siteId),
|
|
4079
|
+
inArray(posts.threadId, chunk),
|
|
4080
|
+
eq(posts.status, "published"),
|
|
4081
|
+
),
|
|
4082
|
+
)
|
|
4083
|
+
.groupBy(posts.threadId),
|
|
4084
|
+
);
|
|
4085
|
+
|
|
4086
|
+
for (const row of rows) {
|
|
4087
|
+
counts.set(row.threadId, row.count);
|
|
4088
|
+
}
|
|
4089
|
+
return counts;
|
|
4090
|
+
},
|
|
4091
|
+
|
|
3921
4092
|
async getThreadPosition(postId) {
|
|
3922
4093
|
const targetRows = await db
|
|
3923
4094
|
.select({ replyToId: posts.replyToId, threadId: posts.threadId })
|
|
@@ -4494,51 +4665,59 @@ export function createPostService(
|
|
|
4494
4665
|
async getThreadTimelineContext(rootIds) {
|
|
4495
4666
|
if (rootIds.length === 0) return new Map();
|
|
4496
4667
|
|
|
4497
|
-
|
|
4498
|
-
|
|
4499
|
-
|
|
4500
|
-
|
|
4501
|
-
|
|
4668
|
+
// A page of Threads can outgrow one statement's parameter budget — the
|
|
4669
|
+
// API lists up to 100. Each Thread lands in one chunk, so every window
|
|
4670
|
+
// below still partitions over a whole Thread.
|
|
4671
|
+
const contextRows = await batchQueryRows(
|
|
4672
|
+
[...new Set(rootIds)],
|
|
4673
|
+
(chunk) => {
|
|
4674
|
+
const rankedReplies = db
|
|
4675
|
+
.select({
|
|
4676
|
+
id: posts.id,
|
|
4677
|
+
threadId: posts.threadId,
|
|
4678
|
+
firstReplyRank: sql<number>`CAST(ROW_NUMBER() OVER (
|
|
4502
4679
|
PARTITION BY ${posts.threadId}
|
|
4503
4680
|
ORDER BY ${sql.join(threadOrder(), sql`, `)}
|
|
4504
4681
|
) AS INTEGER)`.as("first_reply_rank"),
|
|
4505
|
-
|
|
4682
|
+
latestReplyRank: sql<number>`CAST(ROW_NUMBER() OVER (
|
|
4506
4683
|
PARTITION BY ${posts.threadId}
|
|
4507
4684
|
ORDER BY ${sql.join(threadOrder("desc"), sql`, `)}
|
|
4508
4685
|
) AS INTEGER)`.as("latest_reply_rank"),
|
|
4509
|
-
|
|
4686
|
+
totalReplyCount: sql<number>`CAST(COUNT(*) OVER (
|
|
4510
4687
|
PARTITION BY ${posts.threadId}
|
|
4511
4688
|
) AS INTEGER)`.as("total_reply_count"),
|
|
4512
|
-
|
|
4513
|
-
|
|
4514
|
-
|
|
4515
|
-
|
|
4516
|
-
|
|
4517
|
-
|
|
4518
|
-
|
|
4519
|
-
|
|
4520
|
-
|
|
4521
|
-
|
|
4522
|
-
|
|
4523
|
-
|
|
4524
|
-
|
|
4525
|
-
|
|
4526
|
-
|
|
4527
|
-
|
|
4528
|
-
|
|
4529
|
-
|
|
4530
|
-
|
|
4531
|
-
|
|
4532
|
-
|
|
4533
|
-
|
|
4534
|
-
|
|
4535
|
-
|
|
4536
|
-
|
|
4537
|
-
|
|
4538
|
-
|
|
4539
|
-
|
|
4540
|
-
|
|
4541
|
-
|
|
4689
|
+
})
|
|
4690
|
+
.from(posts)
|
|
4691
|
+
.where(
|
|
4692
|
+
and(
|
|
4693
|
+
eq(posts.siteId, siteId),
|
|
4694
|
+
inArray(posts.threadId, chunk),
|
|
4695
|
+
eq(posts.status, "published"),
|
|
4696
|
+
isNotNull(posts.replyToId),
|
|
4697
|
+
),
|
|
4698
|
+
)
|
|
4699
|
+
.as("ranked_replies");
|
|
4700
|
+
|
|
4701
|
+
return db
|
|
4702
|
+
.select({
|
|
4703
|
+
threadId: rankedReplies.threadId,
|
|
4704
|
+
id: rankedReplies.id,
|
|
4705
|
+
firstReplyRank: rankedReplies.firstReplyRank,
|
|
4706
|
+
latestReplyRank: rankedReplies.latestReplyRank,
|
|
4707
|
+
totalReplyCount: rankedReplies.totalReplyCount,
|
|
4708
|
+
})
|
|
4709
|
+
.from(rankedReplies)
|
|
4710
|
+
.where(
|
|
4711
|
+
or(
|
|
4712
|
+
// One past the leading window: that extra row is never rendered,
|
|
4713
|
+
// it is only where the gap link points. Without it the gap has to
|
|
4714
|
+
// aim at a reply already on screen.
|
|
4715
|
+
lte(rankedReplies.firstReplyRank, THREAD_LEADING_REPLIES + 1),
|
|
4716
|
+
lte(rankedReplies.latestReplyRank, THREAD_TRAILING_REPLIES),
|
|
4717
|
+
),
|
|
4718
|
+
);
|
|
4719
|
+
},
|
|
4720
|
+
);
|
|
4542
4721
|
|
|
4543
4722
|
const hydratedPosts = await hydratePostsById(
|
|
4544
4723
|
contextRows.map((row) => row.id),
|
package/src/services/search.ts
CHANGED
|
@@ -25,6 +25,13 @@ export interface SearchOptions {
|
|
|
25
25
|
offset?: number;
|
|
26
26
|
/** Filter by status */
|
|
27
27
|
status?: Status[];
|
|
28
|
+
/**
|
|
29
|
+
* Whether private posts, and replies in a private Thread, may match.
|
|
30
|
+
*
|
|
31
|
+
* Defaults to `false`, so a caller that doesn't say who is asking gets what a
|
|
32
|
+
* signed-out reader would. Pass `true` only for the signed-in author.
|
|
33
|
+
*/
|
|
34
|
+
includePrivate?: boolean;
|
|
28
35
|
/** Filter by format */
|
|
29
36
|
format?: Format;
|
|
30
37
|
/**
|
|
@@ -167,6 +174,62 @@ function buildPgPrefixTsQuery(query: string): string | null {
|
|
|
167
174
|
return terms.map((term) => `${term}:*`).join(" & ");
|
|
168
175
|
}
|
|
169
176
|
|
|
177
|
+
/**
|
|
178
|
+
* A post's visibility after Thread-root inheritance, in the raw-SQL form every
|
|
179
|
+
* search statement shares. Each one joins the root as `root_post`. This is the
|
|
180
|
+
* same rule as `buildEffectiveVisibilityExpr` in `db/post-visibility.ts`.
|
|
181
|
+
*/
|
|
182
|
+
const EFFECTIVE_VISIBILITY_SQL =
|
|
183
|
+
"COALESCE(post.visibility, root_post.visibility)";
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* The conditions every search statement applies after its own match clause:
|
|
187
|
+
* site, status, audience, format, and language.
|
|
188
|
+
*
|
|
189
|
+
* Built once so that no statement can leave one out. Each dialect and each
|
|
190
|
+
* match strategy has its own SQL, and a statement missing the visibility
|
|
191
|
+
* clause does not fail — it returns private posts.
|
|
192
|
+
*
|
|
193
|
+
* @param siteId - Site the search is scoped to
|
|
194
|
+
* @param options - Caller's search options
|
|
195
|
+
* @returns `AND …` lines to follow the match clause, and their bound values in order
|
|
196
|
+
*
|
|
197
|
+
* @example
|
|
198
|
+
* ```ts
|
|
199
|
+
* const filters = buildSearchFilters(siteId, { status: ["published"] });
|
|
200
|
+
* // filters.sql: "AND post.site_id = ?\nAND post.status IN (?)\nAND COALESCE(…) != 'private'"
|
|
201
|
+
* // filters.params: [siteId, "published"]
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
204
|
+
function buildSearchFilters(
|
|
205
|
+
siteId: string,
|
|
206
|
+
options: SearchOptions,
|
|
207
|
+
): { sql: string; params: unknown[] } {
|
|
208
|
+
const status = options.status ?? ["published"];
|
|
209
|
+
const conditions = [
|
|
210
|
+
"post.site_id = ?",
|
|
211
|
+
`post.status IN (${status.map(() => "?").join(", ")})`,
|
|
212
|
+
];
|
|
213
|
+
const params: unknown[] = [siteId, ...status];
|
|
214
|
+
|
|
215
|
+
if (!options.includePrivate) {
|
|
216
|
+
conditions.push(`${EFFECTIVE_VISIBILITY_SQL} != 'private'`);
|
|
217
|
+
}
|
|
218
|
+
if (options.format) {
|
|
219
|
+
conditions.push("post.format = ?");
|
|
220
|
+
params.push(options.format);
|
|
221
|
+
}
|
|
222
|
+
if (options.lang) {
|
|
223
|
+
conditions.push("post.language = ?");
|
|
224
|
+
params.push(options.lang);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return {
|
|
228
|
+
sql: conditions.map((condition) => `AND ${condition}`).join("\n"),
|
|
229
|
+
params,
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
|
|
170
233
|
const PG_TS_HEADLINE_OPTIONS = [
|
|
171
234
|
"MaxWords=18",
|
|
172
235
|
"MinWords=6",
|
|
@@ -188,12 +251,7 @@ export function createSearchService(
|
|
|
188
251
|
): Promise<SearchResult[]> {
|
|
189
252
|
const limit = options.limit ?? 20;
|
|
190
253
|
const offset = options.offset ?? 0;
|
|
191
|
-
const
|
|
192
|
-
const statusPlaceholders = status.map(() => "?").join(", ");
|
|
193
|
-
const formatFilter = options.format ? "AND post.format = ?" : "";
|
|
194
|
-
const formatParams = options.format ? [options.format] : [];
|
|
195
|
-
const langFilter = options.lang ? "AND post.language = ?" : "";
|
|
196
|
-
const langParams = options.lang ? [options.lang] : [];
|
|
254
|
+
const filters = buildSearchFilters(siteId, options);
|
|
197
255
|
|
|
198
256
|
if (databaseDialect === "sqlite") {
|
|
199
257
|
const ftsQuery = buildSqliteFtsQuery(query);
|
|
@@ -202,7 +260,7 @@ export function createSearchService(
|
|
|
202
260
|
const stmt = rawQuery.prepare(`
|
|
203
261
|
SELECT
|
|
204
262
|
post.*,
|
|
205
|
-
|
|
263
|
+
${EFFECTIVE_VISIBILITY_SQL} AS effective_visibility,
|
|
206
264
|
path_registry.path AS slug,
|
|
207
265
|
post_fts.rank AS rank,
|
|
208
266
|
snippet(post_fts, 1, char(2), char(3), '...', 32) AS snippet
|
|
@@ -214,24 +272,13 @@ export function createSearchService(
|
|
|
214
272
|
AND path_registry.site_id = post.site_id
|
|
215
273
|
AND path_registry.kind = 'slug'
|
|
216
274
|
WHERE post_fts MATCH ?
|
|
217
|
-
|
|
218
|
-
AND post.status IN (${statusPlaceholders})
|
|
219
|
-
${formatFilter}
|
|
220
|
-
${langFilter}
|
|
275
|
+
${filters.sql}
|
|
221
276
|
ORDER BY post_fts.rank
|
|
222
277
|
LIMIT ? OFFSET ?
|
|
223
278
|
`);
|
|
224
279
|
|
|
225
280
|
const { results } = await stmt
|
|
226
|
-
.bind(
|
|
227
|
-
ftsQuery,
|
|
228
|
-
siteId,
|
|
229
|
-
...status,
|
|
230
|
-
...formatParams,
|
|
231
|
-
...langParams,
|
|
232
|
-
limit,
|
|
233
|
-
offset,
|
|
234
|
-
)
|
|
281
|
+
.bind(ftsQuery, ...filters.params, limit, offset)
|
|
235
282
|
.all<RawSearchRow>();
|
|
236
283
|
|
|
237
284
|
return withSnippetFallback((results || []).map(mapRow), query);
|
|
@@ -250,7 +297,7 @@ export function createSearchService(
|
|
|
250
297
|
)
|
|
251
298
|
SELECT
|
|
252
299
|
post.*,
|
|
253
|
-
|
|
300
|
+
${EFFECTIVE_VISIBILITY_SQL} AS effective_visibility,
|
|
254
301
|
path_registry.path AS slug,
|
|
255
302
|
ts_rank_cd(post.search_document, search_query.tsq, 32) AS rank,
|
|
256
303
|
NULLIF(
|
|
@@ -297,24 +344,13 @@ export function createSearchService(
|
|
|
297
344
|
AND path_registry.site_id = post.site_id
|
|
298
345
|
AND path_registry.kind = 'slug'
|
|
299
346
|
WHERE post.search_document @@ search_query.tsq
|
|
300
|
-
|
|
301
|
-
AND post.status IN (${statusPlaceholders})
|
|
302
|
-
${formatFilter}
|
|
303
|
-
${langFilter}
|
|
347
|
+
${filters.sql}
|
|
304
348
|
ORDER BY rank DESC, post.published_at DESC NULLS LAST, post.id DESC
|
|
305
349
|
LIMIT ? OFFSET ?
|
|
306
350
|
`);
|
|
307
351
|
|
|
308
352
|
const { results } = await stmt
|
|
309
|
-
.bind(
|
|
310
|
-
tsQuery,
|
|
311
|
-
siteId,
|
|
312
|
-
...status,
|
|
313
|
-
...formatParams,
|
|
314
|
-
...langParams,
|
|
315
|
-
limit,
|
|
316
|
-
offset,
|
|
317
|
-
)
|
|
353
|
+
.bind(tsQuery, ...filters.params, limit, offset)
|
|
318
354
|
.all<RawSearchRow>();
|
|
319
355
|
|
|
320
356
|
return withSnippetFallback((results || []).map(mapRow), query);
|
|
@@ -326,19 +362,14 @@ export function createSearchService(
|
|
|
326
362
|
): Promise<SearchResult[]> {
|
|
327
363
|
const limit = options.limit ?? 20;
|
|
328
364
|
const offset = options.offset ?? 0;
|
|
329
|
-
const status = options.status ?? ["published"];
|
|
330
365
|
const like = `%${query}%`;
|
|
331
|
-
const
|
|
332
|
-
const formatFilter = options.format ? "AND post.format = ?" : "";
|
|
333
|
-
const formatParams = options.format ? [options.format] : [];
|
|
334
|
-
const langFilter = options.lang ? "AND post.language = ?" : "";
|
|
335
|
-
const langParams = options.lang ? [options.lang] : [];
|
|
366
|
+
const filters = buildSearchFilters(siteId, options);
|
|
336
367
|
|
|
337
368
|
if (databaseDialect === "pg") {
|
|
338
369
|
const stmt = rawQuery.prepare(`
|
|
339
370
|
SELECT
|
|
340
371
|
post.*,
|
|
341
|
-
|
|
372
|
+
${EFFECTIVE_VISIBILITY_SQL} AS effective_visibility,
|
|
342
373
|
path_registry.path AS slug,
|
|
343
374
|
GREATEST(
|
|
344
375
|
similarity(coalesce(post.title, ''), ?),
|
|
@@ -354,10 +385,7 @@ export function createSearchService(
|
|
|
354
385
|
AND path_registry.site_id = post.site_id
|
|
355
386
|
AND path_registry.kind = 'slug'
|
|
356
387
|
WHERE post.search_text ILIKE ?
|
|
357
|
-
|
|
358
|
-
AND post.status IN (${statusPlaceholders})
|
|
359
|
-
${formatFilter}
|
|
360
|
-
${langFilter}
|
|
388
|
+
${filters.sql}
|
|
361
389
|
ORDER BY rank DESC, post.published_at DESC NULLS LAST, post.id DESC
|
|
362
390
|
LIMIT ? OFFSET ?
|
|
363
391
|
`);
|
|
@@ -369,10 +397,7 @@ export function createSearchService(
|
|
|
369
397
|
query,
|
|
370
398
|
query,
|
|
371
399
|
like,
|
|
372
|
-
|
|
373
|
-
...status,
|
|
374
|
-
...formatParams,
|
|
375
|
-
...langParams,
|
|
400
|
+
...filters.params,
|
|
376
401
|
limit,
|
|
377
402
|
offset,
|
|
378
403
|
)
|
|
@@ -387,7 +412,7 @@ export function createSearchService(
|
|
|
387
412
|
const stmt = rawQuery.prepare(`
|
|
388
413
|
SELECT
|
|
389
414
|
post.*,
|
|
390
|
-
|
|
415
|
+
${EFFECTIVE_VISIBILITY_SQL} AS effective_visibility,
|
|
391
416
|
path_registry.path AS slug,
|
|
392
417
|
0 AS rank,
|
|
393
418
|
NULL AS snippet
|
|
@@ -403,27 +428,13 @@ export function createSearchService(
|
|
|
403
428
|
post.quote_text ${likeOperator} ? OR
|
|
404
429
|
post.url ${likeOperator} ?
|
|
405
430
|
)
|
|
406
|
-
|
|
407
|
-
AND post.status IN (${statusPlaceholders})
|
|
408
|
-
${formatFilter}
|
|
409
|
-
${langFilter}
|
|
431
|
+
${filters.sql}
|
|
410
432
|
${likeOrderBy}
|
|
411
433
|
LIMIT ? OFFSET ?
|
|
412
434
|
`);
|
|
413
435
|
|
|
414
436
|
const { results } = await stmt
|
|
415
|
-
.bind(
|
|
416
|
-
like,
|
|
417
|
-
like,
|
|
418
|
-
like,
|
|
419
|
-
like,
|
|
420
|
-
siteId,
|
|
421
|
-
...status,
|
|
422
|
-
...formatParams,
|
|
423
|
-
...langParams,
|
|
424
|
-
limit,
|
|
425
|
-
offset,
|
|
426
|
-
)
|
|
437
|
+
.bind(like, like, like, like, ...filters.params, limit, offset)
|
|
427
438
|
.all<RawSearchRow>();
|
|
428
439
|
|
|
429
440
|
return withSnippetFallback((results || []).map(mapRow), query);
|