@openpeepshq/core 0.4.1 → 0.4.11

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.
@@ -30,7 +30,7 @@ export const hasSeen = edgeTable('has_seen');
30
30
  export const hasRead = edgeTable('has_read');
31
31
  export const userGroups = edgeTable('user_groups');
32
32
  export const postGroups = edgeTable('post_groups');
33
- export const hasRole = edgeTable('has_role');
33
+ export const hasRole = edgeTable('has_role', true);
34
34
  export const profileAccessTokens = edgeTable('profile_access_tokens', true);
35
35
  export const accountToPushSubscription = edgeTable('account_to_push_subscription');
36
36
  export const createdReport = edgeTable('created_report');
@@ -0,0 +1,8 @@
1
+ -- Deduplicate existing has_role edges before constraining: keep the oldest
2
+ -- edge per (profile, role), drop the rest. uuidv7 ids are time-ordered.
3
+ DELETE FROM "has_role" a USING "has_role" b
4
+ WHERE a."from_id" = b."from_id"
5
+ AND a."to_id" = b."to_id"
6
+ AND a."id" > b."id";
7
+ --> statement-breakpoint
8
+ CREATE UNIQUE INDEX "has_role_from_to_unique" ON "has_role" USING btree ("from_id","to_id");
@@ -1,6 +1,6 @@
1
1
  {
2
- "id": "dfefc4cf-8580-4232-bcc9-fe7da89adb2e",
3
- "prevId": "3d4c31b7-52a6-4d1b-8074-351a9d74dd4f",
2
+ "id": "cf3c93e7-d862-445f-b74b-6da4dfe23c66",
3
+ "prevId": "dfefc4cf-8580-4232-bcc9-fe7da89adb2e",
4
4
  "version": "7",
5
5
  "dialect": "postgresql",
6
6
  "tables": {
@@ -2378,6 +2378,27 @@
2378
2378
  "concurrently": false,
2379
2379
  "method": "btree",
2380
2380
  "with": {}
2381
+ },
2382
+ "has_role_from_to_unique": {
2383
+ "name": "has_role_from_to_unique",
2384
+ "columns": [
2385
+ {
2386
+ "expression": "from_id",
2387
+ "isExpression": false,
2388
+ "asc": true,
2389
+ "nulls": "last"
2390
+ },
2391
+ {
2392
+ "expression": "to_id",
2393
+ "isExpression": false,
2394
+ "asc": true,
2395
+ "nulls": "last"
2396
+ }
2397
+ ],
2398
+ "isUnique": true,
2399
+ "concurrently": false,
2400
+ "method": "btree",
2401
+ "with": {}
2381
2402
  }
2382
2403
  },
2383
2404
  "foreignKeys": {},
@@ -120,6 +120,13 @@
120
120
  "when": 1791620000000,
121
121
  "tag": "0016_posts_search_vector_title_tags_path",
122
122
  "breakpoints": true
123
+ },
124
+ {
125
+ "idx": 17,
126
+ "version": "7",
127
+ "when": 1794031928690,
128
+ "tag": "0017_unique_has_role_edges",
129
+ "breakpoints": true
123
130
  }
124
131
  ]
125
132
  }
@@ -1,6 +1,6 @@
1
1
  import { notificationAll, } from '@openpeepshq/common/types';
2
2
  import { maybeCreateNotification } from '@openpeepshq/core/notifications';
3
- import { getProfileAvatar, profileName } from '@openpeepshq/common/lib';
3
+ import { getProfileAvatar, isConversationLeavePost, profileName, } from '@openpeepshq/common/lib';
4
4
  import { getConversationByEnd } from '@openpeepshq/core/posts';
5
5
  import { communityConfig } from '../../../config/index.js';
6
6
  import { PUSH_INVALIDATE } from '../../pushInvalidation.js';
