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\Introduction;
19 use Friendica\Util\Proxy;
20 use Psr\Log\LoggerInterface;
22 class IntroductionFactory extends BaseFactory
37 public function __construct(LoggerInterface $logger, Database $dba, BaseURL $baseUrl, L10n $l10n, App $app, IPConfig $pConfig, ISession $session)
39 parent::__construct($logger);
42 $this->baseUrl = $baseUrl;
44 $this->pConfig = $pConfig;
45 $this->session = $session;
46 $this->nick = $app->user['nickname'] ?? '';
52 * @param bool $all If false only include introductions into the query
53 * which aren't marked as ignored
54 * @param int $start Start the query at this point
55 * @param int $limit Maximum number of query results
56 * @param int $id When set, only the introduction with this id is displayed
58 * @return Introduction[]
60 public function getIntroList(bool $all = false, int $start = 0, int $limit = BaseNotifications::DEFAULT_PAGE_LIMIT, int $id = 0)
66 $sql_extra = " AND NOT `ignore` ";
69 $sql_extra .= " AND NOT `intro`.`blocked` ";
71 $sql_extra = sprintf(" AND `intro`.`id` = %d ", intval($id));
74 $formattedNotifications = [];
77 /// @todo Fetch contact details by "Contact::getDetailsByUrl" instead of queries to contact, fcontact and gcontact
78 $stmtNotifications = $this->dba->p(
79 "SELECT `intro`.`id` AS `intro_id`, `intro`.*, `contact`.*,
80 `fcontact`.`name` AS `fname`, `fcontact`.`url` AS `furl`, `fcontact`.`addr` AS `faddr`,
81 `fcontact`.`photo` AS `fphoto`, `fcontact`.`request` AS `frequest`,
82 `gcontact`.`location` AS `glocation`, `gcontact`.`about` AS `gabout`,
83 `gcontact`.`keywords` AS `gkeywords`, `gcontact`.`gender` AS `ggender`,
84 `gcontact`.`network` AS `gnetwork`, `gcontact`.`addr` AS `gaddr`
86 LEFT JOIN `contact` ON `contact`.`id` = `intro`.`contact-id`
87 LEFT JOIN `gcontact` ON `gcontact`.`nurl` = `contact`.`nurl`
88 LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
89 WHERE `intro`.`uid` = ? $sql_extra
96 while ($notification = $this->dba->fetch($stmtNotifications)) {
97 // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests.
98 // We have to distinguish between these two because they use different data.
99 // Contact suggestions
100 if ($notification['fid'] ?? '') {
101 $return_addr = bin2hex($this->nick . '@' .
102 $this->baseUrl->getHostName() .
103 (($this->baseUrl->getURLPath()) ? '/' . $this->baseUrl->getURLPath() : ''));
105 $formattedNotifications[] = new Introduction([
106 'label' => 'friend_suggestion',
107 'str_type' => $this->l10n->t('Friend Suggestion'),
108 'intro_id' => $notification['intro_id'],
109 'madeby' => $notification['name'],
110 'madeby_url' => $notification['url'],
111 'madeby_zrl' => Contact::magicLink($notification['url']),
112 'madeby_addr' => $notification['addr'],
113 'contact_id' => $notification['contact-id'],
114 'photo' => (!empty($notification['fphoto']) ? Proxy::proxifyUrl($notification['fphoto'], false, Proxy::SIZE_SMALL) : "images/person-300.jpg"),
115 'name' => $notification['fname'],
116 'url' => $notification['furl'],
117 'zrl' => Contact::magicLink($notification['furl']),
118 'hidden' => $notification['hidden'] == 1,
119 'post_newfriend' => (intval($this->pConfig->get(local_user(), 'system', 'post_newfriend')) ? '1' : 0),
120 'note' => $notification['note'],
121 'request' => $notification['frequest'] . '?addr=' . $return_addr]);
123 // Normal connection requests
125 $notification = $this->getMissingIntroData($notification);
127 if (empty($notification['url'])) {
131 // Don't show these data until you are connected. Diaspora is doing the same.
132 if ($notification['gnetwork'] === Protocol::DIASPORA) {
133 $notification['glocation'] = "";
134 $notification['gabout'] = "";
135 $notification['ggender'] = "";
138 $formattedNotifications[] = new Introduction([
139 'label' => (($notification['network'] !== Protocol::OSTATUS) ? 'friend_request' : 'follower'),
140 'str_type' => (($notification['network'] !== Protocol::OSTATUS) ? $this->l10n->t('Friend/Connect Request') : $this->l10n->t('New Follower')),
141 'dfrn_id' => $notification['issued-id'],
142 'uid' => $this->session->get('uid'),
143 'intro_id' => $notification['intro_id'],
144 'contact_id' => $notification['contact-id'],
145 'photo' => (!empty($notification['photo']) ? Proxy::proxifyUrl($notification['photo'], false, Proxy::SIZE_SMALL) : "images/person-300.jpg"),
146 'name' => $notification['name'],
147 'location' => BBCode::convert($notification['glocation'], false),
148 'about' => BBCode::convert($notification['gabout'], false),
149 'keywords' => $notification['gkeywords'],
150 'gender' => $notification['ggender'],
151 'hidden' => $notification['hidden'] == 1,
152 'post_newfriend' => (intval($this->pConfig->get(local_user(), 'system', 'post_newfriend')) ? '1' : 0),
153 'url' => $notification['url'],
154 'zrl' => Contact::magicLink($notification['url']),
155 'addr' => $notification['gaddr'],
156 'network' => $notification['gnetwork'],
157 'knowyou' => $notification['knowyou'],
158 'note' => $notification['note'],
162 } catch (Exception $e) {
163 $this->logger->warning('Select failed.', ['uid' => $_SESSION['uid'], 'exception' => $e]);
166 return $formattedNotifications;
170 * Check for missing contact data and try to fetch the data from
173 * @param array $intro The input array with the intro data
175 * @return array The array with the intro data
177 * @throws InternalServerErrorException
179 private function getMissingIntroData(array $intro)
181 // If the network and the addr isn't available from the gcontact
182 // table entry, take the one of the contact table entry
183 if (empty($intro['gnetwork']) && !empty($intro['network'])) {
184 $intro['gnetwork'] = $intro['network'];
186 if (empty($intro['gaddr']) && !empty($intro['addr'])) {
187 $intro['gaddr'] = $intro['addr'];
190 // If the network and addr is still not available
191 // get the missing data data from other sources
192 if (empty($intro['gnetwork']) || empty($intro['gaddr'])) {
193 $ret = Contact::getDetailsByURL($intro['url']);
195 if (empty($intro['gnetwork']) && !empty($ret['network'])) {
196 $intro['gnetwork'] = $ret['network'];
198 if (empty($intro['gaddr']) && !empty($ret['addr'])) {
199 $intro['gaddr'] = $ret['addr'];