@@ -167,7 +167,7 @@ public function setStatus(string $userId,
167167 $ userStatus ->setIsBackup (false );
168168
169169 if ($ userStatus ->getId () === null ) {
170- return $ this ->mapper -> insert ($ userStatus );
170+ return $ this ->insertWithoutThrowingUniqueConstrain ($ userStatus );
171171 }
172172
173173 return $ this ->mapper ->update ($ userStatus );
@@ -211,7 +211,7 @@ public function setPredefinedMessage(string $userId,
211211 $ userStatus ->setStatusMessageTimestamp ($ this ->timeFactory ->now ()->getTimestamp ());
212212
213213 if ($ userStatus ->getId () === null ) {
214- return $ this ->mapper -> insert ($ userStatus );
214+ return $ this ->insertWithoutThrowingUniqueConstrain ($ userStatus );
215215 }
216216
217217 return $ this ->mapper ->update ($ userStatus );
@@ -313,7 +313,7 @@ public function setUserStatus(string $userId,
313313 if ($ userStatus ->getId () !== null ) {
314314 return $ this ->mapper ->update ($ userStatus );
315315 }
316- return $ this ->mapper -> insert ($ userStatus );
316+ return $ this ->insertWithoutThrowingUniqueConstrain ($ userStatus );
317317 }
318318
319319 /**
@@ -360,7 +360,7 @@ public function setCustomMessage(string $userId,
360360 $ userStatus ->setStatusMessageTimestamp ($ this ->timeFactory ->now ()->getTimestamp ());
361361
362362 if ($ userStatus ->getId () === null ) {
363- return $ this ->mapper -> insert ($ userStatus );
363+ return $ this ->insertWithoutThrowingUniqueConstrain ($ userStatus );
364364 }
365365
366366 return $ this ->mapper ->update ($ userStatus );
@@ -584,4 +584,16 @@ public function revertMultipleUserStatus(array $userIds, string $messageId): voi
584584 // For users that matched restore the previous status
585585 $ this ->mapper ->restoreBackupStatuses ($ restoreIds );
586586 }
587+
588+ protected function insertWithoutThrowingUniqueConstrain (UserStatus $ userStatus ): UserStatus {
589+ try {
590+ return $ this ->mapper ->insert ($ userStatus );
591+ } catch (Exception $ e ) {
592+ // Ignore if a parallel request already set the status
593+ if ($ e ->getReason () !== Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION ) {
594+ throw $ e ;
595+ }
596+ }
597+ return $ userStatus ;
598+ }
587599}
0 commit comments