]> git.mxchange.org Git - friendica.git/blob - src/Factory/Notification/IntroductionFactory.php
ReWork Notification Model/Module/Object/Repository/Factory
[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 class IntroductionFactory extends BaseFactory
23 {
24         /** @var Database */
25         private $dba;
26         /** @var BaseURL */
27         private $baseUrl;
28         /** @var L10n */
29         private $l10n;
30         /** @var IPConfig */
31         private $pConfig;
32         /** @var ISession */
33         private $session;
34         /** @var string */
35         private $nick;
36
37         public function __construct(LoggerInterface $logger, Database $dba, BaseURL $baseUrl, L10n $l10n, App $app, IPConfig $pConfig, ISession $session)
38         {
39                 parent::__construct($logger);
40
41                 $this->dba          = $dba;
42                 $this->baseUrl      = $baseUrl;
43                 $this->l10n         = $l10n;
44                 $this->pConfig      = $pConfig;
45                 $this->session      = $session;
46                 $this->nick         = $app->user['nickname'] ?? '';
47         }
48
49         /**
50          * Get introductions
51          *
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
57          *
58          * @return Introduction[]
59          */
60         public function getIntroList(bool $all = false, int $start = 0, int $limit = BaseNotifications::DEFAULT_PAGE_LIMIT, int $id = 0)
61         {
62                 $sql_extra     = "";
63
64                 if (empty($id)) {
65                         if (!$all) {
66                                 $sql_extra = " AND NOT `ignore` ";
67                         }
68
69                         $sql_extra .= " AND NOT `intro`.`blocked` ";
70                 } else {
71                         $sql_extra = sprintf(" AND `intro`.`id` = %d ", intval($id));
72                 }
73
74                 $formattedNotifications = [];
75
76                 try {
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`
85                         FROM `intro`
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
90                         LIMIT ?, ?",
91                                 $_SESSION['uid'],
92                                 $start,
93                                 $limit
94                         );
95
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() : ''));
104
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]);
122
123                                         // Normal connection requests
124                                 } else {
125                                         $notification = $this->getMissingIntroData($notification);
126
127                                         if (empty($notification['url'])) {
128                                                 continue;
129                                         }
130
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']   = "";
136                                         }
137
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'],
159                                         ]);
160                                 }
161                         }
162                 } catch (Exception $e) {
163                         $this->logger->warning('Select failed.', ['uid' => $_SESSION['uid'], 'exception' => $e]);
164                 }
165
166                 return $formattedNotifications;
167         }
168
169         /**
170          * Check for missing contact data and try to fetch the data from
171          * from other sources
172          *
173          * @param array $intro The input array with the intro data
174          *
175          * @return array The array with the intro data
176          *
177          * @throws InternalServerErrorException
178          */
179         private function getMissingIntroData(array $intro)
180         {
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'];
185                 }
186                 if (empty($intro['gaddr']) && !empty($intro['addr'])) {
187                         $intro['gaddr'] = $intro['addr'];
188                 }
189
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']);
194
195                         if (empty($intro['gnetwork']) && !empty($ret['network'])) {
196                                 $intro['gnetwork'] = $ret['network'];
197                         }
198                         if (empty($intro['gaddr']) && !empty($ret['addr'])) {
199                                 $intro['gaddr'] = $ret['addr'];
200                         }
201                 }
202
203                 return $intro;
204         }
205 }