1
0
Fork 0

Replace "gcontact" with "contact" - imroved suggestions

This commit is contained in:
Michael 2020-07-30 14:08:32 +00:00
commit cc85bc4156
7 changed files with 201 additions and 238 deletions

View file

@ -2915,4 +2915,157 @@ class Contact
return in_array($protocol, [Protocol::DFRN, Protocol::DIASPORA, Protocol::ACTIVITYPUB]) && !$self;
}
/**
* Search contact table by nick or name
*
* @param string $search Name or nick
* @param string $mode Search mode (e.g. "community")
*
* @return array with search results
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function searchByName($search, $mode = '')
{
if (empty($search)) {
return [];
}
// check supported networks
if (DI::config()->get('system', 'diaspora_enabled')) {
$diaspora = Protocol::DIASPORA;
} else {
$diaspora = Protocol::DFRN;
}
if (!DI::config()->get('system', 'ostatus_disabled')) {
$ostatus = Protocol::OSTATUS;
} else {
$ostatus = Protocol::DFRN;
}
// check if we search only communities or every contact
if ($mode === 'community') {
$extra_sql = sprintf(' AND `contact-type` = %d', Contact::TYPE_COMMUNITY);
} else {
$extra_sql = '';
}
$search .= '%';
$results = DBA::p("SELECT * FROM `contact`
WHERE NOT `unsearchable` 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
);
$contacts = DBA::toArray($results);
return $contacts;
}
/**
* @param int $uid user
* @param int $start optional, default 0
* @param int $limit optional, default 80
* @return array
*/
static public function getSuggestions(int $uid, int $start = 0, int $limit = 80)
{
$cid = self::getPublicIdByUserId($uid);
$totallimit = $start + $limit;
$contacts = [];
Logger::info('Collecting suggestions', ['uid' => $uid, 'cid' => $cid, 'start' => $start, 'limit' => $limit]);
$diaspora = DI::config()->get('system', 'diaspora_enabled') ? Protocol::DIASPORA : Protocol::ACTIVITYPUB;
$ostatus = !DI::config()->get('system', 'ostatus_disabled') ? Protocol::OSTATUS : Protocol::ACTIVITYPUB;
// The query returns contacts where contacts interacted with who the given user follows.
// Contacts who already are in the user's contact table are ignored.
$results = DBA::select('contact', [],
["`id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` IN
(SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ?)
AND NOT `cid` IN (SELECT `id` FROM `contact` WHERE `uid` = ? AND `nurl` IN
(SELECT `nurl` FROM `contact` WHERE `uid` = ? AND `rel` IN (?, ?))))
AND NOT `hidden` AND `network` IN (?, ?, ?, ?)",
$cid, 0, $uid, Contact::FRIEND, Contact::SHARING,
Protocol::ACTIVITYPUB, Protocol::DFRN, $diaspora, $ostatus],
['order' => ['last-item' => true], 'limit' => $totallimit]
);
while ($contact = DBA::fetch($results)) {
$contacts[$contact['id']] = $contact;
}
DBA::close($results);
Logger::info('Contacts of contacts who are followed by the given user', ['uid' => $uid, 'cid' => $cid, 'count' => count($contacts)]);
if (count($contacts) >= $totallimit) {
return array_slice($contacts, $start, $limit);
}
// The query returns contacts where contacts interacted with who also interacted with the given user.
// Contacts who already are in the user's contact table are ignored.
$results = DBA::select('contact', [],
["`id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` IN
(SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ?)
AND NOT `cid` IN (SELECT `id` FROM `contact` WHERE `uid` = ? AND `nurl` IN
(SELECT `nurl` FROM `contact` WHERE `uid` = ? AND `rel` IN (?, ?))))
AND NOT `hidden` AND `network` IN (?, ?, ?, ?)",
$cid, 0, $uid, Contact::FRIEND, Contact::SHARING,
Protocol::ACTIVITYPUB, Protocol::DFRN, $diaspora, $ostatus],
['order' => ['last-item' => true], 'limit' => $totallimit]
);
while ($contact = DBA::fetch($results)) {
$contacts[$contact['id']] = $contact;
}
DBA::close($results);
Logger::info('Contacts of contacts who are following the given user', ['uid' => $uid, 'cid' => $cid, 'count' => count($contacts)]);
if (count($contacts) >= $totallimit) {
return array_slice($contacts, $start, $limit);
}
// The query returns contacts that follow the given user but aren't followed by that user.
$results = DBA::select('contact', [],
["`nurl` IN (SELECT `nurl` FROM `contact` WHERE `uid` = ? AND `rel` = ?)
AND NOT `hidden` AND `uid` = ? AND `network` IN (?, ?, ?, ?)",
$uid, Contact::FOLLOWER, 0,
Protocol::ACTIVITYPUB, Protocol::DFRN, $diaspora, $ostatus],
['order' => ['last-item' => true], 'limit' => $totallimit]
);
while ($contact = DBA::fetch($results)) {
$contacts[$contact['id']] = $contact;
}
DBA::close($results);
Logger::info('Followers that are not followed by the given user', ['uid' => $uid, 'cid' => $cid, 'count' => count($contacts)]);
if (count($contacts) >= $totallimit) {
return array_slice($contacts, $start, $limit);
}
// The query returns any contact that isn't followed by that user.
$results = DBA::select('contact', [],
["NOT `nurl` IN (SELECT `nurl` FROM `contact` WHERE `uid` = ? AND `rel` IN (?, ?))
AND NOT `hidden` AND `uid` = ? AND `network` IN (?, ?, ?, ?)",
$uid, Contact::FRIEND, Contact::SHARING, 0,
Protocol::ACTIVITYPUB, Protocol::DFRN, $diaspora, $ostatus],
['order' => ['last-item' => true], 'limit' => $totallimit]
);
while ($contact = DBA::fetch($results)) {
$contacts[$contact['id']] = $contact;
}
DBA::close($results);
Logger::info('Any contact', ['uid' => $uid, 'cid' => $cid, 'count' => count($contacts)]);
return array_slice($contacts, $start, $limit);
}
}

