Repository: directus/directus
File: api/src/websocket/handlers/subscribe.ts (TypeScript)

Code before:
		if (getMessageType(message) === 'subscribe') {
			try {
				const collection = String(message.collection!);
				const accountability = client.accountability;
				const schema = await getSchema();

				if (!accountability?.admin && !schema.collections[collection]) {
					throw new WebSocketError(
						'subscribe',
						'INVALID_COLLECTION',
						'The provided collection does not exists or is not accessible.',
						message.uid,
					);
				}

				const subscription: Subscription = {
					client,
					collection,
				};

				if ('event' in message) {
					subscription.event = message.event as SubscriptionEvent;
				}

				if (message.query) {
					if (typeof message.query === 'object' && !Array.isArray(message.query)) {
						throw new WebSocketError('subscribe', 'INVALID_QUERY', 'The provided query must be an object', message.uid);
					}

					subscription.query = sanitizeQuery(message.query, accountability);
				}

				if ('item' in message) subscription.item = String(message.item);

				if ('uid' in message) {
					subscription.uid = String(message.uid);
					// remove the subscription if it already exists
					this.unsubscribe(client, subscription.uid);
				}

				const data =
					subscription.event === undefined ? await getPayload(subscription, accountability, schema) : { event: 'init' };

				// if no errors were thrown register the subscription
				this.subscribe(subscription);

				// send an initial response
				client.send(fmtMessage('subscription', data, subscription.uid));
			} catch (err) {
				handleWebSocketError(client, err, 'subscribe');
			}

Review comment (suggestion):
If im reading this right the WebSocket error gets thrown when the `query` is an object and not is an array. This should be the other way around:
```suggestion
					if (typeof message.query !== 'object' || Array.isArray(message.query)) {
```

Code after:
		if (getMessageType(message) === 'subscribe') {
			try {
				const collection = String(message.collection!);
				const accountability = client.accountability;
				const schema = await getSchema();

				if (!accountability?.admin && !schema.collections[collection]) {
					throw new WebSocketError(
						'subscribe',
						'INVALID_COLLECTION',
						'The provided collection does not exists or is not accessible.',
						message.uid,
					);
				}

				const subscription: Subscription = {
					client,
					collection,
				};

				if ('event' in message) {
					subscription.event = message.event as SubscriptionEvent;
				}

				if (message.query) {
					subscription.query = sanitizeQuery(message.query, accountability);
				}

				if ('item' in message) subscription.item = String(message.item);

				if ('uid' in message) {
					subscription.uid = String(message.uid);
					// remove the subscription if it already exists
					this.unsubscribe(client, subscription.uid);
				}

				const data =
					subscription.event === undefined ? await getPayload(subscription, accountability, schema) : { event: 'init' };

				// if no errors were thrown register the subscription
				this.subscribe(subscription);

				// send an initial response
				client.send(fmtMessage('subscription', data, subscription.uid));
			} catch (err) {
				handleWebSocketError(client, err, 'subscribe');
			}
		}

		if (getMessageType(message) === 'unsubscribe') {
			try {