3 namespace Friendica\Factory\Notification;
7 use Friendica\App\BaseURL;
8 use Friendica\BaseFactory;
9 use Friendica\Content\Text\BBCode;
10 use Friendica\Core\L10n;
11 use Friendica\Core\PConfig\IPConfig;
12 use Friendica\Core\Protocol;
13 use Friendica\Core\Session\ISession;
14 use Friendica\Database\Database;
15 use Friendica\Model\Contact;
16 use Friendica\Module\BaseNotifications;
17 use Friendica\Network\HTTPException\InternalServerErrorException;
18 use Friendica\Object\Notification;
19 use Friendica\Util\Proxy;
20 use Psr\Log\LoggerInterface;
23 * Factory for creating notification objects based on introductions
24 * Currently, there are two main types of introduction based notifications:
26 * - Friend/Follower request
28 class Introduction extends BaseFactory
43 public function __construct(LoggerInterface $logger, Database $dba, BaseURL $baseUrl, L10n $l10n, App $app, IPConfig $pConfig, ISession $session)
45 parent::__construct($logger);
48 $this->baseUrl = $baseUrl;
50 $this->pConfig = $pConfig;
51 $this->session = $session;
52 $this->nick = $app->user['nickname'] ?? '';
58 * @param bool $all If false only include introductions into the query
59 * which aren't marked as ignored
60 * @param int $start Start the query at this point
61 * @param int $limit Maximum number of query results
62 * @param int $id When set, only the introduction with this id is displayed
64 * @return Notification\Introduction[]
66 public function getList(bool $all = false, int $start = 0, int $limit = BaseNotifications::DEFAULT_PAGE_LIMIT, int $id = 0)
72 $sql_extra = " AND NOT `ignore` ";
75 $sql_extra .= " AND NOT `intro`.`blocked` ";
77 $sql_extra = sprintf(" AND `intro`.`id` = %d ", intval($id));
80 $formattedNotifications = [];
83 /// @todo Fetch contact details by "Contact::getDetailsByUrl" instead of queries to contact, fcontact and gcontact
84 $stmtNotifications = $this->dba->p(
85 "SELECT `intro`.`id` AS `intro_id`, `intro`.*, `contact`.*,
86 `fcontact`.`name` AS `fname`, `fcontact`.`url` AS `furl`, `fcontact`.`addr` AS `faddr`,
87 `fcontact`.`photo` AS `fphoto`, `fcontact`.`request` AS `frequest`,
88 `gcontact`.`location` AS `glocation`, `gcontact`.`about` AS `gabout`,
89 `gcontact`.`keywords` AS `gkeywords`, `gcontact`.`gender` AS `ggender`,
90 `gcontact`.`network` AS `gnetwork`, `gcontact`.`addr` AS `gaddr`
92 LEFT JOIN `contact` ON `contact`.`id` = `intro`.`contact-id`
93 LEFT JOIN `gcontact` ON `gcontact`.`nurl` = `contact`.`nurl`
94 LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
95 WHERE `intro`.`uid` = ? $sql_extra
102 while ($notification = $this->dba->fetch($stmtNotifications)) {
103 // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests.
104 // We have to distinguish between these two because they use different data.
105 // Contact suggestions
106 if ($notification['fid'] ?? '') {
107 $return_addr = bin2hex($this->nick . '@' .
108 $this->baseUrl->getHostName() .
109 (($this->baseUrl->getURLPath()) ? '/' . $this->baseUrl->getURLPath() : ''));
111 $formattedNotifications[] = new Notification\Introduction([
112 'label' => 'friend_suggestion',
113 'str_type' => $this->l10n->t('Friend Suggestion'),
114 'intro_id' => $notification['intro_id'],
115 'madeby' => $notification['name'],
116 'madeby_url' => $notification['url'],
117 'madeby_zrl' => Contact::magicLink($notification['url']),
118 'madeby_addr' => $notification['addr'],
119 'contact_id' => $notification['contact-id'],
120 'photo' => (!empty($notification['fphoto']) ? Proxy::proxifyUrl($notification['fphoto'], false, Proxy::SIZE_SMALL) : "images/person-300.jpg"),
121 'name' => $notification['fname'],
122 'url' => $notification['furl'],
123 'zrl' => Contact::magicLink($notification['furl']),
124 'hidden' => $notification['hidden'] == 1,
125 'post_newfriend' => (intval($this->pConfig->get(local_user(), 'system', 'post_newfriend')) ? '1' : 0),
126 'note' => $notification['note'],
127 'request' => $notification['frequest'] . '?addr=' . $return_addr]);
129 // Normal connection requests
131 $notification = $this->getMissingData($notification);
133 if (empty($notification['url'])) {
137 // Don't show these data until you are connected. Diaspora is doing the same.
138 if ($notification['gnetwork'] === Protocol::DIASPORA) {
139 $notification['glocation'] = "";
140 $notification['gabout'] = "";
141 $notification['ggender'] = "";
144 $formattedNotifications[] = new Notification\Introduction([
145 'label' => (($notification['network'] !== Protocol::OSTATUS) ? 'friend_request' : 'follower'),
146 'str_type' => (($notification['network'] !== Protocol::OSTATUS) ? $this->l10n->t('Friend/Connect Request') : $this->l10n->t('New Follower')),
147 'dfrn_id' => $notification['issued-id'],
148 'uid' => $this->session->get('uid'),
149 'intro_id' => $notification['intro_id'],
150 'contact_id' => $notification['contact-id'],
151 'photo' => (!empty($notification['photo']) ? Proxy::proxifyUrl($notification['photo'], false, Proxy::SIZE_SMALL) : "images/person-300.jpg"),
152 'name' => $notification['name'],
153 'location' => BBCode::convert($notification['glocation'], false),
154 'about' => BBCode::convert($notification['gabout'], false),
155 'keywords' => $notification['gkeywords'],
156 'gender' => $notification['ggender'],
157 'hidden' => $notification['hidden'] == 1,
158 'post_newfriend' => (intval($this->pConfig->get(local_user(), 'system', 'post_newfriend')) ? '1' : 0),
159 'url' => $notification['url'],
160 'zrl' => Contact::magicLink($notification['url']),
161 'addr' => $notification['gaddr'],
162 'network' => $notification['gnetwork'],
163 'knowyou' => $notification['knowyou'],
164 'note' => $notification['note'],
168 } catch (Exception $e) {
169 $this->logger->warning('Select failed.', ['uid' => $_SESSION['uid'], 'exception' => $e]);
172 return $formattedNotifications;
176 * Check for missing contact data and try to fetch the data from
179 * @param array $intro The input array with the intro data
181 * @return array The array with the intro data
183 * @throws InternalServerErrorException
185 private function getMissingData(array $intro)
187 // If the network and the addr isn't available from the gcontact
188 // table entry, take the one of the contact table entry
189 if (empty($intro['gnetwork']) && !empty($intro['network'])) {
190 $intro['gnetwork'] = $intro['network'];
192 if (empty($intro['gaddr']) && !empty($intro['addr'])) {
193 $intro['gaddr'] = $intro['addr'];
196 // If the network and addr is still not available
197 // get the missing data data from other sources
198 if (empty($intro['gnetwork']) || empty($intro['gaddr'])) {
199 $ret = Contact::getDetailsByURL($intro['url']);
201 if (empty($intro['gnetwork']) && !empty($ret['network'])) {
202 $intro['gnetwork'] = $ret['network'];
204 if (empty($intro['gaddr']) && !empty($ret['addr'])) {
205 $intro['gaddr'] = $ret['addr'];