3 * @copyright Copyright (C) 2010-2022, 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\Navigation\Notifications\Factory;
26 use Friendica\App\BaseURL;
27 use Friendica\BaseFactory;
28 use Friendica\Content\Text\BBCode;
29 use Friendica\Core\L10n;
30 use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
31 use Friendica\Core\Protocol;
32 use Friendica\Core\Session\Capability\IHandleSessions;
33 use Friendica\Database\Database;
34 use Friendica\Model\Contact;
35 use Friendica\Module\BaseNotifications;
36 use Friendica\Navigation\Notifications\ValueObject;
37 use Friendica\Util\Proxy;
38 use Psr\Log\LoggerInterface;
41 * Factory for creating notification objects based on introductions
42 * Currently, there are two main types of introduction based notifications:
44 * - Friend/Follower request
46 class Introduction extends BaseFactory
54 /** @var IManagePersonalConfigValues */
56 /** @var IHandleSessions */
61 public function __construct(LoggerInterface $logger, Database $dba, BaseURL $baseUrl, L10n $l10n, App $app, IManagePersonalConfigValues $pConfig, IHandleSessions $session)
63 parent::__construct($logger);
66 $this->baseUrl = $baseUrl;
68 $this->pConfig = $pConfig;
69 $this->session = $session;
70 $this->nick = $app->getLoggedInUserNickname() ?? '';
76 * @param bool $all If false only include introductions into the query
77 * which aren't marked as ignored
78 * @param int $start Start the query at this point
79 * @param int $limit Maximum number of query results
80 * @param int $id When set, only the introduction with this id is displayed
82 * @return ValueObject\Introduction[]
84 public function getList(bool $all = false, int $start = 0, int $limit = BaseNotifications::DEFAULT_PAGE_LIMIT, int $id = 0): array
90 $sql_extra = " AND NOT `ignore` ";
93 $sql_extra .= " AND NOT `intro`.`blocked` ";
95 $sql_extra = sprintf(" AND `intro`.`id` = %d ", $id);
98 $formattedIntroductions = [];
101 $stmtNotifications = $this->dba->p(
102 "SELECT `intro`.`id` AS `intro_id`, `intro`.*, `contact`.*,
103 `sugggest-contact`.`name` AS `fname`, `sugggest-contact`.`url` AS `furl`, `sugggest-contact`.`addr` AS `faddr`,
104 `sugggest-contact`.`photo` AS `fphoto`, `sugggest-contact`.`request` AS `frequest`
106 LEFT JOIN `contact` ON `contact`.`id` = `intro`.`contact-id`
107 LEFT JOIN `contact` AS `sugggest-contact` ON `intro`.`suggest-cid` = `sugggest-contact`.`id`
108 WHERE `intro`.`uid` = ? $sql_extra
115 while ($intro = $this->dba->fetch($stmtNotifications)) {
116 if (empty($intro['url'])) {
120 // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests.
121 // We have to distinguish between these two because they use different data.
122 // Contact suggestions
123 if ($intro['suggest-cid'] ?? '') {
124 if (empty($intro['furl'])) {
127 $return_addr = bin2hex($this->nick . '@' .
128 $this->baseUrl->getHostname() .
129 (($this->baseUrl->getUrlPath()) ? '/' . $this->baseUrl->getUrlPath() : ''));
131 $formattedIntroductions[] = new ValueObject\Introduction([
132 'label' => 'friend_suggestion',
133 'str_type' => $this->l10n->t('Friend Suggestion'),
134 'intro_id' => $intro['intro_id'],
135 'madeby' => $intro['name'],
136 'madeby_url' => $intro['url'],
137 'madeby_zrl' => Contact::magicLink($intro['url']),
138 'madeby_addr' => $intro['addr'],
139 'contact_id' => $intro['contact-id'],
140 'photo' => Contact::getAvatarUrlForUrl($intro['furl'], 0, Proxy::SIZE_SMALL),
141 'name' => $intro['fname'],
142 'url' => $intro['furl'],
143 'zrl' => Contact::magicLink($intro['furl']),
144 'hidden' => $intro['hidden'] == 1,
145 'post_newfriend' => (intval($this->pConfig->get(local_user(), 'system', 'post_newfriend')) ? '1' : 0),
146 'note' => $intro['note'],
147 'request' => $intro['frequest'] . '?addr=' . $return_addr]);
149 // Normal connection requests
151 // Don't show these data until you are connected. Diaspora is doing the same.
152 if ($intro['network'] === Protocol::DIASPORA) {
153 $intro['location'] = "";
154 $intro['about'] = "";
157 $formattedIntroductions[] = new ValueObject\Introduction([
158 'label' => (($intro['network'] !== Protocol::OSTATUS) ? 'friend_request' : 'follower'),
159 'str_type' => (($intro['network'] !== Protocol::OSTATUS) ? $this->l10n->t('Friend/Connect Request') : $this->l10n->t('New Follower')),
160 'dfrn_id' => $intro['issued-id'],
161 'uid' => $this->session->get('uid'),
162 'intro_id' => $intro['intro_id'],
163 'contact_id' => $intro['contact-id'],
164 'photo' => Contact::getPhoto($intro),
165 'name' => $intro['name'],
166 'location' => BBCode::convert($intro['location'], false),
167 'about' => BBCode::convert($intro['about'], false),
168 'keywords' => $intro['keywords'],
169 'hidden' => $intro['hidden'] == 1,
170 'post_newfriend' => (intval($this->pConfig->get(local_user(), 'system', 'post_newfriend')) ? '1' : 0),
171 'url' => $intro['url'],
172 'zrl' => Contact::magicLink($intro['url']),
173 'addr' => $intro['addr'],
174 'network' => $intro['network'],
175 'knowyou' => $intro['knowyou'],
176 'note' => $intro['note'],
180 } catch (Exception $e) {
181 $this->logger->warning('Select failed.', ['uid' => $_SESSION['uid'], 'exception' => $e]);
184 return $formattedIntroductions;