From 02889eeda23a0a002ed94ab794e62bf4234af8af Mon Sep 17 00:00:00 2001 From: Kirill Lakhov Date: Fri, 29 Mar 2024 16:17:42 +0300 Subject: [PATCH] Fixed sending `/events` requests from logged out users (#7608) ### Motivation and context This PR fixes the following problem: - When user is logged-out, the /events request is still sent. Its failing due with unauthorized request creating lots of error events on the server. They are quite useless. ### How has this been tested? ### Checklist - [x] I submit my changes into the `develop` branch - [x] I have created a changelog fragment - ~~[ ] I have updated the documentation accordingly~~ - [ ] I have added tests to cover my changes - ~~[ ] I have linked related issues (see [GitHub docs]( https://help.github.com/en/github/managing-your-work-on-github/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword))~~ - [x] I have increased versions of npm packages if it is necessary ([cvat-canvas](https://github.com/opencv/cvat/tree/develop/cvat-canvas#versioning), [cvat-core](https://github.com/opencv/cvat/tree/develop/cvat-core#versioning), [cvat-data](https://github.com/opencv/cvat/tree/develop/cvat-data#versioning) and [cvat-ui](https://github.com/opencv/cvat/tree/develop/cvat-ui#versioning)) ### License - [x] I submit _my code changes_ under the same [MIT License]( https://github.com/opencv/cvat/blob/develop/LICENSE) that covers the project. Feel free to contact the maintainers if that's a concern. --------- Co-authored-by: Boris Sekachev --- ...25513_klakhov_event_for_logged_out_user.md | 3 +++ cvat-core/package.json | 2 +- cvat-core/src/index.ts | 2 +- cvat-core/src/logger.ts | 2 ++ cvat-ui/src/components/cvat-app.tsx | 15 ++++++++---- .../{controls-logger.ts => event-recorder.ts} | 23 ++++++++++++++++--- 6 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 changelog.d/20240320_125513_klakhov_event_for_logged_out_user.md rename cvat-ui/src/utils/{controls-logger.ts => event-recorder.ts} (78%) diff --git a/changelog.d/20240320_125513_klakhov_event_for_logged_out_user.md b/changelog.d/20240320_125513_klakhov_event_for_logged_out_user.md new file mode 100644 index 000000000000..8fec0155e89a --- /dev/null +++ b/changelog.d/20240320_125513_klakhov_event_for_logged_out_user.md @@ -0,0 +1,3 @@ +### Fixed + +- Sending `/events` request from logged-out user () diff --git a/cvat-core/package.json b/cvat-core/package.json index 5e57ad15cc68..ab537343f248 100644 --- a/cvat-core/package.json +++ b/cvat-core/package.json @@ -1,6 +1,6 @@ { "name": "cvat-core", - "version": "15.0.1", + "version": "15.0.2", "type": "module", "description": "Part of Computer Vision Tool which presents an interface for client-side integration", "main": "src/api.ts", diff --git a/cvat-core/src/index.ts b/cvat-core/src/index.ts index e4bdde9ba091..2843767e254d 100644 --- a/cvat-core/src/index.ts +++ b/cvat-core/src/index.ts @@ -160,7 +160,7 @@ export default interface CVATCore { enabled: boolean; onEmptyMaskOccurrence: () => void | null; }; - onOrganizationChange: typeof config.onOrganizationChange; + onOrganizationChange: (newOrgId: number | null) => void | null; globalObjectsCounter: typeof config.globalObjectsCounter; }, client: { diff --git a/cvat-core/src/logger.ts b/cvat-core/src/logger.ts index d13b1d7a6fe3..1e8fdb174213 100644 --- a/cvat-core/src/logger.ts +++ b/cvat-core/src/logger.ts @@ -225,6 +225,8 @@ Object.defineProperties(Logger.prototype.save, { // potentially new events may be generated during saving // that is why we add this.collection this.collection = [...collectionToSend, ...this.collection]; + + throw error; } finally { this.saving = false; } diff --git a/cvat-ui/src/components/cvat-app.tsx b/cvat-ui/src/components/cvat-app.tsx index 9c60ae5adf80..22898f153b63 100644 --- a/cvat-ui/src/components/cvat-app.tsx +++ b/cvat-ui/src/components/cvat-app.tsx @@ -1,5 +1,5 @@ // Copyright (C) 2020-2022 Intel Corporation -// Copyright (C) 2022-2023 CVAT.ai Corporation +// Copyright (C) 2022-2024 CVAT.ai Corporation // // SPDX-License-Identifier: MIT @@ -71,7 +71,7 @@ import showPlatformNotification, { } from 'utils/platform-checker'; import '../styles.scss'; import appConfig from 'config'; -import EventRecorder from 'utils/controls-logger'; +import EventRecorder from 'utils/event-recorder'; import { authQuery } from 'utils/auth-query'; import EmailConfirmationPage from './email-confirmation-pages/email-confirmed'; import EmailVerificationSentPage from './email-confirmation-pages/email-verification-sent'; @@ -149,7 +149,6 @@ class CVATApplication extends React.PureComponent window.document.hasFocus, userActivityCallback); - EventRecorder.initSave(); core.config.onOrganizationChange = (newOrgId: number | null) => { if (newOrgId === null) { @@ -254,7 +253,7 @@ class CVATApplication extends React.PureComponent { - core.logger.save().finally(() => { + const scheduleSave = (): void => { this.#savingTimeout = null; this.initSave(); - }); + }; + core.logger.save() + .then(scheduleSave) + .catch((error) => { + if (error?.code === 401) { + this.cancelSave(); + } else { + scheduleSave(); + } + }); }, CONTROLS_LOGS_INTERVAL); } + public cancelSave(): void { + if (this.#savingTimeout) { + window.clearTimeout(this.#savingTimeout); + this.#savingTimeout = null; + } + } + private filterClassName(cls: string): string { if (typeof cls === 'string') { return cls.split(' ').filter((_cls: string) => _cls.startsWith('cvat')).join(' ');