@openpeepshq/core 0.27.0 → 0.28.0

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.
@@ -12,6 +12,10 @@ export declare const postFilters: {
12
12
  afterActivity: (cursor: FeedCursor) => SqlFilter;
13
13
  hasJam: () => SqlFilter;
14
14
  creatorId: (id: string) => SqlFilter;
15
+ /** Published posts only — the default gate that keeps drafts out of feeds. */
16
+ published: () => SqlFilter;
17
+ /** Unpublished drafts — used by the drafts finder to opt back in. */
18
+ drafts: () => SqlFilter;
15
19
  notCreatorIn: (ids: string[]) => SqlFilter | undefined;
16
20
  myFeed: (profileId: string, followedProfileIds: string[], groupIds: string[]) => SqlFilter;
17
21
  type: (type: string) => SqlFilter;
@@ -1,4 +1,4 @@
1
- import { and, eq, inArray, isNotNull, ne, notInArray, or, sql, } from 'drizzle-orm';
1
+ import { and, eq, inArray, isNotNull, isNull, ne, notInArray, or, sql, } from 'drizzle-orm';
2
2
  import { posts } from '../schema/documents.js';
3
3
  import { postGroups, postHashtags, replyTo } from '../schema/edges.js';
4
4
  import { postHasYesOrMaybeRsvpExpr, postReplyCountExpr, } from '../queries/activity.js';
@@ -32,6 +32,10 @@ export const postFilters = {
32
32
  },
33
33
  hasJam: () => pgSql(isNotNull(sql `${posts.body}->'jam'`)),
34
34
  creatorId: (id) => pgSql(eq(posts.creatorId, id)),
35
+ /** Published posts only — the default gate that keeps drafts out of feeds. */
36
+ published: () => pgSql(isNotNull(posts.publishedAt)),
37
+ /** Unpublished drafts — used by the drafts finder to opt back in. */
38
+ drafts: () => pgSql(isNull(posts.publishedAt)),
35
39
  notCreatorIn: (ids) => ids.length ? pgSql(notInArray(posts.creatorId, ids)) : undefined,
36
40
  myFeed: (profileId, followedProfileIds, groupIds) => {
37
41
  const followed = followedProfileIds.length
@@ -22,6 +22,7 @@ const SCALAR_COLUMNS = {
22
22
  'lastActivityAt',
23
23
  'uri',
24
24
  'inReplyToUri',
25
+ 'publishedAt',
25
26
  ],
26
27
  groups: ['handle'],
27
28
  hashtags: ['name'],
@@ -24,7 +24,7 @@ const withScalars = (...scalarKeys) => (data) => {
24
24
  return { scalars, body };
25
25
  };
26
26
  const postsSplit = (data) => {
27
- const { type, visibility, creatorId, lastActivityAt, data: postBody, uri, inReplyToUri, ...rest } = data;
27
+ const { type, visibility, creatorId, lastActivityAt, publishedAt, data: postBody, uri, inReplyToUri, ...rest } = data;
28
28
  const scalars = {};
29
29
  if (type !== undefined)
30
30
  scalars.type = type;
@@ -34,6 +34,8 @@ const postsSplit = (data) => {
34
34
  scalars.creatorId = creatorId;
35
35
  if (lastActivityAt !== undefined)
36
36
  scalars.lastActivityAt = lastActivityAt;
37
+ if (publishedAt !== undefined)
38
+ scalars.publishedAt = publishedAt;
37
39
  if (uri !== undefined)
38
40
  scalars.uri = uri;
39
41
  if (inReplyToUri !== undefined)
@@ -44,6 +46,7 @@ const postsSplit = (data) => {
44
46
  delete body.visibility;
45
47
  delete body.creatorId;
46
48
  delete body.lastActivityAt;
49
+ delete body.publishedAt;
47
50
  delete body.uri;
48
51
  delete body.inReplyToUri;
49
52
  }
@@ -439,6 +442,9 @@ export const rowToDocument = (collection, row, keepMetadata = false) => {
439
442
  ...(typeof row.inReplyToUri === 'string'
440
443
  ? { inReplyToUri: row.inReplyToUri }
441
444
  : {}),
445
+ ...(typeof row.publishedAt === 'string'
446
+ ? { publishedAt: normalizeIsoDatetime(row.publishedAt) }
447
+ : {}),
442
448
  data: row.body ?? {},
443
449
  }, timestamps),
444
450
  lastActivityAt,
@@ -1,4 +1,4 @@
1
- import { and, count, eq, exists, gte, isNull, lt, sql, } from 'drizzle-orm';
1
+ import { and, count, eq, exists, gte, isNull, lt, max, sql, } from 'drizzle-orm';
2
2
  import { alias, QueryBuilder } from 'drizzle-orm/pg-core';
3
3
  import { posts } from '../schema/documents.js';
4
4
  import { entries, follows, postGroups, repost, replyTo } from '../schema/edges.js';
@@ -43,5 +43,25 @@ export const postHasYesOrMaybeRsvpExpr = (outerTable, profileId) => exists(query
43
43
  .select({ id: entries.id })
44
44
  .from(entries)
45
45
  .where(and(eq(entries.toId, outerIdText(outerTable)), eq(entries.fromId, profileId), sql `${entries.body}->>'type' = 'rsvp'`, sql `${entries.body}->'data'->>'response' IN ('yes', 'tentative')`)));
46
- export const groupLastPostAtExpr = (outerTable) => sql `(SELECT max(${postGroups.createdAt}) FROM ${postGroups} WHERE ${postGroups.toId} = ${outerIdText(outerTable)})`;
47
- export const groupPostsCountExpr = (outerTable) => sql `(SELECT count(*)::int FROM ${postGroups} pg INNER JOIN ${posts} p ON p.id::text = pg.from_id WHERE pg.to_id = ${outerIdText(outerTable)} AND p.deleted_at IS NULL)`;
46
+ // Draft rows (published_at IS NULL) and deleted posts must not count as
47
+ // group activity: they are invisible to everyone but the author. The
48
+ // subqueries are built with the query builder (as in postReplyCountExpr) so
49
+ // every column renders fully qualified: a raw sql`max(${postGroups.createdAt})`
50
+ // template drops the table qualification inside a select-object embedding,
51
+ // which is ambiguous once the subquery joins posts (42702).
52
+ export const groupLastPostAtExpr = (outerTable) => {
53
+ const query = queryBuilder
54
+ .select({ at: max(postGroups.createdAt) })
55
+ .from(postGroups)
56
+ .innerJoin(posts, eq(sql `${posts.id}::text`, postGroups.fromId))
57
+ .where(and(eq(postGroups.toId, outerIdText(outerTable)), sql `${posts.publishedAt} IS NOT NULL`, isNull(posts.deletedAt)));
58
+ return sql `(${query})`;
59
+ };
60
+ export const groupPostsCountExpr = (outerTable) => {
61
+ const query = queryBuilder
62
+ .select({ total: count() })
63
+ .from(postGroups)
64
+ .innerJoin(posts, eq(sql `${posts.id}::text`, postGroups.fromId))
65
+ .where(and(eq(postGroups.toId, outerIdText(outerTable)), sql `${posts.publishedAt} IS NOT NULL`, isNull(posts.deletedAt)));
66
+ return sql `(${query})`.mapWith(Number);
67
+ };
@@ -1,4 +1,4 @@
1
- import { and, eq, inArray, sql } from 'drizzle-orm';
1
+ import { and, eq, inArray, isNotNull, sql } from 'drizzle-orm';
2
2
  import { posts } from '../schema/documents.js';
3
3
  import { entries, postSeen } from '../schema/edges.js';
4
4
  import { normalizeIsoDatetime } from '../mappers.js';
@@ -16,7 +16,7 @@ export const profileDerived = {
16
16
  const rows = await db
17
17
  .select({ count: sql `count(*)::int` })
18
18
  .from(posts)
19
- .where(and(eq(postsTable.creatorId, doc.id), sql `${postsTable.deletedAt} IS NULL`));
19
+ .where(and(eq(postsTable.creatorId, doc.id), sql `${postsTable.deletedAt} IS NULL`, isNotNull(posts.publishedAt)));
20
20
  return rows[0]?.count ?? 0;
21
21
  },
22
22
  lastSeen: async (db, doc) => {
@@ -3,6 +3,7 @@ export declare const sorts: {
3
3
  idDesc: ObjectSort;
4
4
  idAsc: ObjectSort;
5
5
  createdAtDesc: ObjectSort;
6
+ updatedAtDesc: ObjectSort;
6
7
  eventStartAsc: ObjectSort;
7
8
  eventStartDesc: ObjectSort;
8
9
  activityScoreDesc: ObjectSort;
@@ -2,6 +2,7 @@ export const sorts = {
2
2
  idDesc: [['id', 'DESC']],
3
3
  idAsc: [['id', 'ASC']],
4
4
  createdAtDesc: [['createdAt', 'DESC']],
5
+ updatedAtDesc: [['updatedAt', 'DESC']],
5
6
  eventStartAsc: [['data.start', 'ASC']],
6
7
  eventStartDesc: [['data.start', 'DESC']],
7
8
  activityScoreDesc: [['activityScore', 'DESC']],
@@ -1143,6 +1143,23 @@ export declare const posts: import("drizzle-orm/pg-core").PgTableWithColumns<{
1143
1143
  identity: undefined;
1144
1144
  generated: undefined;
1145
1145
  }, {}, {}>;
1146
+ publishedAt: import("drizzle-orm/pg-core").PgColumn<{
1147
+ name: "published_at";
1148
+ tableName: "posts";
1149
+ dataType: "string";
1150
+ columnType: "PgTimestampString";
1151
+ data: string;
1152
+ driverParam: string;
1153
+ notNull: false;
1154
+ hasDefault: false;
1155
+ isPrimaryKey: false;
1156
+ isAutoincrement: false;
1157
+ hasRuntimeDefault: false;
1158
+ enumValues: undefined;
1159
+ baseColumn: never;
1160
+ identity: undefined;
1161
+ generated: undefined;
1162
+ }, {}, {}>;
1146
1163
  body: import("drizzle-orm/pg-core").PgColumn<{
1147
1164
  name: "body";
1148
1165
  tableName: "posts";
@@ -70,6 +70,11 @@ export const posts = pgTable('posts', {
70
70
  creatorId: text('creator_id').notNull(),
71
71
  uri: text('uri'),
72
72
  inReplyToUri: text('in_reply_to_uri'),
73
+ /** NULL while the post is a draft; set to the publish timestamp. */
74
+ publishedAt: timestamp('published_at', {
75
+ withTimezone: true,
76
+ mode: 'string',
77
+ }),
73
78
  body: jsonb('body').notNull().default({}),
74
79
  searchVector: tsvector('search_vector'),
75
80
  lastActivityAt: timestamp('last_activity_at', {
@@ -100,6 +105,7 @@ export const posts = pgTable('posts', {
100
105
  uniqueIndex('posts_slug_profile_unique')
101
106
  .on(sql `(body ->> 'slug')`, t.creatorId)
102
107
  .where(sql `type = 'article' AND body ->> 'slug' IS NOT NULL AND (body ->> 'instanceScoped' IS NULL OR body ->> 'instanceScoped' = 'false')`),
108
+ index('posts_creator_published_idx').on(t.creatorId, t.publishedAt),
103
109
  ]);
104
110
  export const notifications = pgTable('notifications', {
105
111
  id: idColumn(),
@@ -5176,6 +5176,23 @@ export declare const schema: {
5176
5176
  identity: undefined;
5177
5177
  generated: undefined;
5178
5178
  }, {}, {}>;
5179
+ publishedAt: import("drizzle-orm/pg-core").PgColumn<{
5180
+ name: "published_at";
5181
+ tableName: "posts";
5182
+ dataType: "string";
5183
+ columnType: "PgTimestampString";
5184
+ data: string;
5185
+ driverParam: string;
5186
+ notNull: false;
5187
+ hasDefault: false;
5188
+ isPrimaryKey: false;
5189
+ isAutoincrement: false;
5190
+ hasRuntimeDefault: false;
5191
+ enumValues: undefined;
5192
+ baseColumn: never;
5193
+ identity: undefined;
5194
+ generated: undefined;
5195
+ }, {}, {}>;
5179
5196
  body: import("drizzle-orm/pg-core").PgColumn<{
5180
5197
  name: "body";
5181
5198
  tableName: "posts";
@@ -0,0 +1,6 @@
1
+ ALTER TABLE "posts" ADD COLUMN "published_at" timestamp with time zone;--> statement-breakpoint
2
+ -- One-shot backfill of pre-existing rows only: posts created after this
3
+ -- migration rely on every write path (createPost non-draft, publishPost) to
4
+ -- set published_at, so do not re-run this or turn it into a rolling default.
5
+ UPDATE "posts" SET "published_at" = "created_at" WHERE "published_at" IS NULL;--> statement-breakpoint
6
+ CREATE INDEX "posts_creator_published_idx" ON "posts" USING btree ("creator_id","published_at");