Add common relationship methods to Model\ContactRelation
- Introduce DBA::mergeConditions method - Replace GContact relationship method contents with Model\ContactRelation method calls
This commit is contained in:
parent
a6a23802b7
commit
24a82110fd
5 changed files with 425 additions and 117 deletions
|
@ -21,6 +21,7 @@
|
|||
|
||||
namespace Friendica\Model;
|
||||
|
||||
use Exception;
|
||||
use Friendica\Core\Logger;
|
||||
use Friendica\Core\Protocol;
|
||||
use Friendica\Database\DBA;
|
||||
|
@ -29,6 +30,11 @@ use Friendica\Protocol\ActivityPub;
|
|||
use Friendica\Util\DateTimeFormat;
|
||||
use Friendica\Util\Strings;
|
||||
|
||||
/**
|
||||
* This class provides relationship information based on the `contact-relation` table.
|
||||
* This table is directional (cid = source, relation-cid = target), references public contacts (with uid=0) and records both
|
||||
* follows and the last interaction (likes/comments) on public posts.
|
||||
*/
|
||||
class ContactRelation
|
||||
{
|
||||
/**
|
||||
|
@ -200,4 +206,333 @@ class ContactRelation
|
|||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Counts all the known follows of the provided public contact
|
||||
*
|
||||
* @param int $cid Public contact id
|
||||
* @param array $condition Additional condition on the contact table
|
||||
* @return int
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function countFollows(int $cid, array $condition = [])
|
||||
{
|
||||
$condition = DBA::mergeConditions($condition,
|
||||
['`id` IN (
|
||||
SELECT `relation-cid`
|
||||
FROM `contact-relation`
|
||||
WHERE `cid` = ?
|
||||
AND `follows`
|
||||
)', $cid]
|
||||
);
|
||||
|
||||
return DI::dba()->count('contact', $condition);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a paginated list of contacts that are followed the provided public contact.
|
||||
*
|
||||
* @param int $cid Public contact id
|
||||
* @param array $condition Additional condition on the contact table
|
||||
* @param int $count
|
||||
* @param int $offset
|
||||
* @param bool $shuffle
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function listFollows(int $cid, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false)
|
||||
{
|
||||
$condition = DBA::mergeConditions($condition,
|
||||
['`id` IN (
|
||||
SELECT `relation-cid`
|
||||
FROM `contact-relation`
|
||||
WHERE `cid` = ?
|
||||
AND `follows`
|
||||
)', $cid]
|
||||
);
|
||||
|
||||
$follows = DI::dba()->selectToArray(
|
||||
'contact',
|
||||
$condition,
|
||||
[
|
||||
'limit' => [$offset, $count],
|
||||
'order' => [$shuffle ? 'RAND()' : 'name']
|
||||
]
|
||||
);
|
||||
|
||||
return $follows;
|
||||
}
|
||||
|
||||
/**
|
||||
* Counts all the known followers of the provided public contact
|
||||
*
|
||||
* @param int $cid Public contact id
|
||||
* @param array $condition Additional condition on the contact table
|
||||
* @return int
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function countFollowers(int $cid, array $condition = [])
|
||||
{
|
||||
$condition = DBA::mergeConditions($condition,
|
||||
['`id` IN (
|
||||
SELECT `cid`
|
||||
FROM `contact-relation`
|
||||
WHERE `relation-cid` = ?
|
||||
AND `follows`
|
||||
)', $cid]
|
||||
);
|
||||
|
||||
return DI::dba()->count('contact', $condition);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a paginated list of contacts that follow the provided public contact.
|
||||
*
|
||||
* @param int $cid Public contact id
|
||||
* @param array $condition Additional condition on the contact table
|
||||
* @param int $count
|
||||
* @param int $offset
|
||||
* @param bool $shuffle
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function listFollowers(int $cid, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false)
|
||||
{
|
||||
$condition = DBA::mergeConditions($condition,
|
||||
['`id` IN (
|
||||
SELECT `cid`
|
||||
FROM `contact-relation`
|
||||
WHERE `relation-cid` = ?
|
||||
AND `follows`
|
||||
)', $cid]
|
||||
);
|
||||
|
||||
$followers = DI::dba()->selectToArray(
|
||||
'contact',
|
||||
$condition,
|
||||
[
|
||||
'limit' => [$offset, $count],
|
||||
'order' => [$shuffle ? 'RAND()' : 'name']
|
||||
]
|
||||
);
|
||||
|
||||
return $followers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Counts the number of contacts that both provided public contacts have interacted with at least once.
|
||||
* Interactions include follows and likes and comments on public posts.
|
||||
*
|
||||
* @param int $sourceId Public contact id
|
||||
* @param int $targetId Public contact id
|
||||
* @param array $condition Additional condition array on the contact table
|
||||
* @return int
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function countCommon(int $sourceId, int $targetId, array $condition = [])
|
||||
{
|
||||
$condition = DBA::mergeConditions($condition,
|
||||
['`id` IN (
|
||||
SELECT `relation-cid`
|
||||
FROM `contact-relation`
|
||||
WHERE `cid` = ?
|
||||
)
|
||||
AND `id` IN (
|
||||
SELECT `relation-cid`
|
||||
FROM `contact-relation`
|
||||
WHERE `cid` = ?
|
||||
)', $sourceId, $targetId]
|
||||
);
|
||||
|
||||
$total = DI::dba()->count('contact', $condition);
|
||||
|
||||
return $total;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a paginated list of contacts that both provided public contacts have interacted with at least once.
|
||||
* Interactions include follows and likes and comments on public posts.
|
||||
*
|
||||
* @param int $sourceId Public contact id
|
||||
* @param int $targetId Public contact id
|
||||
* @param array $condition Additional condition on the contact table
|
||||
* @param int $count
|
||||
* @param int $offset
|
||||
* @param bool $shuffle
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function listCommon(int $sourceId, int $targetId, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false)
|
||||
{
|
||||
$condition = DBA::mergeConditions($condition,
|
||||
["`id` IN (
|
||||
SELECT `relation-cid`
|
||||
FROM `contact-relation`
|
||||
WHERE `cid` = ?
|
||||
AND `follows`
|
||||
)
|
||||
AND `id` IN (
|
||||
SELECT `relation-cid`
|
||||
FROM `contact-relation`
|
||||
WHERE `cid` = ?
|
||||
AND `follows`
|
||||
)", $sourceId, $targetId]
|
||||
);
|
||||
|
||||
$contacts = DI::dba()->selectToArray(
|
||||
'contact',
|
||||
$condition,
|
||||
[
|
||||
'limit' => [$offset, $count],
|
||||
'order' => [$shuffle ? 'name' : 'RAND()'],
|
||||
]
|
||||
);
|
||||
|
||||
return $contacts;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Counts the number of contacts that are followed by both provided public contacts.
|
||||
*
|
||||
* @param int $sourceId Public contact id
|
||||
* @param int $targetId Public contact id
|
||||
* @param array $condition Additional condition array on the contact table
|
||||
* @return int
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function countCommonFollows(int $sourceId, int $targetId, array $condition = [])
|
||||
{
|
||||
$condition = DBA::mergeConditions($condition,
|
||||
['`id` IN (
|
||||
SELECT `relation-cid`
|
||||
FROM `contact-relation`
|
||||
WHERE `cid` = ?
|
||||
AND `follows`
|
||||
)
|
||||
AND `id` IN (
|
||||
SELECT `relation-cid`
|
||||
FROM `contact-relation`
|
||||
WHERE `cid` = ?
|
||||
AND `follows`
|
||||
)', $sourceId, $targetId]
|
||||
);
|
||||
|
||||
$total = DI::dba()->count('contact', $condition);
|
||||
|
||||
return $total;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a paginated list of contacts that are followed by both provided public contacts.
|
||||
*
|
||||
* @param int $sourceId Public contact id
|
||||
* @param int $targetId Public contact id
|
||||
* @param array $condition Additional condition array on the contact table
|
||||
* @param int $count
|
||||
* @param int $offset
|
||||
* @param bool $shuffle
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function listCommonFollows(int $sourceId, int $targetId, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false)
|
||||
{
|
||||
$condition = DBA::mergeConditions($condition,
|
||||
["`id` IN (
|
||||
SELECT `relation-cid`
|
||||
FROM `contact-relation`
|
||||
WHERE `cid` = ?
|
||||
AND `follows`
|
||||
)
|
||||
AND `id` IN (
|
||||
SELECT `relation-cid`
|
||||
FROM `contact-relation`
|
||||
WHERE `cid` = ?
|
||||
AND `follows`
|
||||
)", $sourceId, $targetId]
|
||||
);
|
||||
|
||||
$contacts = DI::dba()->selectToArray(
|
||||
'contact',
|
||||
$condition,
|
||||
[
|
||||
'limit' => [$offset, $count],
|
||||
'order' => [$shuffle ? 'name' : 'RAND()'],
|
||||
]
|
||||
);
|
||||
|
||||
return $contacts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Counts the number of contacts that follow both provided public contacts.
|
||||
*
|
||||
* @param int $sourceId Public contact id
|
||||
* @param int $targetId Public contact id
|
||||
* @param array $condition Additional condition on the contact table
|
||||
* @return int
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function countCommonFollowers(int $sourceId, int $targetId, array $condition = [])
|
||||
{
|
||||
$condition = DBA::mergeConditions($condition,
|
||||
['`id` IN (
|
||||
SELECT `cid`
|
||||
FROM `contact-relation`
|
||||
WHERE `relation-cid` = ?
|
||||
AND `follows`
|
||||
)
|
||||
AND `id` IN (
|
||||
SELECT `cid`
|
||||
FROM `contact-relation`
|
||||
WHERE `relation-cid` = ?
|
||||
AND `follows`
|
||||
)', $sourceId, $targetId]
|
||||
);
|
||||
|
||||
$total = DI::dba()->count('contact', $condition);
|
||||
|
||||
return $total;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a paginated list of contacts that follow both provided public contacts.
|
||||
*
|
||||
* @param int $sourceId Public contact id
|
||||
* @param int $targetId Public contact id
|
||||
* @param array $condition Additional condition on the contact table
|
||||
* @param int $count
|
||||
* @param int $offset
|
||||
* @param bool $shuffle
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function listCommonFollowers(int $sourceId, int $targetId, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false)
|
||||
{
|
||||
$condition = DBA::mergeConditions($condition,
|
||||
["`id` IN (
|
||||
SELECT `cid`
|
||||
FROM `contact-relation`
|
||||
WHERE `relation-cid` = ?
|
||||
AND `follows`
|
||||
)
|
||||
AND `id` IN (
|
||||
SELECT `cid`
|
||||
FROM `contact-relation`
|
||||
WHERE `relation-cid` = ?
|
||||
AND `follows`
|
||||
)", $sourceId, $targetId]
|
||||
);
|
||||
|
||||
$contacts = DI::dba()->selectToArray(
|
||||
'contact',
|
||||
$condition,
|
||||
[
|
||||
'limit' => [$offset, $count],
|
||||
'order' => [$shuffle ? 'name' : 'RAND()'],
|
||||
]
|
||||
);
|
||||
|
||||
return $contacts;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue