@openpeepshq/core 0.1.33 → 0.1.35
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/finders.js +5 -6
- package/dist/accessTokens/mutations.js +1 -1
- package/dist/accessTokens/tokens.js +8 -11
- package/dist/accounts/helpers.js +6 -2
- package/dist/accounts/mutations.js +36 -33
- package/dist/analytics/cache.js +3 -3
- package/dist/analytics/clicks.js +14 -22
- package/dist/analytics/compile.js +17 -12
- package/dist/analytics/dateRange.js +3 -5
- package/dist/analytics/jobs.js +3 -4
- package/dist/analytics/pdfReport.js +45 -61
- package/dist/analytics/privacy.js +1 -1
- package/dist/analytics/read.js +164 -140
- package/dist/analytics/report.d.ts +22 -1
- package/dist/analytics/report.js +2 -3
- package/dist/analytics/settings.js +3 -4
- package/dist/backups/index.js +82 -25
- package/dist/backups/metadata.d.ts +2 -3
- package/dist/backups/metadata.js +6 -2
- package/dist/config/db.js +2 -4
- package/dist/config/defaults/capabilities.js +13 -2
- package/dist/config/defaults/community.js +8 -3
- package/dist/config/defaults/core.d.ts +1 -1
- package/dist/config/defaults/core.js +16 -8
- package/dist/config/index.d.ts +22 -0
- package/dist/config/index.js +15 -11
- package/dist/db/explorer/query.js +9 -8
- package/dist/db/explorer/sql.js +8 -8
- package/dist/db/helpers.d.ts +1 -0
- package/dist/db/helpers.js +25 -7
- package/dist/db/index.d.ts +1 -1
- package/dist/db/index.js +1 -0
- package/dist/db/init.js +2 -3
- package/dist/db/migration/importCollections.d.ts +14 -9
- package/dist/db/migration/importCollections.js +121 -174
- package/dist/db/migration/shared.d.ts +2 -33
- package/dist/db/migration/shared.js +17 -87
- package/dist/db/pg/client.js +4 -7
- package/dist/db/pg/collections.d.ts +2 -2
- package/dist/db/pg/collections.js +2 -2
- package/dist/db/pg/filters/index.js +1 -1
- package/dist/db/pg/filters/posts.d.ts +12 -0
- package/dist/db/pg/filters/posts.js +30 -4
- package/dist/db/pg/filters/profiles.d.ts +1 -0
- package/dist/db/pg/filters/profiles.js +2 -1
- package/dist/db/pg/helpers.js +2 -14
- package/dist/db/pg/map/filters.js +40 -10
- package/dist/db/pg/map/helpers.js +5 -1
- package/dist/db/pg/map/index.js +46 -51
- package/dist/db/pg/map/mutations.js +65 -25
- package/dist/db/pg/map/registry.d.ts +5 -0
- package/dist/db/pg/map/registry.js +133 -56
- package/dist/db/pg/map/relations.js +76 -68
- package/dist/db/pg/map/search.js +15 -37
- package/dist/db/pg/mappers.js +13 -7
- package/dist/db/pg/migrate.d.ts +1 -2
- package/dist/db/pg/migrate.js +8 -9
- package/dist/db/pg/queries/activity.js +5 -5
- package/dist/db/pg/queries/derived.js +8 -12
- 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 +279 -41
- package/dist/db/pg/schema/documents.js +136 -22
- package/dist/db/pg/schema/edges.d.ts +116 -7
- package/dist/db/pg/schema/edges.js +9 -2
- package/dist/db/pg/schema/index.d.ts +362 -15
- package/dist/db/pg/schema/index.js +5 -1
- 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/0016_posts_search_vector_title_tags_path.sql +54 -0
- package/dist/db/pg/sql/meta/{0009_snapshot.json → 0015_snapshot.json} +291 -36
- package/dist/db/pg/sql/meta/_journal.json +49 -0
- package/dist/db/replaceOrigin.js +1 -1
- package/dist/email/queueStats.js +8 -11
- package/dist/email/render.js +14 -3
- package/dist/email/send.js +19 -17
- package/dist/email/smtpTest.js +10 -6
- package/dist/errors/index.js +1 -1
- package/dist/events/index.d.ts +4 -0
- package/dist/events/index.js +1 -0
- package/dist/events/once.js +2 -4
- package/dist/federation/activities.d.ts +17 -0
- package/dist/federation/activities.js +25 -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 +2 -1
- package/dist/groups/helpers.d.ts +18 -0
- package/dist/groups/helpers.js +1 -2
- package/dist/groups/mapping.d.ts +4 -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/mutations.js +2 -3
- package/dist/jams/export.d.ts +19 -0
- package/dist/jams/export.js +58 -0
- package/dist/jams/finders.js +4 -1
- package/dist/jams/health.d.ts +10 -0
- package/dist/jams/health.js +50 -0
- package/dist/jams/helpers.d.ts +2 -2
- package/dist/jams/helpers.js +6 -2
- package/dist/jams/index.d.ts +2 -0
- package/dist/jams/index.js +25 -18
- package/dist/jams/jobs.js +2 -3
- package/dist/jams/livekit.d.ts +49 -4
- package/dist/jams/livekit.js +62 -45
- package/dist/jams/mapping.d.ts +3 -3
- package/dist/jams/mutations.d.ts +1 -1
- package/dist/jams/recording.d.ts +10 -0
- package/dist/jams/recording.js +33 -1
- package/dist/jams/token.d.ts +13 -1
- package/dist/jams/token.js +25 -18
- package/dist/jams/waitingRoom.js +1 -1
- package/dist/jobs/index.d.ts +1 -0
- package/dist/jobs/index.js +9 -3
- package/dist/jobs/jobDetail.js +6 -12
- package/dist/jobs/jobLog.js +2 -3
- package/dist/lib/streams.js +4 -39
- package/dist/lib/zodDeepPartial.d.ts +1 -1
- package/dist/lib/zodDeepPartial.js +1 -1
- package/dist/log/index.js +13 -29
- package/dist/media/index.d.ts +1 -1
- package/dist/media/index.js +73 -12
- package/dist/media/jobs.js +1 -2
- package/dist/media/openpeeps/index.js +11 -46
- package/dist/media/processing.js +25 -11
- package/dist/media/streaming.js +5 -5
- package/dist/media/streamingCleanupJobs.js +1 -2
- package/dist/media/streamingJobs.js +3 -4
- package/dist/media/streamingPrewarm.js +7 -11
- package/dist/mediaAttachments/index.js +6 -2
- package/dist/notifications/defaultNotifications/announcement/index.js +15 -18
- package/dist/notifications/defaultNotifications/directMessage/index.js +9 -8
- package/dist/notifications/defaultNotifications/follow/index.js +15 -18
- package/dist/notifications/defaultNotifications/groupAdded/index.js +14 -17
- package/dist/notifications/defaultNotifications/groupMemberJoined/index.js +15 -18
- package/dist/notifications/defaultNotifications/groupMemberLeft/index.js +15 -18
- package/dist/notifications/defaultNotifications/index.js +4 -0
- package/dist/notifications/defaultNotifications/jamModerator/index.js +3 -5
- package/dist/notifications/defaultNotifications/jamSpeaker/index.js +14 -18
- package/dist/notifications/defaultNotifications/jamStarted/index.js +18 -22
- package/dist/notifications/defaultNotifications/mention/index.js +20 -24
- package/dist/notifications/defaultNotifications/newGroupPost/index.js +11 -6
- package/dist/notifications/defaultNotifications/newProfile/index.js +14 -17
- 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 +17 -0
- package/dist/notifications/defaultNotifications/pollVote/index.js +14 -17
- package/dist/notifications/defaultNotifications/reaction/index.js +18 -21
- package/dist/notifications/defaultNotifications/reply/index.js +21 -22
- package/dist/notifications/defaultNotifications/repost/index.js +16 -20
- package/dist/notifications/defaultNotifications/rsvp/index.d.ts +6 -1
- package/dist/notifications/defaultNotifications/rsvp/index.js +31 -18
- package/dist/notifications/defaultNotifications/rsvpCanceled/index.d.ts +24 -0
- package/dist/notifications/defaultNotifications/rsvpCanceled/index.js +82 -0
- package/dist/notifications/defaultNotifications/rsvpCanceled/recipients.d.ts +4 -0
- package/dist/notifications/defaultNotifications/rsvpCanceled/recipients.js +11 -0
- package/dist/notifications/fcmCredential.js +1 -1
- package/dist/notifications/finders.js +1 -1
- package/dist/notifications/helpers.js +13 -10
- package/dist/notifications/jobs.js +10 -5
- package/dist/notifications/mutations.js +8 -1
- package/dist/notifications/push.js +11 -16
- package/dist/performance/index.js +3 -3
- package/dist/plugins/helpers.js +3 -6
- package/dist/plugins/index.js +39 -4
- package/dist/plugins/install.d.ts +28 -9
- package/dist/plugins/install.js +302 -56
- package/dist/plugins/pluginAuth.d.ts +1 -1
- package/dist/plugins/pluginAuth.js +13 -10
- package/dist/plugins/state.js +2 -3
- package/dist/posts/activity.d.ts +9 -0
- package/dist/posts/activity.js +34 -0
- package/dist/posts/contextClosure.d.ts +1 -9
- package/dist/posts/contextClosure.js +14 -44
- package/dist/posts/conversationQueries.d.ts +7 -0
- package/dist/posts/conversationQueries.js +76 -4
- package/dist/posts/eventAgenda.d.ts +1 -0
- package/dist/posts/eventAgenda.js +17 -8
- package/dist/posts/eventOccurrenceJobs.js +7 -2
- package/dist/posts/eventOccurrences.d.ts +6 -0
- package/dist/posts/eventOccurrences.js +22 -1
- package/dist/posts/finders.d.ts +56 -9
- package/dist/posts/finders.js +84 -23
- package/dist/posts/helpers/filters.js +13 -9
- package/dist/posts/helpers/index.d.ts +179 -14
- package/dist/posts/helpers/index.js +70 -34
- package/dist/posts/index.d.ts +5 -2
- package/dist/posts/index.js +5 -2
- package/dist/posts/mapping.d.ts +765 -30
- package/dist/posts/mapping.js +74 -31
- package/dist/posts/mutations.d.ts +516 -3
- package/dist/posts/mutations.js +179 -41
- package/dist/posts/pollEnd.d.ts +3 -0
- package/dist/posts/pollEnd.js +4 -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 +23 -15
- package/dist/posts/threadPreview.d.ts +8 -0
- package/dist/posts/threadPreview.js +44 -0
- package/dist/posts/unseenCounts.js +4 -1
- package/dist/processingStats/queries.js +6 -7
- package/dist/profileSettings/cache.js +21 -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 +82 -0
- package/dist/profiles/blocks.d.ts +12 -0
- package/dist/profiles/blocks.js +73 -0
- package/dist/profiles/cache.d.ts +1 -0
- package/dist/profiles/cache.js +30 -10
- package/dist/profiles/export.js +30 -39
- package/dist/profiles/finders.d.ts +2 -1
- package/dist/profiles/finders.js +15 -8
- package/dist/profiles/helpers.d.ts +2 -0
- package/dist/profiles/index.d.ts +2 -0
- package/dist/profiles/index.js +2 -0
- 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 +31 -4
- package/dist/redis/connection.d.ts +2 -0
- package/dist/redis/connection.js +25 -6
- package/dist/reports/helpers.js +7 -3
- 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 +85 -3
- package/dist/search/finders.js +15 -3
- package/dist/search/helpers.js +1 -1
- package/dist/search/mapping.d.ts +1 -1
- package/dist/search/mapping.js +6 -2
- package/dist/sentry/index.js +9 -11
- package/dist/seo/index.d.ts +50 -0
- package/dist/seo/index.js +145 -0
- package/dist/seo/route.d.ts +18 -0
- package/dist/seo/route.js +19 -0
- package/dist/server/index.d.ts +19 -0
- package/dist/server/index.js +31 -14
- 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 +97 -0
- package/dist/session/index.js +2 -2
- package/dist/stats/finders.d.ts +1 -17681
- package/dist/stats/finders.js +22 -9
- package/dist/stats/helpers.js +4 -1
- package/dist/stats/mapping.d.ts +39 -2
- package/dist/stats/mapping.js +8 -4
- package/dist/stripe/cache.js +5 -6
- package/dist/stripe/connection.js +3 -4
- package/dist/stripe/helpers.js +15 -20
- package/package.json +5 -7
- package/dist/db/migration/detect.d.ts +0 -3
- package/dist/db/migration/detect.js +0 -33
- 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 -20
- package/dist/db/migration/transform.js +0 -232
- 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/db/pg/sql/meta/0006_snapshot.json +0 -4200
- package/dist/db/pg/sql/meta/0007_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/migration/index.d.ts +0 -4
- package/dist/migration/index.js +0 -4
- package/dist/roles/defaults.d.ts +0 -2
- package/dist/roles/defaults.js +0 -95
package/dist/plugins/install.js
CHANGED
|
@@ -1,15 +1,5 @@
|
|
|
1
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
2
|
-
var t = {};
|
|
3
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
4
|
-
t[p] = s[p];
|
|
5
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
6
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
7
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
8
|
-
t[p[i]] = s[p[i]];
|
|
9
|
-
}
|
|
10
|
-
return t;
|
|
11
|
-
};
|
|
12
1
|
import { spawn } from 'node:child_process';
|
|
2
|
+
import { createRequire } from 'node:module';
|
|
13
3
|
import fs from 'node:fs/promises';
|
|
14
4
|
import path from 'node:path';
|
|
15
5
|
import { defaultConfig } from '../config/index.js';
|
|
@@ -20,20 +10,24 @@ import { nowIso } from '../db/pg/mappers.js';
|
|
|
20
10
|
import { eq } from 'drizzle-orm';
|
|
21
11
|
import { setPluginEnabledOverride } from './state.js';
|
|
22
12
|
const log = logger('core:plugins:install');
|
|
13
|
+
const requireFromHost = createRequire(new URL('../../package.json', import.meta.url));
|
|
14
|
+
const MAX_COMMAND_OUTPUT = 16_000;
|
|
23
15
|
const INSTALLED_PLUGINS_KEY = 'openpeeps-installed-plugins';
|
|
24
|
-
const getInstalledPlugins = async () => {
|
|
25
|
-
var _a, _b;
|
|
16
|
+
export const getInstalledPlugins = async () => {
|
|
26
17
|
const { db } = await allpeepDb();
|
|
27
18
|
const rows = await db
|
|
28
19
|
.select()
|
|
29
20
|
.from(configs)
|
|
30
21
|
.where(eq(configs.key, INSTALLED_PLUGINS_KEY))
|
|
31
22
|
.limit(1);
|
|
32
|
-
return
|
|
23
|
+
return rows[0]?.body ?? {};
|
|
33
24
|
};
|
|
34
|
-
const setInstalledPlugin = async (pluginKey, source, installedBy) => {
|
|
25
|
+
const setInstalledPlugin = async (pluginKey, source, requiresAuth, installedBy) => {
|
|
35
26
|
const installed = await getInstalledPlugins();
|
|
36
|
-
const body =
|
|
27
|
+
const body = {
|
|
28
|
+
...installed,
|
|
29
|
+
[pluginKey]: { source, requiresAuth, installedAt: nowIso(), installedBy },
|
|
30
|
+
};
|
|
37
31
|
const ts = nowIso();
|
|
38
32
|
const { db } = await allpeepDb();
|
|
39
33
|
await db
|
|
@@ -46,7 +40,7 @@ const setInstalledPlugin = async (pluginKey, source, installedBy) => {
|
|
|
46
40
|
};
|
|
47
41
|
const removeInstalledPlugin = async (pluginKey) => {
|
|
48
42
|
const installed = await getInstalledPlugins();
|
|
49
|
-
const
|
|
43
|
+
const rest = Object.fromEntries(Object.entries(installed).filter(([key]) => key !== pluginKey));
|
|
50
44
|
const ts = nowIso();
|
|
51
45
|
const { db } = await allpeepDb();
|
|
52
46
|
await db
|
|
@@ -62,8 +56,22 @@ const removeInstalledPlugin = async (pluginKey) => {
|
|
|
62
56
|
set: { body: rest, updatedAt: ts },
|
|
63
57
|
});
|
|
64
58
|
};
|
|
65
|
-
const runCommand = (command, args, cwd) => new Promise((resolve) => {
|
|
66
|
-
const child = spawn(command, args, {
|
|
59
|
+
const runCommand = (command, args, cwd, environment = {}) => new Promise((resolve) => {
|
|
60
|
+
const child = spawn(command, args, {
|
|
61
|
+
cwd,
|
|
62
|
+
env: {
|
|
63
|
+
HOME: process.env.HOME,
|
|
64
|
+
LANG: process.env.LANG,
|
|
65
|
+
LC_ALL: process.env.LC_ALL,
|
|
66
|
+
NODE_EXTRA_CA_CERTS: process.env.NODE_EXTRA_CA_CERTS,
|
|
67
|
+
PATH: process.env.PATH,
|
|
68
|
+
SSL_CERT_DIR: process.env.SSL_CERT_DIR,
|
|
69
|
+
SSL_CERT_FILE: process.env.SSL_CERT_FILE,
|
|
70
|
+
TMPDIR: process.env.TMPDIR,
|
|
71
|
+
...environment,
|
|
72
|
+
},
|
|
73
|
+
shell: false,
|
|
74
|
+
});
|
|
67
75
|
let stdout = '';
|
|
68
76
|
let stderr = '';
|
|
69
77
|
child.stdout.on('data', (chunk) => {
|
|
@@ -73,7 +81,7 @@ const runCommand = (command, args, cwd) => new Promise((resolve) => {
|
|
|
73
81
|
stderr += chunk.toString();
|
|
74
82
|
});
|
|
75
83
|
child.on('close', (exitCode) => {
|
|
76
|
-
resolve({ exitCode: exitCode
|
|
84
|
+
resolve({ exitCode: exitCode ?? 1, stdout, stderr });
|
|
77
85
|
});
|
|
78
86
|
child.on('error', (err) => {
|
|
79
87
|
resolve({ exitCode: 1, stdout, stderr: String(err) });
|
|
@@ -83,13 +91,174 @@ const getPluginDir = async () => {
|
|
|
83
91
|
const { plugins } = await defaultConfig;
|
|
84
92
|
return plugins.path;
|
|
85
93
|
};
|
|
94
|
+
const redactUrl = (url) => {
|
|
95
|
+
const parsed = new URL(url);
|
|
96
|
+
if (parsed.protocol !== 'ssh:') {
|
|
97
|
+
parsed.username = '';
|
|
98
|
+
}
|
|
99
|
+
parsed.password = '';
|
|
100
|
+
return parsed.toString();
|
|
101
|
+
};
|
|
86
102
|
const sourceLabel = (source) => source.type === 'npm'
|
|
87
103
|
? `npm:${source.package}${source.version ? `@${source.version}` : ''}`
|
|
88
|
-
: `git:${source.url}${source.ref ? `#${source.ref}` : ''}`;
|
|
89
|
-
|
|
90
|
-
|
|
104
|
+
: `git:${redactUrl(source.url)}${source.ref ? `#${source.ref}` : ''}`;
|
|
105
|
+
/**
|
|
106
|
+
* Inverse of sourceLabel for credentials-free sources; undefined when the
|
|
107
|
+
* stored label is not a label this version produced.
|
|
108
|
+
*/
|
|
109
|
+
const parseInstallSource = (label) => {
|
|
110
|
+
if (label.startsWith('npm:')) {
|
|
111
|
+
const spec = label.slice('npm:'.length);
|
|
112
|
+
const at = spec.lastIndexOf('@');
|
|
113
|
+
// A leading '@' is the scope prefix, not a version separator.
|
|
114
|
+
if (at <= 0) {
|
|
115
|
+
return { type: 'npm', package: spec };
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
type: 'npm',
|
|
119
|
+
package: spec.slice(0, at),
|
|
120
|
+
version: spec.slice(at + 1),
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
if (label.startsWith('git:')) {
|
|
124
|
+
const spec = label.slice('git:'.length);
|
|
125
|
+
const hash = spec.indexOf('#');
|
|
126
|
+
if (hash === -1) {
|
|
127
|
+
return { type: 'git', url: spec };
|
|
128
|
+
}
|
|
129
|
+
return { type: 'git', url: spec.slice(0, hash), ref: spec.slice(hash + 1) };
|
|
130
|
+
}
|
|
131
|
+
return undefined;
|
|
132
|
+
};
|
|
133
|
+
const sourceSecrets = (source) => {
|
|
134
|
+
if (source.type === 'npm') {
|
|
135
|
+
return source.auth ? [source.auth.token] : [];
|
|
136
|
+
}
|
|
137
|
+
if (!source.auth) {
|
|
138
|
+
return [];
|
|
139
|
+
}
|
|
140
|
+
if (source.auth.type === 'token') {
|
|
141
|
+
return [source.auth.token];
|
|
142
|
+
}
|
|
143
|
+
return [source.auth.privateKey];
|
|
144
|
+
};
|
|
145
|
+
const redactSecrets = (value, secrets) => secrets.reduce((redacted, secret) => redacted.split(secret).join('[REDACTED]'), value);
|
|
146
|
+
const trimCommandOutput = (value) => value.length <= MAX_COMMAND_OUTPUT
|
|
147
|
+
? value
|
|
148
|
+
: `${value.slice(0, MAX_COMMAND_OUTPUT)}\n… truncated`;
|
|
149
|
+
const commandFailure = (operation, result, secrets) => {
|
|
150
|
+
const output = [result.stderr, result.stdout]
|
|
151
|
+
.map((part) => part.trim())
|
|
152
|
+
.filter(Boolean)
|
|
153
|
+
.join('\n');
|
|
154
|
+
return redactSecrets(`${operation} failed: ${trimCommandOutput(output)}`, secrets).trim();
|
|
155
|
+
};
|
|
156
|
+
const peerDependencyNames = (pkgJson) => {
|
|
157
|
+
const peers = pkgJson.peerDependencies;
|
|
158
|
+
if (!peers || typeof peers !== 'object' || Array.isArray(peers)) {
|
|
159
|
+
return [];
|
|
160
|
+
}
|
|
161
|
+
return Object.keys(peers);
|
|
162
|
+
};
|
|
163
|
+
const resolveHostPackage = async (name) => {
|
|
164
|
+
let current = path.dirname(requireFromHost.resolve(name));
|
|
165
|
+
while (true) {
|
|
166
|
+
try {
|
|
167
|
+
const pkg = JSON.parse(await fs.readFile(path.join(current, 'package.json'), 'utf8'));
|
|
168
|
+
if (pkg.name === name) {
|
|
169
|
+
return current;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
catch {
|
|
173
|
+
// Keep walking toward the filesystem root.
|
|
174
|
+
}
|
|
175
|
+
const parent = path.dirname(current);
|
|
176
|
+
if (parent === current) {
|
|
177
|
+
throw new Error(`Could not locate package root for "${name}".`);
|
|
178
|
+
}
|
|
179
|
+
current = parent;
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
export const linkHostPeerDependencies = async (installDir, names) => {
|
|
183
|
+
for (const name of names) {
|
|
184
|
+
let resolved;
|
|
185
|
+
try {
|
|
186
|
+
resolved = await resolveHostPackage(name);
|
|
187
|
+
}
|
|
188
|
+
catch {
|
|
189
|
+
return {
|
|
190
|
+
success: false,
|
|
191
|
+
error: `Host does not provide peer dependency "${name}".`,
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
const destination = path.join(installDir, 'node_modules', name);
|
|
195
|
+
await fs.mkdir(path.dirname(destination), { recursive: true });
|
|
196
|
+
await fs.rm(destination, { recursive: true, force: true });
|
|
197
|
+
await fs.symlink(resolved, destination);
|
|
198
|
+
}
|
|
199
|
+
return { success: true };
|
|
200
|
+
};
|
|
201
|
+
const writeNpmConfig = async (tempDir, auth) => {
|
|
202
|
+
if (!auth) {
|
|
203
|
+
return {};
|
|
204
|
+
}
|
|
205
|
+
const registry = new URL(auth.registry ?? 'https://registry.npmjs.org/');
|
|
206
|
+
const registryUrl = registry.toString().replace(/\/?$/, '/');
|
|
207
|
+
const authPath = registry.pathname.replace(/\/?$/, '/');
|
|
208
|
+
const configPath = path.join(tempDir, '.npmrc');
|
|
209
|
+
await fs.writeFile(configPath, [
|
|
210
|
+
`registry=${registryUrl}`,
|
|
211
|
+
`//${registry.host}${authPath}:_authToken=${auth.token}`,
|
|
212
|
+
'always-auth=true',
|
|
213
|
+
'',
|
|
214
|
+
].join('\n'), { mode: 0o600 });
|
|
215
|
+
return { NPM_CONFIG_USERCONFIG: configPath };
|
|
216
|
+
};
|
|
217
|
+
const removeNpmConfig = (tempDir) => fs.rm(path.join(tempDir, '.npmrc'), { force: true });
|
|
218
|
+
const writeGitAuth = async (tempDir, auth) => {
|
|
219
|
+
if (!auth) {
|
|
220
|
+
return { GIT_TERMINAL_PROMPT: '0' };
|
|
221
|
+
}
|
|
222
|
+
if (auth.type === 'token') {
|
|
223
|
+
const askpassPath = path.join(tempDir, 'git-askpass');
|
|
224
|
+
await fs.writeFile(askpassPath, [
|
|
225
|
+
'#!/bin/sh',
|
|
226
|
+
'case "$1" in',
|
|
227
|
+
' *Username*) printf "%s\\n" "$OPENPEEPS_GIT_USERNAME" ;;',
|
|
228
|
+
' *) printf "%s\\n" "$OPENPEEPS_GIT_TOKEN" ;;',
|
|
229
|
+
'esac',
|
|
230
|
+
'',
|
|
231
|
+
].join('\n'), { mode: 0o700 });
|
|
232
|
+
return {
|
|
233
|
+
GIT_ASKPASS: askpassPath,
|
|
234
|
+
GIT_ASKPASS_REQUIRE: 'force',
|
|
235
|
+
GIT_TERMINAL_PROMPT: '0',
|
|
236
|
+
OPENPEEPS_GIT_TOKEN: auth.token,
|
|
237
|
+
OPENPEEPS_GIT_USERNAME: auth.username,
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
const keyPath = path.join(tempDir, 'git-deploy-key');
|
|
241
|
+
const knownHostsPath = path.join(tempDir, 'known_hosts');
|
|
242
|
+
await fs.writeFile(keyPath, `${auth.privateKey.trim()}\n`, { mode: 0o600 });
|
|
243
|
+
await fs.writeFile(knownHostsPath, '', { mode: 0o600 });
|
|
244
|
+
return {
|
|
245
|
+
GIT_SSH_COMMAND: [
|
|
246
|
+
'ssh',
|
|
247
|
+
'-F /dev/null',
|
|
248
|
+
`-i "${keyPath}"`,
|
|
249
|
+
'-o BatchMode=yes',
|
|
250
|
+
'-o IdentitiesOnly=yes',
|
|
251
|
+
'-o StrictHostKeyChecking=accept-new',
|
|
252
|
+
`-o UserKnownHostsFile="${knownHostsPath}"`,
|
|
253
|
+
].join(' '),
|
|
254
|
+
GIT_SSH_VARIANT: 'ssh',
|
|
255
|
+
GIT_TERMINAL_PROMPT: '0',
|
|
256
|
+
};
|
|
257
|
+
};
|
|
258
|
+
export const installPlugin = async (source, installedBy, options) => {
|
|
91
259
|
const pluginsDir = await getPluginDir();
|
|
92
260
|
const label = sourceLabel(source);
|
|
261
|
+
const secrets = sourceSecrets(source);
|
|
93
262
|
let tempDir;
|
|
94
263
|
try {
|
|
95
264
|
tempDir = await fs.mkdtemp(path.join(pluginsDir, '.install-'));
|
|
@@ -100,6 +269,7 @@ export const installPlugin = async (source, installedBy) => {
|
|
|
100
269
|
}
|
|
101
270
|
try {
|
|
102
271
|
let installDir;
|
|
272
|
+
let npmEnvironment;
|
|
103
273
|
if (source.type === 'npm') {
|
|
104
274
|
const pkg = source.version
|
|
105
275
|
? `${source.package}@${source.version}`
|
|
@@ -107,35 +277,46 @@ export const installPlugin = async (source, installedBy) => {
|
|
|
107
277
|
log.info(`Installing npm package ${pkg} to temp dir ${tempDir}`);
|
|
108
278
|
const init = await runCommand('npm', ['init', '-y'], tempDir);
|
|
109
279
|
if (init.exitCode !== 0) {
|
|
110
|
-
|
|
280
|
+
const error = commandFailure('npm init', init, secrets);
|
|
281
|
+
log.error(new Error(error), `npm init failed for ${label}.`);
|
|
282
|
+
return { success: false, error };
|
|
111
283
|
}
|
|
112
|
-
|
|
284
|
+
npmEnvironment = await writeNpmConfig(tempDir, source.auth);
|
|
285
|
+
const install = await runCommand('npm', ['install', '--omit=peer', pkg], tempDir, npmEnvironment);
|
|
113
286
|
if (install.exitCode !== 0) {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
};
|
|
287
|
+
const error = commandFailure('npm install', install, secrets);
|
|
288
|
+
log.error(new Error(error), `npm install failed for ${label}.`);
|
|
289
|
+
return { success: false, error };
|
|
118
290
|
}
|
|
119
291
|
installDir = path.join(tempDir, 'node_modules', source.package);
|
|
120
292
|
}
|
|
121
293
|
else {
|
|
122
|
-
|
|
294
|
+
const repositoryUrl = redactUrl(source.url);
|
|
295
|
+
log.info(`Cloning git repo ${repositoryUrl} to temp dir ${tempDir}`);
|
|
123
296
|
const cloneArgs = ['clone', '--depth', '1'];
|
|
124
297
|
if (source.ref) {
|
|
125
298
|
cloneArgs.push('--branch', source.ref);
|
|
126
299
|
}
|
|
127
|
-
cloneArgs.push(
|
|
128
|
-
const
|
|
300
|
+
cloneArgs.push(repositoryUrl, path.join(tempDir, 'repo'));
|
|
301
|
+
const gitEnvironment = await writeGitAuth(tempDir, source.auth);
|
|
302
|
+
const clone = await runCommand('git', cloneArgs, tempDir, gitEnvironment);
|
|
129
303
|
if (clone.exitCode !== 0) {
|
|
130
|
-
|
|
304
|
+
const error = commandFailure('git clone', clone, secrets);
|
|
305
|
+
log.error(new Error(error), `git clone failed for ${label}.`);
|
|
306
|
+
return { success: false, error };
|
|
131
307
|
}
|
|
132
308
|
installDir = path.join(tempDir, 'repo');
|
|
133
309
|
}
|
|
134
310
|
// Read package.json to determine namespace/name
|
|
135
311
|
const pkgJsonRaw = await fs.readFile(path.join(installDir, 'package.json'), 'utf-8');
|
|
136
312
|
const pkgJson = JSON.parse(pkgJsonRaw);
|
|
137
|
-
const namespace =
|
|
138
|
-
|
|
313
|
+
const namespace = pkgJson.openpeeps?.namespace ??
|
|
314
|
+
pkgJson.name?.split('/')[0]?.replace(/^@/, '') ??
|
|
315
|
+
'custom';
|
|
316
|
+
const name = pkgJson.openpeeps?.name ??
|
|
317
|
+
pkgJson.name?.split('/')[1] ??
|
|
318
|
+
pkgJson.name?.replace(/^@[^/]+\//, '') ??
|
|
319
|
+
'plugin';
|
|
139
320
|
const pluginKey = `${namespace}/${name}`;
|
|
140
321
|
const destDir = path.join(pluginsDir, namespace, name);
|
|
141
322
|
// Check if already exists in the plugins dir (built-in)
|
|
@@ -158,42 +339,59 @@ export const installPlugin = async (source, installedBy) => {
|
|
|
158
339
|
}
|
|
159
340
|
}
|
|
160
341
|
// Build the plugin if it has a build script
|
|
161
|
-
if (
|
|
342
|
+
if (pkgJson.scripts?.build) {
|
|
162
343
|
log.info(`Building plugin ${pluginKey}...`);
|
|
163
|
-
const buildDeps = await runCommand('npm', ['install'], installDir);
|
|
344
|
+
const buildDeps = await runCommand('npm', ['install', '--omit=peer'], installDir, npmEnvironment);
|
|
164
345
|
if (buildDeps.exitCode !== 0) {
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
346
|
+
const error = commandFailure('Dependency install', buildDeps, secrets);
|
|
347
|
+
log.error(new Error(error), `Plugin ${pluginKey} dependency install failed.`);
|
|
348
|
+
return { success: false, error };
|
|
349
|
+
}
|
|
350
|
+
const linked = await linkHostPeerDependencies(installDir, peerDependencyNames(pkgJson));
|
|
351
|
+
if (!linked.success) {
|
|
352
|
+
log.error(new Error(linked.error), `Plugin ${pluginKey} peer link failed.`);
|
|
353
|
+
return linked;
|
|
169
354
|
}
|
|
170
|
-
|
|
355
|
+
// The plugin temp dir sits under the host tree. A plugin script that
|
|
356
|
+
// invokes pnpm would otherwise adopt the host workspace.
|
|
357
|
+
await fs.writeFile(path.join(installDir, 'pnpm-workspace.yaml'), "packages:\n - '.'\n");
|
|
358
|
+
await removeNpmConfig(tempDir);
|
|
359
|
+
const build = await runCommand('npm', ['run', 'build'], installDir, {
|
|
360
|
+
CI: 'true',
|
|
361
|
+
});
|
|
171
362
|
if (build.exitCode !== 0) {
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
};
|
|
363
|
+
const error = commandFailure('Build', build, secrets);
|
|
364
|
+
log.error(new Error(error), `Plugin ${pluginKey} build failed.`);
|
|
365
|
+
return { success: false, error };
|
|
176
366
|
}
|
|
177
367
|
}
|
|
178
368
|
// Move to final location
|
|
369
|
+
await removeNpmConfig(tempDir);
|
|
179
370
|
await fs.mkdir(path.join(pluginsDir, namespace), { recursive: true });
|
|
180
371
|
await fs.rm(destDir, { recursive: true, force: true });
|
|
181
372
|
await fs.cp(installDir, destDir, { recursive: true });
|
|
182
|
-
await
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
373
|
+
await setInstalledPlugin(pluginKey, label, secrets.length > 0, installedBy);
|
|
374
|
+
if (options?.selfHeal) {
|
|
375
|
+
// Re-downloading an already-reviewed plugin keeps its enabled state.
|
|
376
|
+
log.info(`Self-healed plugin ${pluginKey} (re-downloaded from ${label}).`);
|
|
377
|
+
}
|
|
378
|
+
else {
|
|
379
|
+
// Installed plugins never auto-enable — an admin must explicitly
|
|
380
|
+
// activate them via the enable toggle (Phase B) after reviewing them.
|
|
381
|
+
await setPluginEnabledOverride(pluginKey, false);
|
|
382
|
+
log.info(`Plugin ${pluginKey} installed successfully from ${label}${installedBy ? ` by ${installedBy}` : ''}. Disabled by default — activate it explicitly to load it.`);
|
|
383
|
+
}
|
|
188
384
|
return { success: true, pluginKey };
|
|
189
385
|
}
|
|
190
386
|
catch (e) {
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
log.error(e, `Failed to install plugin from ${label}.`);
|
|
387
|
+
const rawMessage = e instanceof Error ? e.message : String(e);
|
|
388
|
+
const message = redactSecrets(rawMessage, secrets);
|
|
389
|
+
log.error(new Error(message), `Failed to install plugin from ${label}.`);
|
|
195
390
|
return { success: false, error: message };
|
|
196
391
|
}
|
|
392
|
+
finally {
|
|
393
|
+
await fs.rm(tempDir, { recursive: true, force: true }).catch(() => { });
|
|
394
|
+
}
|
|
197
395
|
};
|
|
198
396
|
export const uninstallPlugin = async (pluginKey, uninstalledBy) => {
|
|
199
397
|
const installed = await getInstalledPlugins();
|
|
@@ -217,6 +415,54 @@ export const uninstallPlugin = async (pluginKey, uninstalledBy) => {
|
|
|
217
415
|
return { success: false, error: message };
|
|
218
416
|
}
|
|
219
417
|
};
|
|
418
|
+
/**
|
|
419
|
+
* Re-downloads admin-installed plugins whose files are missing from the
|
|
420
|
+
* plugins directory (e.g. after a container recreate whose plugins path was
|
|
421
|
+
* not a persistent volume). Credentials are not stored, so plugins installed
|
|
422
|
+
* from private sources cannot be re-fetched — they are reported for manual
|
|
423
|
+
* re-install instead. Returns pluginKey -> reason for every plugin that is
|
|
424
|
+
* still unavailable after the attempt.
|
|
425
|
+
*/
|
|
426
|
+
export const selfHealInstalledPlugins = async () => {
|
|
427
|
+
const installed = await getInstalledPlugins();
|
|
428
|
+
const pluginsDir = await getPluginDir();
|
|
429
|
+
const failures = {};
|
|
430
|
+
for (const [pluginKey, record] of Object.entries(installed)) {
|
|
431
|
+
const healthy = await fs
|
|
432
|
+
.access(path.join(pluginsDir, pluginKey, 'package.json'))
|
|
433
|
+
.then(() => true)
|
|
434
|
+
.catch(() => false);
|
|
435
|
+
if (healthy)
|
|
436
|
+
continue;
|
|
437
|
+
if (record.requiresAuth) {
|
|
438
|
+
log.error(`Plugin ${pluginKey} files are missing and it was installed from a private source; re-install it with credentials.`);
|
|
439
|
+
failures[pluginKey] =
|
|
440
|
+
'Plugin files are missing. It was installed from a private source, which cannot be re-downloaded automatically — reinstall it with its credentials.';
|
|
441
|
+
continue;
|
|
442
|
+
}
|
|
443
|
+
const source = parseInstallSource(record.source);
|
|
444
|
+
if (!source) {
|
|
445
|
+
log.error(`Plugin ${pluginKey} files are missing and its recorded source "${record.source}" could not be parsed.`);
|
|
446
|
+
failures[pluginKey] =
|
|
447
|
+
'Plugin files are missing and its recorded install source is no longer recognizable — reinstall it.';
|
|
448
|
+
continue;
|
|
449
|
+
}
|
|
450
|
+
try {
|
|
451
|
+
const result = await installPlugin(source, record.installedBy, {
|
|
452
|
+
selfHeal: true,
|
|
453
|
+
});
|
|
454
|
+
if (!result.success) {
|
|
455
|
+
log.error(new Error(result.error ?? 'unknown error'), `Self-heal failed for plugin ${pluginKey}.`);
|
|
456
|
+
failures[pluginKey] = `Automatic re-download failed: ${result.error}`;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
catch (e) {
|
|
460
|
+
log.error(e, `Self-heal failed for plugin ${pluginKey}.`);
|
|
461
|
+
failures[pluginKey] = `Automatic re-download failed: ${e instanceof Error ? e.message : String(e)}`;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
return failures;
|
|
465
|
+
};
|
|
220
466
|
export const getInstalledPluginKeys = async () => {
|
|
221
467
|
const installed = await getInstalledPlugins();
|
|
222
468
|
return Object.keys(installed);
|
|
@@ -26,5 +26,5 @@ declare global {
|
|
|
26
26
|
* or verify whether the profile/account still exists. Plugin authors that need
|
|
27
27
|
* stronger guarantees or the handle should perform their own lookup.
|
|
28
28
|
*/
|
|
29
|
-
export declare const ensurePluginAuth: () => (req: Request, res: Response, next: NextFunction) => Promise<
|
|
29
|
+
export declare const ensurePluginAuth: () => (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
30
30
|
export {};
|
|
@@ -17,28 +17,31 @@ import { jwtUtil } from '../jwt/index.js';
|
|
|
17
17
|
* stronger guarantees or the handle should perform their own lookup.
|
|
18
18
|
*/
|
|
19
19
|
export const ensurePluginAuth = () => async (req, res, next) => {
|
|
20
|
-
|
|
20
|
+
// Express 5's RequestHandler type requires `void | Promise<void>` — never
|
|
21
|
+
// `return res...`, which types as `Promise<Response>` and fails overload
|
|
22
|
+
// resolution wherever this middleware is passed to router.post/get/etc.
|
|
21
23
|
const authHeader = req.headers.authorization;
|
|
22
24
|
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
|
23
|
-
|
|
25
|
+
res.status(401).json({ success: false, message: 'Unauthorized' });
|
|
26
|
+
return;
|
|
24
27
|
}
|
|
25
28
|
const token = authHeader.slice(7);
|
|
26
29
|
if (!token) {
|
|
27
|
-
|
|
30
|
+
res.status(401).json({ success: false, message: 'Unauthorized' });
|
|
31
|
+
return;
|
|
28
32
|
}
|
|
29
33
|
try {
|
|
30
34
|
const jwt = await jwtUtil();
|
|
31
35
|
const verified = await jwt.verify(token);
|
|
32
|
-
const payload = verified
|
|
33
|
-
if (!
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
.json({ success: false, message: 'Invalid token' });
|
|
36
|
+
const payload = verified?.payload;
|
|
37
|
+
if (!payload?.identities?.profile) {
|
|
38
|
+
res.status(401).json({ success: false, message: 'Invalid token' });
|
|
39
|
+
return;
|
|
37
40
|
}
|
|
38
41
|
req.pluginProfile = { id: payload.identities.profile };
|
|
39
42
|
next();
|
|
40
43
|
}
|
|
41
|
-
catch
|
|
42
|
-
|
|
44
|
+
catch {
|
|
45
|
+
res.status(401).json({ success: false, message: 'Unauthorized' });
|
|
43
46
|
}
|
|
44
47
|
};
|
package/dist/plugins/state.js
CHANGED
|
@@ -8,16 +8,15 @@ import { nowIso } from '../db/pg/mappers.js';
|
|
|
8
8
|
// one — this is a small, boot-time-read key/value document, not a relation.
|
|
9
9
|
const PLUGIN_STATE_KEY = 'openpeeps-plugin-state';
|
|
10
10
|
export const getPluginStateOverrides = () => allpeepDb().then(async ({ db }) => {
|
|
11
|
-
var _a, _b;
|
|
12
11
|
const rows = await db
|
|
13
12
|
.select()
|
|
14
13
|
.from(configs)
|
|
15
14
|
.where(eq(configs.key, PLUGIN_STATE_KEY))
|
|
16
15
|
.limit(1);
|
|
17
|
-
return
|
|
16
|
+
return rows[0]?.body ?? {};
|
|
18
17
|
});
|
|
19
18
|
export const setPluginEnabledOverride = (pluginKey, enabled) => getPluginStateOverrides().then((overrides) => {
|
|
20
|
-
const body =
|
|
19
|
+
const body = { ...overrides, [pluginKey]: enabled };
|
|
21
20
|
const ts = nowIso();
|
|
22
21
|
return allpeepDb().then(({ db }) => db
|
|
23
22
|
.insert(configs)
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { PgDb } from '../db/pg/client';
|
|
2
|
+
/**
|
|
3
|
+
* Walk outbound `reply_to` until there is no parent. A root post is itself.
|
|
4
|
+
*/
|
|
5
|
+
export declare const findReplyRootId: (db: PgDb, postId: string) => Promise<string>;
|
|
6
|
+
/** Bump conversation ordering without changing `created_at` or `updated_at`. */
|
|
7
|
+
export declare const touchPostActivity: (db: PgDb, postId: string) => Promise<void>;
|
|
8
|
+
/** Replies and reactions on any descendant bump the original/root post. */
|
|
9
|
+
export declare const bumpConversationActivity: (db: PgDb, postId: string) => Promise<void>;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { sql } from 'drizzle-orm';
|
|
2
|
+
/**
|
|
3
|
+
* Walk outbound `reply_to` until there is no parent. A root post is itself.
|
|
4
|
+
*/
|
|
5
|
+
export const findReplyRootId = async (db, postId) => {
|
|
6
|
+
const result = (await db.execute(sql `
|
|
7
|
+
WITH RECURSIVE walk AS (
|
|
8
|
+
SELECT ${postId}::text AS node_id, 0 AS depth
|
|
9
|
+
UNION ALL
|
|
10
|
+
SELECT rt.to_id, walk.depth + 1
|
|
11
|
+
FROM reply_to rt
|
|
12
|
+
INNER JOIN walk ON rt.from_id = walk.node_id
|
|
13
|
+
WHERE walk.depth < 64
|
|
14
|
+
)
|
|
15
|
+
SELECT node_id
|
|
16
|
+
FROM walk
|
|
17
|
+
ORDER BY depth DESC
|
|
18
|
+
LIMIT 1
|
|
19
|
+
`));
|
|
20
|
+
return result.rows[0]?.node_id ?? postId;
|
|
21
|
+
};
|
|
22
|
+
/** Bump conversation ordering without changing `created_at` or `updated_at`. */
|
|
23
|
+
export const touchPostActivity = async (db, postId) => {
|
|
24
|
+
await db.execute(sql `
|
|
25
|
+
UPDATE posts
|
|
26
|
+
SET last_activity_at = now()
|
|
27
|
+
WHERE id = ${postId}::uuid
|
|
28
|
+
`);
|
|
29
|
+
};
|
|
30
|
+
/** Replies and reactions on any descendant bump the original/root post. */
|
|
31
|
+
export const bumpConversationActivity = async (db, postId) => {
|
|
32
|
+
const rootId = await findReplyRootId(db, postId);
|
|
33
|
+
await touchPostActivity(db, rootId);
|
|
34
|
+
};
|
|
@@ -1,17 +1,9 @@
|
|
|
1
1
|
import type { AuthorizationData, DbPost, PostWithMeta } from '@openpeepshq/common/types';
|
|
2
|
-
import type { PgDb } from '../db/pg/client';
|
|
3
2
|
import type { Mapping } from '../db/pg/map';
|
|
4
|
-
export declare const CONTEXT_NODE_CAP = 100;
|
|
5
3
|
type ClosureDirection = 'ancestors' | 'descendents';
|
|
6
|
-
|
|
7
|
-
export declare const collectReplyClosureIds: (db: PgDb, startId: string, direction: ClosureDirection, { maxDepth, limit, newestFirst, }?: {
|
|
8
|
-
maxDepth?: number;
|
|
9
|
-
limit?: number;
|
|
10
|
-
newestFirst?: boolean;
|
|
11
|
-
}) => Promise<string[]>;
|
|
4
|
+
export { CONTEXT_NODE_CAP, collectReplyClosureIds } from './replyClosure';
|
|
12
5
|
/** Load and hydrate posts in a reply closure, then ACL-filter + transform. */
|
|
13
6
|
export declare const loadReplyContextPosts: (authData: AuthorizationData, startId: string, direction: ClosureDirection, mapping: Mapping<DbPost>, { maxDepth, limit, }?: {
|
|
14
7
|
maxDepth?: number;
|
|
15
8
|
limit?: number;
|
|
16
9
|
}) => Promise<PostWithMeta[]>;
|
|
17
|
-
export {};
|