Add return value to Model\Contact::addRelationship to remove protocol-specific code from it

This commit is contained in:
Hypolite Petovan 2019-05-19 18:46:29 -04:00
parent 10dfe4347b
commit ea77f214d7
2 changed files with 16 additions and 17 deletions

View file

@ -2126,6 +2126,7 @@ class Contact extends BaseObject
* @param array $datarray An item-like array with at least the 'author-id' and 'author-url' keys for the contact. Mandatory. * @param array $datarray An item-like array with at least the 'author-id' and 'author-url' keys for the contact. Mandatory.
* @param bool $sharing True: Contact is now sharing with Owner; False: Contact is now following Owner (default) * @param bool $sharing True: Contact is now sharing with Owner; False: Contact is now following Owner (default)
* @param string $note Introduction additional message * @param string $note Introduction additional message
* @return bool|null True: follow request is accepted; False: relationship is rejected; Null: relationship is pending
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException * @throws \ImagickException
*/ */
@ -2133,14 +2134,14 @@ class Contact extends BaseObject
{ {
// Should always be set // Should always be set
if (empty($datarray['author-id'])) { if (empty($datarray['author-id'])) {
return; return false;
} }
$fields = ['url', 'name', 'nick', 'photo', 'network']; $fields = ['url', 'name', 'nick', 'photo', 'network'];
$pub_contact = DBA::selectFirst('contact', $fields, ['id' => $datarray['author-id']]); $pub_contact = DBA::selectFirst('contact', $fields, ['id' => $datarray['author-id']]);
if (!DBA::isResult($pub_contact)) { if (!DBA::isResult($pub_contact)) {
// Should never happen // Should never happen
return; return false;
} }
$url = defaults($datarray, 'author-link', $pub_contact['url']); $url = defaults($datarray, 'author-link', $pub_contact['url']);
@ -2153,26 +2154,20 @@ class Contact extends BaseObject
// Make sure that the existing contact isn't archived // Make sure that the existing contact isn't archived
self::unmarkForArchival($contact); self::unmarkForArchival($contact);
$protocol = self::getProtocol($url, $contact['network']);
if (($contact['rel'] == self::SHARING) if (($contact['rel'] == self::SHARING)
|| ($sharing && $contact['rel'] == self::FOLLOWER)) { || ($sharing && $contact['rel'] == self::FOLLOWER)) {
DBA::update('contact', ['rel' => self::FRIEND, 'writable' => true, 'pending' => false], DBA::update('contact', ['rel' => self::FRIEND, 'writable' => true, 'pending' => false],
['id' => $contact['id'], 'uid' => $importer['uid']]); ['id' => $contact['id'], 'uid' => $importer['uid']]);
} }
if ($protocol == Protocol::ACTIVITYPUB) { return true;
ActivityPub\Transmitter::sendContactAccept($contact['url'], $contact['hub-verify'], $importer['uid']);
}
} else { } else {
$protocol = self::getProtocol($url, $network);
// send email notification to owner? // send email notification to owner?
if (DBA::exists('contact', ['nurl' => Strings::normaliseLink($url), 'uid' => $importer['uid'], 'pending' => true])) { if (DBA::exists('contact', ['nurl' => Strings::normaliseLink($url), 'uid' => $importer['uid'], 'pending' => true])) {
Logger::log('ignoring duplicated connection request from pending contact ' . $url); Logger::log('ignoring duplicated connection request from pending contact ' . $url);
return; return null;
} }
// create contact record // create contact record
DBA::insert('contact', [ DBA::insert('contact', [
'uid' => $importer['uid'], 'uid' => $importer['uid'],
@ -2237,14 +2232,11 @@ class Contact extends BaseObject
$condition = ['uid' => $importer['uid'], 'url' => $url, 'pending' => true]; $condition = ['uid' => $importer['uid'], 'url' => $url, 'pending' => true];
DBA::update('contact', ['pending' => false], $condition); DBA::update('contact', ['pending' => false], $condition);
$contact = DBA::selectFirst('contact', ['url', 'network', 'hub-verify'], ['id' => $contact_record['id']]); return true;
$protocol = self::getProtocol($contact['url'], $contact['network']); }
}
if ($protocol == Protocol::ACTIVITYPUB) { return null;
ActivityPub\Transmitter::sendContactAccept($contact['url'], $contact['hub-verify'], $importer['uid']);
}
}
}
} }
public static function removeFollower($importer, $contact, array $datarray = [], $item = "") public static function removeFollower($importer, $contact, array $datarray = [], $item = "")

View file

@ -542,6 +542,13 @@ class Processor
self::switchContact($item['author-id']); self::switchContact($item['author-id']);
$result = Contact::addRelationship($owner, $contact, $item, false, $note); $result = Contact::addRelationship($owner, $contact, $item, false, $note);
if ($result === false) {
ActivityPub\Transmitter::sendContactReject($item['author-link'], $item['author-id'], $owner['uid']);
return;
}elseif ($result === true) {
ActivityPub\Transmitter::sendContactAccept($item['author-link'], $item['author-id'], $owner['uid']);
}
$cid = Contact::getIdForURL($activity['actor'], $uid); $cid = Contact::getIdForURL($activity['actor'], $uid);
if (empty($cid)) { if (empty($cid)) {
return; return;