| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @copyright Copyright (c) 2021 Joas Schilling <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @license GNU AGPL version 3 or any later version | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * This program is free software: you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * it under the terms of the GNU Affero General Public License as | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * published by the Free Software Foundation, either version 3 of the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * License, or (at your option) any later version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * This program is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * GNU Affero General Public License for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * You should have received a copy of the GNU Affero General Public License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | namespace OC\KnownUser; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  | class KnownUserService { | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  | 	/** @var KnownUserMapper */ | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  | 	protected $mapper; | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  | 	/** @var array */ | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  | 	protected $knownUsers = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	public function __construct(KnownUserMapper $mapper) { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 33 |  |  | 		$this->mapper = $mapper; | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  | 	 * Delete all matches where the given user is the owner of the phonebook | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  | 	 * | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  | 	 * @param string $knownTo | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  | 	 * @return int Number of deleted matches | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  | 	public function deleteKnownTo(string $knownTo): int { | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  | 		return $this->mapper->deleteKnownTo($knownTo); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  | 	 * Delete all matches where the given user is the one in the phonebook | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  | 	 * | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  | 	 * @param string $contactUserId | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  | 	 * @return int Number of deleted matches | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  | 	public function deleteByContactUserId(string $contactUserId): int { | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  | 		return $this->mapper->deleteKnownUser($contactUserId); | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  | 	 * Store a match because $knownTo has $contactUserId in his phonebook | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  | 	 * | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  | 	 * @param string $knownTo User id of the owner of the phonebook | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  | 	 * @param string $contactUserId User id of the contact in the phonebook | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	public function storeIsKnownToUser(string $knownTo, string $contactUserId): void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		$entity = new KnownUser(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 		$entity->setKnownTo($knownTo); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		$entity->setKnownUser($contactUserId); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 66 |  |  | 		$this->mapper->insert($entity); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 67 |  |  | 	} | 
            
                                                                        
                                                                
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  | 	 * Check if $contactUserId is in the phonebook of $knownTo | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  | 	 * | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  | 	 * @param string $knownTo User id of the owner of the phonebook | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  | 	 * @param string $contactUserId User id of the contact in the phonebook | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  | 	 * @return bool | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	public function isKnownToUser(string $knownTo, string $contactUserId): bool { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 		if (!isset($this->knownUsers[$knownTo])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 			$entities = $this->mapper->getKnownUsers($knownTo); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 			$this->knownUsers[$knownTo] = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 			foreach ($entities as $entity) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 				$this->knownUsers[$knownTo][$entity->getKnownUser()] = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 85 |  |  | 		return isset($this->knownUsers[$knownTo][$contactUserId]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 87 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |  |