Skip to content

Commit

Permalink
Remove dependency to App in Contact::createFromProbe
Browse files Browse the repository at this point in the history
  • Loading branch information
MrPetovan committed Jun 10, 2020
1 parent 5f18d27 commit a5a7855
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 33 deletions.
3 changes: 1 addition & 2 deletions mod/follow.php
Expand Up @@ -40,15 +40,14 @@ function follow_post(App $a)
DI::baseUrl()->redirect('contact');
}

$uid = local_user();
$url = Probe::cleanURI($_REQUEST['url']);
$return_path = 'follow?url=' . urlencode($url);

// Makes the connection request for friendica contacts easier
// This is just a precaution if maybe this page is called somewhere directly via POST
$_SESSION['fastlane'] = $url;

$result = Contact::createFromProbe($uid, $url, true);
$result = Contact::createFromProbe($a->user, $url, true);

if ($result['success'] == false) {
// Possibly it is a remote item and not an account
Expand Down
2 changes: 1 addition & 1 deletion mod/ostatus_subscribe.php
Expand Up @@ -91,7 +91,7 @@ function ostatus_subscribe_content(App $a)

$probed = Probe::uri($url);
if ($probed['network'] == Protocol::OSTATUS) {
$result = Contact::createFromProbe($uid, $url, true, Protocol::OSTATUS);
$result = Contact::createFromProbe($a->user, $url, true, Protocol::OSTATUS);
if ($result['success']) {
$o .= ' - ' . DI::l10n()->t('success');
} else {
Expand Down
2 changes: 1 addition & 1 deletion mod/repair_ostatus.php
Expand Up @@ -70,7 +70,7 @@ function repair_ostatus_content(App $a) {

$o .= "<p>".DI::l10n()->t("Keep this window open until done.")."</p>";

Contact::createFromProbe($uid, $r[0]["url"], true);
Contact::createFromProbe($a->user, $r[0]["url"], true);

DI::page()['htmlhead'] = '<meta http-equiv="refresh" content="1; URL=' . DI::baseUrl() . '/repair_ostatus?counter='.$counter.'">';

Expand Down
10 changes: 3 additions & 7 deletions mod/salmon.php
Expand Up @@ -42,15 +42,11 @@ function salmon_post(App $a, $xml = '') {

$nick = (($a->argc > 1) ? Strings::escapeTags(trim($a->argv[1])) : '');

$r = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `account_expired` = 0 AND `account_removed` = 0 LIMIT 1",
DBA::escape($nick)
);
if (! DBA::isResult($r)) {
$importer = DBA::selectFirst('user', [], ['nickname' => $nick, 'account_expired' => false, 'account_removed' => false]);
if (! DBA::isResult($importer)) {
throw new \Friendica\Network\HTTPException\InternalServerErrorException();
}

$importer = $r[0];

// parse the xml

$dom = simplexml_load_string($xml,'SimpleXMLElement',0, ActivityNamespace::SALMON_ME);
Expand Down Expand Up @@ -175,7 +171,7 @@ function salmon_post(App $a, $xml = '') {
Logger::log('Author ' . $author_link . ' unknown to user ' . $importer['uid'] . '.');

if (DI::pConfig()->get($importer['uid'], 'system', 'ostatus_autofriend')) {
$result = Contact::createFromProbe($importer['uid'], $author_link);
$result = Contact::createFromProbe($importer, $author_link);

if ($result['success']) {
$r = q("SELECT * FROM `contact` WHERE `network` = '%s' AND ( `url` = '%s' OR `alias` = '%s')
Expand Down
36 changes: 18 additions & 18 deletions src/Model/Contact.php
Expand Up @@ -2272,20 +2272,20 @@ public static function getProtocol($url, $network)
* $return['message'] error text if success is false.
*
* Takes a $uid and a url/handle and adds a new contact
* @param int $uid
* @param string $url
*
* @param array $user The user 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($uid, $url, $interactive = false, $network = '')
public static function createFromProbe(array $user, $url, $interactive = false, $network = '')
{
$result = ['cid' => -1, 'success' => false, 'message' => ''];

$a = DI::app();

// remove ajax junk, e.g. Twitter
$url = str_replace('/#!/', '/', $url);

Expand Down Expand Up @@ -2316,7 +2316,7 @@ public static function createFromProbe($uid, $url, $interactive = false, $networ
if (!empty($arr['contact']['name'])) {
$ret = $arr['contact'];
} else {
$ret = Probe::uri($url, $network, $uid, false);
$ret = Probe::uri($url, $network, $user['uid'], false);
}

if (($network != '') && ($ret['network'] != $network)) {
Expand All @@ -2328,10 +2328,10 @@ public static function createFromProbe($uid, $url, $interactive = false, $networ
// the poll url is more reliable than the profile url, as we may have
// indirect links or webfinger links

$condition = ['uid' => $uid, 'poll' => [$ret['poll'], Strings::normaliseLink($ret['poll'])], 'network' => $ret['network'], 'pending' => false];
$condition = ['uid' => $user['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' => $uid, 'nurl' => Strings::normaliseLink($url), 'network' => $ret['network'], 'pending' => false];
$condition = ['uid' => $user['uid'], 'nurl' => Strings::normaliseLink($url), 'network' => $ret['network'], 'pending' => false];
$contact = DBA::selectFirst('contact', ['id', 'rel'], $condition);
}

Expand All @@ -2340,9 +2340,9 @@ public static function createFromProbe($uid, $url, $interactive = false, $networ
if (($protocol === Protocol::DFRN) && !DBA::isResult($contact)) {
if ($interactive) {
if (strlen(DI::baseUrl()->getUrlPath())) {
$myaddr = bin2hex(DI::baseUrl() . '/profile/' . $a->user['nickname']);
$myaddr = bin2hex(DI::baseUrl() . '/profile/' . $user['nickname']);
} else {
$myaddr = bin2hex($a->user['nickname'] . '@' . DI::baseUrl()->getHostname());
$myaddr = bin2hex($user['nickname'] . '@' . DI::baseUrl()->getHostname());
}

DI::baseUrl()->redirect($ret['request'] . "&addr=$myaddr");
Expand Down Expand Up @@ -2417,7 +2417,7 @@ public static function createFromProbe($uid, $url, $interactive = false, $networ

// create contact record
self::insert([
'uid' => $uid,
'uid' => $user['uid'],
'created' => DateTimeFormat::utcNow(),
'url' => $ret['url'],
'nurl' => Strings::normaliseLink($ret['url']),
Expand Down Expand Up @@ -2445,7 +2445,7 @@ public static function createFromProbe($uid, $url, $interactive = false, $networ
]);
}

$contact = DBA::selectFirst('contact', [], ['url' => $ret['url'], 'network' => $ret['network'], 'uid' => $uid]);
$contact = DBA::selectFirst('contact', [], ['url' => $ret['url'], 'network' => $ret['network'], 'uid' => $user['uid']]);
if (!DBA::isResult($contact)) {
$result['message'] .= DI::l10n()->t('Unable to retrieve contact information.') . EOL;
return $result;
Expand All @@ -2454,16 +2454,16 @@ public static function createFromProbe($uid, $url, $interactive = false, $networ
$contact_id = $contact['id'];
$result['cid'] = $contact_id;

Group::addMember(User::getDefaultGroup($uid, $contact["network"]), $contact_id);
Group::addMember(User::getDefaultGroup($user['uid'], $contact["network"]), $contact_id);

// Update the avatar
self::updateAvatar($ret['photo'], $uid, $contact_id);
self::updateAvatar($ret['photo'], $user['uid'], $contact_id);

// pull feed and consume it, which should subscribe to the hub.

Worker::add(PRIORITY_HIGH, "OnePoll", $contact_id, "force");

$owner = User::getOwnerDataById($uid);
$owner = User::getOwnerDataById($user['uid']);

if (DBA::isResult($owner)) {
if (in_array($protocol, [Protocol::OSTATUS, Protocol::DFRN])) {
Expand All @@ -2483,7 +2483,7 @@ public static function createFromProbe($uid, $url, $interactive = false, $networ
Salmon::slapper($owner, $contact['notify'], $slap);
}
} elseif ($protocol == Protocol::DIASPORA) {
$ret = Diaspora::sendShare($a->user, $contact);
$ret = Diaspora::sendShare($owner, $contact);
Logger::log('share returns: ' . $ret);
} elseif ($protocol == Protocol::ACTIVITYPUB) {
$activity_id = ActivityPub\Transmitter::activityIDFromContact($contact_id);
Expand All @@ -2492,7 +2492,7 @@ public static function createFromProbe($uid, $url, $interactive = false, $networ
return false;
}

$ret = ActivityPub\Transmitter::sendActivity('Follow', $contact['url'], $uid, $activity_id);
$ret = ActivityPub\Transmitter::sendActivity('Follow', $contact['url'], $user['uid'], $activity_id);
Logger::log('Follow returns: ' . $ret);
}
}
Expand Down Expand Up @@ -2677,7 +2677,7 @@ public static function addRelationship(array $importer, array $contact, array $d
}
} 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['uid'], $url, false, $network);
self::createFromProbe($importer, $url, false, $network);
}

$condition = ['uid' => $importer['uid'], 'url' => $url, 'pending' => true];
Expand Down
5 changes: 2 additions & 3 deletions src/Module/Contact.php
Expand Up @@ -167,10 +167,9 @@ private static function updateContactFromPoll($contact_id)
return;
}

$uid = $contact['uid'];

if ($contact['network'] == Protocol::OSTATUS) {
$result = Model\Contact::createFromProbe($uid, $contact['url'], false, $contact['network']);
$user = Model\User::getById($contact['uid']);
$result = Model\Contact::createFromProbe($user, $contact['url'], false, $contact['network']);

if ($result['success']) {
DBA::update('contact', ['subhub' => 1], ['id' => $contact_id]);
Expand Down
7 changes: 6 additions & 1 deletion src/Worker/AddContact.php
Expand Up @@ -23,6 +23,7 @@

use Friendica\Core\Logger;
use Friendica\Model\Contact;
use Friendica\Model\User;

class AddContact
{
Expand All @@ -33,7 +34,11 @@ class AddContact
*/
public static function execute(int $uid, string $url)
{
$result = Contact::createFromProbe($uid, $url, '', false);
$user = User::getById($uid);
if (empty($user)) {
return;
}
$result = Contact::createFromProbe($user, $url, '', false);
Logger::info('Added contact', ['uid' => $uid, 'url' => $url, 'result' => $result]);
}
}

0 comments on commit a5a7855

Please sign in to comment.