3535use  OCA \UserStatus \Exception \StatusMessageTooLongException ;
3636use  OCP \AppFramework \Db \DoesNotExistException ;
3737use  OCP \AppFramework \Utility \ITimeFactory ;
38+ use  OCP \DB \Exception ;
3839use  OCP \IConfig ;
40+ use  OCP \IUser ;
3941use  OCP \UserStatus \IUserStatus ;
4042
4143/** 
@@ -229,6 +231,7 @@ public function setPredefinedMessage(string $userId,
229231			$ userStatussetStatus (IUserStatus::OFFLINE );
230232			$ userStatussetStatusTimestamp (0 );
231233			$ userStatussetIsUserDefined (false );
234+ 			$ userStatussetIsBackup (false );
232235		}
233236
234237		if  (!$ this predefinedStatusService ->isValidId ($ messageId
@@ -252,6 +255,60 @@ public function setPredefinedMessage(string $userId,
252255		return  $ this mapper ->update ($ userStatus
253256	}
254257
258+ 	/** 
259+ 	 * @param string $userId 
260+ 	 * @param string $status 
261+ 	 * @param string $messageId 
262+ 	 * @param bool $createBackup 
263+ 	 * @throws InvalidStatusTypeException 
264+ 	 * @throws InvalidMessageIdException 
265+ 	 */ 
266+ 	public  function  setUserStatus (string  $ userId
267+ 										 string  $ status
268+ 										 string  $ messageId
269+ 										 bool  $ createBackupvoid  {
270+ 		// Check if status-type is valid 
271+ 		if  (!\in_array ($ statusself ::PRIORITY_ORDERED_STATUSES , true )) {
272+ 			throw  new  InvalidStatusTypeException ('Status-type " '  . $ status'" is not supported ' );
273+ 		}
274+ 
275+ 		if  (!$ this predefinedStatusService ->isValidId ($ messageId
276+ 			throw  new  InvalidMessageIdException ('Message-Id " '  . $ messageId'" is not supported ' );
277+ 		}
278+ 
279+ 		if  ($ createBackup
280+ 			if  ($ this backupCurrentStatus ($ userIdfalse ) {
281+ 				return ; // Already a status set automatically => abort. 
282+ 			}
283+ 
284+ 			// If we just created the backup 
285+ 			$ userStatusnew  UserStatus ();
286+ 			$ userStatussetUserId ($ userId
287+ 		} else  {
288+ 			try  {
289+ 				$ userStatus$ this mapper ->findByUserId ($ userId
290+ 			} catch  (DoesNotExistException $ ex
291+ 				$ userStatusnew  UserStatus ();
292+ 				$ userStatussetUserId ($ userId
293+ 			}
294+ 		}
295+ 
296+ 		$ userStatussetStatus ($ status
297+ 		$ userStatussetStatusTimestamp ($ this timeFactory ->getTime ());
298+ 		$ userStatussetIsUserDefined (false );
299+ 		$ userStatussetIsBackup (false );
300+ 		$ userStatussetMessageId ($ messageId
301+ 		$ userStatussetCustomIcon (null );
302+ 		$ userStatussetCustomMessage (null );
303+ 		$ userStatussetClearAt (null );
304+ 
305+ 		if  ($ userStatusgetId () !== null ) {
306+ 			$ this mapper ->update ($ userStatus
307+ 			return ;
308+ 		}
309+ 		$ this mapper ->insert ($ userStatus
310+ 	}
311+ 
255312	/** 
256313	 * @param string $userId 
257314	 * @param string|null $statusIcon 
@@ -434,53 +491,71 @@ private function addDefaultMessage(UserStatus $status): void {
434491	}
435492
436493	/** 
437- 	 * @return bool false iff  there is already a backup. In this case abort the procedure. 
494+ 	 * @return bool false if  there is already a backup. In this case abort the procedure. 
438495	 */ 
439496	public  function  backupCurrentStatus (string  $ userIdbool  {
440497		try  {
441- 			$ this mapper ->findByUserId ($ userIdtrue );
442- 			return  false ;
443- 		} catch  (DoesNotExistException $ ex
444- 			// No backup already existing => Good 
445- 		}
446- 
447- 		try  {
448- 			$ userStatus$ this mapper ->findByUserId ($ userId
449- 		} catch  (DoesNotExistException $ ex
450- 			// if there is no status to backup, just return 
498+ 			$ this mapper ->createBackupStatus ($ userId
451499			return  true ;
500+ 		} catch  (Exception $ ex
501+ 			if  ($ exgetReason () === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION ) {
502+ 				return  false ;
503+ 			}
504+ 			throw  $ ex
452505		}
453- 
454- 		$ userStatussetIsBackup (true );
455- 		// Prefix user account with an underscore because user_id is marked as unique 
456- 		// in the table. Starting an username with an underscore is not allowed so this 
457- 		// shouldn't create any trouble. 
458- 		$ userStatussetUserId ('_ '  . $ userStatusgetUserId ());
459- 		$ this mapper ->update ($ userStatus
460- 		return  true ;
461506	}
462507
463- 	public  function  revertUserStatus (string  $ userId? string  $ messageIdstring  $ statusvoid  {
508+ 	public  function  revertUserStatus (string  $ userIdstring  $ messageIdstring  $ statusvoid  {
464509		try  {
465510			/** @var UserStatus $userStatus */ 
466511			$ backupUserStatus$ this mapper ->findByUserId ($ userIdtrue );
467512		} catch  (DoesNotExistException $ ex
468513			// No user status to revert, do nothing 
469514			return ;
470515		}
471- 		try  {
472- 			$ userStatus$ this mapper ->findByUserId ($ userId
473- 			if  ($ userStatusgetMessageId () !== $ messageId$ userStatusgetStatus () !== $ status
474- 				// Another status is set automatically, do nothing 
475- 				return ;
476- 			}
477- 			$ this removeUserStatus ($ userId
478- 		} catch  (DoesNotExistException $ ex
479- 			// No current status => nothing to delete 
516+ 
517+ 		$ deleted$ this mapper ->deleteCurrentStatusToRestoreBackup ($ userId$ messageId$ status
518+ 		if  (!$ deleted
519+ 			// Another status is set automatically or no status, do nothing 
520+ 			return ;
480521		}
522+ 
481523		$ backupUserStatussetIsBackup (false );
482524		// Remove the underscore prefix added when creating the backup 
483525		$ backupUserStatussetUserId (substr ($ backupUserStatusgetUserId (), 1 ));
484526		$ this mapper ->update ($ backupUserStatus
485527	}
528+ 
529+ 	public  function  revertMultipleUserStatus (array  $ userIdsstring  $ messageIdstring  $ statusvoid  {
530+ 		// Get all user statuses and the backups 
531+ 		$ findById$ userIds
532+ 		foreach  ($ userIdsas  $ userId
533+ 			$ findById'_ '  . $ userId
534+ 		}
535+ 		$ userStatuses$ this mapper ->findByUserIds ($ findById
536+ 
537+ 		$ backups$ restoreIds$ statuesToDelete
538+ 		foreach  ($ userStatusesas  $ userStatus
539+ 			if  (!$ userStatusgetIsBackup ()
540+ 				&& $ userStatusgetMessageId () === $ messageId
541+ 				&& $ userStatusgetStatus () === $ status
542+ 				$ statuesToDelete$ userStatusgetUserId ()] = $ userStatusgetId ();
543+ 			} else  if  ($ userStatusgetIsBackup ()) {
544+ 				$ backups$ userStatusgetUserId ()] = $ userStatusgetId ();
545+ 			}
546+ 		}
547+ 
548+ 		// For users with both (normal and backup) delete the status when matching 
549+ 		foreach  ($ statuesToDeleteas  $ userId$ statusId
550+ 			$ backupUserId'_ '  . $ userId
551+ 			if  (isset ($ backups$ backupUserId
552+ 				$ restoreIds$ backups$ backupUserId
553+ 			}
554+ 		}
555+ 
556+ 		$ this mapper ->deleteByIds (array_values ($ statuesToDelete
557+ 
558+ 		// For users that matched restore the previous status 
559+ 		$ this mapper ->restoreBackupStatuses ($ restoreIds
560+ 	}
486561}
0 commit comments