diff options
-rw-r--r-- | apps/user_status/lib/Listener/UserLiveStatusListener.php | 19 | ||||
-rw-r--r-- | apps/user_status/tests/Unit/Listener/UserLiveStatusListenerTest.php | 5 |
2 files changed, 22 insertions, 2 deletions
diff --git a/apps/user_status/lib/Listener/UserLiveStatusListener.php b/apps/user_status/lib/Listener/UserLiveStatusListener.php index b999c51d72f..d8485a6d0cb 100644 --- a/apps/user_status/lib/Listener/UserLiveStatusListener.php +++ b/apps/user_status/lib/Listener/UserLiveStatusListener.php @@ -32,10 +32,12 @@ use OCA\UserStatus\Db\UserStatusMapper; use OCA\UserStatus\Service\StatusService; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Utility\ITimeFactory; +use OCP\DB\Exception; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; use OCP\User\Events\UserLiveStatusEvent; use OCP\UserStatus\IUserStatus; +use Psr\Log\LoggerInterface; /** * Class UserDeletedListener @@ -50,7 +52,8 @@ class UserLiveStatusListener implements IEventListener { public function __construct(UserStatusMapper $mapper, StatusService $statusService, ITimeFactory $timeFactory, - private CalendarStatusService $calendarStatusService) { + private CalendarStatusService $calendarStatusService, + private LoggerInterface $logger) { $this->mapper = $mapper; $this->statusService = $statusService; $this->timeFactory = $timeFactory; @@ -110,7 +113,19 @@ class UserLiveStatusListener implements IEventListener { $userStatus->setIsUserDefined(false); if ($userStatus->getId() === null) { - $this->mapper->insert($userStatus); + try { + $this->mapper->insert($userStatus); + } catch (Exception $e) { + if ($e->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) { + // A different process might have written another status + // update to the DB while we're processing our stuff. + // We can safely ignore it as we're only changing between AWAY and ONLINE + // and not doing anything with the message or icon. + $this->logger->debug('Unique constraint violation for live user status', ['exception' => $e]); + return; + } + throw $e; + } } else { $this->mapper->update($userStatus); } diff --git a/apps/user_status/tests/Unit/Listener/UserLiveStatusListenerTest.php b/apps/user_status/tests/Unit/Listener/UserLiveStatusListenerTest.php index 0f637b75411..2bef6773702 100644 --- a/apps/user_status/tests/Unit/Listener/UserLiveStatusListenerTest.php +++ b/apps/user_status/tests/Unit/Listener/UserLiveStatusListenerTest.php @@ -38,6 +38,7 @@ use OCP\EventDispatcher\GenericEvent; use OCP\IUser; use OCP\User\Events\UserLiveStatusEvent; use PHPUnit\Framework\MockObject\MockObject; +use Psr\Log\LoggerInterface; use Test\TestCase; class UserLiveStatusListenerTest extends TestCase { @@ -54,6 +55,8 @@ class UserLiveStatusListenerTest extends TestCase { private CalendarStatusService|MockObject $calendarStatusService; + private LoggerInterface|MockObject $logger; + protected function setUp(): void { parent::setUp(); @@ -61,12 +64,14 @@ class UserLiveStatusListenerTest extends TestCase { $this->statusService = $this->createMock(StatusService::class); $this->timeFactory = $this->createMock(ITimeFactory::class); $this->calendarStatusService = $this->createMock(CalendarStatusService::class); + $this->logger = $this->createMock(LoggerInterface::class); $this->listener = new UserLiveStatusListener( $this->mapper, $this->statusService, $this->timeFactory, $this->calendarStatusService, + $this->logger, ); } |