| 
									
										
										
										
											2021-04-18 02:17:57 +01:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-10 09:26:18 +01:00
										 |  |  | declare(strict_types = 1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-18 02:17:57 +01:00
										 |  |  | // {{{ License
 | 
					
						
							|  |  |  | // This file is part of GNU social - https://www.gnu.org/software/social
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // GNU social 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.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // GNU social 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 GNU social.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							|  |  |  | // }}}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Component\Avatar; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use App\Core\Cache; | 
					
						
							|  |  |  | use App\Core\DB\DB; | 
					
						
							|  |  |  | use App\Core\Event; | 
					
						
							|  |  |  | use App\Core\GSFile; | 
					
						
							|  |  |  | use App\Core\Modules\Component; | 
					
						
							| 
									
										
										
										
											2021-10-18 13:17:08 +01:00
										 |  |  | use App\Core\Router\RouteLoader; | 
					
						
							| 
									
										
										
										
											2021-08-05 03:15:07 +01:00
										 |  |  | use App\Core\Router\Router; | 
					
						
							| 
									
										
										
										
											2021-04-18 02:17:57 +01:00
										 |  |  | use App\Util\Common; | 
					
						
							| 
									
										
										
										
											2021-12-27 04:47:04 +00:00
										 |  |  | use Component\Attachment\Entity\Attachment; | 
					
						
							|  |  |  | use Component\Attachment\Entity\AttachmentThumbnail; | 
					
						
							| 
									
										
										
										
											2021-07-29 20:20:32 +01:00
										 |  |  | use Component\Avatar\Controller as C; | 
					
						
							| 
									
										
										
										
											2021-04-18 02:17:57 +01:00
										 |  |  | use Component\Avatar\Exception\NoAvatarException; | 
					
						
							| 
									
										
										
										
											2021-07-29 20:20:32 +01:00
										 |  |  | use Symfony\Component\HttpFoundation\Request; | 
					
						
							| 
									
										
										
										
											2021-04-18 02:17:57 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | class Avatar extends Component | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-08-22 13:33:27 +01:00
										 |  |  |     public function onInitializeComponent() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-18 13:17:08 +01:00
										 |  |  |     public function onAddRoute(RouteLoader $r): bool | 
					
						
							| 
									
										
										
										
											2021-04-18 02:17:57 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-12-27 04:47:04 +00:00
										 |  |  |         $r->connect('avatar_actor', '/actor/{actor_id<\d+>}/avatar/{size<full|big|medium|small>?medium}', [Controller\Avatar::class, 'avatar_view']); | 
					
						
							|  |  |  |         $r->connect('avatar_default', '/avatar/default/{size<full|big|medium|small>?medium}', [Controller\Avatar::class, 'default_avatar_view']); | 
					
						
							| 
									
										
										
										
											2021-09-20 17:01:36 +01:00
										 |  |  |         $r->connect('avatar_settings', '/settings/avatar', [Controller\Avatar::class, 'settings_avatar']); | 
					
						
							| 
									
										
										
										
											2021-04-18 02:17:57 +01:00
										 |  |  |         return Event::next; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 04:54:35 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @throws \App\Util\Exception\ClientException | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-12-08 17:53:29 +00:00
										 |  |  |     public function onPopulateSettingsTabs(Request $request, string $section, &$tabs): bool | 
					
						
							| 
									
										
										
										
											2021-07-29 20:20:32 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-12-08 17:53:29 +00:00
										 |  |  |         if ($section === 'profile') { | 
					
						
							|  |  |  |             $tabs[] = [ | 
					
						
							|  |  |  |                 'title'      => 'Avatar', | 
					
						
							|  |  |  |                 'desc'       => 'Change your avatar.', | 
					
						
							|  |  |  |                 'id'         => 'settings-avatar', | 
					
						
							|  |  |  |                 'controller' => C\Avatar::settings_avatar($request), | 
					
						
							|  |  |  |             ]; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-07-29 20:20:32 +01:00
										 |  |  |         return Event::next; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 03:22:27 +01:00
										 |  |  |     public function onAvatarUpdate(int $actor_id): bool | 
					
						
							| 
									
										
										
										
											2021-04-18 02:17:57 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-09-23 14:03:50 +01:00
										 |  |  |         Cache::delete("avatar-{$actor_id}"); | 
					
						
							| 
									
										
										
										
											2021-09-20 17:01:36 +01:00
										 |  |  |         foreach (['full', 'big', 'medium', 'small'] as $size) { | 
					
						
							|  |  |  |             foreach ([Router::ABSOLUTE_PATH, Router::ABSOLUTE_URL] as $type) { | 
					
						
							|  |  |  |                 Cache::delete("avatar-url-{$actor_id}-{$size}-{$type}"); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-12-02 15:34:51 +00:00
										 |  |  |             Cache::delete("avatar-file-info-{$actor_id}-{$size}"); | 
					
						
							| 
									
										
										
										
											2021-09-20 17:01:36 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-08-05 03:15:07 +01:00
										 |  |  |         return Event::next; | 
					
						
							| 
									
										
										
										
											2021-04-18 02:17:57 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // UTILS ----------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-09-18 03:22:27 +01:00
										 |  |  |      * Get the avatar associated with the given Actor id | 
					
						
							| 
									
										
										
										
											2021-04-18 02:17:57 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-09-18 03:22:27 +01:00
										 |  |  |     public static function getAvatar(?int $actor_id = null): Entity\Avatar | 
					
						
							| 
									
										
										
										
											2021-04-18 02:17:57 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-09-18 03:22:27 +01:00
										 |  |  |         $actor_id = $actor_id ?: Common::userId(); | 
					
						
							| 
									
										
										
										
											2021-10-10 09:26:18 +01:00
										 |  |  |         return GSFile::error( | 
					
						
							|  |  |  |             NoAvatarException::class, | 
					
						
							| 
									
										
										
										
											2021-09-18 03:22:27 +01:00
										 |  |  |             $actor_id, | 
					
						
							| 
									
										
										
										
											2021-10-10 09:26:18 +01:00
										 |  |  |             Cache::get( | 
					
						
							|  |  |  |                 "avatar-{$actor_id}", | 
					
						
							| 
									
										
										
										
											2021-09-18 03:22:27 +01:00
										 |  |  |                 function () use ($actor_id) { | 
					
						
							| 
									
										
										
										
											2021-10-10 09:26:18 +01:00
										 |  |  |                     return DB::dql( | 
					
						
							|  |  |  |                         'select a from Component\Avatar\Entity\Avatar a ' | 
					
						
							|  |  |  |                         . 'where a.actor_id = :actor_id', | 
					
						
							|  |  |  |                         ['actor_id' => $actor_id], | 
					
						
							|  |  |  |                     ); | 
					
						
							|  |  |  |                 }, | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2021-04-18 02:17:57 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-09-18 03:22:27 +01:00
										 |  |  |      * Get the cached avatar associated with the given Actor id, or the current user if not given | 
					
						
							| 
									
										
										
										
											2021-04-18 02:17:57 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-12-27 04:47:04 +00:00
										 |  |  |     public static function getUrl(int $actor_id, string $size = 'medium', int $type = Router::ABSOLUTE_PATH): string | 
					
						
							| 
									
										
										
										
											2021-04-18 02:17:57 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-09-20 17:01:36 +01:00
										 |  |  |         try { | 
					
						
							|  |  |  |             return self::getAvatar($actor_id)->getUrl($size, $type); | 
					
						
							|  |  |  |         } catch (NoAvatarException) { | 
					
						
							|  |  |  |             return Router::url('avatar_default', ['size' => $size], $type); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-04-18 02:17:57 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 04:47:04 +00:00
										 |  |  |     public static function getDimensions(int $actor_id, string $size = 'medium') | 
					
						
							| 
									
										
										
										
											2021-12-02 15:36:08 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         try { | 
					
						
							| 
									
										
										
										
											2021-12-26 22:04:55 +00:00
										 |  |  |             $attachment = self::getAvatar($actor_id)->getAttachment(); | 
					
						
							|  |  |  |             return ['width' => (int) $attachment->getWidth(), 'height' => (int) $attachment->getHeight()]; | 
					
						
							| 
									
										
										
										
											2021-12-02 15:36:08 +00:00
										 |  |  |         } catch (NoAvatarException) { | 
					
						
							| 
									
										
										
										
											2021-12-26 22:04:55 +00:00
										 |  |  |             return ['width' => (int) (Common::config('thumbnail', 'small')), 'height' => (int) (Common::config('thumbnail', 'small'))]; | 
					
						
							| 
									
										
										
										
											2021-12-02 15:36:08 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-18 02:17:57 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-09-18 03:22:27 +01:00
										 |  |  |      * Get the cached avatar file info associated with the given Actor id | 
					
						
							| 
									
										
										
										
											2021-04-18 02:17:57 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * Returns the avatar file's hash, mimetype, title and path. | 
					
						
							|  |  |  |      * Ensures exactly one cached value exists | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-12-27 04:47:04 +00:00
										 |  |  |     public static function getAvatarFileInfo(int $actor_id, string $size = 'medium'): array | 
					
						
							| 
									
										
										
										
											2021-04-18 02:17:57 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-10-10 09:26:18 +01:00
										 |  |  |         $res = Cache::get( | 
					
						
							|  |  |  |             "avatar-file-info-{$actor_id}-{$size}", | 
					
						
							| 
									
										
										
										
											2021-09-18 03:22:27 +01:00
										 |  |  |             function () use ($actor_id) { | 
					
						
							| 
									
										
										
										
											2021-10-10 09:26:18 +01:00
										 |  |  |                 return DB::dql( | 
					
						
							|  |  |  |                     'select f.id, f.filename, a.title, f.mimetype ' | 
					
						
							| 
									
										
										
										
											2021-12-02 15:12:31 +00:00
										 |  |  |                     . 'from Component\Attachment\Entity\Attachment f ' | 
					
						
							| 
									
										
										
										
											2021-10-10 09:26:18 +01:00
										 |  |  |                     . 'join Component\Avatar\Entity\Avatar a with f.id = a.attachment_id ' | 
					
						
							|  |  |  |                     . 'where a.actor_id = :actor_id', | 
					
						
							|  |  |  |                     ['actor_id' => $actor_id], | 
					
						
							|  |  |  |                 ); | 
					
						
							|  |  |  |             }, | 
					
						
							| 
									
										
										
										
											2021-08-05 03:15:07 +01:00
										 |  |  |         ); | 
					
						
							|  |  |  |         if ($res === []) { // Avatar not found
 | 
					
						
							| 
									
										
										
										
											2021-04-18 02:17:57 +01:00
										 |  |  |             $filepath = INSTALLDIR . '/public/assets/default-avatar.svg'; | 
					
						
							| 
									
										
										
										
											2021-08-17 21:46:09 +01:00
										 |  |  |             return [ | 
					
						
							|  |  |  |                 'id'       => null, | 
					
						
							|  |  |  |                 'filepath' => $filepath, | 
					
						
							|  |  |  |                 'mimetype' => 'image/svg+xml', | 
					
						
							|  |  |  |                 'filename' => null, | 
					
						
							|  |  |  |                 'title'    => 'default_avatar.svg', | 
					
						
							|  |  |  |             ]; | 
					
						
							| 
									
										
										
										
											2021-08-05 03:15:07 +01:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2021-12-27 04:47:04 +00:00
										 |  |  |             $res = $res[0]; // A user must always only have one avatar.
 | 
					
						
							|  |  |  |             if ($size === 'full') { | 
					
						
							|  |  |  |                 $res['filepath'] = Attachment::getByPK(['id' => $res['id']])->getPath(); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 $res['filepath'] = AttachmentThumbnail::getOrCreate(Attachment::getByPK(['id' => $res['id']]), $size)->getPath(); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-08-05 03:15:07 +01:00
										 |  |  |             return $res; | 
					
						
							| 
									
										
										
										
											2021-04-18 02:17:57 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |