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 bool $sharing True: Contact is now sharing with Owner; False: Contact is now following Owner (default)
* @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 \ImagickException
*/
@ -2133,14 +2134,14 @@ class Contact extends BaseObject
{
// Should always be set
if (empty($datarray['author-id'])) {
return;
return false;
}
$fields = ['url', 'name', 'nick', 'photo', 'network'];
$pub_contact = DBA::selectFirst('contact', $fields, ['id' => $datarray['author-id']]);
if (!DBA::isResult($pub_contact)) {
// Should never happen
return;
return false;
}
$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
self::unmarkForArchival($contact);
$protocol = self::getProtocol($url, $contact['network']);
if (($contact['rel'] == self::SHARING)
|| ($sharing && $contact['rel'] == self::FOLLOWER)) {
DBA::update('contact', ['rel' => self::FRIEND, 'writable' => true, 'pending' => false],
['id' => $contact['id'], 'uid' => $importer['uid']]);
}
if ($protocol == Protocol::ACTIVITYPUB) {
ActivityPub\Transmitter::sendContactAccept($contact['url'], $contact['hub-verify'], $importer['uid']);
}
return true;
} else {
$protocol = self::getProtocol($url, $network);
// send email notification to owner?
if (DBA::exists('contact', ['nurl' => Strings::normaliseLink($url), 'uid' => $importer['uid'], 'pending' => true])) {
Logger::log('ignoring duplicated connection request from pending contact ' . $url);
return;
return null;
}
// create contact record
DBA::insert('contact', [
'uid' => $importer['uid'],
@ -2237,14 +2232,11 @@ class Contact extends BaseObject
$condition = ['uid' => $importer['uid'], 'url' => $url, 'pending' => true];
DBA::update('contact', ['pending' => false], $condition);
$contact = DBA::selectFirst('contact', ['url', 'network', 'hub-verify'], ['id' => $contact_record['id']]);
$protocol = self::getProtocol($contact['url'], $contact['network']);
if ($protocol == Protocol::ACTIVITYPUB) {
ActivityPub\Transmitter::sendContactAccept($contact['url'], $contact['hub-verify'], $importer['uid']);
}
return true;
}
}
return null;
}
public static function removeFollower($importer, $contact, array $datarray = [], $item = "")

View File

@ -542,6 +542,13 @@ class Processor
self::switchContact($item['author-id']);
$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);
if (empty($cid)) {
return;