]> git.mxchange.org Git - friendica.git/blob - src/Navigation/Notifications/Factory/Introduction.php
Deprecated the notify table/classes
[friendica.git] / src / Navigation / Notifications / Factory / Introduction.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\Navigation\Notifications\Factory;
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\Capability\IManagePersonalConfigValues;
31 use Friendica\Core\Protocol;
32 use Friendica\Core\Session\Capability\IHandleSessions;
33 use Friendica\Database\Database;
34 use Friendica\Model\Contact;
35 use Friendica\Module\BaseNotifications;
36 use Friendica\Navigation\Notifications\ValueObject;
37 use Friendica\Util\Proxy;
38 use Psr\Log\LoggerInterface;
39
40 /**
41  * Factory for creating notification objects based on introductions
42  * Currently, there are two main types of introduction based notifications:
43  * - Friend suggestion
44  * - Friend/Follower request
45  */
46 class Introduction extends BaseFactory
47 {
48         /** @var Database */
49         private $dba;
50         /** @var BaseURL */
51         private $baseUrl;
52         /** @var L10n */
53         private $l10n;
54         /** @var IManagePersonalConfigValues */
55         private $pConfig;
56         /** @var IHandleSessions */
57         private $session;
58         /** @var string */
59         private $nick;
60
61         public function __construct(LoggerInterface $logger, Database $dba, BaseURL $baseUrl, L10n $l10n, App $app, IManagePersonalConfigValues $pConfig, IHandleSessions $session)
62         {
63                 parent::__construct($logger);
64
65                 $this->dba          = $dba;
66                 $this->baseUrl      = $baseUrl;
67                 $this->l10n         = $l10n;
68                 $this->pConfig      = $pConfig;
69                 $this->session      = $session;
70                 $this->nick         = $app->getLoggedInUserNickname() ?? '';
71         }
72
73         /**
74          * Get introductions
75          *
76          * @param bool $all     If false only include introductions into the query
77          *                      which aren't marked as ignored
78          * @param int  $start   Start the query at this point
79          * @param int  $limit   Maximum number of query results
80          * @param int  $id      When set, only the introduction with this id is displayed
81          *
82          * @return ValueObject\Introduction[]
83          */
84         public function getList(bool $all = false, int $start = 0, int $limit = BaseNotifications::DEFAULT_PAGE_LIMIT, int $id = 0): array
85         {
86                 $sql_extra = "";
87
88                 if (empty($id)) {
89                         if (!$all) {
90                                 $sql_extra = " AND NOT `ignore` ";
91                         }
92
93                         $sql_extra .= " AND NOT `intro`.`blocked` ";
94                 } else {
95                         $sql_extra = sprintf(" AND `intro`.`id` = %d ", $id);
96                 }
97
98                 $formattedIntroductions = [];
99
100                 try {
101                         $stmtNotifications = $this->dba->p(
102                                 "SELECT `intro`.`id` AS `intro_id`, `intro`.*, `contact`.*,
103                                 `sugggest-contact`.`name` AS `fname`, `sugggest-contact`.`url` AS `furl`, `sugggest-contact`.`addr` AS `faddr`,
104                                 `sugggest-contact`.`photo` AS `fphoto`, `sugggest-contact`.`request` AS `frequest`
105                         FROM `intro`
106                                 LEFT JOIN `contact` ON `contact`.`id` = `intro`.`contact-id`
107                                 LEFT JOIN `contact` AS `sugggest-contact` ON `intro`.`suggest-cid` = `sugggest-contact`.`id`
108                         WHERE `intro`.`uid` = ? $sql_extra
109                         LIMIT ?, ?",
110                                 $_SESSION['uid'],
111                                 $start,
112                                 $limit
113                         );
114
115                         while ($intro = $this->dba->fetch($stmtNotifications)) {
116                                 if (empty($intro['url'])) {
117                                         continue;
118                                 }
119
120                         // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests.
121                                 // We have to distinguish between these two because they use different data.
122                                 // Contact suggestions
123                                 if ($intro['suggest-cid'] ?? '') {
124                                         if (empty($intro['furl'])) {
125                                                 continue;
126                                         }
127                                         $return_addr = bin2hex($this->nick . '@' .
128                                                                $this->baseUrl->getHostname() .
129                                                                (($this->baseUrl->getUrlPath()) ? '/' . $this->baseUrl->getUrlPath() : ''));
130
131                                         $formattedIntroductions[] = new ValueObject\Introduction([
132                                                 'label'          => 'friend_suggestion',
133                                                 'str_type'       => $this->l10n->t('Friend Suggestion'),
134                                                 'intro_id'       => $intro['intro_id'],
135                                                 'madeby'         => $intro['name'],
136                                                 'madeby_url'     => $intro['url'],
137                                                 'madeby_zrl'     => Contact::magicLink($intro['url']),
138                                                 'madeby_addr'    => $intro['addr'],
139                                                 'contact_id'     => $intro['contact-id'],
140                                                 'photo'          => Contact::getAvatarUrlForUrl($intro['furl'], 0, Proxy::SIZE_SMALL),
141                                                 'name'           => $intro['fname'],
142                                                 'url'            => $intro['furl'],
143                                                 'zrl'            => Contact::magicLink($intro['furl']),
144                                                 'hidden'         => $intro['hidden'] == 1,
145                                                 'post_newfriend' => (intval($this->pConfig->get(local_user(), 'system', 'post_newfriend')) ? '1' : 0),
146                                                 'note'           => $intro['note'],
147                                                 'request'        => $intro['frequest'] . '?addr=' . $return_addr]);
148
149                                         // Normal connection requests
150                                 } else {
151                                         // Don't show these data until you are connected. Diaspora is doing the same.
152                                         if ($intro['network'] === Protocol::DIASPORA) {
153                                                 $intro['location'] = "";
154                                                 $intro['about']    = "";
155                                         }
156
157                                         $formattedIntroductions[] = new ValueObject\Introduction([
158                                                 'label'          => (($intro['network'] !== Protocol::OSTATUS) ? 'friend_request' : 'follower'),
159                                                 'str_type'       => (($intro['network'] !== Protocol::OSTATUS) ? $this->l10n->t('Friend/Connect Request') : $this->l10n->t('New Follower')),
160                                                 'dfrn_id'        => $intro['issued-id'],
161                                                 'uid'            => $this->session->get('uid'),
162                                                 'intro_id'       => $intro['intro_id'],
163                                                 'contact_id'     => $intro['contact-id'],
164                                                 'photo'          => Contact::getPhoto($intro),
165                                                 'name'           => $intro['name'],
166                                                 'location'       => BBCode::convert($intro['location'], false),
167                                                 'about'          => BBCode::convert($intro['about'], false),
168                                                 'keywords'       => $intro['keywords'],
169                                                 'hidden'         => $intro['hidden'] == 1,
170                                                 'post_newfriend' => (intval($this->pConfig->get(local_user(), 'system', 'post_newfriend')) ? '1' : 0),
171                                                 'url'            => $intro['url'],
172                                                 'zrl'            => Contact::magicLink($intro['url']),
173                                                 'addr'           => $intro['addr'],
174                                                 'network'        => $intro['network'],
175                                                 'knowyou'        => $intro['knowyou'],
176                                                 'note'           => $intro['note'],
177                                         ]);
178                                 }
179                         }
180                 } catch (Exception $e) {
181                         $this->logger->warning('Select failed.', ['uid' => $_SESSION['uid'], 'exception' => $e]);
182                 }
183
184                 return $formattedIntroductions;
185         }
186 }