2021-04-18 10:17:57 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
// {{{ 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-08-05 11:15:07 +09:00
|
|
|
use App\Core\Router\Router;
|
2021-04-18 10:17:57 +09:00
|
|
|
use App\Util\Common;
|
2021-07-30 04:20:32 +09:00
|
|
|
use Component\Avatar\Controller as C;
|
2021-04-18 10:17:57 +09:00
|
|
|
use Component\Avatar\Exception\NoAvatarException;
|
|
|
|
use Symfony\Component\Asset\Package;
|
|
|
|
use Symfony\Component\Asset\VersionStrategy\EmptyVersionStrategy;
|
2021-07-30 04:20:32 +09:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2021-04-18 10:17:57 +09:00
|
|
|
|
|
|
|
class Avatar extends Component
|
|
|
|
{
|
2021-08-05 03:12:37 +09:00
|
|
|
public function onAddRoute($r): bool
|
2021-04-18 10:17:57 +09:00
|
|
|
{
|
2021-04-29 21:55:26 +09:00
|
|
|
$r->connect('avatar', '/{gsactor_id<\d+>}/avatar/{size<full|big|medium|small>?full}', [Controller\Avatar::class, 'avatar_view']);
|
|
|
|
$r->connect('settings_avatar', '/settings/avatar', [Controller\Avatar::class, 'settings_avatar']);
|
2021-04-18 10:17:57 +09:00
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2021-08-05 03:12:37 +09:00
|
|
|
public function onPopulateProfileSettingsTabs(Request $request, &$tabs): bool
|
2021-07-30 04:20:32 +09:00
|
|
|
{
|
|
|
|
// TODO avatar template shouldn't be on settings folder
|
2021-08-05 03:12:37 +09:00
|
|
|
$tabs[] = [
|
|
|
|
'title' => 'Avatar',
|
|
|
|
'desc' => 'Change your avatar.',
|
|
|
|
'controller' => C\Avatar::settings_avatar($request),
|
2021-07-30 04:20:32 +09:00
|
|
|
];
|
|
|
|
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2021-08-05 03:12:37 +09:00
|
|
|
public function onStartTwigPopulateVars(array &$vars): bool
|
2021-04-18 10:17:57 +09:00
|
|
|
{
|
|
|
|
if (Common::user() != null) {
|
|
|
|
$vars['user_avatar'] = self::getAvatarUrl();
|
|
|
|
}
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2021-08-05 03:12:37 +09:00
|
|
|
public function onGetAvatarUrl(int $gsactor_id, ?string &$url): bool
|
2021-04-18 10:17:57 +09:00
|
|
|
{
|
|
|
|
$url = self::getAvatarUrl($gsactor_id);
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2021-08-05 11:15:07 +09:00
|
|
|
public function onAvatarUpdate(int $gsactor_id): bool
|
2021-04-18 10:17:57 +09:00
|
|
|
{
|
|
|
|
Cache::delete('avatar-' . $gsactor_id);
|
|
|
|
Cache::delete('avatar-url-' . $gsactor_id);
|
|
|
|
Cache::delete('avatar-file-info-' . $gsactor_id);
|
2021-08-05 11:15:07 +09:00
|
|
|
return Event::next;
|
2021-04-18 10:17:57 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// UTILS ----------------------------------
|
|
|
|
|
|
|
|
/**
|
2021-08-05 11:15:07 +09:00
|
|
|
* Get the avatar associated with the given GSActor id
|
2021-04-18 10:17:57 +09:00
|
|
|
*/
|
2021-08-05 03:12:37 +09:00
|
|
|
public static function getAvatar(?int $gsactor_id = null): Entity\Avatar
|
2021-04-18 10:17:57 +09:00
|
|
|
{
|
2021-08-05 11:15:07 +09:00
|
|
|
$gsactor_id = $gsactor_id ?: Common::userId();
|
2021-04-18 10:17:57 +09:00
|
|
|
return GSFile::error(NoAvatarException::class,
|
|
|
|
$gsactor_id,
|
|
|
|
Cache::get("avatar-{$gsactor_id}",
|
|
|
|
function () use ($gsactor_id) {
|
2021-08-05 03:12:37 +09:00
|
|
|
return DB::dql('select a from Component\Avatar\Entity\Avatar a ' .
|
2021-04-18 10:17:57 +09:00
|
|
|
'where a.gsactor_id = :gsactor_id',
|
|
|
|
['gsactor_id' => $gsactor_id]);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-08-05 11:15:07 +09:00
|
|
|
* Get the cached avatar associated with the given GSActor id, or the current user if not given
|
2021-04-18 10:17:57 +09:00
|
|
|
*/
|
|
|
|
public static function getAvatarUrl(?int $gsactor_id = null): string
|
|
|
|
{
|
|
|
|
$gsactor_id = $gsactor_id ?: Common::userId();
|
|
|
|
return Cache::get("avatar-url-{$gsactor_id}", function () use ($gsactor_id) {
|
2021-08-05 11:15:07 +09:00
|
|
|
$attachment_id = self::getAvatarFileInfo($gsactor_id)['id'];
|
|
|
|
if ($attachment_id !== null) {
|
|
|
|
return Router::url('attachment_view', ['id' => $attachment_id]);
|
|
|
|
} else {
|
|
|
|
$package = new Package(new EmptyVersionStrategy());
|
|
|
|
return $package->getUrl(Common::config('avatar', 'default'));
|
2021-04-18 10:17:57 +09:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the cached avatar file info associated with the given GSActor id
|
|
|
|
*
|
|
|
|
* Returns the avatar file's hash, mimetype, title and path.
|
|
|
|
* Ensures exactly one cached value exists
|
|
|
|
*/
|
|
|
|
public static function getAvatarFileInfo(int $gsactor_id): array
|
|
|
|
{
|
2021-08-05 11:15:07 +09:00
|
|
|
$res = Cache::get("avatar-file-info-{$gsactor_id}",
|
|
|
|
function () use ($gsactor_id) {
|
2021-08-15 00:47:45 +09:00
|
|
|
return DB::dql('select f.id, f.filename, f.mimetype ' .
|
2021-08-05 11:15:07 +09:00
|
|
|
'from App\Entity\Attachment f ' .
|
|
|
|
'join Component\Avatar\Entity\Avatar a with f.id = a.attachment_id ' .
|
|
|
|
'where a.gsactor_id = :gsactor_id',
|
|
|
|
['gsactor_id' => $gsactor_id]);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
if ($res === []) { // Avatar not found
|
2021-04-18 10:17:57 +09:00
|
|
|
$filepath = INSTALLDIR . '/public/assets/default-avatar.svg';
|
2021-08-05 11:15:07 +09:00
|
|
|
return ['id' => null, 'file_path' => $filepath, 'mimetype' => 'image/svg+xml', 'title' => null];
|
|
|
|
} else {
|
|
|
|
$res = $res[0]; // A user must always only have one avatar.
|
|
|
|
$res['file_path'] = DB::findOneBy('attachment', ['id' => $res['id']])->getPath();
|
|
|
|
return $res;
|
2021-04-18 10:17:57 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|