@@ -9,6 +9,10 @@ const eventHandler = async (data) => {
9
9
  if (post.data?.type === 'event')
10
10
  return;
11
11
  if (post.visibility === 'direct' && post.audience) {
12
+ // A leave post (isConversationLeavePost) is a system event, not a
13
+ // message, so it must not notify the remaining members.
14
+ if (isConversationLeavePost(post))
15
+ return;
12
16
  for (const recipientProfile of post.audience) {
13
17
  if (post.profile.id !== recipientProfile.id) {
14
18
  await maybeCreateNotification(recipientProfile, {
@@ -526,11 +526,12 @@ export declare const rsvpManageByOrganizer: (actingProfile: Profile, targetProfi
526
526
  * participants as audience. The leaving user is excluded so future
527
527
  * replies (which inherit the audience) won't notify them, and the thread
528
528
  * disappears from their inbox.
529
- * - MessageInThread detects the leave via the audience diff: the leaving
530
- * user was in the previous post's audience but not in the left-post's.
531
- * - The conversation post-creation endpoint detects leavers (author not in
532
- * their own post's audience) to block further posting and exclude them
533
- * from future replies' audience.
529
+ * - The "left" post is recognized everywhere via isConversationLeavePost
530
+ * (common): a reply that leaves the author out of the audience. Thread
531
+ * and conversation-list renderers show the "left" label, the
532
+ * directMessage notification handler skips it, and the conversation
533
+ * post-creation endpoint uses it to block leavers and exclude them from
534
+ * future replies' audience.
534
535
  * - A 1:1 conversation can span several roots, so each root's thread gets
535
536
  * its own "left" post — otherwise the leaver's inbox would keep the
536
537
  * threads whose newest post is still in their audience.
@@ -513,11 +513,12 @@ const organizerPreviousResponse = (post, profileId, recurrenceId) => [...(post.r
513
513
  * participants as audience. The leaving user is excluded so future
514
514
  * replies (which inherit the audience) won't notify them, and the thread
515
515
  * disappears from their inbox.
516
- * - MessageInThread detects the leave via the audience diff: the leaving
517
- * user was in the previous post's audience but not in the left-post's.
518
- * - The conversation post-creation endpoint detects leavers (author not in
519
- * their own post's audience) to block further posting and exclude them
520
- * from future replies' audience.
516
+ * - The "left" post is recognized everywhere via isConversationLeavePost
517
+ * (common): a reply that leaves the author out of the audience. Thread
518
+ * and conversation-list renderers show the "left" label, the
519
+ * directMessage notification handler skips it, and the conversation
520
+ * post-creation endpoint uses it to block leavers and exclude them from
521
+ * future replies' audience.
521
522
  * - A 1:1 conversation can span several roots, so each root's thread gets
522
523
  * its own "left" post — otherwise the leaver's inbox would keep the
523
524
  * threads whose newest post is still in their audience.
@@ -1,5 +1,6 @@
1
1
  import { Profile, ProfileWithMeta, Role } from '@openpeepshq/common/types';
2
- export declare const followConnector: (db: import("../db").PgDb, from: Profile, to: Profile, data?: {
2
+ import type { PgDb } from '../db/pg/client';
3
+ export declare const followConnector: (db: PgDb, from: Profile, to: Profile, data?: {
3
4
  uri?: string | undefined;
4
5
  reblogs?: boolean | undefined;
5
6
  notify?: boolean | undefined;
@@ -15,17 +16,12 @@ export declare const followConnector: (db: import("../db").PgDb, from: Profile,
15
16
  _from: string;
16
17
  _to: string;
17
18
  }>>;
18
- export declare const followDisconnector: (db: import("../db").PgDb, from: Profile, to: Profile) => Promise<void>;
19
- export declare const followFinder: (db: import("../db").PgDb, from: Profile, to: Profile) => Promise<Record<string, unknown> | undefined>;
19
+ export declare const followDisconnector: (db: PgDb, from: Profile, to: Profile) => Promise<void>;
20
+ export declare const followFinder: (db: PgDb, from: Profile, to: Profile) => Promise<Record<string, unknown> | undefined>;
20
21
  export declare const expandProfiles: (profiles: Profile[]) => Promise<ProfileWithMeta[]>;
21
- export declare const roleAssigner: (db: import("../db").PgDb, from: Profile, to: Role, data?: object & {
22
- id?: string;
23
- }) => Promise<import("../db/pg/map").WithId<object & {
24
- _from: string;
25
- _to: string;
26
- }>>;
27
- export declare const roleUnassigner: (db: import("../db").PgDb, from: Profile, to: Role) => Promise<void>;
28
- export declare const giveControl: (db: import("../db").PgDb, from: {
22
+ export declare const roleAssigner: (db: PgDb, profile: Profile, role: Role) => Promise<void>;
23
+ export declare const roleUnassigner: (db: PgDb, from: Profile, to: Role) => Promise<void>;
24
+ export declare const giveControl: (db: PgDb, from: {
29
25
  id: string;
30
26
  }, to: {
31
27
  id: string;
@@ -1,10 +1,30 @@
1
+ import { uuidv7 } from 'uuidv7';
1
2
  import { connectionFinder, connector, disconnector } from '../db/helpers.js';
2
3
  import { collectionInfos } from '../db/index.js';
4
+ import { nowIso } from '../db/pg/mappers.js';
5
+ import { hasRole } from '../db/pg/schema/index.js';
3
6
  import { getProfiles } from './cache.js';
4
7
  export const followConnector = connector(collectionInfos.profilesCollection, collectionInfos.profilesCollection, collectionInfos.followsCollection);
5
8
  export const followDisconnector = disconnector(collectionInfos.profilesCollection, collectionInfos.profilesCollection, collectionInfos.followsCollection);
6
9
  export const followFinder = connectionFinder(collectionInfos.profilesCollection, collectionInfos.profilesCollection, collectionInfos.followsCollection);
7
10
  export const expandProfiles = (profiles) => getProfiles(profiles.map((p) => p.id));
8
- export const roleAssigner = connector(collectionInfos.profilesCollection, collectionInfos.rolesCollection, collectionInfos.hasRoleCollection);
11
+ // Idempotent and race-safe: a role the profile already holds is a no-op, and
12
+ // concurrent assigns (parallel first logins, login racing email validation)
13
+ // insert against has_role_from_to_unique instead of one failing with a
14
+ // unique violation.
15
+ export const roleAssigner = async (db, profile, role) => {
16
+ const ts = nowIso();
17
+ await db
18
+ .insert(hasRole)
19
+ .values({
20
+ id: uuidv7(),
21
+ fromId: profile.id,
22
+ toId: role.id,
23
+ body: {},
24
+ createdAt: ts,
25
+ updatedAt: ts,
26
+ })
27
+ .onConflictDoNothing();
28
+ };
9
29
  export const roleUnassigner = disconnector(collectionInfos.profilesCollection, collectionInfos.rolesCollection, collectionInfos.hasRoleCollection);
10
30
  export const giveControl = connector(collectionInfos.accountsCollection, collectionInfos.profilesCollection, collectionInfos.controlsCollection);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openpeepshq/core",
3
- "version": "0.4.1",
3
+ "version": "0.4.11",
4
4
  "type": "module",
5
5
  "license": "GPL-3.0-or-later",
6
6
  "dependencies": {
@@ -37,10 +37,10 @@
37
37
  "type-fest": "^4.31.0",
38
38
  "uuidv7": "^1.0.2",
39
39
  "web-push": "^3.6.7",
40
- "@openpeepshq/common": "0.5.0",
41
- "@openpeepshq/fetch-client": "^0.2.1",
40
+ "@openpeepshq/common": "0.5.12",
42
41
  "@openpeepshq/geocoder": "^0.1.2",
43
- "@openpeepshq/i18n": "^0.4.1"
42
+ "@openpeepshq/fetch-client": "^0.2.1",
43
+ "@openpeepshq/i18n": "^0.4.11"
44
44
  },
45
45
  "main": "dist/index.js",
46
46
  "types": "dist/index.d.ts",