@openpeepshq/core 0.1.31 → 0.1.32
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/analytics/clicks.d.ts +20 -0
- package/dist/analytics/clicks.js +194 -0
- package/dist/analytics/index.d.ts +1 -0
- package/dist/analytics/index.js +1 -0
- package/dist/analytics/metrics.d.ts +1 -0
- package/dist/analytics/metrics.js +5 -0
- package/dist/analytics/pdfReport.js +2 -1
- package/dist/analytics/read.js +51 -6
- package/dist/analytics/report.d.ts +31 -0
- package/dist/backups/index.js +7 -2
- package/dist/config/defaults/core.js +1 -0
- package/dist/config/index.d.ts +10 -0
- package/dist/db/pg/queries/derived.d.ts +5 -0
- package/dist/db/pg/queries/derived.js +21 -0
- package/dist/db/pg/schema/analytics.d.ts +76 -0
- package/dist/db/pg/schema/analytics.js +11 -1
- package/dist/db/pg/schema/edges.js +1 -1
- package/dist/db/pg/schema/index.d.ts +75 -0
- package/dist/db/pg/sql/0005_open_power_man.sql +9 -0
- package/dist/db/pg/sql/0006_fine_trish_tilby.sql +8 -0
- package/dist/db/pg/sql/meta/0004_snapshot.json +1 -1
- package/dist/db/pg/sql/meta/0005_snapshot.json +4179 -0
- package/dist/db/pg/sql/meta/0006_snapshot.json +4200 -0
- package/dist/db/pg/sql/meta/_journal.json +14 -0
- package/dist/db/replaceOrigin.d.ts +11 -0
- package/dist/db/replaceOrigin.js +110 -0
- package/dist/jams/helpers.d.ts +4 -4
- package/dist/jams/index.js +5 -1
- package/dist/jams/waitingRoom.d.ts +2 -0
- package/dist/jams/waitingRoom.js +13 -5
- package/dist/notifications/jobs.js +13 -2
- package/dist/plugins/helpers.d.ts +5 -2
- package/dist/plugins/helpers.js +21 -2
- package/dist/plugins/index.d.ts +1 -1
- package/dist/plugins/index.js +14 -10
- package/dist/posts/contextClosure.d.ts +2 -1
- package/dist/posts/contextClosure.js +6 -1
- package/dist/posts/conversationQueries.d.ts +23 -0
- package/dist/posts/conversationQueries.js +135 -0
- package/dist/posts/finders.d.ts +7 -5
- package/dist/posts/finders.js +24 -21
- package/dist/posts/helpers/index.d.ts +18 -18
- package/dist/posts/helpers/index.js +2 -2
- package/dist/posts/index.d.ts +1 -0
- package/dist/posts/index.js +1 -0
- package/dist/posts/mapping.d.ts +179 -0
- package/dist/posts/mapping.js +6 -6
- package/dist/posts/mutations.d.ts +3 -1
- package/dist/posts/mutations.js +27 -5
- package/dist/posts/unseenCounts.d.ts +10 -0
- package/dist/posts/unseenCounts.js +81 -0
- package/dist/profiles/cache.d.ts +2 -0
- package/dist/profiles/cache.js +19 -1
- package/dist/profiles/index.d.ts +1 -0
- package/dist/profiles/index.js +1 -0
- package/dist/profiles/mapping.d.ts +41 -0
- package/dist/profiles/mapping.js +24 -0
- package/dist/reports/helpers.js +2 -1
- package/dist/reports/mapping.js +6 -2
- package/dist/roles/mutations.d.ts +3 -3
- package/dist/roles/mutations.js +5 -3
- package/dist/server/index.d.ts +8 -0
- package/dist/session/index.d.ts +28 -0
- package/dist/session/index.js +79 -0
- package/dist/stats/finders.d.ts +220 -220
- package/dist/stats/mapping.d.ts +5 -5
- package/package.json +6 -6
- package/dist/db/replaceHostname.d.ts +0 -1
- package/dist/db/replaceHostname.js +0 -88
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { sql } from 'drizzle-orm';
|
|
2
|
+
/** Group unseen counts via anti-join (no post hydration). */
|
|
3
|
+
export const countUnseenGroupPosts = async (db, profileId, groupIds) => {
|
|
4
|
+
const counts = Object.fromEntries(groupIds.map((groupId) => [groupId, 0]));
|
|
5
|
+
if (groupIds.length === 0)
|
|
6
|
+
return counts;
|
|
7
|
+
const result = (await db.execute(sql `
|
|
8
|
+
SELECT pg.to_id AS group_id, count(*)::int AS cnt
|
|
9
|
+
FROM post_groups pg
|
|
10
|
+
INNER JOIN posts p
|
|
11
|
+
ON p.id::text = pg.from_id
|
|
12
|
+
AND p.deleted_at IS NULL
|
|
13
|
+
WHERE pg.to_id IN (${sql.join(groupIds.map((id) => sql `${id}`), sql `, `)})
|
|
14
|
+
AND p.visibility <> 'direct'
|
|
15
|
+
AND p.creator_id <> ${profileId}
|
|
16
|
+
AND NOT EXISTS (
|
|
17
|
+
SELECT 1
|
|
18
|
+
FROM post_seen ps
|
|
19
|
+
WHERE ps.from_id = ${profileId}
|
|
20
|
+
AND ps.to_id = p.id::text
|
|
21
|
+
)
|
|
22
|
+
GROUP BY pg.to_id
|
|
23
|
+
`));
|
|
24
|
+
for (const row of result.rows) {
|
|
25
|
+
const groupId = row.group_id;
|
|
26
|
+
if (groupId in counts) {
|
|
27
|
+
counts[groupId] = Number(row.cnt) || 0;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return counts;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Direct-message unread counts keyed by conversation root.
|
|
34
|
+
* Walks reply_to to roots without hydrating posts.
|
|
35
|
+
*/
|
|
36
|
+
export const countUnseenDirectPosts = async (db, profileId) => {
|
|
37
|
+
const result = (await db.execute(sql `
|
|
38
|
+
WITH RECURSIVE my_direct AS (
|
|
39
|
+
SELECT p.id::text AS post_id, p.creator_id
|
|
40
|
+
FROM audience a
|
|
41
|
+
INNER JOIN posts p
|
|
42
|
+
ON p.id::text = a.from_id
|
|
43
|
+
AND p.deleted_at IS NULL
|
|
44
|
+
WHERE a.to_id = ${profileId}
|
|
45
|
+
AND p.visibility = 'direct'
|
|
46
|
+
),
|
|
47
|
+
walk AS (
|
|
48
|
+
SELECT d.post_id, d.post_id AS node_id, 0 AS depth
|
|
49
|
+
FROM my_direct d
|
|
50
|
+
UNION ALL
|
|
51
|
+
SELECT walk.post_id, rt.to_id, walk.depth + 1
|
|
52
|
+
FROM reply_to rt
|
|
53
|
+
INNER JOIN walk ON rt.from_id = walk.node_id
|
|
54
|
+
),
|
|
55
|
+
roots AS (
|
|
56
|
+
SELECT DISTINCT ON (post_id) post_id, node_id AS root_id
|
|
57
|
+
FROM walk
|
|
58
|
+
ORDER BY post_id, depth DESC
|
|
59
|
+
)
|
|
60
|
+
SELECT roots.root_id, count(*)::int AS cnt
|
|
61
|
+
FROM my_direct d
|
|
62
|
+
INNER JOIN roots ON roots.post_id = d.post_id
|
|
63
|
+
WHERE d.creator_id <> ${profileId}
|
|
64
|
+
AND NOT EXISTS (
|
|
65
|
+
SELECT 1
|
|
66
|
+
FROM post_seen ps
|
|
67
|
+
WHERE ps.from_id = ${profileId}
|
|
68
|
+
AND ps.to_id = d.post_id
|
|
69
|
+
)
|
|
70
|
+
GROUP BY roots.root_id
|
|
71
|
+
HAVING count(*) > 0
|
|
72
|
+
`));
|
|
73
|
+
return Object.fromEntries(result.rows.map((row) => [row.root_id, Number(row.cnt) || 0]));
|
|
74
|
+
};
|
|
75
|
+
export const queryUnseenPostCounts = async (db, profileId, groupIds) => {
|
|
76
|
+
const [groups, direct] = await Promise.all([
|
|
77
|
+
countUnseenGroupPosts(db, profileId, groupIds),
|
|
78
|
+
countUnseenDirectPosts(db, profileId),
|
|
79
|
+
]);
|
|
80
|
+
return { groups, direct };
|
|
81
|
+
};
|
package/dist/profiles/cache.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { ProfileWithMeta } from '@openpeepshq/common/types';
|
|
2
2
|
export declare const profilesCache: import("cache-manager").Cache;
|
|
3
3
|
export declare const getProfile: (id: string, ignoreSoftDelete?: boolean) => Promise<ProfileWithMeta | undefined>;
|
|
4
|
+
/** Lean profile for post/feed hydrate — no followers/following trees. */
|
|
5
|
+
export declare const getPublicProfile: (id: string, ignoreSoftDelete?: boolean) => Promise<ProfileWithMeta | undefined>;
|
|
4
6
|
export declare const getProfileByHandle: (handle: string, ignoreSoftDelete?: boolean) => Promise<ProfileWithMeta | undefined>;
|
|
5
7
|
export declare const getProfiles: (ids: string[]) => Promise<ProfileWithMeta[]>;
|
package/dist/profiles/cache.js
CHANGED
|
@@ -1,13 +1,31 @@
|
|
|
1
1
|
import { createCache } from 'cache-manager';
|
|
2
2
|
import { allpeepDb } from '../db/index.js';
|
|
3
|
-
import { profilesMapping } from './mapping.js';
|
|
3
|
+
import { profilesMapping, publicProfilesMapping } from './mapping.js';
|
|
4
4
|
export const profilesCache = createCache({
|
|
5
5
|
ttl: 60 * 60 * 1000,
|
|
6
6
|
refreshThreshold: 60 * 1000,
|
|
7
7
|
});
|
|
8
|
+
const publicProfilesCache = createCache({
|
|
9
|
+
ttl: 60 * 60 * 1000,
|
|
10
|
+
refreshThreshold: 60 * 1000,
|
|
11
|
+
});
|
|
12
|
+
const withEmptyFollowGraphs = (profile) => {
|
|
13
|
+
var _a, _b, _c, _d;
|
|
14
|
+
return (Object.assign(Object.assign({}, profile), { roles: (_a = profile.roles) !== null && _a !== void 0 ? _a : [], followers: [], following: [], controllers: (_b = profile.controllers) !== null && _b !== void 0 ? _b : [], memberships: (_c = profile.memberships) !== null && _c !== void 0 ? _c : [], profileStats: (_d = profile.profileStats) !== null && _d !== void 0 ? _d : {
|
|
15
|
+
followersCount: 0,
|
|
16
|
+
followingCount: 0,
|
|
17
|
+
} }));
|
|
18
|
+
};
|
|
8
19
|
export const getProfile = async (id, ignoreSoftDelete = false) => profilesCache.wrap(id, () => allpeepDb().then(({ db }) => ignoreSoftDelete
|
|
9
20
|
? profilesMapping.ignoreSoftDelete().find(db, id)
|
|
10
21
|
: profilesMapping.find(db, id)));
|
|
22
|
+
/** Lean profile for post/feed hydrate — no followers/following trees. */
|
|
23
|
+
export const getPublicProfile = async (id, ignoreSoftDelete = false) => {
|
|
24
|
+
const profile = await publicProfilesCache.wrap(`${ignoreSoftDelete ? 'all:' : ''}${id}`, () => allpeepDb().then(({ db }) => ignoreSoftDelete
|
|
25
|
+
? publicProfilesMapping.ignoreSoftDelete().find(db, id)
|
|
26
|
+
: publicProfilesMapping.find(db, id)));
|
|
27
|
+
return profile ? withEmptyFollowGraphs(profile) : undefined;
|
|
28
|
+
};
|
|
11
29
|
export const getProfileByHandle = async (handle, ignoreSoftDelete = false) => profilesCache.wrap(handle, () => allpeepDb().then(({ db }) => ignoreSoftDelete
|
|
12
30
|
? profilesMapping
|
|
13
31
|
.ignoreSoftDelete()
|
package/dist/profiles/index.d.ts
CHANGED
package/dist/profiles/index.js
CHANGED
|
@@ -3,6 +3,7 @@ export * from './mapping.js';
|
|
|
3
3
|
export * from './mutations.js';
|
|
4
4
|
export * from './finders.js';
|
|
5
5
|
export * from './export.js';
|
|
6
|
+
export { getProfile, getPublicProfile, getProfiles } from './cache.js';
|
|
6
7
|
const sanitizeHandle = (name) => name.replace(/[^a-zA-Z0-9]/g, '-');
|
|
7
8
|
export const findNewFreeHandle = async (seed) => {
|
|
8
9
|
let counter = 2;
|
|
@@ -78,6 +78,47 @@ export declare const publicProfilesSearchMapping: import("../db/pg/map").Mapping
|
|
|
78
78
|
} | undefined;
|
|
79
79
|
} | undefined;
|
|
80
80
|
}, ProfileWithMeta, ProfileWithMeta>;
|
|
81
|
+
/**
|
|
82
|
+
* Lean public profile for feed/API hydrate — memberships + SQL follower
|
|
83
|
+
* counts, no followers/following trees.
|
|
84
|
+
*/
|
|
85
|
+
export declare const publicProfilesMapping: import("../db/pg/map").Mapping<{
|
|
86
|
+
handle: string;
|
|
87
|
+
type: "guest" | "local" | "federated";
|
|
88
|
+
deletedHandle?: string | undefined;
|
|
89
|
+
avatar?: string | null | undefined;
|
|
90
|
+
header?: string | null | undefined;
|
|
91
|
+
displayName?: string | undefined;
|
|
92
|
+
bio?: string | undefined;
|
|
93
|
+
timeZone?: string | undefined;
|
|
94
|
+
location?: {
|
|
95
|
+
text: string;
|
|
96
|
+
coordinates?: {
|
|
97
|
+
lat: number;
|
|
98
|
+
lng: number;
|
|
99
|
+
alt?: number | undefined;
|
|
100
|
+
} | undefined;
|
|
101
|
+
} | undefined;
|
|
102
|
+
locked?: boolean | undefined;
|
|
103
|
+
bot?: boolean | undefined;
|
|
104
|
+
discoverable?: boolean | undefined;
|
|
105
|
+
hideSocialGraph?: boolean | undefined;
|
|
106
|
+
indexable?: boolean | undefined;
|
|
107
|
+
fields?: {
|
|
108
|
+
name: string;
|
|
109
|
+
value: string;
|
|
110
|
+
}[] | undefined;
|
|
111
|
+
activityPub?: {
|
|
112
|
+
domain: string;
|
|
113
|
+
} | undefined;
|
|
114
|
+
guestData?: {
|
|
115
|
+
email?: string | undefined;
|
|
116
|
+
resource?: {
|
|
117
|
+
type: "profiles" | "groups" | "posts" | "notifications" | "reports" | "db" | "self" | "jams" | "*" | "jam" | "webhooks" | "analytics" | "render";
|
|
118
|
+
id?: string | undefined;
|
|
119
|
+
} | undefined;
|
|
120
|
+
} | undefined;
|
|
121
|
+
}, ProfileWithMeta, ProfileWithMeta>;
|
|
81
122
|
export declare const profilesMapping: import("../db/pg/map").Mapping<{
|
|
82
123
|
handle: string;
|
|
83
124
|
type: "guest" | "local" | "federated";
|
package/dist/profiles/mapping.js
CHANGED
|
@@ -85,6 +85,30 @@ export const publicProfilesSearchMapping = map({
|
|
|
85
85
|
},
|
|
86
86
|
],
|
|
87
87
|
});
|
|
88
|
+
/**
|
|
89
|
+
* Lean public profile for feed/API hydrate — memberships + SQL follower
|
|
90
|
+
* counts, no followers/following trees.
|
|
91
|
+
*/
|
|
92
|
+
export const publicProfilesMapping = map({
|
|
93
|
+
collection: 'profiles',
|
|
94
|
+
softDelete: true,
|
|
95
|
+
postFilterRelations: [
|
|
96
|
+
{
|
|
97
|
+
alias: 'memberships',
|
|
98
|
+
edgeCollection: 'userGroups',
|
|
99
|
+
vertexAlias: 'group',
|
|
100
|
+
direction: 'OUTBOUND',
|
|
101
|
+
cardinality: 'many',
|
|
102
|
+
mapping: groupsMapping.data(),
|
|
103
|
+
},
|
|
104
|
+
],
|
|
105
|
+
postFilterDerivedProperties: [
|
|
106
|
+
{
|
|
107
|
+
alias: 'profileStats',
|
|
108
|
+
resolve: profileDerived.profileStatsCounts,
|
|
109
|
+
},
|
|
110
|
+
],
|
|
111
|
+
});
|
|
88
112
|
export const profilesMapping = map({
|
|
89
113
|
collection: 'profiles',
|
|
90
114
|
postFilterRelations: profileRelations,
|
package/dist/reports/helpers.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { connector } from '../db/helpers.js';
|
|
2
2
|
import { collectionInfos } from '../db/index.js';
|
|
3
|
+
import { tombstoneProfileWithMetaIfDeleted } from '@openpeepshq/common/lib';
|
|
3
4
|
import { transformPost } from '../posts/index.js';
|
|
4
5
|
export const transformReport = async (report) => {
|
|
5
6
|
var _a;
|
|
6
7
|
const rawPosts = ((_a = report.reportedPosts) !== null && _a !== void 0 ? _a : []);
|
|
7
|
-
return Object.assign(Object.assign({}, report), { reportedPosts: await Promise.all(rawPosts.map((post) => transformPost(post))) });
|
|
8
|
+
return Object.assign(Object.assign({}, report), { reporterProfile: tombstoneProfileWithMetaIfDeleted(report.reporterProfile), reportedProfile: tombstoneProfileWithMetaIfDeleted(report.reportedProfile), reportedPosts: await Promise.all(rawPosts.map((post) => transformPost(post))) });
|
|
8
9
|
};
|
|
9
10
|
export const transformReports = async (reports) => Promise.all(reports.map(transformReport));
|
|
10
11
|
export const createdReportConnector = connector(collectionInfos.profilesCollection, collectionInfos.reportsCollection, collectionInfos.createdReportCollection);
|
package/dist/reports/mapping.js
CHANGED
|
@@ -2,13 +2,17 @@ import { collectionInfos } from '../db/index.js';
|
|
|
2
2
|
import { postsMapping } from '../posts/mapping.js';
|
|
3
3
|
import { profilesMapping } from '../profiles/mapping.js';
|
|
4
4
|
import { map } from '../db/pg/map/index.js';
|
|
5
|
+
// Keep reporter/reported profiles even after the account is soft-deleted so
|
|
6
|
+
// historical reports still resolve (and serialize) — `transformReport`
|
|
7
|
+
// tombstones the deleted profile. Without this the relation resolves to
|
|
8
|
+
// `undefined`, fails output validation, and blanks the whole moderation queue.
|
|
5
9
|
const reportRelations = [
|
|
6
10
|
{
|
|
7
11
|
alias: 'reporterProfile',
|
|
8
12
|
edgeCollection: collectionInfos.createdReportCollection.name,
|
|
9
13
|
direction: 'INBOUND',
|
|
10
14
|
cardinality: 'one',
|
|
11
|
-
mapping: profilesMapping.data(),
|
|
15
|
+
mapping: profilesMapping.ignoreSoftDelete().data(),
|
|
12
16
|
skipEdge: true,
|
|
13
17
|
},
|
|
14
18
|
{
|
|
@@ -16,7 +20,7 @@ const reportRelations = [
|
|
|
16
20
|
edgeCollection: collectionInfos.isReportedProfileCollection.name,
|
|
17
21
|
direction: 'OUTBOUND',
|
|
18
22
|
cardinality: 'one',
|
|
19
|
-
mapping: profilesMapping.data(),
|
|
23
|
+
mapping: profilesMapping.ignoreSoftDelete().data(),
|
|
20
24
|
skipEdge: true,
|
|
21
25
|
},
|
|
22
26
|
{
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { RoleData } from '@openpeepshq/common/types';
|
|
2
|
-
export declare const createRole: (roleData: RoleData) => Promise<
|
|
3
|
-
export declare const updateRole: (id: string, roleData: Partial<RoleData>) => Promise<
|
|
1
|
+
import { Role, RoleData } from '@openpeepshq/common/types';
|
|
2
|
+
export declare const createRole: (roleData: RoleData) => Promise<Role>;
|
|
3
|
+
export declare const updateRole: (id: string, roleData: Partial<RoleData>) => Promise<Role>;
|
|
4
4
|
export declare const setDefaultRoles: () => Promise<void>;
|
package/dist/roles/mutations.js
CHANGED
|
@@ -14,9 +14,11 @@ export const createRole = (roleData) => allpeepDb().then(async ({ db }) => {
|
|
|
14
14
|
}
|
|
15
15
|
return rolesMapping.create(db, roleData);
|
|
16
16
|
});
|
|
17
|
-
export const updateRole = (id, roleData) => allpeepDb()
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
export const updateRole = (id, roleData) => allpeepDb().then(async ({ db }) => {
|
|
18
|
+
const role = await rolesMapping.update(db, id, roleData);
|
|
19
|
+
await profilesCache.clear();
|
|
20
|
+
return role;
|
|
21
|
+
});
|
|
20
22
|
export const setDefaultRoles = async () => {
|
|
21
23
|
for (const role of defaultRoles) {
|
|
22
24
|
const existingRole = await findRoleByKey(role.key);
|
package/dist/server/index.d.ts
CHANGED
|
@@ -13,6 +13,10 @@ export declare const serverInfo: () => Promise<{
|
|
|
13
13
|
primaryHex: string;
|
|
14
14
|
light: {
|
|
15
15
|
primaryHex: string;
|
|
16
|
+
secondaryHex?: string | undefined;
|
|
17
|
+
fontFamily?: string | undefined;
|
|
18
|
+
buttonRadius?: string | undefined;
|
|
19
|
+
radius?: string | undefined;
|
|
16
20
|
logoSmall?: string | undefined;
|
|
17
21
|
defaultProfileAvatar?: string | undefined;
|
|
18
22
|
defaultGroupAvatar?: string | undefined;
|
|
@@ -21,6 +25,10 @@ export declare const serverInfo: () => Promise<{
|
|
|
21
25
|
};
|
|
22
26
|
dark: {
|
|
23
27
|
primaryHex: string;
|
|
28
|
+
secondaryHex?: string | undefined;
|
|
29
|
+
fontFamily?: string | undefined;
|
|
30
|
+
buttonRadius?: string | undefined;
|
|
31
|
+
radius?: string | undefined;
|
|
24
32
|
logoSmall?: string | undefined;
|
|
25
33
|
defaultProfileAvatar?: string | undefined;
|
|
26
34
|
defaultGroupAvatar?: string | undefined;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { SessionEvent, SessionPlatform } from '@openpeepshq/common/types';
|
|
2
|
+
export declare const publishSessionEvent: (profileId: string, event: SessionEvent) => Promise<void>;
|
|
3
|
+
export declare const subscribeToSessionEvents: (profileId: string, onEvent: (event: SessionEvent) => void | Promise<void>) => Promise<() => Promise<void>>;
|
|
4
|
+
export declare const registerSessionPresence: ({ profileId, connectionId, platform, }: {
|
|
5
|
+
profileId: string;
|
|
6
|
+
connectionId: string;
|
|
7
|
+
platform: SessionPlatform;
|
|
8
|
+
}) => Promise<void>;
|
|
9
|
+
export declare const refreshSessionPresence: (profileId: string, connectionId: string) => Promise<void>;
|
|
10
|
+
export declare const unregisterSessionPresence: (profileId: string, connectionId: string) => Promise<void>;
|
|
11
|
+
export declare const getSessionPresence: (profileId: string) => Promise<{
|
|
12
|
+
profileId: string;
|
|
13
|
+
connections: {
|
|
14
|
+
platform: SessionPlatform;
|
|
15
|
+
connectedAt: number;
|
|
16
|
+
connectionId: string;
|
|
17
|
+
}[];
|
|
18
|
+
platforms: ("ios" | "android" | "web")[];
|
|
19
|
+
} | undefined>;
|
|
20
|
+
export declare const listOnlineSessionPresence: () => Promise<{
|
|
21
|
+
profileId: string;
|
|
22
|
+
connections: {
|
|
23
|
+
platform: SessionPlatform;
|
|
24
|
+
connectedAt: number;
|
|
25
|
+
connectionId: string;
|
|
26
|
+
}[];
|
|
27
|
+
platforms: ("ios" | "android" | "web")[];
|
|
28
|
+
}[]>;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { disconnect, getConnection, getSharedConnection, } from '../redis/connection.js';
|
|
2
|
+
import { logger } from '../log/index.js';
|
|
3
|
+
const log = logger('core:session');
|
|
4
|
+
const eventsChannel = (profileId) => `session:events:profile:${profileId}`;
|
|
5
|
+
const presenceOnlineKey = 'session:presence:online';
|
|
6
|
+
const presenceProfileKey = (profileId) => `session:presence:profile:${profileId}`;
|
|
7
|
+
const PRESENCE_TTL_SEC = 90;
|
|
8
|
+
export const publishSessionEvent = async (profileId, event) => {
|
|
9
|
+
const conn = await getSharedConnection();
|
|
10
|
+
await conn.publish(eventsChannel(profileId), JSON.stringify(event));
|
|
11
|
+
};
|
|
12
|
+
export const subscribeToSessionEvents = async (profileId, onEvent) => {
|
|
13
|
+
const conn = await getConnection();
|
|
14
|
+
await conn.subscribe(eventsChannel(profileId), (message) => {
|
|
15
|
+
try {
|
|
16
|
+
void onEvent(JSON.parse(message));
|
|
17
|
+
}
|
|
18
|
+
catch (error) {
|
|
19
|
+
log.error('Failed to parse session event', error);
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
return () => disconnect(conn);
|
|
23
|
+
};
|
|
24
|
+
export const registerSessionPresence = async ({ profileId, connectionId, platform, }) => {
|
|
25
|
+
const conn = await getSharedConnection();
|
|
26
|
+
const key = presenceProfileKey(profileId);
|
|
27
|
+
await conn.hSet(key, connectionId, JSON.stringify({ platform, connectedAt: Date.now() }));
|
|
28
|
+
await conn.expire(key, PRESENCE_TTL_SEC);
|
|
29
|
+
await conn.sAdd(presenceOnlineKey, profileId);
|
|
30
|
+
};
|
|
31
|
+
export const refreshSessionPresence = async (profileId, connectionId) => {
|
|
32
|
+
const conn = await getSharedConnection();
|
|
33
|
+
const key = presenceProfileKey(profileId);
|
|
34
|
+
if (!(await conn.hExists(key, connectionId)))
|
|
35
|
+
return;
|
|
36
|
+
await conn.expire(key, PRESENCE_TTL_SEC);
|
|
37
|
+
};
|
|
38
|
+
export const unregisterSessionPresence = async (profileId, connectionId) => {
|
|
39
|
+
const conn = await getSharedConnection();
|
|
40
|
+
const key = presenceProfileKey(profileId);
|
|
41
|
+
await conn.hDel(key, connectionId);
|
|
42
|
+
if ((await conn.hLen(key)) === 0) {
|
|
43
|
+
await conn.del(key);
|
|
44
|
+
await conn.sRem(presenceOnlineKey, profileId);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
export const getSessionPresence = async (profileId) => {
|
|
48
|
+
const conn = await getSharedConnection();
|
|
49
|
+
const raw = await conn.hGetAll(presenceProfileKey(profileId));
|
|
50
|
+
const connections = Object.entries(raw).flatMap(([connectionId, value]) => {
|
|
51
|
+
try {
|
|
52
|
+
const parsed = JSON.parse(value);
|
|
53
|
+
return [Object.assign({ connectionId }, parsed)];
|
|
54
|
+
}
|
|
55
|
+
catch (_a) {
|
|
56
|
+
return [];
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
if (connections.length === 0)
|
|
60
|
+
return undefined;
|
|
61
|
+
return {
|
|
62
|
+
profileId,
|
|
63
|
+
connections,
|
|
64
|
+
platforms: [...new Set(connections.map((c) => c.platform))],
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
export const listOnlineSessionPresence = async () => {
|
|
68
|
+
const conn = await getSharedConnection();
|
|
69
|
+
const profileIds = await conn.sMembers(presenceOnlineKey);
|
|
70
|
+
const profiles = [];
|
|
71
|
+
for (const profileId of profileIds) {
|
|
72
|
+
const presence = await getSessionPresence(profileId);
|
|
73
|
+
if (presence)
|
|
74
|
+
profiles.push(presence);
|
|
75
|
+
else
|
|
76
|
+
await conn.sRem(presenceOnlineKey, profileId);
|
|
77
|
+
}
|
|
78
|
+
return profiles;
|
|
79
|
+
};
|