3 * @copyright Copyright (C) 2020, Friendica
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::getDetailsByUrl" instead of queries to contact, fcontact and gcontact
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`,
107 `gcontact`.`location` AS `glocation`, `gcontact`.`about` AS `gabout`,
108 `gcontact`.`keywords` AS `gkeywords`, `gcontact`.`gender` AS `ggender`,
109 `gcontact`.`network` AS `gnetwork`, `gcontact`.`addr` AS `gaddr`
111 LEFT JOIN `contact` ON `contact`.`id` = `intro`.`contact-id`
112 LEFT JOIN `gcontact` ON `gcontact`.`nurl` = `contact`.`nurl`
113 LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
114 WHERE `intro`.`uid` = ? $sql_extra
121 while ($notification = $this->dba->fetch($stmtNotifications)) {
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 $return_addr = bin2hex($this->nick . '@' .
127 $this->baseUrl->getHostName() .
128 (($this->baseUrl->getURLPath()) ? '/' . $this->baseUrl->getURLPath() : ''));
130 $formattedNotifications[] = new Notification\Introduction([
131 'label' => 'friend_suggestion',
132 'str_type' => $this->l10n->t('Friend Suggestion'),
133 'intro_id' => $notification['intro_id'],
134 'madeby' => $notification['name'],
135 'madeby_url' => $notification['url'],
136 'madeby_zrl' => Contact::magicLink($notification['url']),
137 'madeby_addr' => $notification['addr'],
138 'contact_id' => $notification['contact-id'],
139 'photo' => (!empty($notification['fphoto']) ? Proxy::proxifyUrl($notification['fphoto'], false, Proxy::SIZE_SMALL) : "images/person-300.jpg"),
140 'name' => $notification['fname'],
141 'url' => $notification['furl'],
142 'zrl' => Contact::magicLink($notification['furl']),
143 'hidden' => $notification['hidden'] == 1,
144 'post_newfriend' => (intval($this->pConfig->get(local_user(), 'system', 'post_newfriend')) ? '1' : 0),
145 'note' => $notification['note'],
146 'request' => $notification['frequest'] . '?addr=' . $return_addr]);
148 // Normal connection requests
150 $notification = $this->getMissingData($notification);
152 if (empty($notification['url'])) {
156 // Don't show these data until you are connected. Diaspora is doing the same.
157 if ($notification['gnetwork'] === Protocol::DIASPORA) {
158 $notification['glocation'] = "";
159 $notification['gabout'] = "";
160 $notification['ggender'] = "";
163 $formattedNotifications[] = new Notification\Introduction([
164 'label' => (($notification['network'] !== Protocol::OSTATUS) ? 'friend_request' : 'follower'),
165 'str_type' => (($notification['network'] !== Protocol::OSTATUS) ? $this->l10n->t('Friend/Connect Request') : $this->l10n->t('New Follower')),
166 'dfrn_id' => $notification['issued-id'],
167 'uid' => $this->session->get('uid'),
168 'intro_id' => $notification['intro_id'],
169 'contact_id' => $notification['contact-id'],
170 'photo' => (!empty($notification['photo']) ? Proxy::proxifyUrl($notification['photo'], false, Proxy::SIZE_SMALL) : "images/person-300.jpg"),
171 'name' => $notification['name'],
172 'location' => BBCode::convert($notification['glocation'], false),
173 'about' => BBCode::convert($notification['gabout'], false),
174 'keywords' => $notification['gkeywords'],
175 'gender' => $notification['ggender'],
176 'hidden' => $notification['hidden'] == 1,
177 'post_newfriend' => (intval($this->pConfig->get(local_user(), 'system', 'post_newfriend')) ? '1' : 0),
178 'url' => $notification['url'],
179 'zrl' => Contact::magicLink($notification['url']),
180 'addr' => $notification['gaddr'],
181 'network' => $notification['gnetwork'],
182 'knowyou' => $notification['knowyou'],
183 'note' => $notification['note'],
187 } catch (Exception $e) {
188 $this->logger->warning('Select failed.', ['uid' => $_SESSION['uid'], 'exception' => $e]);
191 return $formattedNotifications;
195 * Check for missing contact data and try to fetch the data from
198 * @param array $intro The input array with the intro data
200 * @return array The array with the intro data
202 * @throws InternalServerErrorException
204 private function getMissingData(array $intro)
206 // If the network and the addr isn't available from the gcontact
207 // table entry, take the one of the contact table entry
208 if (empty($intro['gnetwork']) && !empty($intro['network'])) {
209 $intro['gnetwork'] = $intro['network'];
211 if (empty($intro['gaddr']) && !empty($intro['addr'])) {
212 $intro['gaddr'] = $intro['addr'];
215 // If the network and addr is still not available
216 // get the missing data data from other sources
217 if (empty($intro['gnetwork']) || empty($intro['gaddr'])) {
218 $ret = Contact::getDetailsByURL($intro['url']);
220 if (empty($intro['gnetwork']) && !empty($ret['network'])) {
221 $intro['gnetwork'] = $ret['network'];
223 if (empty($intro['gaddr']) && !empty($ret['addr'])) {
224 $intro['gaddr'] = $ret['addr'];