Refactor User::getAvatarUrlForId into getAvatarUrl

- Use user/owner array as parameter instead of uid
- Rename $profile variables to $owner when it's the result of User::getOwnerDataByNick
- Replace Module\Photo::stripExtension with native pathinfo() calls
This commit is contained in:
Hypolite Petovan 2021-10-02 17:28:29 -04:00
parent 810699b454
commit 7cdd2d1336
14 changed files with 78 additions and 107 deletions

View file

@ -653,9 +653,9 @@ class Contact
'nick' => $user['nickname'],
'pubkey' => $user['pubkey'],
'prvkey' => $user['prvkey'],
'photo' => User::getAvatarUrlForId($user['uid']),
'thumb' => User::getAvatarUrlForId($user['uid'], Proxy::SIZE_THUMB),
'micro' => User::getAvatarUrlForId($user['uid'], Proxy::SIZE_MICRO),
'photo' => User::getAvatarUrl($user),
'thumb' => User::getAvatarUrl($user, Proxy::SIZE_THUMB),
'micro' => User::getAvatarUrl($user, Proxy::SIZE_MICRO),
'blocked' => 0,
'pending' => 0,
'url' => DI::baseUrl() . '/profile/' . $user['nickname'],
@ -709,7 +709,7 @@ class Contact
return false;
}
$fields = ['nickname', 'page-flags', 'account-type', 'prvkey', 'pubkey'];
$fields = ['uid', 'nickname', 'page-flags', 'account-type', 'prvkey', 'pubkey'];
$user = DBA::selectFirst('user', $fields, ['uid' => $uid, 'account_expired' => false]);
if (!DBA::isResult($user)) {
return false;
@ -768,7 +768,7 @@ class Contact
$fields['micro'] = self::getDefaultAvatar($fields, Proxy::SIZE_MICRO);
}
$fields['avatar'] = User::getAvatarUrlForId($uid);
$fields['avatar'] = User::getAvatarUrl($user);
$fields['forum'] = $user['page-flags'] == User::PAGE_FLAGS_COMMUNITY;
$fields['prv'] = $user['page-flags'] == User::PAGE_FLAGS_PRVGROUP;
$fields['unsearchable'] = !$profile['net-publish'];
@ -795,8 +795,8 @@ class Contact
// Update the profile
$fields = [
'photo' => User::getAvatarUrlForId($uid),
'thumb' => User::getAvatarUrlForId($uid, Proxy::SIZE_THUMB)
'photo' => User::getAvatarUrl($user),
'thumb' => User::getAvatarUrl($user, Proxy::SIZE_THUMB)
];
DBA::update('profile', $fields, ['uid' => $uid]);

View file

@ -799,22 +799,6 @@ class Photo
Photo::update($fields, $condition);
}
/**
* Strips known picture extensions from picture links
*
* @param string $name Picture link
* @return string stripped picture link
* @throws \Exception
*/
public static function stripExtension($name)
{
$name = str_replace([".jpg", ".png", ".gif"], ["", "", ""], $name);
foreach (Images::supportedTypes() as $m => $e) {
$name = str_replace("." . $e, "", $name);
}
return $name;
}
/**
* Fetch the guid and scale from picture links
*
@ -831,7 +815,7 @@ class Photo
return [];
}
$guid = self::stripExtension($guid);
$guid = pathinfo($guid, PATHINFO_FILENAME);
if (substr($guid, -2, 1) != "-") {
return [];
}

View file

@ -841,13 +841,14 @@ class User
}
/**
* Get avatar link for given user id
* Get avatar link for given user
*
* @param integer $uid user id
* @param string $size One of the ProxyUtils::SIZE_* constants
* @param array $user
* @param string $size One of the ProxyUtils::SIZE_* constants
* @return string avatar link
* @throws Exception
*/
public static function getAvatarUrlForId(int $uid, string $size = ''):string
public static function getAvatarUrl(array $user, string $size = ''):string
{
$url = DI::baseUrl() . '/photo/';
@ -869,26 +870,16 @@ class User
$updated = '';
$imagetype = IMAGETYPE_JPEG;
$photo = Photo::selectFirst(['type', 'created', 'edited', 'updated'], ["scale" => $scale, 'uid' => $uid, 'profile' => true]);
$photo = Photo::selectFirst(['type', 'created', 'edited', 'updated'], ["scale" => $scale, 'uid' => $user['uid'], 'profile' => true]);
if (!empty($photo)) {
$updated = max($photo['created'], $photo['edited'], $photo['updated']);
switch ($photo['type']) {
case 'image/png':
$imagetype = IMAGETYPE_PNG;
break;
case 'image/gif':
$imagetype = IMAGETYPE_PNG;
break;
default:
$imagetype = IMAGETYPE_JPEG;
break;
if (in_array($photo['type'], ['image/png', 'image/gif'])) {
$imagetype = IMAGETYPE_PNG;
}
}
return $url . $uid . image_type_to_extension($imagetype) . ($updated ? '?ts=' . strtotime($updated) : '');
return $url . $user['uid'] . image_type_to_extension($imagetype) . ($updated ? '?ts=' . strtotime($updated) : '');
}
/**
@ -1105,8 +1096,8 @@ class User
$insert_result = DBA::insert('profile', [
'uid' => $uid,
'name' => $username,
'photo' => self::getAvatarUrlForId($uid),
'thumb' => self::getAvatarUrlForId($uid, Proxy::SIZE_THUMB),
'photo' => self::getAvatarUrl($user),
'thumb' => self::getAvatarUrl($user, Proxy::SIZE_THUMB),
'publish' => $publish,
'net-publish' => $netpublish,
]);