3 * @copyright Copyright (C) 2010-2021, 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\IPConfig;
31 use Friendica\Core\Protocol;
32 use Friendica\Core\Session\ISession;
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
61 public function __construct(LoggerInterface $logger, Database $dba, BaseURL $baseUrl, L10n $l10n, App $app, IPConfig $pConfig, ISession $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 /// @todo Fetch contact details by "Contact::getByUrl" instead of queries to contact and fcontact
102 $stmtNotifications = $this->dba->p(
103 "SELECT `intro`.`id` AS `intro_id`, `intro`.*, `contact`.*,
104 `fcontact`.`name` AS `fname`, `fcontact`.`url` AS `furl`, `fcontact`.`addr` AS `faddr`,
105 `fcontact`.`photo` AS `fphoto`, `fcontact`.`request` AS `frequest`
107 LEFT JOIN `contact` ON `contact`.`id` = `intro`.`contact-id`
108 LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
109 WHERE `intro`.`uid` = ? $sql_extra
116 while ($intro = $this->dba->fetch($stmtNotifications)) {
117 if (empty($intro['url'])) {
121 // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests.
122 // We have to distinguish between these two because they use different data.
123 // Contact suggestions
124 if ($intro['fid'] ?? '') {
125 if (empty($intro['furl'])) {
128 $return_addr = bin2hex($this->nick . '@' .
129 $this->baseUrl->getHostname() .
130 (($this->baseUrl->getUrlPath()) ? '/' . $this->baseUrl->getUrlPath() : ''));
132 $formattedIntroductions[] = new ValueObject\Introduction([
133 'label' => 'friend_suggestion',
134 'str_type' => $this->l10n->t('Friend Suggestion'),
135 'intro_id' => $intro['intro_id'],
136 'madeby' => $intro['name'],
137 'madeby_url' => $intro['url'],
138 'madeby_zrl' => Contact::magicLink($intro['url']),
139 'madeby_addr' => $intro['addr'],
140 'contact_id' => $intro['contact-id'],
141 'photo' => Contact::getAvatarUrlForUrl($intro['furl'], 0, Proxy::SIZE_SMALL),
142 'name' => $intro['fname'],
143 'url' => $intro['furl'],
144 'zrl' => Contact::magicLink($intro['furl']),
145 'hidden' => $intro['hidden'] == 1,
146 'post_newfriend' => (intval($this->pConfig->get(local_user(), 'system', 'post_newfriend')) ? '1' : 0),
147 'note' => $intro['note'],
148 'request' => $intro['frequest'] . '?addr=' . $return_addr]);
150 // Normal connection requests
152 // Don't show these data until you are connected. Diaspora is doing the same.
153 if ($intro['network'] === Protocol::DIASPORA) {
154 $intro['location'] = "";
155 $intro['about'] = "";
158 $formattedIntroductions[] = new ValueObject\Introduction([
159 'label' => (($intro['network'] !== Protocol::OSTATUS) ? 'friend_request' : 'follower'),
160 'str_type' => (($intro['network'] !== Protocol::OSTATUS) ? $this->l10n->t('Friend/Connect Request') : $this->l10n->t('New Follower')),
161 'dfrn_id' => $intro['issued-id'],
162 'uid' => $this->session->get('uid'),
163 'intro_id' => $intro['intro_id'],
164 'contact_id' => $intro['contact-id'],
165 'photo' => Contact::getPhoto($intro),
166 'name' => $intro['name'],
167 'location' => BBCode::convert($intro['location'], false),
168 'about' => BBCode::convert($intro['about'], false),
169 'keywords' => $intro['keywords'],
170 'hidden' => $intro['hidden'] == 1,
171 'post_newfriend' => (intval($this->pConfig->get(local_user(), 'system', 'post_newfriend')) ? '1' : 0),
172 'url' => $intro['url'],
173 'zrl' => Contact::magicLink($intro['url']),
174 'addr' => $intro['addr'],
175 'network' => $intro['network'],
176 'knowyou' => $intro['knowyou'],
177 'note' => $intro['note'],
181 } catch (Exception $e) {
182 $this->logger->warning('Select failed.', ['uid' => $_SESSION['uid'], 'exception' => $e]);
185 return $formattedIntroductions;