Merge pull request #9314 from annando/suggestions

New function for contact suggestions
This commit is contained in:
Hypolite Petovan 2020-10-03 13:32:21 -04:00 committed by GitHub
commit 2bfd9851d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 63 additions and 153 deletions

View File

@ -177,15 +177,15 @@ function ping_init(App $a)
$intros1 = q( $intros1 = q(
"SELECT `intro`.`id`, `intro`.`datetime`, "SELECT `intro`.`id`, `intro`.`datetime`,
`fcontact`.`name`, `fcontact`.`url`, `fcontact`.`photo` `fcontact`.`name`, `fcontact`.`url`, `fcontact`.`photo`
FROM `intro` LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id` FROM `intro` INNER JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`fid` != 0", WHERE `intro`.`uid` = %d AND NOT `intro`.`blocked` AND NOT `intro`.`ignore` AND `intro`.`fid` != 0",
intval(local_user()) intval(local_user())
); );
$intros2 = q( $intros2 = q(
"SELECT `intro`.`id`, `intro`.`datetime`, "SELECT `intro`.`id`, `intro`.`datetime`,
`contact`.`name`, `contact`.`url`, `contact`.`photo` `contact`.`name`, `contact`.`url`, `contact`.`photo`
FROM `intro` LEFT JOIN `contact` ON `intro`.`contact-id` = `contact`.`id` FROM `intro` INNER JOIN `contact` ON `intro`.`contact-id` = `contact`.`id`
WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`contact-id` != 0", WHERE `intro`.`uid` = %d AND NOT `intro`.`blocked` AND NOT `intro`.`ignore` AND `intro`.`contact-id` != 0 AND `intro`.`fid` = 0",
intval(local_user()) intval(local_user())
); );

View File

@ -43,12 +43,12 @@ class FContact
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException * @throws \ImagickException
*/ */
public static function getByURL($handle, $update = null) public static function getByURL($handle, $update = null, $network = Protocol::DIASPORA)
{ {
$person = DBA::selectFirst('fcontact', [], ['network' => Protocol::DIASPORA, 'addr' => $handle]); $person = DBA::selectFirst('fcontact', [], ['network' => $network, 'addr' => $handle]);
if (!DBA::isResult($person)) { if (!DBA::isResult($person)) {
$urls = [$handle, str_replace('http://', 'https://', $handle), Strings::normaliseLink($handle)]; $urls = [$handle, str_replace('http://', 'https://', $handle), Strings::normaliseLink($handle)];
$person = DBA::selectFirst('fcontact', [], ['network' => Protocol::DIASPORA, 'url' => $urls]); $person = DBA::selectFirst('fcontact', [], ['network' => $network, 'url' => $urls]);
} }
if (DBA::isResult($person)) { if (DBA::isResult($person)) {
@ -73,14 +73,14 @@ class FContact
if ($update) { if ($update) {
Logger::info('create or refresh', ['handle' => $handle]); Logger::info('create or refresh', ['handle' => $handle]);
$r = Probe::uri($handle, Protocol::DIASPORA); $r = Probe::uri($handle, $network);
// Note that Friendica contacts will return a "Diaspora person" // Note that Friendica contacts will return a "Diaspora person"
// if Diaspora connectivity is enabled on their server // if Diaspora connectivity is enabled on their server
if ($r && ($r["network"] === Protocol::DIASPORA)) { if ($r && ($r["network"] === $network)) {
self::updateFContact($r); self::updateFContact($r);
$person = self::getByURL($handle, false); $person = self::getByURL($handle, false, $network);
} }
} }
@ -135,56 +135,45 @@ class FContact
} }
/** /**
* Add suggestions for a given contact * Suggest a given contact to a given user from a given contact
* *
* @param integer $uid * @param integer $uid
* @param integer $cid * @param integer $cid
* @param integer $from_cid
* @return bool Was the adding successful? * @return bool Was the adding successful?
*/ */
public static function addSuggestion(int $uid, int $cid) public static function addSuggestion(int $uid, int $cid, int $from_cid, string $note = '')
{ {
$owner = User::getOwnerDataById($uid); $owner = User::getOwnerDataById($uid);
$contact = Contact::getById($cid); $contact = Contact::getById($cid);
$from_contact = Contact::getById($from_cid);
if (DBA::exists('contact', ['nurl' => Strings::normaliseLink($contact['url']), 'uid' => $uid])) { if (DBA::exists('contact', ['nurl' => Strings::normaliseLink($contact['url']), 'uid' => $uid])) {
return false; return false;
} }
$fcontact = self::getByURL($contact['url'], null, $contact['network']);
if (empty($fcontact)) {
Logger::warning('FContact had not been found', ['fcontact' => $contact['url']]);
return false;
}
$fid = $fcontact['id'];
// Quit if we already have an introduction for this person
if (DBA::exists('intro', ['uid' => $uid, 'fid' => $fid])) {
return false;
}
$suggest = []; $suggest = [];
$suggest['uid'] = $uid; $suggest['uid'] = $uid;
$suggest['cid'] = $contact['id']; $suggest['cid'] = $from_cid;
$suggest['url'] = $contact['url']; $suggest['url'] = $contact['url'];
$suggest['name'] = $contact['name']; $suggest['name'] = $contact['name'];
$suggest['photo'] = $contact['photo']; $suggest['photo'] = $contact['photo'];
$suggest['request'] = $contact['request']; $suggest['request'] = $contact['request'];
$suggest['title'] = ''; $suggest['title'] = '';
$suggest['body'] = ''; $suggest['body'] = $note;
// Do we already have an fcontact record for this person?
$fid = 0;
$fcontact = DBA::selectFirst('fcontact', ['id'], ['url' => $suggest['url']]);
if (DBA::isResult($fcontact)) {
$fid = $fcontact['id'];
$fields = ['name' => $suggest['name'], 'photo' => $suggest['photo'], 'request' => $suggest['request']];
DBA::update('fcontact', $fields, ['id' => $fid]);
// Quit if we already have an introduction for this person
if (DBA::exists('intro', ['uid' => $suggest['uid'], 'fid' => $fid])) {
return false;
}
}
if (empty($fid)) {
$fields = ['name' => $suggest['name'], 'url' => $suggest['url'],
'photo' => $suggest['photo'], 'request' => $suggest['request']];
DBA::insert('fcontact', $fields);
$fid = DBA::lastInsertId();
if (empty($fid)) {
Logger::warning('FContact had not been created', ['fcontact' => $fields]);
return false;
}
}
$hash = Strings::getRandomHex(); $hash = Strings::getRandomHex();
$fields = ['uid' => $suggest['uid'], 'fid' => $fid, 'contact-id' => $suggest['cid'], $fields = ['uid' => $suggest['uid'], 'fid' => $fid, 'contact-id' => $suggest['cid'],
@ -200,9 +189,9 @@ class FContact
'uid' => $owner['uid'], 'uid' => $owner['uid'],
'item' => $suggest, 'item' => $suggest,
'link' => DI::baseUrl().'/notifications/intros', 'link' => DI::baseUrl().'/notifications/intros',
'source_name' => $contact['name'], 'source_name' => $from_contact['name'],
'source_link' => $contact['url'], 'source_link' => $from_contact['url'],
'source_photo' => $contact['photo'], 'source_photo' => $from_contact['photo'],
'verb' => Activity::REQ_FRIEND, 'verb' => Activity::REQ_FRIEND,
'otype' => 'intro' 'otype' => 'intro'
]); ]);

View File

@ -164,11 +164,7 @@ class Introduction extends BaseModel
} }
$contact = Contact::selectFirst([], ['id' => $this->{'contact-id'}, 'uid' => $this->uid]); $contact = Contact::selectFirst([], ['id' => $this->{'contact-id'}, 'uid' => $this->uid]);
if (!empty($contact)) {
if (!$contact) {
throw new HTTPException\NotFoundException('Contact record not found.');
}
if (!empty($contact['protocol'])) { if (!empty($contact['protocol'])) {
$protocol = $contact['protocol']; $protocol = $contact['protocol'];
} else { } else {
@ -178,6 +174,7 @@ class Introduction extends BaseModel
if ($protocol == Protocol::ACTIVITYPUB) { if ($protocol == Protocol::ACTIVITYPUB) {
ActivityPub\Transmitter::sendContactReject($contact['url'], $contact['hub-verify'], $contact['uid']); ActivityPub\Transmitter::sendContactReject($contact['url'], $contact['hub-verify'], $contact['uid']);
} }
}
return $this->intro->delete($this); return $this->intro->delete($this);
} }

View File

@ -27,6 +27,7 @@ use Friendica\Core\Protocol;
use Friendica\Core\Renderer; use Friendica\Core\Renderer;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\DI; use Friendica\DI;
use Friendica\Model\User;
use Friendica\Module\BaseNotifications; use Friendica\Module\BaseNotifications;
use Friendica\Object\Notification\Introduction; use Friendica\Object\Notification\Introduction;
@ -76,18 +77,13 @@ class Introductions extends BaseNotifications
'text' => (!$all ? DI::l10n()->t('Show Ignored Requests') : DI::l10n()->t('Hide Ignored Requests')), 'text' => (!$all ? DI::l10n()->t('Show Ignored Requests') : DI::l10n()->t('Hide Ignored Requests')),
]; ];
$owner = User::getOwnerDataById(local_user());
// Loop through all introduction notifications.This creates an array with the output html for each // Loop through all introduction notifications.This creates an array with the output html for each
// introduction // introduction
/** @var Introduction $notification */ /** @var Introduction $notification */
foreach ($notifications['notifications'] as $notification) { foreach ($notifications['notifications'] as $notification) {
$helptext = DI::l10n()->t('Shall your connection be bidirectional or not?');
$helptext2 = DI::l10n()->t('Accepting %s as a friend allows %s to subscribe to your posts, and you will also receive updates from them in your news feed.', $notification->getName(), $notification->getName());
$helptext3 = DI::l10n()->t('Accepting %s as a subscriber allows them to subscribe to your posts, but you will not receive updates from them in your news feed.', $notification->getName());
$friend = ['duplex', DI::l10n()->t('Friend'), '1', $helptext2, true];
$follower = ['duplex', DI::l10n()->t('Subscriber'), '0', $helptext3, false];
// There are two kind of introduction. Contacts suggested by other contacts and normal connection requests. // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests.
// We have to distinguish between these two because they use different data. // We have to distinguish between these two because they use different data.
switch ($notification->getLabel()) { switch ($notification->getLabel()) {
@ -105,18 +101,14 @@ class Introductions extends BaseNotifications
'$contact_id' => $notification->getContactId(), '$contact_id' => $notification->getContactId(),
'$photo' => $notification->getPhoto(), '$photo' => $notification->getPhoto(),
'$fullname' => $notification->getName(), '$fullname' => $notification->getName(),
'$lbl_connection_type' => $helptext, '$dfrn_url' => $owner['url'],
'$friend' => $friend,
'$follower' => $follower,
'$url' => $notification->getUrl(), '$url' => $notification->getUrl(),
'$zrl' => $notification->getZrl(), '$zrl' => $notification->getZrl(),
'$lbl_url' => DI::l10n()->t('Profile URL'), '$lbl_url' => DI::l10n()->t('Profile URL'),
'$addr' => $notification->getAddr(), '$addr' => $notification->getAddr(),
'$hidden' => ['hidden', DI::l10n()->t('Hide this contact from others'), $notification->isHidden(), ''], '$action' => 'follow',
'$knowyou' => $notification->getKnowYou(),
'$approve' => DI::l10n()->t('Approve'), '$approve' => DI::l10n()->t('Approve'),
'$note' => $notification->getNote(), '$note' => $notification->getNote(),
'$request' => $notification->getRequest(),
'$ignore' => DI::l10n()->t('Ignore'), '$ignore' => DI::l10n()->t('Ignore'),
'$discard' => DI::l10n()->t('Discard'), '$discard' => DI::l10n()->t('Discard'),
]); ]);
@ -132,6 +124,13 @@ class Introductions extends BaseNotifications
$knowyou = ''; $knowyou = '';
} }
$helptext = DI::l10n()->t('Shall your connection be bidirectional or not?');
$helptext2 = DI::l10n()->t('Accepting %s as a friend allows %s to subscribe to your posts, and you will also receive updates from them in your news feed.', $notification->getName(), $notification->getName());
$helptext3 = DI::l10n()->t('Accepting %s as a subscriber allows them to subscribe to your posts, but you will not receive updates from them in your news feed.', $notification->getName());
$friend = ['duplex', DI::l10n()->t('Friend'), '1', $helptext2, true];
$follower = ['duplex', DI::l10n()->t('Subscriber'), '0', $helptext3, false];
$contact = DBA::selectFirst('contact', ['network', 'protocol'], ['id' => $notification->getContactId()]); $contact = DBA::selectFirst('contact', ['network', 'protocol'], ['id' => $notification->getContactId()]);
if (($contact['network'] != Protocol::DFRN) || ($contact['protocol'] == Protocol::ACTIVITYPUB)) { if (($contact['network'] != Protocol::DFRN) || ($contact['protocol'] == Protocol::ACTIVITYPUB)) {

View File

@ -1793,91 +1793,13 @@ class DFRN
*/ */
private static function processSuggestion($xpath, $suggestion, $importer) private static function processSuggestion($xpath, $suggestion, $importer)
{ {
Logger::log('Processing suggestions'); Logger::notice('Processing suggestions');
/// @TODO Rewrite this to one statement $url = $xpath->evaluate('string(dfrn:url[1]/text())', $suggestion);
$suggest = []; $cid = Contact::getIdForURL($url);
$suggest['uid'] = $importer['importer_uid']; $note = $xpath->evaluate('string(dfrn:note[1]/text())', $suggestion);
$suggest['cid'] = $importer['id'];
$suggest['url'] = $xpath->query('dfrn:url/text()', $suggestion)->item(0)->nodeValue;
$suggest['name'] = $xpath->query('dfrn:name/text()', $suggestion)->item(0)->nodeValue;
$suggest['photo'] = $xpath->query('dfrn:photo/text()', $suggestion)->item(0)->nodeValue;
$suggest['request'] = $xpath->query('dfrn:request/text()', $suggestion)->item(0)->nodeValue;
$suggest['body'] = $xpath->query('dfrn:note/text()', $suggestion)->item(0)->nodeValue;
// Does our member already have a friend matching this description? return FContact::addSuggestion($importer['importer_uid'], $cid, $importer['id'], $note);
/*
* The valid result means the friend we're about to send a friend
* suggestion already has them in their contact, which means no further
* action is required.
*
* @see https://github.com/friendica/friendica/pull/3254#discussion_r107315246
*/
$condition = ['nurl' => Strings::normaliseLink($suggest['url']), 'uid' => $suggest['uid']];
if (DBA::exists('contact', $condition)) {
return false;
}
// Do we already have an fcontact record for this person?
$fid = 0;
$fcontact = DBA::selectFirst('fcontact', ['id'], ['url' => $suggest['url']]);
if (DBA::isResult($fcontact)) {
$fid = $fcontact['id'];
// OK, we do. Do we already have an introduction for this person?
if (DBA::exists('intro', ['uid' => $suggest['uid'], 'fid' => $fid])) {
/*
* The valid result means the friend we're about to send a friend
* suggestion already has them in their contact, which means no further
* action is required.
*
* @see https://github.com/friendica/friendica/pull/3254#discussion_r107315246
*/
return false;
}
}
if (!$fid) {
$fields = ['name' => $suggest['name'], 'url' => $suggest['url'],
'photo' => $suggest['photo'], 'request' => $suggest['request']];
DBA::insert('fcontact', $fields);
$fid = DBA::lastInsertId();
}
/*
* If no record in fcontact is found, below INSERT statement will not
* link an introduction to it.
*/
if (empty($fid)) {
// Database record did not get created. Quietly give up.
exit();
}
$hash = Strings::getRandomHex();
$fields = ['uid' => $suggest['uid'], 'fid' => $fid, 'contact-id' => $suggest['cid'],
'note' => $suggest['body'], 'hash' => $hash, 'datetime' => DateTimeFormat::utcNow(), 'blocked' => false];
DBA::insert('intro', $fields);
notification(
[
'type' => Type::SUGGEST,
'notify_flags' => $importer['notify-flags'],
'language' => $importer['language'],
'to_name' => $importer['username'],
'to_email' => $importer['email'],
'uid' => $importer['importer_uid'],
'item' => $suggest,
'link' => DI::baseUrl().'/notifications/intros',
'source_name' => $importer['name'],
'source_link' => $importer['url'],
'source_photo' => $importer['photo'],
'verb' => Activity::REQ_FRIEND,
'otype' => 'intro']
);
return true;
} }
/** /**

View File

@ -52,17 +52,20 @@
<h3 class="heading">{{$fullname}}{{if $addr}}&nbsp;({{$addr}}){{/if}}</h3> <h3 class="heading">{{$fullname}}{{if $addr}}&nbsp;({{$addr}}){{/if}}</h3>
<form class="intro-approve-form" {{if $request}}action="{{$request}}" method="get"{{else}}action="{{$action}}" method="post"{{/if}}> <form class="intro-approve-form" {{if $request}}action="{{$request}}" method="get"{{else}}action="{{$action}}" method="post"{{/if}}>
{{if $type != "friend_suggestion"}}
{{include file="field_checkbox.tpl" field=$hidden}} {{include file="field_checkbox.tpl" field=$hidden}}
<div role="radiogroup" aria-labelledby="connection_type"> <div role="radiogroup" aria-labelledby="connection_type">
<label id="connection_type">{{$lbl_connection_type}}</label> <label id="connection_type">{{$lbl_connection_type}}</label>
{{include file="field_radio.tpl" field=$friend}} {{include file="field_radio.tpl" field=$friend}}
{{include file="field_radio.tpl" field=$follower}} {{include file="field_radio.tpl" field=$follower}}
</div> </div>
{{if $type != "friend_suggestion"}}
<input type="hidden" name="dfrn_id" value="{{$dfrn_id}}" > <input type="hidden" name="dfrn_id" value="{{$dfrn_id}}" >
<input type="hidden" name="intro_id" value="{{$intro_id}}" > <input type="hidden" name="intro_id" value="{{$intro_id}}" >
<input type="hidden" name="contact_id" value="{{$contact_id}}" > <input type="hidden" name="contact_id" value="{{$contact_id}}" >
{{else}}
{{if $note}}<div>{{$note}}</div>{{/if}}
<input type="hidden" name="url" value="{{$url}}" >
<input type="hidden" name="dfrn-url" value="{{$dfrn_url}}" >
{{/if}} {{/if}}
<div class="pull-right"> <div class="pull-right">
<button class="btn btn-primary intro-submit-approve" type="submit" name="submit" value="{{$approve}}">{{$approve}}</button> <button class="btn btn-primary intro-submit-approve" type="submit" name="submit" value="{{$approve}}">{{$approve}}</button>