Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ See [STATUS.md](server/STATUS.md) to learn more about which features will remain

## UNRELEASED

- A commit is also sent to WebSocket subscribers of the resource's parent. A
resource shared out of a drive the recipient can't read (a chatroom) can only
be subscribed to on its own, and a new child is a new subject, so its new
messages reached neither that subscription nor a drive subscription the
recipient could hold. Read rights are checked per subscriber against the
resource when it is sent, and a connection already reached through the
resource or its drive doesn't get it twice.

- New default ontology `notifications` (`lib/defaults/notifications.json`):
the `Inbox` and `Notification` classes, and an `inbox` property the private
drive points to its Inbox with.
Expand Down
21 changes: 21 additions & 0 deletions browser/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,27 @@ This changelog covers all five packages, as they are (for now) updated as a whol

## UNRELEASED

- Someone given a chat, or anything else, out of a drive they can't open sees
all of it. A list answered from the local database was trusted whenever it
was non-empty, but only a drive synced to this device has its whole list
there: the guest's own messages and the ones present when they joined were
shown as the entire chat, and a reload asked the same database again. For a
drive this device hasn't synced the server is now asked once per list per
session, and anything only the local database knows (a write not yet
confirmed) is kept.
- Things shared with you out of someone else's drive update live and notify
you. The app only listened to the drive you had open, so a guest in a shared
chat never received new messages from the host, and got no notifications for
them. Everything in "Shared with me" is now subscribed for as long as the app
runs (`Store.subscribeLive`).
- Presence works in things shared out of someone else's drive. Presence was a
channel per drive, which a guest given one chat can't read: the server
refused them, so host and guest never saw each other there, nor who was
typing. A view now also announces in the channel of the shared resource it
is in (`Store.presenceScope`: the nearest resource with its own `read`
list), and leaves the drive channel alone when it can't read the drive. One
tab is one session across channels (`Store.presenceSessionId`), so nobody
shows up twice.
- Notifications are less noisy. The Notifications page shows one row per
conversation ("Sanne and Polle: 3 new messages in Team chat") with names and
titles as they are now, in your language. Coming back to the window while
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
recordNotification,
} from '../../helpers/notifications/inbox';
import { usePrivateDrive } from '../../hooks/usePrivateDrive';
import { usePrivateDriveList } from '../../hooks/usePrivateDriveList';

const TEXT_MAX = 140;

Expand Down Expand Up @@ -101,6 +102,7 @@ export function MessageNotifier(): null {
const { activePanel, setPanelOpen } = useRightPanel();
const navigate = useNavigateWithTransition();
const { privateDrive } = usePrivateDrive();
const [sharedWithMe] = usePrivateDriveList(core.properties.sharedWithMe);

const handled = useRef(new Set<string>());
// Announced while away, with system notifications not decided yet.
Expand Down Expand Up @@ -314,6 +316,18 @@ export function MessageNotifier(): null {
showOsNotification({ title, body, tag: subject, onClick: () => open(n) });
});

// Things shared with you out of drives you can't open are only delivered to
// a subscription on the thing itself; the open drive's subscription never
// covers them. Hold one for as long as the app runs, not only while the
// sidebar happens to show the item, or a shared chat stays silent.
useEffect(() => {
const unsubscribers = sharedWithMe.map(subject =>
store.subscribeLive(subject),
);

return () => unsubscribers.forEach(unsubscribe => unsubscribe());
}, [store, sharedWithMe]);

