| 
									
										
										
										
											2018-09-28 11:41:24 +00:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2022-04-15 11:34:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-28 11:41:24 +00:00
										 |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Nextcloud - Social Support | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This file is licensed under the Affero General Public License version 3 or | 
					
						
							|  |  |  |  * later. See the COPYING file. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @author Maxence Lange <maxence@artificial-owl.com> | 
					
						
							|  |  |  |  * @copyright 2018, Maxence Lange <maxence@artificial-owl.com> | 
					
						
							|  |  |  |  * @license GNU AGPL version 3 or any later version | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU Affero General Public License as | 
					
						
							|  |  |  |  * published by the Free Software Foundation, either version 3 of the | 
					
						
							|  |  |  |  * License, or (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU Affero General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  |  * along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace OCA\Social\Db; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-02 11:20:11 +00:00
										 |  |  | use DateTime; | 
					
						
							| 
									
										
										
										
											2019-05-17 10:19:45 +00:00
										 |  |  | use Exception; | 
					
						
							| 
									
										
										
										
											2018-09-28 11:41:24 +00:00
										 |  |  | use OCA\Social\Exceptions\ActorDoesNotExistException; | 
					
						
							| 
									
										
										
										
											2018-11-15 11:26:25 +00:00
										 |  |  | use OCA\Social\Exceptions\SocialAppConfigException; | 
					
						
							| 
									
										
										
										
											2018-12-13 09:40:27 +00:00
										 |  |  | use OCA\Social\Model\ActivityPub\Actor\Person; | 
					
						
							| 
									
										
										
										
											2019-01-02 11:20:11 +00:00
										 |  |  | use OCP\DB\QueryBuilder\IQueryBuilder; | 
					
						
							| 
									
										
										
										
											2018-09-28 11:41:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class ActorsRequest extends ActorsRequestBuilder { | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2022-04-15 11:34:01 +00:00
										 |  |  | 	 * Create a new Person in the database. | 
					
						
							| 
									
										
										
										
											2018-11-28 15:39:25 +00:00
										 |  |  | 	 * @throws SocialAppConfigException | 
					
						
							| 
									
										
										
										
											2018-09-28 11:41:24 +00:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2022-04-15 11:34:01 +00:00
										 |  |  | 	public function create(Person $actor): void { | 
					
						
							| 
									
										
										
										
											2019-09-23 10:34:21 +00:00
										 |  |  | 		$actor->setId($this->configService->getSocialUrl() . '@' . $actor->getPreferredUsername()); | 
					
						
							| 
									
										
										
										
											2018-11-28 15:39:25 +00:00
										 |  |  | 		$qb = $this->getActorsInsertSql(); | 
					
						
							| 
									
										
										
										
											2018-11-12 22:55:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-23 10:34:21 +00:00
										 |  |  | 		$qb->setValue('id', $qb->createNamedParameter($actor->getId())) | 
					
						
							|  |  |  | 		   ->setValue('id_prim', $qb->createNamedParameter($this->prim($actor->getId()))) | 
					
						
							| 
									
										
										
										
											2018-11-28 15:39:25 +00:00
										 |  |  | 		   ->setValue('user_id', $qb->createNamedParameter($actor->getUserId())) | 
					
						
							|  |  |  | 		   ->setValue('name', $qb->createNamedParameter($actor->getName())) | 
					
						
							|  |  |  | 		   ->setValue('summary', $qb->createNamedParameter($actor->getSummary())) | 
					
						
							| 
									
										
										
										
											2018-12-05 18:37:49 +00:00
										 |  |  | 		   ->setValue('avatar_version', $qb->createNamedParameter($actor->getAvatarVersion())) | 
					
						
							| 
									
										
										
										
											2018-11-28 15:39:25 +00:00
										 |  |  | 		   ->setValue( | 
					
						
							| 
									
										
										
										
											2022-11-21 20:03:59 +00:00
										 |  |  | 		   	'preferred_username', $qb->createNamedParameter($actor->getPreferredUsername()) | 
					
						
							| 
									
										
										
										
											2018-11-28 15:39:25 +00:00
										 |  |  | 		   ) | 
					
						
							|  |  |  | 		   ->setValue('public_key', $qb->createNamedParameter($actor->getPublicKey())) | 
					
						
							| 
									
										
										
										
											2019-01-02 11:20:11 +00:00
										 |  |  | 		   ->setValue('private_key', $qb->createNamedParameter($actor->getPrivateKey())) | 
					
						
							|  |  |  | 		   ->setValue( | 
					
						
							| 
									
										
										
										
											2022-11-21 20:03:59 +00:00
										 |  |  | 		   	'creation', | 
					
						
							|  |  |  | 		   	$qb->createNamedParameter(new DateTime('now'), IQueryBuilder::PARAM_DATE) | 
					
						
							| 
									
										
										
										
											2019-01-02 11:20:11 +00:00
										 |  |  | 		   ); | 
					
						
							| 
									
										
										
										
											2018-11-28 15:39:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 11:34:01 +00:00
										 |  |  | 		$qb->executeStatement(); | 
					
						
							| 
									
										
										
										
											2018-09-28 11:41:24 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 11:34:01 +00:00
										 |  |  | 	public function update(Person $actor): void { | 
					
						
							| 
									
										
										
										
											2018-12-05 18:37:49 +00:00
										 |  |  | 		$qb = $this->getActorsUpdateSql(); | 
					
						
							|  |  |  | 		$qb->set('avatar_version', $qb->createNamedParameter($actor->getAvatarVersion())); | 
					
						
							|  |  |  | 		$this->limitToIdString($qb, $actor->getId()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 11:34:01 +00:00
										 |  |  | 		$qb->executeStatement(); | 
					
						
							| 
									
										
										
										
											2018-12-05 18:37:49 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 11:34:01 +00:00
										 |  |  | 	public function refreshKeys(Person $actor): void { | 
					
						
							| 
									
										
										
										
											2019-01-02 11:20:11 +00:00
										 |  |  | 		$qb = $this->getActorsUpdateSql(); | 
					
						
							|  |  |  | 		$qb->set('public_key', $qb->createNamedParameter($actor->getPublicKey())) | 
					
						
							| 
									
										
										
										
											2019-05-17 10:19:45 +00:00
										 |  |  | 		   ->set('private_key', $qb->createNamedParameter($actor->getPrivateKey())); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		try { | 
					
						
							|  |  |  | 			$qb->set( | 
					
						
							|  |  |  | 				'creation', | 
					
						
							|  |  |  | 				$qb->createNamedParameter(new DateTime('now'), IQueryBuilder::PARAM_DATE) | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 		} catch (Exception $e) { | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-02 11:20:11 +00:00
										 |  |  | 		$this->limitToIdString($qb, $actor->getId()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 11:34:01 +00:00
										 |  |  | 		$qb->executeStatement(); | 
					
						
							| 
									
										
										
										
											2019-01-02 11:20:11 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-28 11:41:24 +00:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2022-04-15 11:34:01 +00:00
										 |  |  | 	 * Return Actor from database based on the username | 
					
						
							| 
									
										
										
										
											2018-09-28 11:41:24 +00:00
										 |  |  | 	 * | 
					
						
							|  |  |  | 	 * @throws ActorDoesNotExistException | 
					
						
							| 
									
										
										
										
											2018-11-15 11:26:25 +00:00
										 |  |  | 	 * @throws SocialAppConfigException | 
					
						
							| 
									
										
										
										
											2018-09-28 11:41:24 +00:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2018-11-12 22:55:39 +00:00
										 |  |  | 	public function getFromUsername(string $username): Person { | 
					
						
							| 
									
										
										
										
											2018-09-28 11:41:24 +00:00
										 |  |  | 		$qb = $this->getActorsSelectSql(); | 
					
						
							|  |  |  | 		$this->limitToPreferredUsername($qb, $username); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 11:34:01 +00:00
										 |  |  | 		$cursor = $qb->executeQuery(); | 
					
						
							| 
									
										
										
										
											2018-09-28 11:41:24 +00:00
										 |  |  | 		$data = $cursor->fetch(); | 
					
						
							|  |  |  | 		$cursor->closeCursor(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-13 14:30:48 +00:00
										 |  |  | 		if ($data === false) { | 
					
						
							|  |  |  | 			throw new ActorDoesNotExistException('Actor not found'); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return $this->parseActorsSelectSql($data); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @throws ActorDoesNotExistException | 
					
						
							| 
									
										
										
										
											2018-11-15 11:26:25 +00:00
										 |  |  | 	 * @throws SocialAppConfigException | 
					
						
							| 
									
										
										
										
											2018-11-13 14:30:48 +00:00
										 |  |  | 	 */ | 
					
						
							|  |  |  | 	public function getFromId(string $id): Person { | 
					
						
							|  |  |  | 		$qb = $this->getActorsSelectSql(); | 
					
						
							|  |  |  | 		$this->limitToIdString($qb, $id); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 11:34:01 +00:00
										 |  |  | 		$cursor = $qb->executeQuery(); | 
					
						
							| 
									
										
										
										
											2018-11-13 14:30:48 +00:00
										 |  |  | 		$data = $cursor->fetch(); | 
					
						
							|  |  |  | 		$cursor->closeCursor(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-28 11:41:24 +00:00
										 |  |  | 		if ($data === false) { | 
					
						
							|  |  |  | 			throw new ActorDoesNotExistException('Actor not found'); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return $this->parseActorsSelectSql($data); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2018-10-01 12:14:23 +00:00
										 |  |  | 	 * return Actor from database, based on the userId of the owner. | 
					
						
							| 
									
										
										
										
											2018-09-28 11:41:24 +00:00
										 |  |  | 	 * | 
					
						
							|  |  |  | 	 * @param string $userId | 
					
						
							|  |  |  | 	 * | 
					
						
							| 
									
										
										
										
											2018-11-12 22:55:39 +00:00
										 |  |  | 	 * @return Person | 
					
						
							| 
									
										
										
										
											2018-09-28 11:41:24 +00:00
										 |  |  | 	 * @throws ActorDoesNotExistException | 
					
						
							| 
									
										
										
										
											2018-11-15 11:26:25 +00:00
										 |  |  | 	 * @throws SocialAppConfigException | 
					
						
							| 
									
										
										
										
											2018-09-28 11:41:24 +00:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2018-11-12 22:55:39 +00:00
										 |  |  | 	public function getFromUserId(string $userId): Person { | 
					
						
							| 
									
										
										
										
											2018-09-28 11:41:24 +00:00
										 |  |  | 		$qb = $this->getActorsSelectSql(); | 
					
						
							|  |  |  | 		$this->limitToUserId($qb, $userId); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 11:34:01 +00:00
										 |  |  | 		$cursor = $qb->executeQuery(); | 
					
						
							| 
									
										
										
										
											2018-09-28 11:41:24 +00:00
										 |  |  | 		$data = $cursor->fetch(); | 
					
						
							|  |  |  | 		$cursor->closeCursor(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if ($data === false) { | 
					
						
							|  |  |  | 			throw new ActorDoesNotExistException('Actor not found'); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return $this->parseActorsSelectSql($data); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-26 12:33:48 +00:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @return Person[] | 
					
						
							|  |  |  | 	 * @throws SocialAppConfigException | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	public function getAll(): array { | 
					
						
							|  |  |  | 		$qb = $this->getActorsSelectSql(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		$accounts = []; | 
					
						
							| 
									
										
										
										
											2022-04-15 11:34:01 +00:00
										 |  |  | 		$cursor = $qb->executeQuery(); | 
					
						
							| 
									
										
										
										
											2018-11-26 12:33:48 +00:00
										 |  |  | 		while ($data = $cursor->fetch()) { | 
					
						
							|  |  |  | 			$accounts[] = $this->parseActorsSelectSql($data); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		$cursor->closeCursor(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return $accounts; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-14 11:00:05 +00:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @return Person[] | 
					
						
							| 
									
										
										
										
											2018-11-15 11:26:25 +00:00
										 |  |  | 	 * @throws SocialAppConfigException | 
					
						
							| 
									
										
										
										
											2018-11-14 11:00:05 +00:00
										 |  |  | 	 */ | 
					
						
							|  |  |  | 	public function searchFromUsername(string $search): array { | 
					
						
							|  |  |  | 		$qb = $this->getActorsSelectSql(); | 
					
						
							|  |  |  | 		$this->searchInPreferredUsername($qb, $search); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		$accounts = []; | 
					
						
							| 
									
										
										
										
											2022-04-15 11:34:01 +00:00
										 |  |  | 		$cursor = $qb->executeQuery(); | 
					
						
							| 
									
										
										
										
											2018-11-14 11:00:05 +00:00
										 |  |  | 		while ($data = $cursor->fetch()) { | 
					
						
							|  |  |  | 			$accounts[] = $this->parseActorsSelectSql($data); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		$cursor->closeCursor(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return $accounts; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-09-28 11:41:24 +00:00
										 |  |  | } |