Skip to content

Commit

Permalink
Merge branch 'collab-readonly-awareness' into 'main'
Browse files Browse the repository at this point in the history
Collab: Do not send awareness events for readonly connections

See merge request reportcreator/reportcreator!728
  • Loading branch information
MWedl committed Oct 9, 2024
2 parents cec24ef + 96bdbd9 commit 1ee3946
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
4 changes: 4 additions & 0 deletions frontend/src/utils/collab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,10 @@ export function useCollab<T = any>(storeState: CollabStoreState<T>) {
}

function sendAwarenessThrottled() {
if (!storeState.permissions.write) {
return;
}

sendEventsThrottled({
type: CollabEventType.AWARENESS,
path: storeState.awareness.self.path,
Expand Down
35 changes: 35 additions & 0 deletions frontend/test/collab.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { v4 as uuid4 } from 'uuid'
import { cloneDeep } from 'lodash-es'
import { CollabEventType, type CollabEvent, type Comment, type User } from '#imports';
import { ChangeSet, EditorSelection } from 'reportcreator-markdown/editor';
import { fa } from 'vuetify/locale';


async function createCollab(options?: { collabInitEvent?: Partial<CollabEvent> }) {
Expand Down Expand Up @@ -183,6 +184,40 @@ describe('connection', () => {
});


describe('readonly connection', () => {
let { connection, collab, collabInitEvent }: Awaited<ReturnType<typeof createCollab>> = {} as any;
beforeEach(async () => {
const res = await createCollab({
collabInitEvent: {
permissions: {
read: true,
write: false,
}
}
});
collab = res.collab;
connection = res.connection;
collabInitEvent = res.collabInitEvent;
});

test('no events sent', async () => {
collab.onCollabEvent({ type: CollabEventType.AWARENESS, path: collab.storeState.apiPath + 'field_text'});
collab.onCollabEvent({ type: CollabEventType.UPDATE_KEY, path: collab.storeState.apiPath + 'field_key', value: 'x' });
collab.onCollabEvent({ type: CollabEventType.DELETE, path: collab.storeState.apiPath + 'field_list[0]' });
collab.onCollabEvent({ type: CollabEventType.CREATE, path: collab.storeState.apiPath + 'field_list', value: 'new'});
collab.onCollabEvent({
type: CollabEventType.UPDATE_TEXT,
path: collab.storeState.apiPath + 'field_text',
updates: [{changes: ChangeSet.fromJSON([4, [0, 'E']])}]
});
await vi.runOnlyPendingTimersAsync();
expect(connection.send).not.toHaveBeenCalled();
expect(collab.storeState.data).toEqual(collabInitEvent.data);
expect(collab.storeState.awareness.self).toEqual({ path: '' });
});
});


describe('send and receive', () => {
let { collab, connection, collabInitEvent }: Awaited<ReturnType<typeof createCollab>> = {} as any;
beforeEach(async () => {
Expand Down

0 comments on commit 1ee3946

Please sign in to comment.