]> git.mxchange.org Git - friendica.git/blob - src/Factory/Notification/IntroductionFactory.php
Fix API result, add PHPDoc and cleanup object construction
[friendica.git] / src / Factory / Notification / IntroductionFactory.php
1 <?php
2
3 namespace Friendica\Factory\Notification;
4
5 use Exception;
6 use Friendica\App;
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;
21
22 /**
23  * Factory for creating notification objects based on introductions
24  * Currently, there are two main types of introduction based notifications:
25  * - Friend suggestion
26  * - Friend/Follower request
27  */
28 class IntroductionFactory extends BaseFactory
29 {
30         /** @var Database */
31         private $dba;
32         /** @var BaseURL */
33         private $baseUrl;
34         /** @var L10n */
35         private $l10n;
36         /** @var IPConfig */
37         private $pConfig;
38         /** @var ISession */
39         private $session;
40         /** @var string */
41         private $nick;
42
43         public function __construct(LoggerInterface $logger, Database $dba, BaseURL $baseUrl, L10n $l10n, App $app, IPConfig $pConfig, ISession $session)
44         {
45                 parent::__construct($logger);
46
47                 $this->dba          = $dba;
48                 $this->baseUrl      = $baseUrl;
49                 $this->l10n         = $l10n;
50                 $this->pConfig      = $pConfig;
51                 $this->session      = $session;
52                 $this->nick         = $app->user['nickname'] ?? '';
53         }
54
55         /**
56          * Get introductions
57          *
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
63          *
64          * @return Introduction[]
65          */
66         public function getIntroList(bool $all = false, int $start = 0, int $limit = BaseNotifications::DEFAULT_PAGE_LIMIT, int $id = 0)
67         {
68                 $sql_extra     = "";
69
70                 if (empty($id)) {
71                         if (!$all) {
72                                 $sql_extra = " AND NOT `ignore` ";
73                         }
74
75                         $sql_extra .= " AND NOT `intro`.`blocked` ";
76                 } else {
77                         $sql_extra = sprintf(" AND `intro`.`id` = %d ", intval($id));
78                 }
79
80                 $formattedNotifications = [];
81
82                 try {
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`
91                         FROM `intro`
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
96                         LIMIT ?, ?",
97                                 $_SESSION['uid'],
98                                 $start,
99                                 $limit
100                         );
101
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() : ''));
110
111                                         $formattedNotifications[] = new 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]);
128
129                                         // Normal connection requests
130                                 } else {
131                                         $notification = $this->getMissingIntroData($notification);
132
133                                         if (empty($notification['url'])) {
134                                                 continue;
135                                         }
136
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']   = "";
142                                         }
143
144                                         $formattedNotifications[] = new 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'],
165                                         ]);
166                                 }
167                         }
168                 } catch (Exception $e) {
169                         $this->logger->warning('Select failed.', ['uid' => $_SESSION['uid'], 'exception' => $e]);
170                 }
171
172                 return $formattedNotifications;
173         }
174
175         /**
176          * Check for missing contact data and try to fetch the data from
177          * from other sources
178          *
179          * @param array $intro The input array with the intro data
180          *
181          * @return array The array with the intro data
182          *
183          * @throws InternalServerErrorException
184          */
185         private function getMissingIntroData(array $intro)
186         {
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'];
191                 }
192                 if (empty($intro['gaddr']) && !empty($intro['addr'])) {
193                         $intro['gaddr'] = $intro['addr'];
194                 }
195
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']);
200
201                         if (empty($intro['gnetwork']) && !empty($ret['network'])) {
202                                 $intro['gnetwork'] = $ret['network'];
203                         }
204                         if (empty($intro['gaddr']) && !empty($ret['addr'])) {
205                                 $intro['gaddr'] = $ret['addr'];
206                         }
207                 }
208
209                 return $intro;
210         }
211 }