Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable28] fix(userstatus): catch unique constraint violation #42854

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 17 additions & 2 deletions apps/user_status/lib/Listener/UserLiveStatusListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@
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
Expand All @@ -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;
Expand Down Expand Up @@ -110,7 +113,19 @@ public function handle(Event $event): void {
$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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use OCP\IUser;
use OCP\User\Events\UserLiveStatusEvent;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Test\TestCase;

class UserLiveStatusListenerTest extends TestCase {
Expand All @@ -54,19 +55,23 @@ class UserLiveStatusListenerTest extends TestCase {

private CalendarStatusService|MockObject $calendarStatusService;

private LoggerInterface|MockObject $logger;

protected function setUp(): void {
parent::setUp();

$this->mapper = $this->createMock(UserStatusMapper::class);
$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,
);
}

Expand Down