View file

@ -28,14 +28,12 @@ use Friendica\Core\Logger;
use Friendica\Core\Protocol;
use Friendica\Core\Search;
use Friendica\Core\System;
use Friendica\Core\Worker;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Network\Probe;
use Friendica\Protocol\ActivityPub;
use Friendica\Protocol\PortableContact;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Network;
use Friendica\Util\Strings;
/**
@ -43,67 +41,6 @@ use Friendica\Util\Strings;
*/
class GContact
{
/**
* Search global contact table by nick or name
*
* @param string $search Name or nick
* @param string $mode Search mode (e.g. "community")
*
* @return array with search results
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function searchByName($search, $mode = '')
{
if (empty($search)) {
return [];
}
// check supported networks
if (DI::config()->get('system', 'diaspora_enabled')) {
$diaspora = Protocol::DIASPORA;
} else {
$diaspora = Protocol::DFRN;
}
if (!DI::config()->get('system', 'ostatus_disabled')) {
$ostatus = Protocol::OSTATUS;
} else {
$ostatus = Protocol::DFRN;
}
// check if we search only communities or every contact
if ($mode === 'community') {
$extra_sql = ' AND `community`';
} else {
$extra_sql = '';
}
$search .= '%';
$results = DBA::p("SELECT `nurl` FROM `gcontact`
WHERE NOT `hide` AND `network` IN (?, ?, ?, ?) AND
NOT `failed` AND
(`addr` LIKE ? OR `name` LIKE ? OR `nick` LIKE ?) $extra_sql
GROUP BY `nurl` ORDER BY `nurl` DESC LIMIT 1000",
Protocol::DFRN, Protocol::ACTIVITYPUB, $ostatus, $diaspora, $search, $search, $search
);
$gcontacts = [];
while ($result = DBA::fetch($results)) {
$urlparts = parse_url($result['nurl']);
// Ignore results that look strange.
// For historic reasons the gcontact table does contain some garbage.
if (empty($result['nurl']) || !empty($urlparts['query']) || !empty($urlparts['fragment'])) {
continue;
}
$gcontacts[] = Contact::getByURLForUser($result['nurl'], local_user());
}
DBA::close($results);
return $gcontacts;
}
/**
* Link the gcontact entry with user, contact and global contact
*
@ -424,92 +361,6 @@ class GContact
return $r;
}
/**
* @param int $uid user
* @param integer $start optional, default 0
* @param integer $limit optional, default 80
* @return array
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function suggestionQuery($uid, $start = 0, $limit = 80)
{
if (!$uid) {
return [];
}
$network = [Protocol::DFRN, Protocol::ACTIVITYPUB];
if (DI::config()->get('system', 'diaspora_enabled')) {
$network[] = Protocol::DIASPORA;
}
if (!DI::config()->get('system', 'ostatus_disabled')) {
$network[] = Protocol::OSTATUS;
}
$sql_network = "'" . implode("', '", $network) . "'";
/// @todo This query is really slow
// By now we cache the data for five minutes
$r = q(
"SELECT count(glink.gcid) as `total`, gcontact.* from gcontact
INNER JOIN `glink` ON `glink`.`gcid` = `gcontact`.`id`
where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d )
AND NOT `gcontact`.`name` IN (SELECT `name` FROM `contact` WHERE `uid` = %d)
AND NOT `gcontact`.`id` IN (SELECT `gcid` FROM `gcign` WHERE `uid` = %d)
AND `gcontact`.`updated` >= '%s' AND NOT `gcontact`.`hide`
AND NOT `gcontact`.`failed`
AND `gcontact`.`network` IN (%s)
GROUP BY `glink`.`gcid` ORDER BY `gcontact`.`updated` DESC,`total` DESC LIMIT %d, %d",
intval($uid),
intval($uid),
intval($uid),
intval($uid),
DBA::NULL_DATETIME,
$sql_network,
intval($start),
intval($limit)
);
if (DBA::isResult($r) && count($r) >= ($limit -1)) {
return $r;
}
$r2 = q(
"SELECT gcontact.* FROM gcontact
INNER JOIN `glink` ON `glink`.`gcid` = `gcontact`.`id`
WHERE `glink`.`uid` = 0 AND `glink`.`cid` = 0 AND `glink`.`zcid` = 0 AND NOT `gcontact`.`nurl` IN (SELECT `nurl` FROM `contact` WHERE `uid` = %d)
AND NOT `gcontact`.`name` IN (SELECT `name` FROM `contact` WHERE `uid` = %d)
AND NOT `gcontact`.`id` IN (SELECT `gcid` FROM `gcign` WHERE `uid` = %d)
AND `gcontact`.`updated` >= '%s'
AND NOT `gcontact`.`failed`
AND `gcontact`.`network` IN (%s)
ORDER BY rand() LIMIT %d, %d",
intval($uid),
intval($uid),
intval($uid),
DBA::NULL_DATETIME,
$sql_network,
intval($start),
intval($limit)
);
$list = [];
foreach ($r2 as $suggestion) {
$list[$suggestion['nurl']] = $suggestion;
}
foreach ($r as $suggestion) {
$list[$suggestion['nurl']] = $suggestion;
}
while (sizeof($list) > ($limit)) {
array_pop($list);
}
return $list;
}
/**
* @return void
* @throws \Friendica\Network\HTTPException\InternalServerErrorException