Merge pull request #14481 from annando/issue-14121

Issue 14121: Show precise contact counts for own user
This commit is contained in:
Tobias Diekershoff 2024-10-20 20:03:53 +02:00 committed by GitHub
commit 4380d7fa12
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 69 additions and 10 deletions

View file

@ -91,23 +91,59 @@ class Widget
$networks = [Protocol::PHANTOM, Protocol::FACEBOOK, Protocol::APPNET, Protocol::TWITTER, Protocol::ZOT, Protocol::OSTATUS, Protocol::STATUSNET];
Addon::loadAddons();
if (!Addon::isEnabled("discourse")) {
if (!Addon::isEnabled('discourse')) {
$networks[] = Protocol::DISCOURSE;
}
if (!Addon::isEnabled("pumpio")) {
if (!Addon::isEnabled('pumpio')) {
$networks[] = Protocol::PUMPIO;
}
if (!Addon::isEnabled("tumblr")) {
if (!Addon::isEnabled('tumblr')) {
$networks[] = Protocol::TUMBLR;
}
if (!DI::config()->get("system", "diaspora_enabled")) {
if (!DI::config()->get('system', 'diaspora_enabled')) {
$networks[] = Protocol::DIASPORA;
}
if (!Addon::isEnabled("pnut")) {
if (!Addon::isEnabled('pnut')) {
$networks[] = Protocol::PNUT;
}
return $networks;
}
/**
* Return available networks as array
*
* @return array Supported networks
*/
public static function availableNetworks(): array
{
$networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::FEED];
Addon::loadAddons();
if (Addon::isEnabled('discourse')) {
$networks[] = Protocol::DISCOURSE;
}
if (Addon::isEnabled('pumpio')) {
$networks[] = Protocol::PUMPIO;
}
if (Addon::isEnabled('tumblr')) {
$networks[] = Protocol::TUMBLR;
}
if (DI::config()->get('system', 'diaspora_enabled')) {
$networks[] = Protocol::DIASPORA;
}
if (function_exists('imap_open') && !DI::config()->get('system', 'imap_disabled')) {
$networks[] = Protocol::MAIL;
}
if (Addon::isEnabled('pnut')) {
$networks[] = Protocol::PNUT;
}
return $networks;
@ -382,7 +418,7 @@ class Widget
$tpl = Renderer::getMarkupTemplate('widget/remote_friends_common.tpl');
return Renderer::replaceMacros($tpl, [
'$desc' => DI::l10n()->tt("%d contact in common", "%d contacts in common", $total),
'$desc' => DI::l10n()->tt('%d contact in common', '%d contacts in common', $total),
'$base' => DI::baseUrl(),
'$nickname' => $nickname,
'$linkmore' => $total > 5 ? 'true' : '',

View file

@ -10,8 +10,10 @@ namespace Friendica\Factory\Api\Mastodon;
use Friendica\App\BaseURL;
use Friendica\BaseFactory;
use Friendica\Collection\Api\Mastodon\Fields;
use Friendica\Content\Widget;
use Friendica\Database\DBA;
use Friendica\Model\Contact;
use Friendica\Model\User;
use Friendica\Network\HTTPException;
use Friendica\Profile\ProfileField\Repository\ProfileField as ProfileFieldRepository;
use ImagickException;
@ -75,10 +77,15 @@ class Account extends BaseFactory
$fields = new Fields();
if (Contact::isLocal($account['url'])) {
$self_contact = Contact::selectFirst(['uid'], ['nurl' => $account['nurl'], 'self' => true]);
if (!empty($self_contact['uid'])) {
$profileFields = $this->profileFieldRepo->selectPublicFieldsByUserId($self_contact['uid']);
$profile_uid = User::getIdForContactId($account['id']);
if ($profile_uid) {
$profileFields = $this->profileFieldRepo->selectPublicFieldsByUserId($profile_uid);
$fields = $this->mstdnFieldFactory->createFromProfileFields($profileFields);
if ($profile_uid == $uid) {
$account['ap-followers_count'] = $this->getContactRelationCountForUid($uid, [Contact::FOLLOWER, Contact::FRIEND]);
$account['ap-following_count'] = $this->getContactRelationCountForUid($uid, [Contact::SHARING, Contact::FRIEND]);
}
}
}
@ -98,4 +105,20 @@ class Account extends BaseFactory
return new \Friendica\Object\Api\Mastodon\Account($this->baseUrl, $account, $fields);
}
private function getContactRelationCountForUid(int $uid, array $rel): int
{
$condition = [
'uid' => $uid,
'rel' => $rel,
'self' => false,
'deleted' => false,
'archive' => false,
'pending' => false,
'blocked' => false,
'network' => Widget::availableNetworks(),
];
return DBA::count('contact', $condition);
}
}