X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FModel%2FContact.php;h=30158c5b4f7b627a474c268875ee16bb9f53238c;hb=456ae169aba480141c43616eca385605aa1911d4;hp=b0b5fcc91b0dd37bedfb3de5b4efe83d0906434a;hpb=531ef6e6e2a7c5968036ce1e7b8a0caaca7accea;p=friendica.git diff --git a/src/Model/Contact.php b/src/Model/Contact.php index b0b5fcc91b..cbe370a4aa 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -1,6 +1,6 @@ DBA::lastInsertId()]); + DBA::insert('contact', $fields, $duplicate_mode); + $contact = DBA::selectFirst('contact', [], ['id' => DBA::lastInsertId()]); if (!DBA::isResult($contact)) { // Shouldn't happen - return $ret; + Logger::warning('Created contact could not be found', ['fields' => $fields]); + return 0; } + Contact\User::insertForContactArray($contact); + // Search for duplicated contacts and get rid of them - self::removeDuplicates($contact['nurl'], $contact['uid']); + if (!$contact['self']) { + self::removeDuplicates($contact['nurl'], $contact['uid']); + } + + return $contact['id']; + } + + /** + * Updates rows in the contact table + * + * @param array $fields contains the fields that are updated + * @param array $condition condition array with the key values + * @param array|boolean $old_fields array with the old field values that are about to be replaced (true = update on duplicate, false = don't update identical fields) + * + * @return boolean was the update successfull? + * @throws \Exception + */ + public static function update(array $fields, array $condition, $old_fields = []) + { + $ret = DBA::update('contact', $fields, $condition, $old_fields); + + // Apply changes to the "user-contact" table on dedicated fields + Contact\User::updateByContactUpdate($fields, $condition); return $ret; } @@ -264,14 +291,14 @@ class Contact $condition = ['`alias` IN (?, ?, ?) AND `uid` = ? AND NOT `deleted`', $url, Strings::normaliseLink($url), $ssl_url, $uid]; $contact = DBA::selectFirst('contact', $fields, $condition, $options); } - + if (!DBA::isResult($contact)) { return []; } // Update the contact in the background if needed $updated = max($contact['success_update'], $contact['created'], $contact['updated'], $contact['last-update'], $contact['failure_update']); - if (($updated < DateTimeFormat::utc('now -7 days')) && in_array($contact['network'], Protocol::FEDERATED)) { + if (($updated < DateTimeFormat::utc('now -7 days')) && in_array($contact['network'], Protocol::FEDERATED) && !self::isLocalById($contact['id'])) { Worker::add(PRIORITY_LOW, "UpdateContact", $contact['id']); } @@ -307,7 +334,7 @@ class Contact } $contact = self::getByURL($url, $update, $fields); - if (!empty($contact['id'])) { + if (!empty($contact['id'])) { $contact['cid'] = 0; $contact['zid'] = $contact['id']; } @@ -330,7 +357,7 @@ class Contact return false; } - $cdata = self::getPublicAndUserContacID($cid, $uid); + $cdata = self::getPublicAndUserContactID($cid, $uid); if (empty($cdata['user'])) { return false; } @@ -376,7 +403,7 @@ class Contact return false; } - $cdata = self::getPublicAndUserContacID($cid, $uid); + $cdata = self::getPublicAndUserContactID($cid, $uid); if (empty($cdata['user'])) { return false; } @@ -452,6 +479,11 @@ class Contact */ public static function isLocal($url) { + if (!parse_url($url, PHP_URL_SCHEME)) { + $addr_parts = explode('@', $url); + return (count($addr_parts) == 2) && ($addr_parts[1] == DI::baseUrl()->getHostname()); + } + return Strings::compareLink(self::getBasepath($url, true), DI::baseUrl()); } @@ -505,7 +537,48 @@ class Contact * @throws HTTPException\InternalServerErrorException * @throws \ImagickException */ - public static function getPublicAndUserContacID($cid, $uid) + public static function getPublicAndUserContactID($cid, $uid) + { + // We have to use the legacy function as long as the post update hasn't finished + if (DI::config()->get('system', 'post_update_version') < 1427) { + return self::legacyGetPublicAndUserContactID($cid, $uid); + } + + if (empty($uid) || empty($cid)) { + return []; + } + + $contact = DBA::selectFirst('account-user-view', ['id', 'uid', 'pid'], ['id' => $cid]); + if (!DBA::isResult($contact) || !in_array($contact['uid'], [0, $uid])) { + return []; + } + + $pcid = $contact['pid']; + if ($contact['uid'] == $uid) { + $ucid = $contact['id']; + } else { + $contact = DBA::selectFirst('account-user-view', ['id', 'uid'], ['pid' => $cid, 'uid' => $uid]); + if (DBA::isResult($contact)) { + $ucid = $contact['id']; + } else { + $ucid = 0; + } + } + + return ['public' => $pcid, 'user' => $ucid]; + } + + /** + * Helper function for "getPublicAndUserContactID" + * + * @param int $cid Either public contact id or user's contact id + * @param int $uid User ID + * + * @return array with public and user's contact id + * @throws HTTPException\InternalServerErrorException + * @throws \ImagickException + */ + private static function legacyGetPublicAndUserContactID($cid, $uid) { if (empty($uid) || empty($cid)) { return []; @@ -566,18 +639,13 @@ class Contact */ public static function createSelfFromUserId($uid) { - // Only create the entry if it doesn't exist yet - if (DBA::exists('contact', ['uid' => $uid, 'self' => true])) { - return true; - } - $user = DBA::selectFirst('user', ['uid', 'username', 'nickname', 'pubkey', 'prvkey'], ['uid' => $uid, 'account_expired' => false]); if (!DBA::isResult($user)) { return false; } - $return = DBA::insert('contact', [ + $contact = [ 'uid' => $user['uid'], 'created' => DateTimeFormat::utcNow(), 'self' => 1, @@ -585,9 +653,9 @@ class Contact 'nick' => $user['nickname'], 'pubkey' => $user['pubkey'], 'prvkey' => $user['prvkey'], - 'photo' => DI::baseUrl() . '/photo/profile/' . $user['uid'] . '.jpg', - 'thumb' => DI::baseUrl() . '/photo/avatar/' . $user['uid'] . '.jpg', - 'micro' => DI::baseUrl() . '/photo/micro/' . $user['uid'] . '.jpg', + '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'], @@ -602,7 +670,23 @@ class Contact 'uri-date' => DateTimeFormat::utcNow(), 'avatar-date' => DateTimeFormat::utcNow(), 'closeness' => 0 - ]); + ]; + + $return = true; + + // Only create the entry if it doesn't exist yet + if (!DBA::exists('contact', ['uid' => $uid, 'self' => true])) { + $return = (bool)self::insert($contact); + } + + // Create the public contact + if (!DBA::exists('contact', ['nurl' => $contact['nurl'], 'uid' => 0])) { + $contact['self'] = false; + $contact['uid'] = 0; + $contact['prvkey'] = null; + + self::insert($contact, Database::INSERT_IGNORE); + } return $return; } @@ -612,29 +696,30 @@ class Contact * * @param int $uid * @param boolean $update_avatar Force the avatar update + * @return bool "true" if updated * @throws HTTPException\InternalServerErrorException */ public static function updateSelfFromUserID($uid, $update_avatar = false) { $fields = ['id', 'name', 'nick', 'location', 'about', 'keywords', 'avatar', 'prvkey', 'pubkey', - 'xmpp', 'contact-type', 'forum', 'prv', 'avatar-date', 'url', 'nurl', 'unsearchable', - 'photo', 'thumb', 'micro', 'addr', 'request', 'notify', 'poll', 'confirm', 'poco']; + 'xmpp', 'matrix', 'contact-type', 'forum', 'prv', 'avatar-date', 'url', 'nurl', 'unsearchable', + 'photo', 'thumb', 'micro', 'addr', 'request', 'notify', 'poll', 'confirm', 'poco', 'network']; $self = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]); if (!DBA::isResult($self)) { - return; + 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; + return false; } $fields = ['name', 'photo', 'thumb', 'about', 'address', 'locality', 'region', - 'country-name', 'pub_keywords', 'xmpp', 'net-publish']; + 'country-name', 'pub_keywords', 'xmpp', 'matrix', 'net-publish']; $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid]); if (!DBA::isResult($profile)) { - return; + return false; } $file_suffix = 'jpg'; @@ -643,7 +728,7 @@ class Contact 'avatar-date' => $self['avatar-date'], 'location' => Profile::formatLocation($profile), 'about' => $profile['about'], 'keywords' => $profile['pub_keywords'], 'contact-type' => $user['account-type'], 'prvkey' => $user['prvkey'], - 'pubkey' => $user['pubkey'], 'xmpp' => $profile['xmpp']]; + 'pubkey' => $user['pubkey'], 'xmpp' => $profile['xmpp'], 'matrix' => $profile['matrix'], 'network' => Protocol::DFRN]; // it seems as if ported accounts can have wrong values, so we make sure that now everything is fine. $fields['url'] = DI::baseUrl() . '/profile/' . $user['nickname']; @@ -683,7 +768,7 @@ class Contact $fields['micro'] = self::getDefaultAvatar($fields, Proxy::SIZE_MICRO); } - $fields['avatar'] = DI::baseUrl() . '/photo/profile/' .$uid . '.' . $file_suffix; + $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']; @@ -701,23 +786,29 @@ class Contact $fields['name-date'] = DateTimeFormat::utcNow(); } $fields['updated'] = DateTimeFormat::utcNow(); - DBA::update('contact', $fields, ['id' => $self['id']]); + self::update($fields, ['id' => $self['id']]); // Update the public contact as well - DBA::update('contact', $fields, ['uid' => 0, 'nurl' => $self['nurl']]); + $fields['prvkey'] = null; + $fields['self'] = false; + self::update($fields, ['uid' => 0, 'nurl' => $self['nurl']]); // Update the profile - $fields = ['photo' => DI::baseUrl() . '/photo/profile/' .$uid . '.' . $file_suffix, - 'thumb' => DI::baseUrl() . '/photo/avatar/' . $uid .'.' . $file_suffix]; + $fields = [ + 'photo' => User::getAvatarUrl($user), + 'thumb' => User::getAvatarUrl($user, Proxy::SIZE_THUMB) + ]; + DBA::update('profile', $fields, ['uid' => $uid]); } + + return $update; } /** * Marks a contact for removal * * @param int $id contact id - * @return null * @throws HTTPException\InternalServerErrorException */ public static function remove($id) @@ -729,61 +820,69 @@ class Contact } // Archive the contact - DBA::update('contact', ['archive' => true, 'network' => Protocol::PHANTOM, 'deleted' => true], ['id' => $id]); + self::update(['archive' => true, 'network' => Protocol::PHANTOM, 'deleted' => true], ['id' => $id]); // Delete it in the background Worker::add(PRIORITY_MEDIUM, 'RemoveContact', $id); } /** - * Sends an unfriend message. Does not remove the contact + * Sends an unfriend message. Removes the contact for two-way unfriending or sharing only protocols (feed an mail) + * + * @param array $user User unfriending + * @param array $contact Contact (uid != 0) unfriended + * @param boolean $two_way Revoke eventual inbound follow as well + * @return bool|null true if successful, false if not, null if no remote action was performed + * @throws HTTPException\InternalServerErrorException + * @throws \ImagickException + */ + public static function terminateFriendship(array $user, array $contact): ?bool + { + $result = Protocol::terminateFriendship($user, $contact); + + if ($contact['rel'] == Contact::SHARING || in_array($contact['network'], [Protocol::FEED, Protocol::MAIL])) { + self::remove($contact['id']); + } else { + self::update(['rel' => Contact::FOLLOWER], ['id' => $contact['id']]); + } + + return $result; + } + + /** + * Revoke follow privileges of the remote user contact * - * @param array $user User unfriending * @param array $contact Contact unfriended - * @param boolean $dissolve Remove the contact on the remote side - * @return void + * @return bool|null Whether the remote operation is successful or null if no remote operation was performed * @throws HTTPException\InternalServerErrorException * @throws \ImagickException */ - public static function terminateFriendship(array $user, array $contact, $dissolve = false) + public static function revokeFollow(array $contact): bool { if (empty($contact['network'])) { - return; + throw new \InvalidArgumentException('Empty network in contact array'); } - $protocol = $contact['network']; - if (($protocol == Protocol::DFRN) && !self::isLegacyDFRNContact($contact)) { - $protocol = Protocol::ACTIVITYPUB; + if (empty($contact['uid'])) { + throw new \InvalidArgumentException('Unexpected public contact record'); } - if (($protocol == Protocol::DFRN) && $dissolve) { - DFRN::deliver($user, $contact, 'placeholder', true); - } elseif (in_array($protocol, [Protocol::OSTATUS, Protocol::DFRN])) { - // create an unfollow slap - $item = []; - $item['verb'] = Activity::O_UNFOLLOW; - $item['gravity'] = GRAVITY_ACTIVITY; - $item['follow'] = $contact["url"]; - $item['body'] = ''; - $item['title'] = ''; - $item['guid'] = ''; - $item['uri-id'] = 0; - $slap = OStatus::salmon($item, $user); - - if (!empty($contact['notify'])) { - Salmon::slapper($user, $contact['notify'], $slap); - } - } elseif ($protocol == Protocol::DIASPORA) { - Diaspora::sendUnshare($user, $contact); - } elseif ($protocol == Protocol::ACTIVITYPUB) { - ActivityPub\Transmitter::sendContactUndo($contact['url'], $contact['id'], $user['uid']); + $result = Protocol::revokeFollow($contact); - if ($dissolve) { - ActivityPub\Transmitter::sendContactReject($contact['url'], $contact['hub-verify'], $user['uid']); + // A null value here means the remote network doesn't support explicit follow revocation, we can still + // break the locally recorded relationship + if ($result !== false) { + if ($contact['rel'] == self::FRIEND) { + self::update(['rel' => self::SHARING], ['id' => $contact['id']]); + } else { + self::remove($contact['id']); } } + + return $result; } + /** * Marks a contact for archival after a communication issue delay * @@ -817,8 +916,8 @@ class Contact } if ($contact['term-date'] <= DBA::NULL_DATETIME) { - DBA::update('contact', ['term-date' => DateTimeFormat::utcNow()], ['id' => $contact['id']]); - DBA::update('contact', ['term-date' => DateTimeFormat::utcNow()], ['`nurl` = ? AND `term-date` <= ? AND NOT `self`', Strings::normaliseLink($contact['url']), DBA::NULL_DATETIME]); + self::update(['term-date' => DateTimeFormat::utcNow()], ['id' => $contact['id']]); + self::update(['term-date' => DateTimeFormat::utcNow()], ['`nurl` = ? AND `term-date` <= ? AND NOT `self`', Strings::normaliseLink($contact['url']), DBA::NULL_DATETIME]); } else { /* @todo * We really should send a notification to the owner after 2-3 weeks @@ -835,8 +934,8 @@ class Contact * delete, though if the owner tries to unarchive them we'll start * the whole process over again. */ - DBA::update('contact', ['archive' => true], ['id' => $contact['id']]); - DBA::update('contact', ['archive' => true], ['nurl' => Strings::normaliseLink($contact['url']), 'self' => false]); + self::update(['archive' => true], ['id' => $contact['id']]); + self::update(['archive' => true], ['nurl' => Strings::normaliseLink($contact['url']), 'self' => false]); } } } @@ -857,7 +956,7 @@ class Contact $fields = ['failed' => false, 'term-date' => DBA::NULL_DATETIME, 'archive' => false]; $condition = ['uid' => 0, 'network' => Protocol::FEDERATED, 'batch' => $contact['batch'], 'contact-type' => self::TYPE_RELAY]; if (!DBA::exists('contact', array_merge($condition, $fields))) { - DBA::update('contact', $fields, $condition); + self::update($fields, $condition); } } @@ -881,8 +980,8 @@ class Contact // It's a miracle. Our dead contact has inexplicably come back to life. $fields = ['failed' => false, 'term-date' => DBA::NULL_DATETIME, 'archive' => false]; - DBA::update('contact', $fields, ['id' => $contact['id']]); - DBA::update('contact', $fields, ['nurl' => Strings::normaliseLink($contact['url']), 'self' => false]); + self::update($fields, ['id' => $contact['id']]); + self::update($fields, ['nurl' => Strings::normaliseLink($contact['url']), 'self' => false]); } /** @@ -899,7 +998,6 @@ class Contact $pm_url = ''; $status_link = ''; $photos_link = ''; - $contact_drop_link = ''; $poke_link = ''; if ($uid == 0) { @@ -951,13 +1049,9 @@ class Contact $posts_link = DI::baseUrl() . '/contact/' . $contact['id'] . '/conversations'; - if (!$contact['self']) { - $contact_drop_link = DI::baseUrl() . '/contact/' . $contact['id'] . '/drop?confirm=1'; - } - $follow_link = ''; $unfollow_link = ''; - if (!$contact['self'] && in_array($contact['network'], Protocol::NATIVE_SUPPORT)) { + if (!$contact['self'] && Protocol::supportsFollow($contact['network'])) { if ($contact['uid'] && in_array($contact['rel'], [self::SHARING, self::FRIEND])) { $unfollow_link = 'unfollow?url=' . urlencode($contact['url']) . '&auto=1'; } elseif(!$contact['pending']) { @@ -965,10 +1059,6 @@ class Contact } } - if (!empty($follow_link) || !empty($unfollow_link)) { - $contact_drop_link = ''; - } - /** * Menu array: * "name" => [ "Label", "link", (bool)Should the link opened in a new tab? ] @@ -988,7 +1078,6 @@ class Contact 'photos' => [DI::l10n()->t('View Photos') , $photos_link , true], 'network' => [DI::l10n()->t('Network Posts') , $posts_link , false], 'edit' => [DI::l10n()->t('View Contact') , $contact_url , false], - 'drop' => [DI::l10n()->t('Drop Contact') , $contact_drop_link, false], 'pm' => [DI::l10n()->t('Send PM') , $pm_url , false], 'poke' => [DI::l10n()->t('Poke') , $poke_link , false], 'follow' => [DI::l10n()->t('Connect/Follow'), $follow_link , true], @@ -1054,12 +1143,12 @@ class Contact return 0; } - $contact = self::getByURL($url, false, ['id', 'network'], $uid); + $contact = self::getByURL($url, false, ['id', 'network', 'uri-id'], $uid); if (!empty($contact)) { $contact_id = $contact["id"]; - if (empty($update)) { + if (empty($update) && (!empty($contact['uri-id']) || is_bool($update))) { Logger::debug('Contact found', ['url' => $url, 'uid' => $uid, 'update' => $update, 'cid' => $contact_id]); return $contact_id; } @@ -1087,7 +1176,7 @@ class Contact if (($uid == 0) && (empty($data['network']) || ($data['network'] == Protocol::PHANTOM))) { // Fetch data for the public contact via the first found personal contact /// @todo Check if this case can happen at all (possibly with mail accounts?) - $fields = ['name', 'nick', 'url', 'addr', 'alias', 'avatar', 'contact-type', + $fields = ['name', 'nick', 'url', 'addr', 'alias', 'avatar', 'header', 'contact-type', 'keywords', 'location', 'about', 'unsearchable', 'batch', 'notify', 'poll', 'request', 'confirm', 'poco', 'subscribe', 'network', 'baseurl', 'gsid']; @@ -1148,8 +1237,7 @@ class Contact $contact_id = $contact['id']; Logger::notice('Contact had been created (shortly) before', ['id' => $contact_id, 'url' => $url, 'uid' => $uid]); } else { - DBA::insert('contact', $fields); - $contact_id = DBA::lastInsertId(); + $contact_id = self::insert($fields); if ($contact_id) { Logger::info('Contact inserted', ['id' => $contact_id, 'url' => $url, 'uid' => $uid]); } @@ -1274,14 +1362,15 @@ class Contact * * @param string $contact_url Contact URL * @param bool $thread_mode - * @param int $update Update mode + * @param int $update Update mode * @param int $parent Item parent ID for the update mode + * @param bool $only_media Only display media content * @return string posts in HTML * @throws \Exception */ - public static function getPostsFromUrl($contact_url, $thread_mode = false, $update = 0, $parent = 0) + public static function getPostsFromUrl($contact_url, $thread_mode = false, $update = 0, $parent = 0, bool $only_media = false) { - return self::getPostsFromId(self::getIdForURL($contact_url), $thread_mode, $update, $parent); + return self::getPostsFromId(self::getIdForURL($contact_url), $thread_mode, $update, $parent, $only_media); } /** @@ -1289,15 +1378,14 @@ class Contact * * @param int $cid Contact ID * @param bool $thread_mode - * @param int $update Update mode - * @param int $parent Item parent ID for the update mode + * @param int $update Update mode + * @param int $parent Item parent ID for the update mode + * @param bool $only_media Only display media content * @return string posts in HTML * @throws \Exception */ - public static function getPostsFromId($cid, $thread_mode = false, $update = 0, $parent = 0) + public static function getPostsFromId($cid, $thread_mode = false, $update = 0, $parent = 0, bool $only_media = false) { - $a = DI::app(); - $contact = DBA::selectFirst('contact', ['contact-type', 'network'], ['id' => $cid]); if (!DBA::isResult($contact)) { return ''; @@ -1328,6 +1416,11 @@ class Contact } } + if ($only_media) { + $condition = DBA::mergeConditions($condition, ["`uri-id` IN (SELECT `uri-id` FROM `post-media` WHERE `type` IN (?, ?, ?))", + Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO]); + } + if (DI::mode()->isMobile()) { $itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network', DI::config()->get('system', 'itemspage_network_mobile')); @@ -1347,14 +1440,14 @@ class Contact $o = ''; } - if ($thread_mode) { + if ($thread_mode) { $items = Post::toArray(Post::selectForUser(local_user(), ['uri-id', 'gravity', 'parent-uri-id', 'thr-parent-id', 'author-id'], $condition, $params)); - $o .= conversation($a, $items, 'contacts', $update, false, 'commented', local_user()); + $o .= DI::conversation()->create($items, 'contacts', $update, false, 'commented', local_user()); } else { $items = Post::toArray(Post::selectForUser(local_user(), Item::DISPLAY_FIELDLIST, $condition, $params)); - $o .= conversation($a, $items, 'contact-posts', $update); + $o .= DI::conversation()->create($items, 'contact-posts', $update); } if (!$update) { @@ -1431,7 +1524,7 @@ class Contact */ public static function block($cid, $reason = null) { - $return = DBA::update('contact', ['blocked' => true, 'block_reason' => $reason], ['id' => $cid]); + $return = self::update(['blocked' => true, 'block_reason' => $reason], ['id' => $cid]); return $return; } @@ -1445,7 +1538,7 @@ class Contact */ public static function unblock($cid) { - $return = DBA::update('contact', ['blocked' => false, 'block_reason' => null], ['id' => $cid]); + $return = self::update(['blocked' => false, 'block_reason' => null], ['id' => $cid]); return $return; } @@ -1481,67 +1574,46 @@ class Contact * @param bool $no_update Don't perfom an update if no cached avatar was found * @return string photo path */ - private static function getAvatarPath(array $contact, string $field, string $size, string $avatar, $no_update = false) + private static function getAvatarPath(array $contact, string $size, $no_update = false) { - if (!empty($contact)) { - $contact = self::checkAvatarCacheByArray($contact, $no_update); - if (!empty($contact[$field])) { - $avatar = $contact[$field]; - } - } - - if ($no_update && empty($avatar) && !empty($contact['avatar'])) { - $avatar = $contact['avatar']; - } - - if (empty($avatar)) { - $avatar = self::getDefaultAvatar([], $size); - } - - if (Proxy::isLocalImage($avatar)) { - return $avatar; - } else { - return Proxy::proxifyUrl($avatar, false, $size); - } + $contact = self::checkAvatarCacheByArray($contact, $no_update); + return self::getAvatarUrlForId($contact['id'], $size, $contact['updated'] ?? ''); } /** * Return the photo path for a given contact array * * @param array $contact Contact array - * @param string $avatar Avatar path that is displayed when no photo had been found * @param bool $no_update Don't perfom an update if no cached avatar was found * @return string photo path */ - public static function getPhoto(array $contact, string $avatar = '', bool $no_update = false) + public static function getPhoto(array $contact, bool $no_update = false) { - return self::getAvatarPath($contact, 'photo', Proxy::SIZE_SMALL, $avatar, $no_update); + return self::getAvatarPath($contact, Proxy::SIZE_SMALL, $no_update); } /** * Return the photo path (thumb size) for a given contact array * * @param array $contact Contact array - * @param string $avatar Avatar path that is displayed when no photo had been found * @param bool $no_update Don't perfom an update if no cached avatar was found * @return string photo path */ - public static function getThumb(array $contact, string $avatar = '', bool $no_update = false) + public static function getThumb(array $contact, bool $no_update = false) { - return self::getAvatarPath($contact, 'thumb', Proxy::SIZE_THUMB, $avatar, $no_update); + return self::getAvatarPath($contact, Proxy::SIZE_THUMB, $no_update); } /** * Return the photo path (micro size) for a given contact array * * @param array $contact Contact array - * @param string $avatar Avatar path that is displayed when no photo had been found * @param bool $no_update Don't perfom an update if no cached avatar was found * @return string photo path */ - public static function getMicro(array $contact, string $avatar = '', bool $no_update = false) + public static function getMicro(array $contact, bool $no_update = false) { - return self::getAvatarPath($contact, 'micro', Proxy::SIZE_MICRO, $avatar, $no_update); + return self::getAvatarPath($contact, Proxy::SIZE_MICRO, $no_update); } /** @@ -1598,7 +1670,7 @@ class Contact * * @param array $contact contact array * @param string $size Size of the avatar picture - * @return void + * @return string avatar URL */ public static function getDefaultAvatar(array $contact, string $size) { @@ -1607,12 +1679,12 @@ class Contact $avatar['size'] = 48; $default = self::DEFAULT_AVATAR_MICRO; break; - + case Proxy::SIZE_THUMB: $avatar['size'] = 80; $default = self::DEFAULT_AVATAR_THUMB; break; - + case Proxy::SIZE_SMALL: default: $avatar['size'] = 300; @@ -1646,6 +1718,105 @@ class Contact return DI::baseUrl() . $default; } + /** + * Get avatar link for given contact id + * + * @param integer $cid contact id + * @param string $size One of the Proxy::SIZE_* constants + * @param string $updated Contact update date + * @return string avatar link + */ + public static function getAvatarUrlForId(int $cid, string $size = '', string $updated = '', string $guid = ''):string + { + // We have to fetch the "updated" variable when it wasn't provided + // The parameter can be provided to improve performance + if (empty($updated) || empty($guid)) { + $account = DBA::selectFirst('account-user-view', ['updated', 'guid'], ['id' => $cid]); + $updated = $account['updated'] ?? ''; + $guid = $account['guid'] ?? ''; + } + + $guid = urlencode($guid); + + $url = DI::baseUrl() . '/photo/contact/'; + switch ($size) { + case Proxy::SIZE_MICRO: + $url .= Proxy::PIXEL_MICRO . '/'; + break; + case Proxy::SIZE_THUMB: + $url .= Proxy::PIXEL_THUMB . '/'; + break; + case Proxy::SIZE_SMALL: + $url .= Proxy::PIXEL_SMALL . '/'; + break; + case Proxy::SIZE_MEDIUM: + $url .= Proxy::PIXEL_MEDIUM . '/'; + break; + case Proxy::SIZE_LARGE: + $url .= Proxy::PIXEL_LARGE . '/'; + break; + } + return $url . ($guid ?: $cid) . ($updated ? '?ts=' . strtotime($updated) : ''); + } + + /** + * Get avatar link for given contact URL + * + * @param string $url contact url + * @param integer $uid user id + * @param string $size One of the Proxy::SIZE_* constants + * @return string avatar link + */ + public static function getAvatarUrlForUrl(string $url, int $uid, string $size = ''):string + { + $condition = ["`nurl` = ? AND ((`uid` = ? AND `network` IN (?, ?)) OR `uid` = ?)", + Strings::normaliseLink($url), $uid, Protocol::FEED, Protocol::MAIL, 0]; + $contact = self::selectFirst(['id', 'updated'], $condition, ['order' => ['uid' => true]]); + return self::getAvatarUrlForId($contact['id'] ?? 0, $size, $contact['updated'] ?? ''); + } + + /** + * Get header link for given contact id + * + * @param integer $cid contact id + * @param string $size One of the Proxy::SIZE_* constants + * @param string $updated Contact update date + * @return string header link + */ + public static function getHeaderUrlForId(int $cid, string $size = '', string $updated = '', string $guid = ''):string + { + // We have to fetch the "updated" variable when it wasn't provided + // The parameter can be provided to improve performance + if (empty($updated) || empty($guid)) { + $account = DBA::selectFirst('account-user-view', ['updated', 'guid'], ['id' => $cid]); + $updated = $account['updated'] ?? ''; + $guid = $account['guid'] ?? ''; + } + + $guid = urlencode($guid); + + $url = DI::baseUrl() . '/photo/header/'; + switch ($size) { + case Proxy::SIZE_MICRO: + $url .= Proxy::PIXEL_MICRO . '/'; + break; + case Proxy::SIZE_THUMB: + $url .= Proxy::PIXEL_THUMB . '/'; + break; + case Proxy::SIZE_SMALL: + $url .= Proxy::PIXEL_SMALL . '/'; + break; + case Proxy::SIZE_MEDIUM: + $url .= Proxy::PIXEL_MEDIUM . '/'; + break; + case Proxy::SIZE_LARGE: + $url .= Proxy::PIXEL_LARGE . '/'; + break; + } + + return $url . ($guid ?: $cid) . ($updated ? '?ts=' . strtotime($updated) : ''); + } + /** * Updates the avatar links in a contact only if needed * @@ -1672,7 +1843,7 @@ class Contact // Only update the cached photo links of public contacts when they already are cached if (($uid == 0) && !$force && empty($contact['thumb']) && empty($contact['micro']) && !$create_cache) { if ($contact['avatar'] != $avatar) { - DBA::update('contact', ['avatar' => $avatar], ['id' => $cid]); + self::update(['avatar' => $avatar], ['id' => $cid]); Logger::info('Only update the avatar', ['id' => $cid, 'avatar' => $avatar, 'contact' => $contact]); } return; @@ -1680,7 +1851,7 @@ class Contact // 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); + $pcid = self::getIdForURL($contact['url'], 0, 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); @@ -1720,7 +1891,7 @@ class Contact $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])) { @@ -1769,7 +1940,7 @@ class Contact $cids[] = $cid; $uids[] = $uid; Logger::info('Updating cached contact avatars', ['cid' => $cids, 'uid' => $uids, 'fields' => $fields]); - DBA::update('contact', $fields, ['id' => $cids]); + self::update($fields, ['id' => $cids]); } public static function deleteContactByUrl(string $url) @@ -1796,12 +1967,7 @@ class Contact */ private static function updateContact(int $id, int $uid, string $old_url, string $new_url, array $fields) { - if (Strings::normaliseLink($new_url) != Strings::normaliseLink($old_url)) { - Logger::notice('New URL differs from old URL', ['old' => $old_url, 'new' => $new_url]); - // @todo It is to decide what to do when the URL is changed - } - - if (!DBA::update('contact', $fields, ['id' => $id])) { + if (!self::update($fields, ['id' => $id])) { Logger::info('Couldn\'t update contact.', ['id' => $id, 'fields' => $fields]); return; } @@ -1834,7 +2000,7 @@ class Contact $condition = ['self' => false, 'nurl' => Strings::normaliseLink($old_url)]; $condition['network'] = [Protocol::DFRN, Protocol::DIASPORA, Protocol::ACTIVITYPUB]; - DBA::update('contact', $fields, $condition); + self::update($fields, $condition); // We mustn't set the update fields for OStatus contacts since they are updated in OnePoll $condition['network'] = Protocol::OSTATUS; @@ -1850,7 +2016,7 @@ class Contact return; } - DBA::update('contact', $fields, $condition); + self::update($fields, $condition); } /** @@ -1863,7 +2029,7 @@ class Contact */ public static function removeDuplicates(string $nurl, int $uid) { - $condition = ['nurl' => $nurl, 'uid' => $uid, 'deleted' => false, 'network' => Protocol::FEDERATED]; + $condition = ['nurl' => $nurl, 'uid' => $uid, 'self' => false, 'deleted' => false, 'network' => Protocol::FEDERATED]; $count = DBA::count('contact', $condition); if ($count <= 1) { return false; @@ -1931,16 +2097,25 @@ class Contact */ // These fields aren't updated by this routine: - // 'xmpp', 'sensitive' + // 'sensitive' - $fields = ['uid', 'avatar', 'name', 'nick', 'location', 'keywords', 'about', 'subscribe', 'manually-approve', - 'unsearchable', 'url', 'addr', 'batch', 'notify', 'poll', 'request', 'confirm', 'poco', - 'network', 'alias', 'baseurl', 'gsid', 'forum', 'prv', 'contact-type', 'pubkey', 'last-item']; + $fields = ['uid', 'uri-id', 'avatar', 'header', 'name', 'nick', 'location', 'keywords', 'about', 'subscribe', + 'manually-approve', 'unsearchable', 'url', 'addr', 'batch', 'notify', 'poll', 'request', 'confirm', 'poco', + 'network', 'alias', 'baseurl', 'gsid', 'forum', 'prv', 'contact-type', 'pubkey', 'last-item', 'xmpp', 'matrix']; $contact = DBA::selectFirst('contact', $fields, ['id' => $id]); if (!DBA::isResult($contact)) { return false; } + if (self::isLocal($ret['url'])) { + if ($contact['uid'] == 0) { + Logger::info('Local contacts are not updated here.'); + } else { + self::updateFromPublicContact($id, $contact); + } + return true; + } + if (!empty($ret['account-type']) && $ret['account-type'] == User::ACCOUNT_TYPE_DELETED) { Logger::info('Deleted account', ['id' => $id, 'url' => $ret['url'], 'ret' => $ret]); self::remove($id); @@ -1953,6 +2128,9 @@ class Contact $uid = $contact['uid']; unset($contact['uid']); + $uriid = $contact['uri-id']; + unset($contact['uri-id']); + $pubkey = $contact['pubkey']; unset($contact['pubkey']); @@ -1961,6 +2139,12 @@ class Contact $updated = DateTimeFormat::utcNow(); + if (Strings::normaliseLink($contact['url']) != Strings::normaliseLink($ret['url'])) { + Logger::notice('New URL differs from old URL', ['id' => $id, 'uid' => $uid, 'old' => $contact['url'], 'new' => $ret['url']]); + self::updateContact($id, $uid, $contact['url'], $ret['url'], ['failed' => true, 'last-update' => $updated, 'failure_update' => $updated]); + return false; + } + // We must not try to update relay contacts via probe. They are no real contacts. // We check after the probing to be able to correct falsely detected contact types. if (($contact['contact-type'] == self::TYPE_RELAY) && @@ -1976,6 +2160,14 @@ class Contact return false; } + if (Strings::normaliseLink($ret['url']) != Strings::normaliseLink($contact['url'])) { + $cid = self::getIdForURL($ret['url'], 0, false); + if (!empty($cid) && ($cid != $id)) { + Logger::notice('URL of contact changed.', ['id' => $id, 'new_id' => $cid, 'old' => $contact['url'], 'new' => $ret['url']]); + return self::updateFromProbeArray($cid, $ret); + } + } + if (isset($ret['hide']) && is_bool($ret['hide'])) { $ret['unsearchable'] = $ret['hide']; } @@ -1998,6 +2190,7 @@ class Contact } $update = false; + $guid = ($ret['guid'] ?? '') ?: Item::guidFromUri($ret['url'], parse_url($ret['url'], PHP_URL_HOST)); // make sure to not overwrite existing values with blank entries except some technical fields $keep = ['batch', 'notify', 'poll', 'request', 'confirm', 'poco', 'baseurl']; @@ -2017,17 +2210,23 @@ class Contact unset($ret['last-item']); } + if (empty($uriid)) { + $update = true; + } + if (!empty($ret['photo']) && ($ret['network'] != Protocol::FEED)) { self::updateAvatar($id, $ret['photo'], $update); } + $uriid = ItemURI::insert(['uri' => $ret['url'], 'guid' => $guid]); + if (!$update) { self::updateContact($id, $uid, $contact['url'], $ret['url'], ['failed' => false, 'last-update' => $updated, 'success_update' => $updated]); if (Contact\Relation::isDiscoverable($ret['url'])) { Worker::add(PRIORITY_LOW, 'ContactDiscovery', $ret['url']); } - + // Update the public contact if ($uid != 0) { $contact = self::getByURL($ret['url'], false, ['id']); @@ -2039,9 +2238,10 @@ class Contact return true; } - $ret['nurl'] = Strings::normaliseLink($ret['url']); + $ret['uri-id'] = $uriid; + $ret['nurl'] = Strings::normaliseLink($ret['url']); $ret['updated'] = $updated; - $ret['failed'] = false; + $ret['failed'] = false; // Only fill the pubkey if it had been empty before. We have to prevent identity theft. if (empty($pubkey) && !empty($new_pubkey)) { @@ -2049,10 +2249,10 @@ class Contact } if ((!empty($ret['addr']) && ($ret['addr'] != $contact['addr'])) || (!empty($ret['alias']) && ($ret['alias'] != $contact['alias']))) { - $ret['uri-date'] = DateTimeFormat::utcNow(); + $ret['uri-date'] = $updated; } - if (($ret['name'] != $contact['name']) || ($ret['nick'] != $contact['nick'])) { + if ((!empty($ret['name']) && ($ret['name'] != $contact['name'])) || (!empty($ret['nick']) && ($ret['nick'] != $contact['nick']))) { $ret['name-date'] = $updated; } @@ -2072,6 +2272,26 @@ class Contact return true; } + private static function updateFromPublicContact(int $id, array $contact) + { + $public = self::getByURL($contact['url'], false); + + $fields = []; + + foreach ($contact as $field => $value) { + if ($field == 'uid') { + continue; + } + if ($public[$field] != $value) { + $fields[$field] = $public[$field]; + } + } + if (!empty($fields)) { + self::update($fields, ['id' => $id, 'self' => false]); + Logger::info('Updating local contact', ['id' => $id]); + } + } + /** * @param integer $url contact url * @return integer Contact id @@ -2091,18 +2311,6 @@ class Contact return $id; } - /** - * Detects if a given contact array belongs to a legacy DFRN connection - * - * @param array $contact - * @return boolean - */ - public static function isLegacyDFRNContact($contact) - { - // Newer Friendica contacts are connected via AP, then these fields aren't set - return !empty($contact['dfrn-id']) || !empty($contact['issued-id']); - } - /** * Detects the communication protocol for a given contact url. * This is used to detect Friendica contacts that we can communicate via AP. @@ -2139,16 +2347,15 @@ class Contact * * Takes a $uid and a url/handle and adds a new contact * - * @param array $user The user the contact should be created for + * @param int $uid The user id the contact should be created for * @param string $url The profile URL of the contact - * @param bool $interactive * @param string $network * @return array * @throws HTTPException\InternalServerErrorException * @throws HTTPException\NotFoundException * @throws \ImagickException */ - public static function createFromProbe(array $user, $url, $interactive = false, $network = '') + public static function createFromProbeForUser(int $uid, $url, $network = '') { $result = ['cid' => -1, 'success' => false, 'message' => '']; @@ -2180,9 +2387,11 @@ class Contact } if (!empty($arr['contact']['name'])) { + $probed = false; $ret = $arr['contact']; } else { - $ret = Probe::uri($url, $network, $user['uid']); + $probed = true; + $ret = Probe::uri($url, $network, $uid); } if (($network != '') && ($ret['network'] != $network)) { @@ -2194,33 +2403,15 @@ class Contact // the poll url is more reliable than the profile url, as we may have // indirect links or webfinger links - $condition = ['uid' => $user['uid'], 'poll' => [$ret['poll'], Strings::normaliseLink($ret['poll'])], 'network' => $ret['network'], 'pending' => false]; + $condition = ['uid' => $uid, 'poll' => [$ret['poll'], Strings::normaliseLink($ret['poll'])], 'network' => $ret['network'], 'pending' => false]; $contact = DBA::selectFirst('contact', ['id', 'rel'], $condition); if (!DBA::isResult($contact)) { - $condition = ['uid' => $user['uid'], 'nurl' => Strings::normaliseLink($ret['url']), 'network' => $ret['network'], 'pending' => false]; + $condition = ['uid' => $uid, 'nurl' => Strings::normaliseLink($ret['url']), 'network' => $ret['network'], 'pending' => false]; $contact = DBA::selectFirst('contact', ['id', 'rel'], $condition); } $protocol = self::getProtocol($ret['url'], $ret['network']); - if (($protocol === Protocol::DFRN) && !DBA::isResult($contact)) { - if ($interactive) { - if (strlen(DI::baseUrl()->getUrlPath())) { - $myaddr = bin2hex(DI::baseUrl() . '/profile/' . $user['nickname']); - } else { - $myaddr = bin2hex($user['nickname'] . '@' . DI::baseUrl()->getHostname()); - } - - DI::baseUrl()->redirect($ret['request'] . "&addr=$myaddr"); - - // NOTREACHED - } - } elseif (DI::config()->get('system', 'dfrn_only') && ($ret['network'] != Protocol::DFRN)) { - $result['message'] = DI::l10n()->t('This site is not configured to allow communications with other networks.') . EOL; - $result['message'] .= DI::l10n()->t('No compatible communication protocols or feeds were discovered.') . EOL; - return $result; - } - // This extra param just confuses things, remove it if ($protocol === Protocol::DIASPORA) { $ret['url'] = str_replace('?absolute=true', '', $ret['url']); @@ -2274,13 +2465,13 @@ class Contact $new_relation = (($contact['rel'] == self::FOLLOWER) ? self::FRIEND : self::SHARING); $fields = ['rel' => $new_relation, 'subhub' => $subhub, 'readonly' => false]; - DBA::update('contact', $fields, ['id' => $contact['id']]); + self::update($fields, ['id' => $contact['id']]); } else { $new_relation = (in_array($protocol, [Protocol::MAIL]) ? self::FRIEND : self::SHARING); // create contact record self::insert([ - 'uid' => $user['uid'], + 'uid' => $uid, 'created' => DateTimeFormat::utcNow(), 'url' => $ret['url'], 'nurl' => Strings::normaliseLink($ret['url']), @@ -2308,7 +2499,7 @@ class Contact ]); } - $contact = DBA::selectFirst('contact', [], ['url' => $ret['url'], 'network' => $ret['network'], 'uid' => $user['uid']]); + $contact = DBA::selectFirst('contact', [], ['url' => $ret['url'], 'network' => $ret['network'], 'uid' => $uid]); if (!DBA::isResult($contact)) { $result['message'] .= DI::l10n()->t('Unable to retrieve contact information.') . EOL; return $result; @@ -2317,7 +2508,7 @@ class Contact $contact_id = $contact['id']; $result['cid'] = $contact_id; - Group::addMember(User::getDefaultGroup($user['uid'], $contact["network"]), $contact_id); + Group::addMember(User::getDefaultGroup($uid, $contact["network"]), $contact_id); // Update the avatar self::updateAvatar($contact_id, $ret['photo']); @@ -2325,11 +2516,15 @@ class Contact // pull feed and consume it, which should subscribe to the hub. if ($contact['network'] == Protocol::OSTATUS) { Worker::add(PRIORITY_HIGH, 'OnePoll', $contact_id, 'force'); + } + + if ($probed) { + self::updateFromProbeArray($contact_id, $ret); } else { Worker::add(PRIORITY_HIGH, 'UpdateContact', $contact_id); } - $owner = User::getOwnerDataById($user['uid']); + $owner = User::getOwnerDataById($uid); if (DBA::isResult($owner)) { if (in_array($protocol, [Protocol::OSTATUS, Protocol::DFRN])) { @@ -2358,7 +2553,7 @@ class Contact return false; } - $ret = ActivityPub\Transmitter::sendActivity('Follow', $contact['url'], $user['uid'], $activity_id); + $ret = ActivityPub\Transmitter::sendActivity('Follow', $contact['url'], $uid, $activity_id); Logger::log('Follow returns: ' . $ret); } } @@ -2403,12 +2598,51 @@ class Contact $fields = ['url' => $contact['url'], 'request' => $contact['request'], 'notify' => $contact['notify'], 'poll' => $contact['poll'], 'confirm' => $contact['confirm'], 'poco' => $contact['poco']]; - DBA::update('contact', $fields, ['id' => $contact['id']]); + self::update($fields, ['id' => $contact['id']]); } return $contact; } + /** + * Follow a contact + * + * @param int $cid Public contact id + * @param int $uid User ID + * + * @return bool "true" if following had been successful + */ + public static function follow(int $cid, int $uid) + { + $contact = self::getById($cid, ['url']); + + $result = self::createFromProbeForUser($uid, $contact['url']); + + return $result['cid']; + } + + /** + * Unfollow a contact + * + * @param int $cid Public contact id + * @param int $uid User ID + * + * @return bool "true" if unfollowing had been successful + */ + public static function unfollow(int $cid, int $uid) + { + $cdata = self::getPublicAndUserContactID($cid, $uid); + if (empty($cdata['user'])) { + return false; + } + + $contact = self::getById($cdata['user']); + + self::removeSharer([], $contact); + + return true; + } + /** * @param array $importer Owner (local user) data * @param array $contact Existing owner-specific contact data we want to expand the relationship with. Optional. @@ -2467,13 +2701,15 @@ class Contact if (($contact['rel'] == self::SHARING) || ($sharing && $contact['rel'] == self::FOLLOWER)) { - DBA::update('contact', ['rel' => self::FRIEND, 'writable' => true, 'pending' => false], + self::update(['rel' => self::FRIEND, 'writable' => true, 'pending' => false], ['id' => $contact['id'], 'uid' => $importer['uid']]); } // Ensure to always have the correct network type, independent from the connection request method self::updateFromProbe($contact['id']); + Post\UserNotification::insertNotification($contact['id'], Activity::FOLLOW, $importer['uid']); + return true; } else { // send email notification to owner? @@ -2483,7 +2719,7 @@ class Contact } // create contact record - DBA::insert('contact', [ + $contact_id = self::insert([ 'uid' => $importer['uid'], 'created' => DateTimeFormat::utcNow(), 'url' => $url, @@ -2498,13 +2734,13 @@ class Contact 'writable' => 1, ]); - $contact_id = DBA::lastInsertId(); - // Ensure to always have the correct network type, independent from the connection request method self::updateFromProbe($contact_id); self::updateAvatar($contact_id, $photo, true); + Post\UserNotification::insertNotification($contact_id, Activity::FOLLOW, $importer['uid']); + $contact_record = DBA::selectFirst('contact', ['id', 'network', 'name', 'url', 'photo'], ['id' => $contact_id]); /// @TODO Encapsulate this into a function/method @@ -2536,7 +2772,7 @@ class Contact } } elseif (DBA::isResult($user) && in_array($user['page-flags'], [User::PAGE_FLAGS_SOAPBOX, User::PAGE_FLAGS_FREELOVE, User::PAGE_FLAGS_COMMUNITY])) { if (($user['page-flags'] == User::PAGE_FLAGS_FREELOVE) && ($network != Protocol::DIASPORA)) { - self::createFromProbe($importer, $url, false, $network); + self::createFromProbeForUser($importer['uid'], $url, $network); } $condition = ['uid' => $importer['uid'], 'url' => $url, 'pending' => true]; @@ -2545,7 +2781,7 @@ class Contact $fields['rel'] = self::FRIEND; } - DBA::update('contact', $fields, $condition); + self::update($fields, $condition); return true; } @@ -2554,19 +2790,21 @@ class Contact return null; } - public static function removeFollower($importer, $contact, array $datarray = [], $item = "") + public static function removeFollower(array $contact) { - if (($contact['rel'] == self::FRIEND) || ($contact['rel'] == self::SHARING)) { + if (in_array($contact['rel'] ?? [], [self::FRIEND, self::SHARING])) { DBA::update('contact', ['rel' => self::SHARING], ['id' => $contact['id']]); - } else { + } elseif (!empty($contact['id'])) { self::remove($contact['id']); + } else { + DI::logger()->info('Couldn\'t remove follower because of invalid contact array', ['contact' => $contact, 'callstack' => System::callstack()]); } } - public static function removeSharer($importer, $contact, array $datarray = [], $item = "") + public static function removeSharer($importer, $contact) { if (($contact['rel'] == self::FRIEND) || ($contact['rel'] == self::FOLLOWER)) { - DBA::update('contact', ['rel' => self::FOLLOWER], ['id' => $contact['id']]); + self::update(['rel' => self::FOLLOWER], ['id' => $contact['id']]); } else { self::remove($contact['id']); } @@ -2763,11 +3001,12 @@ class Contact * * @param string $search Name or nick * @param string $mode Search mode (e.g. "community") + * @param int $uid User ID * * @return array with search results * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public static function searchByName($search, $mode = '') + public static function searchByName(string $search, string $mode = '', int $uid = 0) { if (empty($search)) { return []; @@ -2796,11 +3035,12 @@ class Contact $search .= '%'; $results = DBA::p("SELECT * FROM `contact` - WHERE NOT `unsearchable` AND `network` IN (?, ?, ?, ?) AND + WHERE (NOT `unsearchable` OR `nurl` IN (SELECT `nurl` FROM `owner-view` where `publish` OR `net-publish`)) + AND `network` IN (?, ?, ?, ?) AND NOT `failed` AND `uid` = ? AND (`addr` LIKE ? OR `name` LIKE ? OR `nick` LIKE ?) $extra_sql ORDER BY `nurl` DESC LIMIT 1000", - Protocol::DFRN, Protocol::ACTIVITYPUB, $ostatus, $diaspora, 0, $search, $search, $search + Protocol::DFRN, Protocol::ACTIVITYPUB, $ostatus, $diaspora, $uid, $search, $search, $search ); $contacts = DBA::toArray($results); @@ -2821,6 +3061,9 @@ class Contact $count = 0; foreach ($urls as $url) { + if (empty($url) || !is_string($url)) { + continue; + } $contact = self::getByURL($url, false, ['id', 'updated']); if (empty($contact['id'])) { Worker::add(PRIORITY_LOW, 'AddContact', 0, $url);