3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Module\Contact;
25 use Friendica\BaseModule;
26 use Friendica\Content\Pager;
27 use Friendica\Content\Widget;
28 use Friendica\Core\L10n;
29 use Friendica\Core\Renderer;
30 use Friendica\Core\Session\Capability\IHandleUserSessions;
32 use Friendica\Model\User;
34 use Friendica\Module\Response;
35 use Friendica\Network\HTTPException;
36 use Friendica\Util\Profiler;
37 use Psr\Log\LoggerInterface;
39 class Contacts extends BaseModule
41 /** @var IHandleUserSessions */
46 public function __construct(App\Page $page, IHandleUserSessions $userSession, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
48 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
50 $this->userSession = $userSession;
54 protected function content(array $request = []): string
56 if (!$this->userSession->getLocalUserId()) {
57 throw new HTTPException\ForbiddenException();
60 $cid = $this->parameters['id'];
61 $type = $this->parameters['type'] ?? 'all';
62 $accounttype = $request['accounttype'] ?? '';
63 $accounttypeid = User::getAccountTypeByString($accounttype);
66 throw new HTTPException\BadRequestException($this->t('Invalid contact.'));
69 $contact = Model\Contact::getById($cid, []);
70 if (empty($contact)) {
71 throw new HTTPException\NotFoundException($this->t('Contact not found.'));
74 $localContactId = Model\Contact::getPublicIdByUserId($this->userSession->getLocalUserId());
76 $this->page['aside'] = Widget\VCard::getHTML($contact);
85 if (isset($accounttypeid)) {
86 $condition['contact-type'] = $accounttypeid;
89 $noresult_label = $this->t('No known contacts.');
93 $total = Model\Contact\Relation::countFollowers($cid, $condition);
96 $total = Model\Contact\Relation::countFollows($cid, $condition);
99 $total = Model\Contact\Relation::countMutuals($cid, $condition);
102 $total = Model\Contact\Relation::countCommon($localContactId, $cid, $condition);
103 $noresult_label = $this->t('No common contacts.');
106 $total = Model\Contact\Relation::countAll($cid, $condition);
109 $pager = new Pager($this->l10n, $this->args->getQueryString(), 30);
114 $friends = Model\Contact\Relation::listFollowers($cid, $condition, $pager->getItemsPerPage(), $pager->getStart());
115 $title = $this->tt('Follower (%s)', 'Followers (%s)', $total);
118 $friends = Model\Contact\Relation::listFollows($cid, $condition, $pager->getItemsPerPage(), $pager->getStart());
119 $title = $this->tt('Following (%s)', 'Following (%s)', $total);
122 $friends = Model\Contact\Relation::listMutuals($cid, $condition, $pager->getItemsPerPage(), $pager->getStart());
123 $title = $this->tt('Mutual friend (%s)', 'Mutual friends (%s)', $total);
125 'These contacts both follow and are followed by <strong>%s</strong>.',
126 htmlentities($contact['name'], ENT_COMPAT, 'UTF-8')
130 $friends = Model\Contact\Relation::listCommon($localContactId, $cid, $condition, $pager->getItemsPerPage(), $pager->getStart());
131 $title = $this->tt('Common contact (%s)', 'Common contacts (%s)', $total);
133 'Both <strong>%s</strong> and yourself have publicly interacted with these contacts (follow, comment or likes on public posts).',
134 htmlentities($contact['name'], ENT_COMPAT, 'UTF-8')
138 $friends = Model\Contact\Relation::listAll($cid, $condition, $pager->getItemsPerPage(), $pager->getStart());
139 $title = $this->tt('Contact (%s)', 'Contacts (%s)', $total);
142 $o = Module\Contact::getTabsHTML($contact, Module\Contact::TAB_CONTACTS);
144 $tabs = self::getContactFilterTabs('contact/' . $cid, $type, true);
146 // Contact list is obtained from the visited contact, but the contact display is visitor dependent
147 $contacts = array_map(
148 function ($contact) {
149 $contact = Model\Contact::selectFirst(
151 ['uri-id' => $contact['uri-id'], 'uid' => [0, $this->userSession->getLocalUserId()]],
152 ['order' => ['uid' => 'DESC']]
154 return Module\Contact::getContactTemplateVars($contact);
159 $tpl = Renderer::getMarkupTemplate('profile/contacts.tpl');
160 $o .= Renderer::replaceMacros($tpl, [
165 '$noresult_label' => $noresult_label,
167 '$contacts' => $contacts,
168 '$paginate' => $pager->renderFull($total),
171 $this->page['aside'] .= Widget::accountTypes($_SERVER['REQUEST_URI'], $accounttype);