*/
class Contact
{
+ const DEFAULT_AVATAR_PHOTO = '/images/person-300.jpg';
+ const DEFAULT_AVATAR_THUMB = '/images/person-80.jpg';
+ const DEFAULT_AVATAR_MICRO = '/images/person-48.jpg';
+
/**
* @deprecated since version 2019.03
* @see User::PAGE_FLAGS_NORMAL
$fields['micro'] = $prefix . '6' . $suffix;
} else {
// We hadn't found a photo entry, so we use the default avatar
- $fields['photo'] = DI::baseUrl() . '/images/person-300.jpg';
- $fields['thumb'] = DI::baseUrl() . '/images/person-80.jpg';
- $fields['micro'] = DI::baseUrl() . '/images/person-48.jpg';
+ $fields['photo'] = DI::baseUrl() . self::DEFAULT_AVATAR_PHOTO;
+ $fields['thumb'] = DI::baseUrl() . self::DEFAULT_AVATAR_THUMB;
+ $fields['micro'] = DI::baseUrl() . self::DEFAULT_AVATAR_MICRO;
}
$fields['avatar'] = DI::baseUrl() . '/photo/profile/' .$uid . '.' . $file_suffix;
$contact_field = ((($contact["contact-type"] == self::TYPE_COMMUNITY) || ($contact['network'] == Protocol::MAIL)) ? 'owner-id' : 'author-id');
if ($thread_mode) {
- $condition = ["`$contact_field` = ? AND `gravity` = ? AND " . $sql,
- $cid, GRAVITY_PARENT, local_user()];
+ $condition = ["`$contact_field` = ? AND (`gravity` = ? OR (`gravity` = ? AND `vid` = ?)) AND " . $sql,
+ $cid, GRAVITY_PARENT, GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE), local_user()];
} else {
$condition = ["`$contact_field` = ? AND `gravity` IN (?, ?) AND " . $sql,
$cid, GRAVITY_PARENT, GRAVITY_COMMENT, local_user()];
'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
if ($thread_mode) {
- $r = Item::selectThreadForUser(local_user(), ['uri'], $condition, $params);
-
- $items = Item::inArray($r);
+ $r = Item::selectForUser(local_user(), ['uri', 'gravity', 'parent-uri'], $condition, $params);
+ $items = [];
+ while ($item = DBA::fetch($r)) {
+ if ($item['gravity'] != GRAVITY_PARENT) {
+ $item['uri'] = $item['parent-uri'];
+ }
+ unset($item['parent-uri']);
+ unset($item['gravity']);
+
+ $items[] = $item;
+ }
+ DBA::close($r);
$o = conversation($a, $items, 'contacts', $update, false, 'commented', local_user());
} else {
*/
public static function getPhoto(array $contact, string $avatar = '')
{
- return self::getAvatarPath($contact, 'photo', DI::baseUrl() . '/images/person-300.jpg', Proxy::SIZE_SMALL, $avatar);
+ return self::getAvatarPath($contact, 'photo', DI::baseUrl() . self::DEFAULT_AVATAR_PHOTO, Proxy::SIZE_SMALL, $avatar);
}
/**
*/
public static function getThumb(array $contact, string $avatar = '')
{
- return self::getAvatarPath($contact, 'thumb', DI::baseUrl() . '/images/person-80.jpg', Proxy::SIZE_THUMB, $avatar);
+ return self::getAvatarPath($contact, 'thumb', DI::baseUrl() . self::DEFAULT_AVATAR_THUMB, Proxy::SIZE_THUMB, $avatar);
}
/**
*/
public static function getMicro(array $contact, string $avatar = '')
{
- return self::getAvatarPath($contact, 'micro', DI::baseUrl() . '/images/person-48.jpg', Proxy::SIZE_MICRO, $avatar);
+ return self::getAvatarPath($contact, 'micro', DI::baseUrl() . self::DEFAULT_AVATAR_MICRO, Proxy::SIZE_MICRO, $avatar);
}
/**
/// add the default avatars if the fields aren't filled
if (isset($contact['photo']) && empty($contact['photo'])) {
- $contact['photo'] = DI::baseUrl() . '/images/person-300.jpg';
+ $contact['photo'] = DI::baseUrl() . self::DEFAULT_AVATAR_PHOTO;
}
if (isset($contact['thumb']) && empty($contact['thumb'])) {
- $contact['thumb'] = DI::baseUrl() . '/images/person-80.jpg';
+ $contact['thumb'] = DI::baseUrl() . self::DEFAULT_AVATAR_THUMB;
}
if (isset($contact['micro']) && empty($contact['micro'])) {
- $contact['micro'] = DI::baseUrl() . '/images/person-48.jpg';
+ $contact['micro'] = DI::baseUrl() . self::DEFAULT_AVATAR_MICRO;
}
return $contact;
/**
* Updates the avatar links in a contact only if needed
*
- * @param int $cid Contact id
- * @param string $avatar Link to avatar picture
- * @param bool $force force picture update
+ * @param int $cid Contact id
+ * @param string $avatar Link to avatar picture
+ * @param bool $force force picture update
+ * @param bool $create_cache Enforces the creation of cached avatar fields
*
* @return void
* @throws HTTPException\InternalServerErrorException
* @throws HTTPException\NotFoundException
* @throws \ImagickException
*/
- public static function updateAvatar(int $cid, string $avatar, bool $force = false)
+ public static function updateAvatar(int $cid, string $avatar, bool $force = false, bool $create_cache = false)
{
- $contact = DBA::selectFirst('contact', ['uid', 'avatar', 'photo', 'thumb', 'micro', 'nurl'], ['id' => $cid, 'self' => false]);
+ $contact = DBA::selectFirst('contact', ['uid', 'avatar', 'photo', 'thumb', 'micro', 'nurl', 'url', 'network'], ['id' => $cid, 'self' => false]);
if (!DBA::isResult($contact)) {
return;
}
$uid = $contact['uid'];
// Only update the cached photo links of public contacts when they already are cached
- if (($uid == 0) && !$force && empty($contact['thumb']) && empty($contact['micro'])) {
+ if (($uid == 0) && !$force && empty($contact['thumb']) && empty($contact['micro']) && !$create_cache) {
if ($contact['avatar'] != $avatar) {
DBA::update('contact', ['avatar' => $avatar], ['id' => $cid]);
Logger::info('Only update the avatar', ['id' => $cid, 'avatar' => $avatar, 'contact' => $contact]);
return;
}
- $data = [
- $contact['photo'] ?? '',
- $contact['thumb'] ?? '',
- $contact['micro'] ?? '',
- ];
+ // User contacts use are updated through the public contacts
+ if (($uid != 0) && !in_array($contact['network'], [Protocol::FEED, Protocol::MAIL])) {
+ $pcid = self::getIdForURL($contact['url'], false);
+ if (!empty($pcid)) {
+ Logger::debug('Update the private contact via the public contact', ['id' => $cid, 'uid' => $uid, 'public' => $pcid]);
+ self::updateAvatar($pcid, $avatar, $force, true);
+ return;
+ }
+ }
+
+ // Replace cached avatar pictures from the default avatar with the default avatars in different sizes
+ if (strpos($avatar, self::DEFAULT_AVATAR_PHOTO)) {
+ $fields = ['avatar' => $avatar, 'avatar-date' => DateTimeFormat::utcNow(),
+ 'photo' => DI::baseUrl() . self::DEFAULT_AVATAR_PHOTO,
+ 'thumb' => DI::baseUrl() . self::DEFAULT_AVATAR_THUMB,
+ 'micro' => DI::baseUrl() . self::DEFAULT_AVATAR_MICRO];
+ Logger::debug('Use default avatar', ['id' => $cid, 'uid' => $uid]);
+ }
- $update = ($contact['avatar'] != $avatar) || $force;
+ // Use the data from the self account
+ if (empty($fields)) {
+ $local_uid = User::getIdForURL($contact['url']);
+ if (!empty($local_uid)) {
+ $fields = self::selectFirst(['avatar', 'avatar-date', 'photo', 'thumb', 'micro'], ['self' => true, 'uid' => $local_uid]);
+ Logger::debug('Use owner data', ['id' => $cid, 'uid' => $uid, 'owner-uid' => $local_uid]);
+ }
+ }
- if (!$update) {
- foreach ($data as $image_uri) {
- $image_rid = Photo::ridFromURI($image_uri);
- if ($image_rid && !Photo::exists(['resource-id' => $image_rid, 'uid' => $uid])) {
- Logger::info('Regenerating avatar', ['contact uid' => $uid, 'cid' => $cid, 'missing photo' => $image_rid, 'avatar' => $contact['avatar']]);
- $update = true;
+ if (empty($fields)) {
+ $update = ($contact['avatar'] != $avatar) || $force;
+
+ if (!$update) {
+ $data = [
+ $contact['photo'] ?? '',
+ $contact['thumb'] ?? '',
+ $contact['micro'] ?? '',
+ ];
+
+ foreach ($data as $image_uri) {
+ $image_rid = Photo::ridFromURI($image_uri);
+ if ($image_rid && !Photo::exists(['resource-id' => $image_rid, 'uid' => $uid])) {
+ Logger::debug('Regenerating avatar', ['contact uid' => $uid, 'cid' => $cid, 'missing photo' => $image_rid, 'avatar' => $contact['avatar']]);
+ $update = true;
+ }
+ }
+ }
+
+ if ($update) {
+ $photos = Photo::importProfilePhoto($avatar, $uid, $cid, true);
+ if ($photos) {
+ $fields = ['avatar' => $avatar, 'photo' => $photos[0], 'thumb' => $photos[1], 'micro' => $photos[2], 'avatar-date' => DateTimeFormat::utcNow()];
+ $update = !empty($fields);
+ Logger::debug('Created new cached avatars', ['id' => $cid, 'uid' => $uid, 'owner-uid' => $local_uid]);
+ } else {
+ $update = false;
}
}
+ } else {
+ $update = ($fields['photo'] . $fields['thumb'] . $fields['micro'] != $contact['photo'] . $contact['thumb'] . $contact['micro']) || $force;
}
- if ($update) {
- $photos = Photo::importProfilePhoto($avatar, $uid, $cid, true);
- if ($photos) {
- $fields = ['avatar' => $avatar, 'photo' => $photos[0], 'thumb' => $photos[1], 'micro' => $photos[2], 'avatar-date' => DateTimeFormat::utcNow()];
- DBA::update('contact', $fields, ['id' => $cid]);
- } elseif (empty($contact['avatar'])) {
- // Ensure that the avatar field is set
- DBA::update('contact', ['avatar' => $avatar], ['id' => $cid]);
- Logger::info('Failed profile import', ['id' => $cid, 'force' => $force, 'avatar' => $avatar, 'contact' => $contact]);
+ if (!$update) {
+ return;
+ }
+
+ $cids = [];
+ $uids = [];
+ if (($uid == 0) && !in_array($contact['network'], [Protocol::FEED, Protocol::MAIL])) {
+ // Collect all user contacts of the given public contact
+ $personal_contacts = DBA::select('contact', ['id', 'uid'],
+ ["`nurl` = ? AND `id` != ? AND NOT `self`", $contact['nurl'], $cid]);
+ while ($personal_contact = DBA::fetch($personal_contacts)) {
+ $cids[] = $personal_contact['id'];
+ $uids[] = $personal_contact['uid'];
+ }
+ DBA::close($personal_contacts);
+
+ if (!empty($cids)) {
+ // Delete possibly existing cached user contact avatars
+ Photo::delete(['uid' => $uids, 'contact-id' => $cids, 'album' => Photo::CONTACT_PHOTOS]);
}
}
+
+ $cids[] = $cid;
+ $uids[] = $uid;
+ Logger::info('Updating cached contact avatars', ['cid' => $cids, 'uid' => $uids, 'fields' => $fields]);
+ DBA::update('contact', $fields, ['id' => $cids]);
}
/**