@openpeepshq/core 0.1.32 → 0.1.34
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/accessTokens/helpers.d.ts +2 -2
- package/dist/accessTokens/tokens.js +14 -7
- package/dist/accounts/mutations.d.ts +2 -2
- package/dist/accounts/mutations.js +33 -35
- package/dist/analytics/cache.js +12 -7
- package/dist/analytics/compile.js +16 -10
- package/dist/analytics/pdfReport.d.ts +1 -0
- package/dist/analytics/pdfReport.js +5 -4
- package/dist/analytics/privacy.d.ts +18 -0
- package/dist/analytics/privacy.js +10 -0
- package/dist/analytics/read.js +75 -19
- package/dist/analytics/report.d.ts +21 -1
- package/dist/analytics/report.js +9 -2
- package/dist/backups/index.js +78 -15
- package/dist/backups/metadata.d.ts +8 -2
- package/dist/backups/metadata.js +6 -2
- package/dist/config/defaults/capabilities.js +13 -2
- package/dist/config/defaults/community.js +7 -9
- package/dist/config/defaults/core.d.ts +1 -1
- package/dist/config/defaults/core.js +3 -0
- package/dist/config/defaults/db.d.ts +0 -2
- package/dist/config/defaults/db.js +0 -2
- package/dist/config/index.d.ts +25 -3
- package/dist/config/index.js +7 -1
- package/dist/db/helpers.d.ts +7 -28
- package/dist/db/helpers.js +30 -152
- package/dist/db/index.d.ts +3 -1
- package/dist/db/index.js +5 -0
- package/dist/db/init.js +2 -2
- package/dist/db/migration/importCollections.d.ts +15 -10
- package/dist/db/migration/importCollections.js +121 -155
- package/dist/db/migration/shared.d.ts +2 -37
- package/dist/db/migration/shared.js +2 -59
- package/dist/db/pg/client.js +4 -0
- package/dist/db/pg/collections.d.ts +2 -2
- package/dist/db/pg/collections.js +2 -2
- package/dist/db/pg/filters/edges.d.ts +1 -1
- package/dist/db/pg/filters/edges.js +1 -1
- package/dist/db/pg/filters/posts.d.ts +11 -0
- package/dist/db/pg/filters/posts.js +37 -3
- package/dist/db/pg/filters/profiles.d.ts +1 -0
- package/dist/db/pg/filters/profiles.js +2 -1
- package/dist/db/pg/map/filters.js +37 -2
- package/dist/db/pg/map/mutations.js +13 -8
- package/dist/db/pg/map/registry.d.ts +5 -0
- package/dist/db/pg/map/registry.js +64 -24
- package/dist/db/pg/map/relations.d.ts +1 -1
- package/dist/db/pg/map/relations.js +98 -46
- package/dist/db/pg/migrate.d.ts +22 -0
- package/dist/db/pg/migrate.js +88 -2
- package/dist/db/pg/queries/activity.js +3 -3
- package/dist/db/pg/queries/sorts.d.ts +1 -0
- package/dist/db/pg/queries/sorts.js +4 -0
- package/dist/db/pg/schema/documents.d.ts +422 -41
- package/dist/db/pg/schema/documents.js +43 -9
- package/dist/db/pg/schema/edges.d.ts +117 -7
- package/dist/db/pg/schema/edges.js +4 -2
- package/dist/db/pg/schema/index.d.ts +505 -15
- package/dist/db/pg/sql/0007_shallow_oracle.sql +3 -0
- package/dist/db/pg/sql/0008_group_owner_roles.sql +31 -0
- package/dist/db/pg/sql/0009_event_occurrences.sql +26 -0
- package/dist/db/pg/sql/0010_user_group_membership_roles.sql +16 -0
- package/dist/db/pg/sql/0011_drop_data_migrations.sql +1 -0
- package/dist/db/pg/sql/0012_posts_last_activity_at.sql +5 -0
- package/dist/db/pg/sql/0013_add_blocks_edge.sql +12 -0
- package/dist/db/pg/sql/0014_federation_datamodel.sql +59 -0
- package/dist/db/pg/sql/0015_profile_hashtags.sql +12 -0
- package/dist/db/pg/sql/meta/{0006_snapshot.json → 0015_snapshot.json} +393 -39
- package/dist/db/pg/sql/meta/_journal.json +64 -1
- package/dist/db/replaceOrigin.js +6 -0
- package/dist/db/types.d.ts +0 -4
- package/dist/email/render.js +11 -3
- package/dist/events/index.d.ts +6 -1
- package/dist/events/index.js +55 -33
- package/dist/federation/activities.d.ts +17 -0
- package/dist/federation/activities.js +26 -0
- package/dist/federation/identity.d.ts +16 -0
- package/dist/federation/identity.js +52 -0
- package/dist/federation/index.d.ts +4 -0
- package/dist/federation/index.js +3 -0
- package/dist/federation/profiles.d.ts +3 -0
- package/dist/federation/profiles.js +14 -0
- package/dist/groups/finders.d.ts +3 -2
- package/dist/groups/helpers.d.ts +24 -6
- package/dist/groups/mapping.d.ts +6 -4
- package/dist/groups/mapping.js +2 -2
- package/dist/groups/mutations.js +1 -1
- package/dist/hashtags/finders.d.ts +6 -3
- package/dist/hashtags/finders.js +2 -0
- package/dist/hashtags/helpers.d.ts +8 -0
- package/dist/hashtags/helpers.js +4 -0
- package/dist/hashtags/index.d.ts +2 -0
- package/dist/hashtags/index.js +2 -0
- package/dist/hashtags/mutations.d.ts +3 -0
- package/dist/hashtags/mutations.js +13 -1
- package/dist/i18n/index.d.ts +1 -1
- package/dist/i18n/index.js +3 -1
- package/dist/i18n/pluginLocales.d.ts +6 -0
- package/dist/i18n/pluginLocales.js +15 -0
- package/dist/inviteLinks/helpers.d.ts +4 -4
- package/dist/jams/cache.d.ts +1 -1
- package/dist/jams/cache.js +2 -1
- package/dist/jams/export.d.ts +19 -0
- package/dist/jams/export.js +60 -0
- package/dist/jams/finders.d.ts +2 -1
- package/dist/jams/finders.js +18 -7
- package/dist/jams/health.d.ts +8 -0
- package/dist/jams/health.js +46 -0
- package/dist/jams/helpers.d.ts +6 -2
- package/dist/jams/index.d.ts +6 -3
- package/dist/jams/index.js +41 -27
- package/dist/jams/jobs.js +1 -1
- package/dist/jams/livekit.d.ts +93 -10
- package/dist/jams/livekit.js +151 -52
- package/dist/jams/mapping.d.ts +17 -7
- package/dist/jams/mutations.d.ts +3 -1
- package/dist/jams/recording.d.ts +10 -0
- package/dist/jams/recording.js +32 -0
- package/dist/jams/rtmp.d.ts +5 -0
- package/dist/jams/rtmp.js +8 -0
- package/dist/jams/token.d.ts +15 -3
- package/dist/jams/token.js +30 -16
- package/dist/jams/waitingRoom.d.ts +6 -6
- package/dist/jams/waitingRoom.js +24 -23
- package/dist/jobs/index.d.ts +1 -0
- package/dist/jobs/index.js +6 -1
- package/dist/jobs/jobDetail.js +1 -6
- package/dist/media/index.d.ts +1 -1
- package/dist/media/index.js +23 -8
- package/dist/media/processing.js +18 -7
- package/dist/notifications/defaultNotifications/follow/index.js +2 -2
- package/dist/notifications/defaultNotifications/groupMemberJoined/index.js +3 -1
- package/dist/notifications/defaultNotifications/groupMemberLeft/index.js +3 -1
- package/dist/notifications/defaultNotifications/index.js +4 -0
- package/dist/notifications/defaultNotifications/newGroupPost/index.js +12 -6
- package/dist/notifications/defaultNotifications/pollEnded/index.d.ts +19 -0
- package/dist/notifications/defaultNotifications/pollEnded/index.js +39 -0
- package/dist/notifications/defaultNotifications/pollEnded/recipients.d.ts +3 -0
- package/dist/notifications/defaultNotifications/pollEnded/recipients.js +18 -0
- package/dist/notifications/defaultNotifications/rsvp/index.d.ts +6 -1
- package/dist/notifications/defaultNotifications/rsvp/index.js +7 -0
- package/dist/notifications/defaultNotifications/rsvpCanceled/index.d.ts +24 -0
- package/dist/notifications/defaultNotifications/rsvpCanceled/index.js +75 -0
- package/dist/notifications/defaultNotifications/rsvpCanceled/recipients.d.ts +4 -0
- package/dist/notifications/defaultNotifications/rsvpCanceled/recipients.js +11 -0
- package/dist/notifications/helpers.js +6 -2
- package/dist/notifications/jobs.js +3 -1
- package/dist/notifications/mutations.js +4 -0
- package/dist/notifications/push.js +11 -4
- package/dist/plugins/helpers.d.ts +1 -0
- package/dist/plugins/helpers.js +1 -8
- package/dist/plugins/index.d.ts +5 -0
- package/dist/plugins/index.js +78 -5
- package/dist/plugins/install.d.ts +39 -0
- package/dist/plugins/install.js +455 -0
- package/dist/plugins/pluginAuth.d.ts +1 -1
- package/dist/plugins/pluginAuth.js +10 -6
- package/dist/plugins/state.d.ts +4 -0
- package/dist/plugins/state.js +29 -0
- package/dist/posts/activity.d.ts +9 -0
- package/dist/posts/activity.js +35 -0
- package/dist/posts/contextClosure.d.ts +1 -9
- package/dist/posts/contextClosure.js +15 -44
- package/dist/posts/conversationQueries.d.ts +7 -0
- package/dist/posts/conversationQueries.js +76 -2
- package/dist/posts/eventAgenda.d.ts +166 -0
- package/dist/posts/eventAgenda.js +126 -0
- package/dist/posts/eventOccurrenceJobs.d.ts +4 -0
- package/dist/posts/eventOccurrenceJobs.js +28 -0
- package/dist/posts/eventOccurrences.d.ts +10 -0
- package/dist/posts/eventOccurrences.js +61 -0
- package/dist/posts/finders.d.ts +47 -16
- package/dist/posts/finders.js +93 -62
- package/dist/posts/helpers/filters.js +14 -9
- package/dist/posts/helpers/index.d.ts +129 -30
- package/dist/posts/helpers/index.js +66 -33
- package/dist/posts/index.d.ts +7 -1
- package/dist/posts/index.js +7 -1
- package/dist/posts/mapping.d.ts +491 -13
- package/dist/posts/mapping.js +45 -10
- package/dist/posts/mutations.d.ts +441 -4
- package/dist/posts/mutations.js +177 -43
- package/dist/posts/pollEnd.d.ts +3 -0
- package/dist/posts/pollEnd.js +7 -0
- package/dist/posts/pollEndJobs.d.ts +8 -0
- package/dist/posts/pollEndJobs.js +49 -0
- package/dist/posts/replyClosure.d.ts +10 -0
- package/dist/posts/replyClosure.js +43 -0
- package/dist/posts/rsvpConfirmationEmail.js +14 -15
- package/dist/posts/threadPreview.d.ts +8 -0
- package/dist/posts/threadPreview.js +44 -0
- package/dist/posts/unseenCounts.d.ts +2 -0
- package/dist/posts/unseenCounts.js +28 -8
- package/dist/profileSettings/cache.js +22 -5
- package/dist/profileSettings/mapping.d.ts +2 -0
- package/dist/profiles/activityQueries.d.ts +5 -0
- package/dist/profiles/activityQueries.js +85 -0
- package/dist/profiles/activitySummary.d.ts +3 -0
- package/dist/profiles/activitySummary.js +87 -0
- package/dist/profiles/blocks.d.ts +12 -0
- package/dist/profiles/blocks.js +73 -0
- package/dist/profiles/cache.d.ts +4 -1
- package/dist/profiles/cache.js +28 -7
- package/dist/profiles/finders.d.ts +2 -1
- package/dist/profiles/finders.js +8 -3
- package/dist/profiles/helpers.d.ts +7 -5
- package/dist/profiles/index.d.ts +3 -1
- package/dist/profiles/index.js +3 -1
- package/dist/profiles/mapping.d.ts +10 -5
- package/dist/profiles/mapping.js +13 -1
- package/dist/profiles/mutations.d.ts +8 -4
- package/dist/profiles/mutations.js +42 -15
- package/dist/pushSubscriptions/helpers.d.ts +2 -2
- package/dist/pushSubscriptions/mutations.js +13 -1
- package/dist/redis/connection.d.ts +2 -0
- package/dist/redis/connection.js +25 -6
- package/dist/reports/helpers.d.ts +6 -6
- package/dist/roles/index.d.ts +1 -1
- package/dist/roles/index.js +1 -1
- package/dist/roles/mapping.d.ts +1 -0
- package/dist/roles/mutations.js +86 -3
- package/dist/search/mapping.d.ts +1 -1
- package/dist/search/mapping.js +6 -2
- package/dist/server/index.d.ts +19 -1
- package/dist/server/index.js +37 -18
- package/dist/server/stablePublicServerInfo.d.ts +17 -0
- package/dist/server/stablePublicServerInfo.js +37 -0
- package/dist/server/status.d.ts +25 -0
- package/dist/server/status.js +98 -0
- package/dist/stats/finders.d.ts +1 -16131
- package/dist/stats/finders.js +9 -5
- package/dist/stats/mapping.d.ts +21 -1
- package/dist/stats/mapping.js +0 -3
- package/package.json +6 -12
- package/dist/db/arango.d.ts +0 -2
- package/dist/db/arango.js +0 -110
- package/dist/db/migration/autoMigrate.d.ts +0 -2
- package/dist/db/migration/autoMigrate.js +0 -111
- package/dist/db/migration/cli.d.ts +0 -1
- package/dist/db/migration/cli.js +0 -56
- package/dist/db/migration/detect.d.ts +0 -6
- package/dist/db/migration/detect.js +0 -87
- package/dist/db/migration/exportArango.d.ts +0 -4
- package/dist/db/migration/exportArango.js +0 -80
- package/dist/db/migration/importPostgres.d.ts +0 -3
- package/dist/db/migration/importPostgres.js +0 -18
- package/dist/db/migration/transform.d.ts +0 -18
- package/dist/db/migration/transform.js +0 -209
- package/dist/db/migration/validate.d.ts +0 -18
- package/dist/db/migration/validate.js +0 -69
- package/dist/db/pg/sql/meta/0000_snapshot.json +0 -3380
- package/dist/db/pg/sql/meta/0004_snapshot.json +0 -4114
- package/dist/db/pg/sql/meta/0005_snapshot.json +0 -4179
- package/dist/mastodon/index.d.ts +0 -4
- package/dist/mastodon/index.js +0 -51
- package/dist/mastodon/types.d.ts +0 -7579
- package/dist/mastodon/types.js +0 -523
- package/dist/mastodon/typescript.d.ts +0 -1578
- package/dist/mastodon/typescript.js +0 -1
- package/dist/roles/defaults.d.ts +0 -2
- package/dist/roles/defaults.js +0 -94
|
@@ -1,53 +1,14 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { blockedPairIds, toHiddenPost } from '@openpeepshq/common/lib';
|
|
2
2
|
import { allpeepDb } from '../db/index.js';
|
|
3
3
|
import { fetchRowsByIds, hydrateMapData } from '../db/pg/map/relations.js';
|
|
4
4
|
import { applySort } from '../db/pg/map/filters.js';
|
|
5
5
|
import { capabilitiesConfig } from '../config/index.js';
|
|
6
6
|
import { canReadPost, transformPost } from './helpers/index.js';
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
export const collectReplyClosureIds = async (db, startId, direction, { maxDepth = 9999, limit = CONTEXT_NODE_CAP,
|
|
10
|
-
/** Newest-first by uuidv7 id (useful for long DM threads). */
|
|
11
|
-
newestFirst = false, } = {}) => {
|
|
12
|
-
if (!startId || limit <= 0)
|
|
13
|
-
return [];
|
|
14
|
-
const orderClause = newestFirst ? sql `ORDER BY id DESC` : sql ``;
|
|
15
|
-
const query = direction === 'ancestors'
|
|
16
|
-
? sql `
|
|
17
|
-
WITH RECURSIVE t AS (
|
|
18
|
-
SELECT to_id AS id, 1 AS depth
|
|
19
|
-
FROM reply_to
|
|
20
|
-
WHERE from_id = ${startId}
|
|
21
|
-
UNION ALL
|
|
22
|
-
SELECT r.to_id, t.depth + 1
|
|
23
|
-
FROM reply_to r
|
|
24
|
-
INNER JOIN t ON r.from_id = t.id
|
|
25
|
-
WHERE t.depth < ${maxDepth}
|
|
26
|
-
)
|
|
27
|
-
SELECT id FROM t
|
|
28
|
-
${orderClause}
|
|
29
|
-
LIMIT ${limit}
|
|
30
|
-
`
|
|
31
|
-
: sql `
|
|
32
|
-
WITH RECURSIVE t AS (
|
|
33
|
-
SELECT from_id AS id, 1 AS depth
|
|
34
|
-
FROM reply_to
|
|
35
|
-
WHERE to_id = ${startId}
|
|
36
|
-
UNION ALL
|
|
37
|
-
SELECT r.from_id, t.depth + 1
|
|
38
|
-
FROM reply_to r
|
|
39
|
-
INNER JOIN t ON r.to_id = t.id
|
|
40
|
-
WHERE t.depth < ${maxDepth}
|
|
41
|
-
)
|
|
42
|
-
SELECT id FROM t
|
|
43
|
-
${orderClause}
|
|
44
|
-
LIMIT ${limit}
|
|
45
|
-
`;
|
|
46
|
-
const result = (await db.execute(query));
|
|
47
|
-
return result.rows.map((row) => row.id);
|
|
48
|
-
};
|
|
7
|
+
import { CONTEXT_NODE_CAP, collectReplyClosureIds } from './replyClosure.js';
|
|
8
|
+
export { CONTEXT_NODE_CAP, collectReplyClosureIds } from './replyClosure.js';
|
|
49
9
|
/** Load and hydrate posts in a reply closure, then ACL-filter + transform. */
|
|
50
10
|
export const loadReplyContextPosts = async (authData, startId, direction, mapping, { maxDepth = 9999, limit = CONTEXT_NODE_CAP, } = {}) => {
|
|
11
|
+
var _a, _b;
|
|
51
12
|
const { db } = await allpeepDb();
|
|
52
13
|
const mapData = mapping.data();
|
|
53
14
|
const ids = await collectReplyClosureIds(db, startId, direction, {
|
|
@@ -61,9 +22,19 @@ export const loadReplyContextPosts = async (authData, startId, direction, mappin
|
|
|
61
22
|
const sorted = applySort(hydrated, mapData.sort);
|
|
62
23
|
const config = await capabilitiesConfig();
|
|
63
24
|
const readable = canReadPost(config, authData);
|
|
25
|
+
const pair = new Set(blockedPairIds(authData.profile));
|
|
64
26
|
const posts = [];
|
|
65
27
|
for (const post of sorted) {
|
|
66
|
-
const transformed = await transformPost(post, authData.profile
|
|
28
|
+
const transformed = await transformPost(post, authData.profile, {
|
|
29
|
+
embedThreadPreview: false,
|
|
30
|
+
});
|
|
31
|
+
const creatorId = (_a = transformed.creatorId) !== null && _a !== void 0 ? _a : (_b = transformed.profile) === null || _b === void 0 ? void 0 : _b.id;
|
|
32
|
+
if (creatorId && pair.has(creatorId)) {
|
|
33
|
+
if (direction === 'descendents') {
|
|
34
|
+
posts.push(toHiddenPost(transformed));
|
|
35
|
+
}
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
67
38
|
if (readable(transformed))
|
|
68
39
|
posts.push(transformed);
|
|
69
40
|
}
|
|
@@ -13,6 +13,13 @@ export declare const findLatestThreadPostId: (rootId: string) => Promise<string>
|
|
|
13
13
|
* they are the same). Avoids walking full ancestor chains.
|
|
14
14
|
*/
|
|
15
15
|
export declare const listConversationPreviews: (authData: AuthorizationData) => Promise<PostWithMeta[][]>;
|
|
16
|
+
/**
|
|
17
|
+
* Archived conversations: threads the profile has left — detected by posts
|
|
18
|
+
* they authored but are no longer in the audience of. Returns
|
|
19
|
+
* [root, lastReadablePost] per conversation so the user can still find and
|
|
20
|
+
* view the conversation history after leaving.
|
|
21
|
+
*/
|
|
22
|
+
export declare const listLeftConversations: (authData: AuthorizationData) => Promise<PostWithMeta[][]>;
|
|
16
23
|
/**
|
|
17
24
|
* Conversation detail: root plus newest capped descendants (lean mapping +
|
|
18
25
|
* seenBatch). Prefer newest messages so long DMs stay usable.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { sql } from 'drizzle-orm';
|
|
2
|
-
import { getUniqueBy } from '@openpeepshq/common/lib';
|
|
2
|
+
import { blockedPairIds, getUniqueBy, isOneToOneWithBlocked, toHiddenPost, } from '@openpeepshq/common/lib';
|
|
3
3
|
import { allpeepDb } from '../db/index.js';
|
|
4
4
|
import { fetchRowsByIds, hydrateMapData } from '../db/pg/map/relations.js';
|
|
5
5
|
import { capabilitiesConfig } from '../config/index.js';
|
|
@@ -76,6 +76,7 @@ export const findLatestThreadPostId = async (rootId) => {
|
|
|
76
76
|
return (_b = (_a = result.rows[0]) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : rootId;
|
|
77
77
|
};
|
|
78
78
|
const hydrateLeanPostsByIds = async (authData, ids) => {
|
|
79
|
+
var _a, _b;
|
|
79
80
|
if (!ids.length)
|
|
80
81
|
return new Map();
|
|
81
82
|
const { db } = await allpeepDb();
|
|
@@ -84,9 +85,17 @@ const hydrateLeanPostsByIds = async (authData, ids) => {
|
|
|
84
85
|
const hydrated = (await hydrateMapData(db, mapData, rows));
|
|
85
86
|
const config = await capabilitiesConfig();
|
|
86
87
|
const readable = canReadPost(config, authData);
|
|
88
|
+
const pair = new Set(blockedPairIds(authData.profile));
|
|
87
89
|
const byId = new Map();
|
|
88
90
|
for (const post of hydrated) {
|
|
89
|
-
const transformed = await transformPost(post, authData.profile
|
|
91
|
+
const transformed = await transformPost(post, authData.profile, {
|
|
92
|
+
embedThreadPreview: false,
|
|
93
|
+
});
|
|
94
|
+
const creatorId = (_a = transformed.creatorId) !== null && _a !== void 0 ? _a : (_b = transformed.profile) === null || _b === void 0 ? void 0 : _b.id;
|
|
95
|
+
if (creatorId && pair.has(creatorId)) {
|
|
96
|
+
byId.set(transformed.id, toHiddenPost(transformed));
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
90
99
|
if (readable(transformed))
|
|
91
100
|
byId.set(transformed.id, transformed);
|
|
92
101
|
}
|
|
@@ -105,12 +114,77 @@ export const listConversationPreviews = async (authData) => {
|
|
|
105
114
|
const pairs = await listConversationRootLeafPairs(db, profile.id);
|
|
106
115
|
const ids = [...new Set(pairs.flatMap((pair) => [pair.rootId, pair.leafId]))];
|
|
107
116
|
const byId = await hydrateLeanPostsByIds(authData, ids);
|
|
117
|
+
const blocked = new Set(blockedPairIds(profile));
|
|
118
|
+
return getUniqueBy(pairs
|
|
119
|
+
.map(({ rootId, leafId }) => {
|
|
120
|
+
const root = byId.get(rootId);
|
|
121
|
+
const leaf = byId.get(leafId);
|
|
122
|
+
if (!root || !leaf)
|
|
123
|
+
return null;
|
|
124
|
+
if (isOneToOneWithBlocked(root, profile, blocked))
|
|
125
|
+
return null;
|
|
126
|
+
return rootId === leafId ? [root] : [root, leaf];
|
|
127
|
+
})
|
|
128
|
+
.filter((conversation) => !!conversation), (conversation) => conversation[0].id);
|
|
129
|
+
};
|
|
130
|
+
/**
|
|
131
|
+
* Archived conversations: threads the profile has left — detected by posts
|
|
132
|
+
* they authored but are no longer in the audience of. Returns
|
|
133
|
+
* [root, lastReadablePost] per conversation so the user can still find and
|
|
134
|
+
* view the conversation history after leaving.
|
|
135
|
+
*/
|
|
136
|
+
export const listLeftConversations = async (authData) => {
|
|
137
|
+
const profile = authData.profile;
|
|
138
|
+
if (!profile) {
|
|
139
|
+
throw new Error('AuthorizationData.profile is required');
|
|
140
|
+
}
|
|
141
|
+
const { db } = await allpeepDb();
|
|
142
|
+
const result = (await db.execute(sql `
|
|
143
|
+
WITH left_posts AS (
|
|
144
|
+
SELECT p.id AS left_id, rt.to_id AS parent_id
|
|
145
|
+
FROM posts p
|
|
146
|
+
INNER JOIN reply_to rt ON rt.from_id = p.id
|
|
147
|
+
WHERE p.creator_id = ${profile.id}
|
|
148
|
+
AND p.deleted_at IS NULL
|
|
149
|
+
AND NOT EXISTS (
|
|
150
|
+
SELECT 1 FROM audience a
|
|
151
|
+
WHERE a.from_id = p.id AND a.to_id = p.creator_id
|
|
152
|
+
)
|
|
153
|
+
),
|
|
154
|
+
walk AS (
|
|
155
|
+
SELECT lp.left_id AS conv_id, lp.parent_id AS leaf_id,
|
|
156
|
+
lp.parent_id AS node_id, 0 AS depth
|
|
157
|
+
FROM left_posts lp
|
|
158
|
+
UNION ALL
|
|
159
|
+
SELECT walk.conv_id, walk.leaf_id, rt.to_id, walk.depth + 1
|
|
160
|
+
FROM reply_to rt
|
|
161
|
+
INNER JOIN walk ON rt.from_id = walk.node_id
|
|
162
|
+
WHERE walk.depth < 9999
|
|
163
|
+
),
|
|
164
|
+
roots AS (
|
|
165
|
+
SELECT DISTINCT ON (conv_id) conv_id, node_id AS root_id, leaf_id
|
|
166
|
+
FROM walk
|
|
167
|
+
ORDER BY conv_id, depth DESC
|
|
168
|
+
)
|
|
169
|
+
SELECT root_id, leaf_id FROM roots ORDER BY leaf_id DESC
|
|
170
|
+
`));
|
|
171
|
+
const pairs = result.rows.map((row) => ({
|
|
172
|
+
rootId: row.root_id,
|
|
173
|
+
leafId: row.leaf_id,
|
|
174
|
+
}));
|
|
175
|
+
if (pairs.length === 0)
|
|
176
|
+
return [];
|
|
177
|
+
const ids = [...new Set(pairs.flatMap((pair) => [pair.rootId, pair.leafId]))];
|
|
178
|
+
const byId = await hydrateLeanPostsByIds(authData, ids);
|
|
179
|
+
const blocked = new Set(blockedPairIds(profile));
|
|
108
180
|
return getUniqueBy(pairs
|
|
109
181
|
.map(({ rootId, leafId }) => {
|
|
110
182
|
const root = byId.get(rootId);
|
|
111
183
|
const leaf = byId.get(leafId);
|
|
112
184
|
if (!root || !leaf)
|
|
113
185
|
return null;
|
|
186
|
+
if (isOneToOneWithBlocked(root, profile, blocked))
|
|
187
|
+
return null;
|
|
114
188
|
return rootId === leafId ? [root] : [root, leaf];
|
|
115
189
|
})
|
|
116
190
|
.filter((conversation) => !!conversation), (conversation) => conversation[0].id);
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import type { AuthorizationData, PostWithMeta } from '@openpeepshq/common/types';
|
|
2
|
+
import { type PgDb } from '../db';
|
|
3
|
+
export type EventAgendaWindow = 'upcoming' | 'current' | 'past';
|
|
4
|
+
export type EventAgendaOptions = {
|
|
5
|
+
offset?: number;
|
|
6
|
+
limit?: number;
|
|
7
|
+
jamOnly?: boolean;
|
|
8
|
+
mine?: boolean;
|
|
9
|
+
groupId?: string;
|
|
10
|
+
};
|
|
11
|
+
export type EventAgendaQueryArgs = {
|
|
12
|
+
window: EventAgendaWindow;
|
|
13
|
+
now: string;
|
|
14
|
+
offset: number;
|
|
15
|
+
limit: number;
|
|
16
|
+
jamOnly?: boolean;
|
|
17
|
+
mine?: boolean;
|
|
18
|
+
groupId?: string;
|
|
19
|
+
profileId?: string;
|
|
20
|
+
excludeCreatorIds?: string[];
|
|
21
|
+
};
|
|
22
|
+
export declare const eventAgendaOccurrenceQuery: (db: Pick<PgDb, "select" | "selectDistinctOn">, args: EventAgendaQueryArgs) => Omit<import("drizzle-orm/pg-core").PgSelectBase<"event_agenda", {
|
|
23
|
+
postId: import("drizzle-orm/pg-core").PgColumn<{
|
|
24
|
+
name: "post_id";
|
|
25
|
+
tableName: "event_agenda";
|
|
26
|
+
dataType: "string";
|
|
27
|
+
columnType: "PgUUID";
|
|
28
|
+
data: string;
|
|
29
|
+
driverParam: string;
|
|
30
|
+
notNull: true;
|
|
31
|
+
hasDefault: false;
|
|
32
|
+
isPrimaryKey: false;
|
|
33
|
+
isAutoincrement: false;
|
|
34
|
+
hasRuntimeDefault: false;
|
|
35
|
+
enumValues: undefined;
|
|
36
|
+
baseColumn: never;
|
|
37
|
+
identity: undefined;
|
|
38
|
+
generated: undefined;
|
|
39
|
+
}, {}, {}>;
|
|
40
|
+
recurrenceId: import("drizzle-orm/pg-core").PgColumn<{
|
|
41
|
+
name: "recurrence_id";
|
|
42
|
+
tableName: "event_agenda";
|
|
43
|
+
dataType: "string";
|
|
44
|
+
columnType: "PgTimestampString";
|
|
45
|
+
data: string;
|
|
46
|
+
driverParam: string;
|
|
47
|
+
notNull: true;
|
|
48
|
+
hasDefault: false;
|
|
49
|
+
isPrimaryKey: false;
|
|
50
|
+
isAutoincrement: false;
|
|
51
|
+
hasRuntimeDefault: false;
|
|
52
|
+
enumValues: undefined;
|
|
53
|
+
baseColumn: never;
|
|
54
|
+
identity: undefined;
|
|
55
|
+
generated: undefined;
|
|
56
|
+
}, {}, {}>;
|
|
57
|
+
start: import("drizzle-orm/pg-core").PgColumn<{
|
|
58
|
+
name: "start";
|
|
59
|
+
tableName: "event_agenda";
|
|
60
|
+
dataType: "string";
|
|
61
|
+
columnType: "PgTimestampString";
|
|
62
|
+
data: string;
|
|
63
|
+
driverParam: string;
|
|
64
|
+
notNull: true;
|
|
65
|
+
hasDefault: false;
|
|
66
|
+
isPrimaryKey: false;
|
|
67
|
+
isAutoincrement: false;
|
|
68
|
+
hasRuntimeDefault: false;
|
|
69
|
+
enumValues: undefined;
|
|
70
|
+
baseColumn: never;
|
|
71
|
+
identity: undefined;
|
|
72
|
+
generated: undefined;
|
|
73
|
+
}, {}, {}>;
|
|
74
|
+
end: import("drizzle-orm/pg-core").PgColumn<{
|
|
75
|
+
name: "end";
|
|
76
|
+
tableName: "event_agenda";
|
|
77
|
+
dataType: "string";
|
|
78
|
+
columnType: "PgTimestampString";
|
|
79
|
+
data: string;
|
|
80
|
+
driverParam: string;
|
|
81
|
+
notNull: false;
|
|
82
|
+
hasDefault: false;
|
|
83
|
+
isPrimaryKey: false;
|
|
84
|
+
isAutoincrement: false;
|
|
85
|
+
hasRuntimeDefault: false;
|
|
86
|
+
enumValues: undefined;
|
|
87
|
+
baseColumn: never;
|
|
88
|
+
identity: undefined;
|
|
89
|
+
generated: undefined;
|
|
90
|
+
}, {}, {}>;
|
|
91
|
+
}, "partial", Record<"event_agenda", "not-null">, false, "limit" | "offset" | "orderBy", {
|
|
92
|
+
postId: string;
|
|
93
|
+
recurrenceId: string;
|
|
94
|
+
start: string;
|
|
95
|
+
end: string | null;
|
|
96
|
+
}[], {
|
|
97
|
+
postId: import("drizzle-orm/pg-core").PgColumn<{
|
|
98
|
+
name: "post_id";
|
|
99
|
+
tableName: "event_agenda";
|
|
100
|
+
dataType: "string";
|
|
101
|
+
columnType: "PgUUID";
|
|
102
|
+
data: string;
|
|
103
|
+
driverParam: string;
|
|
104
|
+
notNull: true;
|
|
105
|
+
hasDefault: false;
|
|
106
|
+
isPrimaryKey: false;
|
|
107
|
+
isAutoincrement: false;
|
|
108
|
+
hasRuntimeDefault: false;
|
|
109
|
+
enumValues: undefined;
|
|
110
|
+
baseColumn: never;
|
|
111
|
+
identity: undefined;
|
|
112
|
+
generated: undefined;
|
|
113
|
+
}, {}, {}>;
|
|
114
|
+
recurrenceId: import("drizzle-orm/pg-core").PgColumn<{
|
|
115
|
+
name: "recurrence_id";
|
|
116
|
+
tableName: "event_agenda";
|
|
117
|
+
dataType: "string";
|
|
118
|
+
columnType: "PgTimestampString";
|
|
119
|
+
data: string;
|
|
120
|
+
driverParam: string;
|
|
121
|
+
notNull: true;
|
|
122
|
+
hasDefault: false;
|
|
123
|
+
isPrimaryKey: false;
|
|
124
|
+
isAutoincrement: false;
|
|
125
|
+
hasRuntimeDefault: false;
|
|
126
|
+
enumValues: undefined;
|
|
127
|
+
baseColumn: never;
|
|
128
|
+
identity: undefined;
|
|
129
|
+
generated: undefined;
|
|
130
|
+
}, {}, {}>;
|
|
131
|
+
start: import("drizzle-orm/pg-core").PgColumn<{
|
|
132
|
+
name: "start";
|
|
133
|
+
tableName: "event_agenda";
|
|
134
|
+
dataType: "string";
|
|
135
|
+
columnType: "PgTimestampString";
|
|
136
|
+
data: string;
|
|
137
|
+
driverParam: string;
|
|
138
|
+
notNull: true;
|
|
139
|
+
hasDefault: false;
|
|
140
|
+
isPrimaryKey: false;
|
|
141
|
+
isAutoincrement: false;
|
|
142
|
+
hasRuntimeDefault: false;
|
|
143
|
+
enumValues: undefined;
|
|
144
|
+
baseColumn: never;
|
|
145
|
+
identity: undefined;
|
|
146
|
+
generated: undefined;
|
|
147
|
+
}, {}, {}>;
|
|
148
|
+
end: import("drizzle-orm/pg-core").PgColumn<{
|
|
149
|
+
name: "end";
|
|
150
|
+
tableName: "event_agenda";
|
|
151
|
+
dataType: "string";
|
|
152
|
+
columnType: "PgTimestampString";
|
|
153
|
+
data: string;
|
|
154
|
+
driverParam: string;
|
|
155
|
+
notNull: false;
|
|
156
|
+
hasDefault: false;
|
|
157
|
+
isPrimaryKey: false;
|
|
158
|
+
isAutoincrement: false;
|
|
159
|
+
hasRuntimeDefault: false;
|
|
160
|
+
enumValues: undefined;
|
|
161
|
+
baseColumn: never;
|
|
162
|
+
identity: undefined;
|
|
163
|
+
generated: undefined;
|
|
164
|
+
}, {}, {}>;
|
|
165
|
+
}>, "limit" | "offset" | "orderBy">;
|
|
166
|
+
export declare const listEventAgenda: (authData: AuthorizationData, window: EventAgendaWindow, { offset, limit, jamOnly, mine, groupId, }?: EventAgendaOptions) => Promise<PostWithMeta[]>;
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { notInArray, and, asc, desc, eq, gt, isNotNull, isNull, lte, or, sql, } from 'drizzle-orm';
|
|
2
|
+
import { canReadPost } from './helpers/filters.js';
|
|
3
|
+
import { transformPost } from './helpers/index.js';
|
|
4
|
+
import { allpeepDb } from '../db/index.js';
|
|
5
|
+
import { capabilitiesConfig } from '../config/index.js';
|
|
6
|
+
import { normalizeComputedDatetime } from '../db/pg/mappers.js';
|
|
7
|
+
import { eventOccurrences, posts } from '../db/pg/schema/index.js';
|
|
8
|
+
import { postGroups } from '../db/pg/schema/edges.js';
|
|
9
|
+
import { postHasYesOrMaybeRsvpExpr } from '../db/pg/queries/index.js';
|
|
10
|
+
import { postFilters } from '../db/pg/filters/index.js';
|
|
11
|
+
import { fetchRowsByIds, hydrateMapData } from '../db/pg/map/relations.js';
|
|
12
|
+
import { postsMappingForProfile } from './mapping.js';
|
|
13
|
+
import { blockedPairIds } from '@openpeepshq/common/lib';
|
|
14
|
+
const occurrenceTimeFilter = (window, now) => {
|
|
15
|
+
const start = eventOccurrences.start;
|
|
16
|
+
const end = eventOccurrences.end;
|
|
17
|
+
if (window === 'upcoming') {
|
|
18
|
+
return or(gt(start, now), and(isNotNull(end), gt(end, now)));
|
|
19
|
+
}
|
|
20
|
+
if (window === 'current') {
|
|
21
|
+
// A missing end is treated as a one-hour event, not an infinite one.
|
|
22
|
+
return and(lte(start, now), sql `COALESCE(${end}, ${start} + interval '1 hour') >= ${now}`);
|
|
23
|
+
}
|
|
24
|
+
return sql `COALESCE(${end}, ${start} + interval '1 hour') < ${now}`;
|
|
25
|
+
};
|
|
26
|
+
const eventAgendaConditions = ({ window, now, jamOnly, mine, groupId, profileId, excludeCreatorIds, }) => {
|
|
27
|
+
const conditions = [
|
|
28
|
+
isNull(posts.deletedAt),
|
|
29
|
+
eq(posts.type, 'event'),
|
|
30
|
+
eq(eventOccurrences.cancelled, false),
|
|
31
|
+
occurrenceTimeFilter(window, now),
|
|
32
|
+
];
|
|
33
|
+
if (jamOnly) {
|
|
34
|
+
conditions.push(isNotNull(sql `${posts.body}->'jam'`));
|
|
35
|
+
}
|
|
36
|
+
if (mine && profileId) {
|
|
37
|
+
conditions.push(or(eq(posts.creatorId, profileId), postFilters.isJamModerator(profileId).where, postHasYesOrMaybeRsvpExpr(posts, profileId)));
|
|
38
|
+
}
|
|
39
|
+
if (groupId) {
|
|
40
|
+
conditions.push(sql `EXISTS (SELECT 1 FROM ${postGroups} WHERE ${postGroups.fromId} = ${posts.id}::text AND ${postGroups.toId} = ${groupId})`);
|
|
41
|
+
}
|
|
42
|
+
if (excludeCreatorIds === null || excludeCreatorIds === void 0 ? void 0 : excludeCreatorIds.length) {
|
|
43
|
+
conditions.push(notInArray(posts.creatorId, excludeCreatorIds));
|
|
44
|
+
}
|
|
45
|
+
return conditions;
|
|
46
|
+
};
|
|
47
|
+
// One row per event: a daily series would otherwise fill the first page.
|
|
48
|
+
export const eventAgendaOccurrenceQuery = (db, args) => {
|
|
49
|
+
const pickOrder = args.window === 'past'
|
|
50
|
+
? desc(eventOccurrences.start)
|
|
51
|
+
: asc(eventOccurrences.start);
|
|
52
|
+
const distinctOccurrences = db
|
|
53
|
+
.selectDistinctOn([eventOccurrences.postId], {
|
|
54
|
+
postId: eventOccurrences.postId,
|
|
55
|
+
recurrenceId: eventOccurrences.recurrenceId,
|
|
56
|
+
start: eventOccurrences.start,
|
|
57
|
+
end: eventOccurrences.end,
|
|
58
|
+
})
|
|
59
|
+
.from(eventOccurrences)
|
|
60
|
+
.innerJoin(posts, eq(posts.id, eventOccurrences.postId))
|
|
61
|
+
.where(and(...eventAgendaConditions(args)))
|
|
62
|
+
.orderBy(eventOccurrences.postId, pickOrder)
|
|
63
|
+
.as('event_agenda');
|
|
64
|
+
return db
|
|
65
|
+
.select({
|
|
66
|
+
postId: distinctOccurrences.postId,
|
|
67
|
+
recurrenceId: distinctOccurrences.recurrenceId,
|
|
68
|
+
start: distinctOccurrences.start,
|
|
69
|
+
end: distinctOccurrences.end,
|
|
70
|
+
})
|
|
71
|
+
.from(distinctOccurrences)
|
|
72
|
+
.orderBy(args.window === 'past'
|
|
73
|
+
? desc(distinctOccurrences.start)
|
|
74
|
+
: asc(distinctOccurrences.start))
|
|
75
|
+
.limit(args.limit)
|
|
76
|
+
.offset(args.offset);
|
|
77
|
+
};
|
|
78
|
+
const loadPostsByIds = async (ids, authData) => {
|
|
79
|
+
if (ids.length === 0)
|
|
80
|
+
return new Map();
|
|
81
|
+
const profile = authData.profile;
|
|
82
|
+
const { db } = await allpeepDb();
|
|
83
|
+
const mapData = postsMappingForProfile(profile).data();
|
|
84
|
+
const rows = await fetchRowsByIds(db, mapData.collection, ids, mapData.softDelete);
|
|
85
|
+
const hydrated = (await hydrateMapData(db, mapData, rows));
|
|
86
|
+
const config = await capabilitiesConfig();
|
|
87
|
+
const readable = canReadPost(config, authData);
|
|
88
|
+
const entries = await Promise.all(hydrated.map(async (post) => {
|
|
89
|
+
const transformed = await transformPost(post, profile);
|
|
90
|
+
return readable(transformed) ? transformed : undefined;
|
|
91
|
+
}));
|
|
92
|
+
return new Map(entries
|
|
93
|
+
.filter((post) => !!post)
|
|
94
|
+
.map((post) => [post.id, post]));
|
|
95
|
+
};
|
|
96
|
+
export const listEventAgenda = async (authData, window, { offset = 0, limit = 100, jamOnly = false, mine = false, groupId, } = {}) => {
|
|
97
|
+
var _a;
|
|
98
|
+
const { db } = await allpeepDb();
|
|
99
|
+
const now = new Date().toISOString();
|
|
100
|
+
const rows = await eventAgendaOccurrenceQuery(db, {
|
|
101
|
+
window,
|
|
102
|
+
now,
|
|
103
|
+
offset,
|
|
104
|
+
limit,
|
|
105
|
+
jamOnly,
|
|
106
|
+
mine,
|
|
107
|
+
groupId,
|
|
108
|
+
profileId: (_a = authData.profile) === null || _a === void 0 ? void 0 : _a.id,
|
|
109
|
+
excludeCreatorIds: blockedPairIds(authData.profile),
|
|
110
|
+
});
|
|
111
|
+
const postsById = await loadPostsByIds([...new Set(rows.map((row) => row.postId))], authData);
|
|
112
|
+
return rows.flatMap((row) => {
|
|
113
|
+
const post = postsById.get(row.postId);
|
|
114
|
+
if (!post)
|
|
115
|
+
return [];
|
|
116
|
+
const occurrenceRecurrenceId = normalizeComputedDatetime(row.recurrenceId);
|
|
117
|
+
const occurrenceStart = normalizeComputedDatetime(row.start);
|
|
118
|
+
const occurrenceEnd = normalizeComputedDatetime(row.end);
|
|
119
|
+
if (!occurrenceRecurrenceId || !occurrenceStart)
|
|
120
|
+
return [];
|
|
121
|
+
return [
|
|
122
|
+
Object.assign(Object.assign(Object.assign({}, post), { occurrenceRecurrenceId,
|
|
123
|
+
occurrenceStart }), (occurrenceEnd ? { occurrenceEnd } : {})),
|
|
124
|
+
];
|
|
125
|
+
});
|
|
126
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare const EVENT_OCCURRENCE_CRON = "0 4 * * *";
|
|
2
|
+
declare const eventOccurrenceQueue: () => import("bullmq").Queue, eventOccurrenceWorker: () => import("bullmq").Worker;
|
|
3
|
+
export declare const ensureEventOccurrenceSchedule: () => Promise<void>;
|
|
4
|
+
export { eventOccurrenceQueue, eventOccurrenceWorker };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { queueAndWorker } from '../jobs/index.js';
|
|
2
|
+
import { logger } from '../log/index.js';
|
|
3
|
+
import { backfillMissingEventOccurrences, rebuildRecurringEventOccurrences, } from './eventOccurrences.js';
|
|
4
|
+
const log = logger('app:posts:eventOccurrences');
|
|
5
|
+
const QUEUE_NAME = 'event-occurrences';
|
|
6
|
+
const SCHEDULER_ID = 'event-occurrences-daily';
|
|
7
|
+
const JOB_NAME = 'rebuild-recurring';
|
|
8
|
+
export const EVENT_OCCURRENCE_CRON = '0 4 * * *';
|
|
9
|
+
const [eventOccurrenceQueue, eventOccurrenceWorker] = queueAndWorker(QUEUE_NAME, async () => {
|
|
10
|
+
const backfilled = await backfillMissingEventOccurrences();
|
|
11
|
+
const rebuilt = await rebuildRecurringEventOccurrences();
|
|
12
|
+
log.info(`Backfilled ${backfilled} events missing an occurrence index; rebuilt ${rebuilt} recurring events`);
|
|
13
|
+
}, {
|
|
14
|
+
defaultJobOptions: {
|
|
15
|
+
removeOnComplete: { count: 10 },
|
|
16
|
+
removeOnFail: { age: 86400 * 14, count: 50 },
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
export const ensureEventOccurrenceSchedule = async () => {
|
|
20
|
+
const queue = eventOccurrenceQueue();
|
|
21
|
+
await queue.upsertJobScheduler(SCHEDULER_ID, { pattern: EVENT_OCCURRENCE_CRON }, { name: JOB_NAME, data: { type: 'rebuild' } });
|
|
22
|
+
log.info(`Event occurrence schedule registered: cron="${EVENT_OCCURRENCE_CRON}"`);
|
|
23
|
+
const backfilled = await backfillMissingEventOccurrences();
|
|
24
|
+
if (backfilled > 0) {
|
|
25
|
+
log.info(`Backfilled occurrence index for ${backfilled} events`);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
export { eventOccurrenceQueue, eventOccurrenceWorker };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Event } from '@openpeepshq/common/types';
|
|
2
|
+
export declare const rebuildEventOccurrences: (postId: string, event: Event) => Promise<void>;
|
|
3
|
+
export declare const clearEventOccurrences: (postId: string) => Promise<void>;
|
|
4
|
+
/**
|
|
5
|
+
* JSONL restores bring posts back without `event_occurrences`. The agenda
|
|
6
|
+
* reads only that index, so a restored community would show no events until
|
|
7
|
+
* each one is edited. Recurring series are rebuilt by the daily job.
|
|
8
|
+
*/
|
|
9
|
+
export declare const backfillMissingEventOccurrences: () => Promise<number>;
|
|
10
|
+
export declare const rebuildRecurringEventOccurrences: () => Promise<number>;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { and, eq, isNotNull, isNull, sql } from 'drizzle-orm';
|
|
2
|
+
import { uuidv7 } from 'uuidv7';
|
|
3
|
+
import { occurrencesForIndex } from '@openpeepshq/common/lib';
|
|
4
|
+
import { database } from '../db/index.js';
|
|
5
|
+
import { eventOccurrences, posts } from '../db/pg/schema/index.js';
|
|
6
|
+
export const rebuildEventOccurrences = async (postId, event) => {
|
|
7
|
+
const db = await database();
|
|
8
|
+
await db.delete(eventOccurrences).where(eq(eventOccurrences.postId, postId));
|
|
9
|
+
const rows = occurrencesForIndex(event);
|
|
10
|
+
if (rows.length === 0)
|
|
11
|
+
return;
|
|
12
|
+
await db.insert(eventOccurrences).values(rows.map((row) => ({
|
|
13
|
+
id: uuidv7(),
|
|
14
|
+
postId,
|
|
15
|
+
recurrenceId: row.recurrenceId,
|
|
16
|
+
start: row.start,
|
|
17
|
+
end: row.end,
|
|
18
|
+
cancelled: row.cancelled,
|
|
19
|
+
})));
|
|
20
|
+
};
|
|
21
|
+
export const clearEventOccurrences = async (postId) => {
|
|
22
|
+
const db = await database();
|
|
23
|
+
await db.delete(eventOccurrences).where(eq(eventOccurrences.postId, postId));
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* JSONL restores bring posts back without `event_occurrences`. The agenda
|
|
27
|
+
* reads only that index, so a restored community would show no events until
|
|
28
|
+
* each one is edited. Recurring series are rebuilt by the daily job.
|
|
29
|
+
*/
|
|
30
|
+
export const backfillMissingEventOccurrences = async () => {
|
|
31
|
+
const db = await database();
|
|
32
|
+
const rows = await db
|
|
33
|
+
.select({ id: posts.id, body: posts.body })
|
|
34
|
+
.from(posts)
|
|
35
|
+
.where(and(eq(posts.type, 'event'), isNull(posts.deletedAt), sql `NOT EXISTS (SELECT 1 FROM ${eventOccurrences} WHERE ${eventOccurrences.postId} = ${posts.id})`));
|
|
36
|
+
let rebuilt = 0;
|
|
37
|
+
for (const row of rows) {
|
|
38
|
+
const event = row.body;
|
|
39
|
+
if ((event === null || event === void 0 ? void 0 : event.type) !== 'event' || !event.start)
|
|
40
|
+
continue;
|
|
41
|
+
await rebuildEventOccurrences(row.id, event);
|
|
42
|
+
rebuilt += 1;
|
|
43
|
+
}
|
|
44
|
+
return rebuilt;
|
|
45
|
+
};
|
|
46
|
+
export const rebuildRecurringEventOccurrences = async () => {
|
|
47
|
+
const db = await database();
|
|
48
|
+
const rows = await db
|
|
49
|
+
.select({ id: posts.id, body: posts.body })
|
|
50
|
+
.from(posts)
|
|
51
|
+
.where(and(eq(posts.type, 'event'), isNull(posts.deletedAt), isNotNull(sql `${posts.body}->'recurrence'`)));
|
|
52
|
+
let rebuilt = 0;
|
|
53
|
+
for (const row of rows) {
|
|
54
|
+
const event = row.body;
|
|
55
|
+
if ((event === null || event === void 0 ? void 0 : event.type) !== 'event' || !event.start)
|
|
56
|
+
continue;
|
|
57
|
+
await rebuildEventOccurrences(row.id, event);
|
|
58
|
+
rebuilt += 1;
|
|
59
|
+
}
|
|
60
|
+
return rebuilt;
|
|
61
|
+
};
|