useEffect(
() =>
store.on(StoreEvents.ResourceUpdated, resource => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
useCurrentAgent,
useDrive,
useDrivePresence,
useDrivePresenceManager,
useStore,
type PresenceItem,
type Store,
Expand Down Expand Up @@ -153,7 +154,8 @@ export function FollowProvider({
* reload ends leadership; the banner hides once presence expires). */
const [activeMeeting, setActiveMeeting] = useState<string>();

const manager = drive ? store.getPresence(drive) : undefined;
// Not the drive's channel when this agent can't read the drive.
const manager = useDrivePresenceManager();

// While leading a meeting, the presence session points at it — joiners
// read it and open the meeting chat. (Following without a meeting shares
Expand Down
85 changes: 85 additions & 0 deletions browser/lib/src/collection-partial-local.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { describe, expect, it, vi } from 'vitest';
import { CollectionBuilder } from './collectionBuilder.js';
import { Store } from './store.js';
import { core } from './ontologies/core.js';
import { collections } from './ontologies/collections.js';
import type { ClientDbWorker } from './client-db.js';

/**
* A non-empty local answer is only the whole answer for a drive this client
* has synced. A guest in a chatroom shared from someone else's drive never
* syncs that drive: their local database holds their own messages and the
* ones that were there when they joined. Trusting it hid every later message
* from the host, and a reload asked the same local database again.
*/
const HOME = 'https://app.example.com';
const OWN_DRIVE = 'did:ad:own-drive';
const HOST_DRIVE = 'did:ad:host-drive';
const ROOM = 'did:ad:room';
const MINE = 'did:ad:my-message';
const THEIRS = 'did:ad:their-message';

function setup() {
const store = new Store({ serverUrl: HOME, connect: false });
store.setClientDb({
isReady: true,
waitForReady: async () => true,
query: vi.fn(async () => ({ subjects: [MINE], count: 1 })),
} as unknown as ClientDbWorker);
const requests: URL[] = [];
store.injectFetch(async input => {
const url = new URL(String(input));
requests.push(url);

return new Response(
JSON.stringify({
'@id': url.href,
[collections.properties.members]: [MINE, THEIRS],
[collections.properties.totalMembers]: 2,
}),
{ status: 200 },
);
});
store.setDrive(OWN_DRIVE);
store.finishDriveSync(OWN_DRIVE, 1, Date.now());
store.setServerConnected(true);

return { store, requests };
}

function messagesIn(store: Store, drive: string) {
return new CollectionBuilder(store)
.setProperty(core.properties.parent)
.setValue(ROOM)
.setDrive(drive)
.buildAndFetch();
}

describe('a non-empty local result for a drive this client never synced', () => {
it('asks the server for the rest', async () => {
const { store, requests } = setup();
const collection = await messagesIn(store, HOST_DRIVE);

expect(requests.length).toBeGreaterThan(0);
expect(collection.totalMembers).toBe(2);
expect(await collection.getMemberWithIndex(1)).toBe(THEIRS);
});

it('asks once per query, not on every mount', async () => {
const { store, requests } = setup();
await messagesIn(store, HOST_DRIVE);
const asked = requests.length;
await messagesIn(store, HOST_DRIVE);

expect(asked).toBeGreaterThan(0);
expect(requests.length).toBe(asked);
});

it('still trusts the local database for a synced drive', async () => {
const { store, requests } = setup();
const collection = await messagesIn(store, OWN_DRIVE);

expect(requests).toEqual([]);
expect(collection.totalMembers).toBe(1);
});
});
29 changes: 25 additions & 4 deletions browser/lib/src/collection-sort-order.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,31 @@ describe('Collection: local surplus is not "stale"', () => {
await collection.waitForReady();
await new Promise(r => setTimeout(r, 30));

// The drive isn't synced here, so the server is asked (see
// collection-partial-local.test.ts); its smaller answer must not drop
// the row only the local index has.
expect(urls.length).toBeGreaterThan(0);
expect(collection.totalMembers).toBe(local.length);
const fullFetches = urls.filter(
u => new URL(u).searchParams.get('page_size') !== '1',
);
expect(fullFetches).toEqual([]);
const members: Array<string | undefined> = [];

for (let i = 0; i < collection.totalMembers; i++) {
members.push(await collection.getMemberWithIndex(i));
}

expect(members).toContain('atomic:pending');
});

it('does not ask the server at all once the drive has synced', async ({
expect,
}) => {
const { store, urls } = storeWithServer(['atomic:row1']);
store.setClientDb(stubClientDb(['atomic:row1', 'atomic:row2']));
store.finishDriveSync(DRIVE, 2, Date.now());

const collection = new Collection(store, 'https://example.com', params());
await collection.waitForReady();

expect(urls).toEqual([]);
expect(collection.totalMembers).toBe(2);
});
});
57 changes: 57 additions & 0 deletions browser/lib/src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@ export class Collection {
*/
private _assemblingPage = false;
private static legacyQueryServers = new WeakMap<Store, Set<string>>();
/** Pages of unsynced drives the server has answered this session. Its
* members were written to the local DB as they loaded and live updates keep
* it current, so the local answer is whole from then on. */
private static completedByServer = new WeakMap<Store, Set<string>>();
private legacyQuery = false;
private legacyMembers?: Promise<string[]>;

Expand Down Expand Up @@ -945,6 +949,24 @@ export class Collection {
// here. The cold-load case pays at most one extra OPFS wait
// before the server fetch fires.
if (hasClientDb && (await this.fetchPageFromLocalDb(page)) === 'ok') {
// Only a drive synced into this client has its whole membership here.
// Anything else holds what this client happened to fetch or create: a
// guest in a chatroom shared from someone else's drive had their own
// messages and the ones present when they joined, and trusting that
// hid every later message from the host, on every reload. Asked once
// per page per session, not on every mount.
if (
this.store.serverConnected &&
!this.store.hasCompletedDriveSyncFor(
this.params.drive ?? this.store.getDrive(),
) &&
!Collection.completedByServer
.get(this.store)
?.has(this.buildSubject(page))
) {
await this.completeFromServer(page);
}

return;
}

Expand Down Expand Up @@ -984,6 +1006,41 @@ export class Collection {
}
}

/**
* Replace a page the local DB answered with the server's, keeping any member
* only the local DB knows: a write the server hasn't acknowledged yet must
* not vanish because the server answered first. When the server can't be
* reached, the local page stands.
*/
private async completeFromServer(page: number): Promise<void> {
const local = this.pages.get(page)?.props.members ?? [];

try {
await this.fetchPageFromServer(page);
} catch {
return;
}

const answered =
Collection.completedByServer.get(this.store) ?? new Set<string>();
answered.add(this.buildSubject(page));
Collection.completedByServer.set(this.store, answered);

const fetched = this.pages.get(page);

if (!fetched) return;

const members = fetched.props.members ?? [];
const localOnly = local.filter(s => !members.includes(s));

if (localOnly.length === 0) return;

const total = (this._totalMembers ?? members.length) + localOnly.length;
this.writePageMembers(fetched, [...members, ...localOnly], total);
this.setPage(page, fetched);
this._totalMembers = total;
}

/**
* Resolve a page from the local WASM DB. Returns:
* - `'ok'` — query ran and the page is populated (possibly with zero
Expand Down
5 changes: 4 additions & 1 deletion browser/lib/src/presence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,10 @@ export class DrivePresenceManager {
private store: Store,
private drive: string,
) {
this.sessionId = crypto.randomUUID();
// One id per tab across every channel it is in: a session announcing in
// its drive and in a shared resource's channel is one person to anyone
// who reads both.
this.sessionId = store.presenceSessionId;
}

/**
Expand Down
65 changes: 65 additions & 0 deletions browser/lib/src/store.presence-scope.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { describe, expect, it } from 'vitest';
import { Store } from './store.js';
import { JSONADParser } from './parse.js';
import { core } from './ontologies/core.js';
import { server } from './ontologies/server.js';

/**
* Someone given one chat out of a drive they can't read is refused that
* drive's presence channel. Host and guest meet in the channel of the nearest
* resource carrying its own `read` list: what the invite wrote to.
*/
describe('Store.presenceScope', () => {
const DRIVE = 'did:ad:drive';
const ROOM = 'did:ad:room';
const MESSAGE = 'did:ad:message';
const FOLDER = 'did:ad:folder';
const NOTE = 'did:ad:note';

function setup() {
const store = new Store({
serverUrl: 'https://example.com',
connect: false,
});
const parser = new JSONADParser();

const add = (json: Record<string, unknown>) => {
for (const r of parser.parse(json, json['@id'] as string)) {
r.loading = false;
store.addResource(r);
}
};

add({
'@id': DRIVE,
[core.properties.isA]: [server.classes.drive],
[core.properties.read]: ['did:ad:agent:host'],
});
add({
'@id': ROOM,
[core.properties.parent]: DRIVE,
[core.properties.read]: ['did:ad:agent:guest'],
});
add({ '@id': MESSAGE, [core.properties.parent]: ROOM });
add({ '@id': FOLDER, [core.properties.parent]: DRIVE });
add({ '@id': NOTE, [core.properties.parent]: FOLDER });

return store;
}

it('is the shared resource itself, and so for what is in it', () => {
const store = setup();
const room = store.normalizeSubject(ROOM);

expect(store.presenceScope(ROOM)).toBe(room);
expect(store.presenceScope(MESSAGE)).toBe(room);
});

it('leaves the rest of the drive to the drive channel', () => {
const store = setup();

expect(store.presenceScope(NOTE)).toBeUndefined();
expect(store.presenceScope(DRIVE)).toBeUndefined();
expect(store.presenceScope('did:ad:not-loaded')).toBeUndefined();
});
});
Loading
Loading