]> git.mxchange.org Git - friendica.git/blob - src/Factory/Notification/Introduction.php
Fix notices
[friendica.git] / src / Factory / Notification / Introduction.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Factory\Notification;
23
24 use Exception;
25 use Friendica\App;
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;
40
41 /**
42  * Factory for creating notification objects based on introductions
43  * Currently, there are two main types of introduction based notifications:
44  * - Friend suggestion
45  * - Friend/Follower request
46  */
47 class Introduction extends BaseFactory
48 {
49         /** @var Database */
50         private $dba;
51         /** @var BaseURL */
52         private $baseUrl;
53         /** @var L10n */
54         private $l10n;
55         /** @var IPConfig */
56         private $pConfig;
57         /** @var ISession */
58         private $session;
59         /** @var string */
60         private $nick;
61
62         public function __construct(LoggerInterface $logger, Database $dba, BaseURL $baseUrl, L10n $l10n, App $app, IPConfig $pConfig, ISession $session)
63         {
64                 parent::__construct($logger);
65
66                 $this->dba          = $dba;
67                 $this->baseUrl      = $baseUrl;
68                 $this->l10n         = $l10n;
69                 $this->pConfig      = $pConfig;
70                 $this->session      = $session;
71                 $this->nick         = $app->user['nickname'] ?? '';
72         }
73
74         /**
75          * Get introductions
76          *
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
82          *
83          * @return Notification\Introduction[]
84          */
85         public function getList(bool $all = false, int $start = 0, int $limit = BaseNotifications::DEFAULT_PAGE_LIMIT, int $id = 0)
86         {
87                 $sql_extra     = "";
88
89                 if (empty($id)) {
90                         if (!$all) {
91                                 $sql_extra = " AND NOT `ignore` ";
92                         }
93
94                         $sql_extra .= " AND NOT `intro`.`blocked` ";
95                 } else {
96                         $sql_extra = sprintf(" AND `intro`.`id` = %d ", intval($id));
97                 }
98
99                 $formattedNotifications = [];
100
101                 try {
102                         /// @todo Fetch contact details by "Contact::getByUrl" instead of queries to contact and fcontact
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                         FROM `intro`
108                                 LEFT JOIN `contact` ON `contact`.`id` = `intro`.`contact-id`
109                                 LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
110                         WHERE `intro`.`uid` = ? $sql_extra
111                         LIMIT ?, ?",
112                                 $_SESSION['uid'],
113                                 $start,
114                                 $limit
115                         );
116
117                         while ($notification = $this->dba->fetch($stmtNotifications)) {
118                                 if (empty($notification['url'])) {
119                                         continue;
120                                 }
121
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                                         if (empty($notification['furl'])) {
127                                                 continue;
128                                         }
129                                         $return_addr = bin2hex($this->nick . '@' .
130                                                                $this->baseUrl->getHostName() .
131                                                                (($this->baseUrl->getURLPath()) ? '/' . $this->baseUrl->getURLPath() : ''));
132
133                                         $formattedNotifications[] = new Notification\Introduction([
134                                                 'label'          => 'friend_suggestion',
135                                                 'str_type'       => $this->l10n->t('Friend Suggestion'),
136                                                 'intro_id'       => $notification['intro_id'],
137                                                 'madeby'         => $notification['name'],
138                                                 'madeby_url'     => $notification['url'],
139                                                 'madeby_zrl'     => Contact::magicLink($notification['url']),
140                                                 'madeby_addr'    => $notification['addr'],
141                                                 'contact_id'     => $notification['contact-id'],
142                                                 'photo'          => (!empty($notification['fphoto']) ? Proxy::proxifyUrl($notification['fphoto'], false, Proxy::SIZE_SMALL) : Contact::DEFAULT_AVATAR_PHOTO),
143                                                 'name'           => $notification['fname'],
144                                                 'url'            => $notification['furl'],
145                                                 'zrl'            => Contact::magicLink($notification['furl']),
146                                                 'hidden'         => $notification['hidden'] == 1,
147                                                 'post_newfriend' => (intval($this->pConfig->get(local_user(), 'system', 'post_newfriend')) ? '1' : 0),
148                                                 'note'           => $notification['note'],
149                                                 'request'        => $notification['frequest'] . '?addr=' . $return_addr]);
150
151                                         // Normal connection requests
152                                 } else {
153                                         // Don't show these data until you are connected. Diaspora is doing the same.
154                                         if ($notification['network'] === Protocol::DIASPORA) {
155                                                 $notification['location'] = "";
156                                                 $notification['about']    = "";
157                                         }
158
159                                         $formattedNotifications[] = new Notification\Introduction([
160                                                 'label'          => (($notification['network'] !== Protocol::OSTATUS) ? 'friend_request' : 'follower'),
161                                                 'str_type'       => (($notification['network'] !== Protocol::OSTATUS) ? $this->l10n->t('Friend/Connect Request') : $this->l10n->t('New Follower')),
162                                                 'dfrn_id'        => $notification['issued-id'],
163                                                 'uid'            => $this->session->get('uid'),
164                                                 'intro_id'       => $notification['intro_id'],
165                                                 'contact_id'     => $notification['contact-id'],
166                                                 'photo'          => Contact::getPhoto($notification),
167                                                 'name'           => $notification['name'],
168                                                 'location'       => BBCode::convert($notification['location'], false),
169                                                 'about'          => BBCode::convert($notification['about'], false),
170                                                 'keywords'       => $notification['keywords'],
171                                                 'hidden'         => $notification['hidden'] == 1,
172                                                 'post_newfriend' => (intval($this->pConfig->get(local_user(), 'system', 'post_newfriend')) ? '1' : 0),
173                                                 'url'            => $notification['url'],
174                                                 'zrl'            => Contact::magicLink($notification['url']),
175                                                 'addr'           => $notification['addr'],
176                                                 'network'        => $notification['network'],
177                                                 'knowyou'        => $notification['knowyou'],
178                                                 'note'           => $notification['note'],
179                                         ]);
180                                 }
181                         }
182                 } catch (Exception $e) {
183                         $this->logger->warning('Select failed.', ['uid' => $_SESSION['uid'], 'exception' => $e]);
184                 }
185
186                 return $formattedNotifications;
187         }
188 }