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\Factory\Notification;
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\Network\HTTPException\InternalServerErrorException;
37 use Friendica\Object\Notification;
38 use Friendica\Util\Proxy;
39 use Psr\Log\LoggerInterface;
42 * Factory for creating notification objects based on introductions
43 * Currently, there are two main types of introduction based notifications:
45 * - Friend/Follower request
47 class Introduction extends BaseFactory
62 public function __construct(LoggerInterface $logger, Database $dba, BaseURL $baseUrl, L10n $l10n, App $app, IPConfig $pConfig, ISession $session)
64 parent::__construct($logger);
67 $this->baseUrl = $baseUrl;
69 $this->pConfig = $pConfig;
70 $this->session = $session;
71 $this->nick = $app->user['nickname'] ?? '';
77 * @param bool $all If false only include introductions into the query
78 * which aren't marked as ignored
79 * @param int $start Start the query at this point
80 * @param int $limit Maximum number of query results
81 * @param int $id When set, only the introduction with this id is displayed
83 * @return Notification\Introduction[]
85 public function getList(bool $all = false, int $start = 0, int $limit = BaseNotifications::DEFAULT_PAGE_LIMIT, int $id = 0)
91 $sql_extra = " AND NOT `ignore` ";
94 $sql_extra .= " AND NOT `intro`.`blocked` ";
96 $sql_extra = sprintf(" AND `intro`.`id` = %d ", intval($id));
99 $formattedNotifications = [];
102 /// @todo Fetch contact details by "Contact::getByUrl" instead of queries to contact and fcontact
103 $stmtNotifications = $this->dba->p(
104 "SELECT `intro`.`id` AS `intro_id`, `intro`.*, `contact`.*,
105 `fcontact`.`name` AS `fname`, `fcontact`.`url` AS `furl`, `fcontact`.`addr` AS `faddr`,
106 `fcontact`.`photo` AS `fphoto`, `fcontact`.`request` AS `frequest`
108 LEFT JOIN `contact` ON `contact`.`id` = `intro`.`contact-id`
109 LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
110 WHERE `intro`.`uid` = ? $sql_extra
117 while ($notification = $this->dba->fetch($stmtNotifications)) {
118 if (empty($notification['url'])) {
122 // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests.
123 // We have to distinguish between these two because they use different data.
124 // Contact suggestions
125 if ($notification['fid'] ?? '') {
126 if (empty($notification['furl'])) {
129 $return_addr = bin2hex($this->nick . '@' .
130 $this->baseUrl->getHostName() .
131 (($this->baseUrl->getURLPath()) ? '/' . $this->baseUrl->getURLPath() : ''));
133 $formattedNotifications[] = new Notification\Introduction([
134 'label' => 'friend_suggestion',
135 'str_type' => $this->l10n->t('Friend Suggestion'),
136 'intro_id' => $notification['intro_id'],
137 'madeby' => $notification['name'],
138 'madeby_url' => $notification['url'],
139 'madeby_zrl' => Contact::magicLink($notification['url']),
140 'madeby_addr' => $notification['addr'],
141 'contact_id' => $notification['contact-id'],
142 'photo' => Contact::getAvatarUrlForUrl($notification['furl'], 0, Proxy::SIZE_SMALL),
143 'name' => $notification['fname'],
144 'url' => $notification['furl'],
145 'zrl' => Contact::magicLink($notification['furl']),
146 'hidden' => $notification['hidden'] == 1,
147 'post_newfriend' => (intval($this->pConfig->get(local_user(), 'system', 'post_newfriend')) ? '1' : 0),
148 'note' => $notification['note'],
149 'request' => $notification['frequest'] . '?addr=' . $return_addr]);
151 // Normal connection requests
153 // Don't show these data until you are connected. Diaspora is doing the same.
154 if ($notification['network'] === Protocol::DIASPORA) {
155 $notification['location'] = "";
156 $notification['about'] = "";
159 $formattedNotifications[] = new Notification\Introduction([
160 'label' => (($notification['network'] !== Protocol::OSTATUS) ? 'friend_request' : 'follower'),
161 'str_type' => (($notification['network'] !== Protocol::OSTATUS) ? $this->l10n->t('Friend/Connect Request') : $this->l10n->t('New Follower')),
162 'dfrn_id' => $notification['issued-id'],
163 'uid' => $this->session->get('uid'),
164 'intro_id' => $notification['intro_id'],
165 'contact_id' => $notification['contact-id'],
166 'photo' => Contact::getPhoto($notification),
167 'name' => $notification['name'],
168 'location' => BBCode::convert($notification['location'], false),
169 'about' => BBCode::convert($notification['about'], false),
170 'keywords' => $notification['keywords'],
171 'hidden' => $notification['hidden'] == 1,
172 'post_newfriend' => (intval($this->pConfig->get(local_user(), 'system', 'post_newfriend')) ? '1' : 0),
173 'url' => $notification['url'],
174 'zrl' => Contact::magicLink($notification['url']),
175 'addr' => $notification['addr'],
176 'network' => $notification['network'],
177 'knowyou' => $notification['knowyou'],
178 'note' => $notification['note'],
182 } catch (Exception $e) {
183 $this->logger->warning('Select failed.', ['uid' => $_SESSION['uid'], 'exception' => $e]);
186 return $formattedNotifications;