3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Navigation\Notifications\Factory;
25 use Friendica\App\BaseURL;
26 use Friendica\BaseFactory;
27 use Friendica\Content\Text\BBCode;
28 use Friendica\Core\L10n;
29 use Friendica\Core\Protocol;
30 use Friendica\Database\Database;
31 use Friendica\Model\Contact;
32 use Friendica\Model\Post;
33 use Friendica\Module\BaseNotifications;
34 use Friendica\Navigation\Notifications\Collection\FormattedNotifies;
35 use Friendica\Navigation\Notifications\Repository;
36 use Friendica\Navigation\Notifications\ValueObject;
37 use Friendica\Network\HTTPException\InternalServerErrorException;
38 use Friendica\Protocol\Activity;
39 use Friendica\Util\DateTimeFormat;
40 use Friendica\Util\Proxy;
41 use Friendica\Util\Temporal;
42 use Friendica\Util\XML;
43 use Psr\Log\LoggerInterface;
46 * Factory for creating notification objects based on items
47 * Currently, there are the following types of item based notifications:
53 * @deprecated since 2022.05 Use \Friendica\Navigation\Notifications\Factory\FormattedNotification instead
55 class FormattedNotify extends BaseFactory
59 /** @var Repository\Notify */
66 public function __construct(LoggerInterface $logger, Database $dba, Repository\Notify $notification, BaseURL $baseUrl, L10n $l10n)
68 parent::__construct($logger);
71 $this->notify = $notification;
72 $this->baseUrl = $baseUrl;
77 * @param array $formattedItem The return of $this->formatItem
79 * @return ValueObject\FormattedNotify
81 private function createFromFormattedItem(array $formattedItem): ValueObject\FormattedNotify
83 // Transform the different types of notification in a usable array
84 switch ($formattedItem['verb'] ?? '') {
86 return new ValueObject\FormattedNotify(
88 $this->baseUrl->get(true) . '/display/' . $formattedItem['parent-guid'],
89 $formattedItem['author-avatar'],
90 $formattedItem['author-link'],
91 $this->l10n->t("%s liked %s's post", $formattedItem['author-name'], $formattedItem['parent-author-name']),
92 $formattedItem['when'],
93 $formattedItem['ago'],
94 $formattedItem['seen']
97 case Activity::DISLIKE:
98 return new ValueObject\FormattedNotify(
100 $this->baseUrl->get(true) . '/display/' . $formattedItem['parent-guid'],
101 $formattedItem['author-avatar'],
102 $formattedItem['author-link'],
103 $this->l10n->t("%s disliked %s's post", $formattedItem['author-name'], $formattedItem['parent-author-name']),
104 $formattedItem['when'],
105 $formattedItem['ago'],
106 $formattedItem['seen']
109 case Activity::ATTEND:
110 return new ValueObject\FormattedNotify(
112 $this->baseUrl->get(true) . '/display/' . $formattedItem['parent-guid'],
113 $formattedItem['author-avatar'],
114 $formattedItem['author-link'],
115 $this->l10n->t("%s is attending %s's event", $formattedItem['author-name'], $formattedItem['parent-author-name']),
116 $formattedItem['when'],
117 $formattedItem['ago'],
118 $formattedItem['seen']
121 case Activity::ATTENDNO:
122 return new ValueObject\FormattedNotify(
124 $this->baseUrl->get(true) . '/display/' . $formattedItem['parent-guid'],
125 $formattedItem['author-avatar'],
126 $formattedItem['author-link'],
127 $this->l10n->t("%s is not attending %s's event", $formattedItem['author-name'], $formattedItem['parent-author-name']),
128 $formattedItem['when'],
129 $formattedItem['ago'],
130 $formattedItem['seen']
133 case Activity::ATTENDMAYBE:
134 return new ValueObject\FormattedNotify(
136 $this->baseUrl->get(true) . '/display/' . $formattedItem['parent-guid'],
137 $formattedItem['author-avatar'],
138 $formattedItem['author-link'],
139 $this->l10n->t("%s may attending %s's event", $formattedItem['author-name'], $formattedItem['parent-author-name']),
140 $formattedItem['when'],
141 $formattedItem['ago'],
142 $formattedItem['seen']
145 case Activity::FRIEND:
146 if (!isset($formattedItem['object'])) {
147 return new ValueObject\FormattedNotify(
149 $formattedItem['link'],
150 $formattedItem['image'],
151 $formattedItem['url'],
152 $formattedItem['text'],
153 $formattedItem['when'],
154 $formattedItem['ago'],
155 $formattedItem['seen']
159 $xmlHead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">";
160 $obj = XML::parseString($xmlHead . $formattedItem['object']);
162 $formattedItem['fname'] = $obj->title;
164 return new ValueObject\FormattedNotify(
166 $this->baseUrl->get(true) . '/display/' . $formattedItem['parent-guid'],
167 $formattedItem['author-avatar'],
168 $formattedItem['author-link'],
169 $this->l10n->t("%s is now friends with %s", $formattedItem['author-name'], $formattedItem['fname']),
170 $formattedItem['when'],
171 $formattedItem['ago'],
172 $formattedItem['seen']
176 return new ValueObject\FormattedNotify(
177 $formattedItem['label'] ?? '',
178 $formattedItem['link'] ?? '',
179 $formattedItem['image'] ?? '',
180 $formattedItem['url'] ?? '',
181 $formattedItem['text'] ?? '',
182 $formattedItem['when'] ?? '',
183 $formattedItem['ago'] ?? '',
184 $formattedItem['seen'] ?? false
190 * Get system notifications
192 * @param bool $seen False => only include notifications into the query
193 * which aren't marked as "seen"
194 * @param int $start Start the query at this point
195 * @param int $limit Maximum number of query results
197 * @return FormattedNotifies
199 public function getSystemList(bool $seen = false, int $start = 0, int $limit = BaseNotifications::DEFAULT_PAGE_LIMIT): FormattedNotifies
203 $conditions['seen'] = false;
207 $params['order'] = ['date' => 'DESC'];
208 $params['limit'] = [$start, $limit];
210 $formattedNotifications = new FormattedNotifies();
212 $Notifies = $this->notify->selectForUser(local_user(), $conditions, $params);
214 foreach ($Notifies as $Notify) {
215 $formattedNotifications[] = new ValueObject\FormattedNotify(
217 $this->baseUrl->get(true) . '/notify/' . $Notify->id,
218 Contact::getAvatarUrlForUrl($Notify->url, $Notify->uid, Proxy::SIZE_MICRO),
220 strip_tags(BBCode::toPlaintext($Notify->msg)),
221 DateTimeFormat::local($Notify->date->format(DateTimeFormat::MYSQL), 'r'),
222 Temporal::getRelativeDate($Notify->date->format(DateTimeFormat::MYSQL)),
226 } catch (Exception $e) {
227 $this->logger->warning('Select failed.', ['conditions' => $conditions, 'exception' => $e]);
230 return $formattedNotifications;
234 * Get network notifications
236 * @param bool $seen False => only include notifications into the query
237 * which aren't marked as "seen"
238 * @param int $start Start the query at this point
239 * @param int $limit Maximum number of query results
241 * @return FormattedNotifies
243 public function getNetworkList(bool $seen = false, int $start = 0, int $limit = BaseNotifications::DEFAULT_PAGE_LIMIT): FormattedNotifies
245 $condition = ['wall' => false, 'uid' => local_user()];
248 $condition['unseen'] = true;
251 $fields = ['id', 'parent', 'verb', 'author-name', 'unseen', 'author-link', 'author-avatar', 'contact-avatar',
252 'network', 'created', 'object', 'parent-author-name', 'parent-author-link', 'parent-guid', 'gravity'];
253 $params = ['order' => ['received' => true], 'limit' => [$start, $limit]];
255 $formattedNotifications = new FormattedNotifies();
258 $userPosts = Post::selectForUser(local_user(), $fields, $condition, $params);
259 while ($userPost = $this->dba->fetch($userPosts)) {
260 $formattedNotifications[] = $this->createFromFormattedItem($this->formatItem($userPost));
262 } catch (Exception $e) {
263 $this->logger->warning('Select failed.', ['condition' => $condition, 'exception' => $e]);
266 return $formattedNotifications;
270 * Get personal notifications
272 * @param bool $seen False => only include notifications into the query
273 * which aren't marked as "seen"
274 * @param int $start Start the query at this point
275 * @param int $limit Maximum number of query results
277 * @return FormattedNotifies
279 public function getPersonalList(bool $seen = false, int $start = 0, int $limit = BaseNotifications::DEFAULT_PAGE_LIMIT): FormattedNotifies
281 $condition = ['wall' => false, 'uid' => local_user(), 'author-id' => public_contact()];
284 $condition['unseen'] = true;
287 $fields = ['id', 'parent', 'verb', 'author-name', 'unseen', 'author-link', 'author-avatar', 'contact-avatar',
288 'network', 'created', 'object', 'parent-author-name', 'parent-author-link', 'parent-guid', 'gravity'];
289 $params = ['order' => ['received' => true], 'limit' => [$start, $limit]];
291 $formattedNotifications = new FormattedNotifies();
294 $userPosts = Post::selectForUser(local_user(), $fields, $condition, $params);
295 while ($userPost = $this->dba->fetch($userPosts)) {
296 $formattedNotifications[] = $this->createFromFormattedItem($this->formatItem($userPost));
298 } catch (Exception $e) {
299 $this->logger->warning('Select failed.', ['conditions' => $condition, 'exception' => $e]);
302 return $formattedNotifications;
306 * Get home notifications
308 * @param bool $seen False => only include notifications into the query
309 * which aren't marked as "seen"
310 * @param int $start Start the query at this point
311 * @param int $limit Maximum number of query results
313 * @return FormattedNotifies
315 public function getHomeList(bool $seen = false, int $start = 0, int $limit = BaseNotifications::DEFAULT_PAGE_LIMIT): FormattedNotifies
317 $condition = ['wall' => true, 'uid' => local_user()];
320 $condition['unseen'] = true;
323 $fields = ['id', 'parent', 'verb', 'author-name', 'unseen', 'author-link', 'author-avatar', 'contact-avatar',
324 'network', 'created', 'object', 'parent-author-name', 'parent-author-link', 'parent-guid', 'gravity'];
325 $params = ['order' => ['received' => true], 'limit' => [$start, $limit]];
327 $formattedNotifications = new FormattedNotifies();
330 $userPosts = Post::selectForUser(local_user(), $fields, $condition, $params);
331 while ($userPost = $this->dba->fetch($userPosts)) {
332 $formattedItem = $this->formatItem($userPost);
334 // Overwrite specific fields, not default item format
335 $formattedItem['label'] = 'comment';
336 $formattedItem['text'] = $this->l10n->t("%s commented on %s's post", $formattedItem['author-name'], $formattedItem['parent-author-name']);
338 $formattedNotifications[] = $this->createFromFormattedItem($formattedItem);
340 } catch (Exception $e) {
341 $this->logger->warning('Select failed.', ['conditions' => $condition, 'exception' => $e]);
344 return $formattedNotifications;
348 * Format the item query in a usable array
350 * @param array $item The item from the db query
352 * @return array The item, extended with the notification-specific information
354 * @throws InternalServerErrorException
357 private function formatItem(array $item): array
359 $item['seen'] = !($item['unseen'] > 0);
361 // For feed items we use the user's contact, since the avatar is mostly self choosen.
362 if (!empty($item['network']) && $item['network'] == Protocol::FEED) {
363 $item['author-avatar'] = $item['contact-avatar'];
366 $item['label'] = (($item['gravity'] == GRAVITY_PARENT) ? 'post' : 'comment');
367 $item['link'] = $this->baseUrl->get(true) . '/display/' . $item['parent-guid'];
368 $item['image'] = $item['author-avatar'];
369 $item['url'] = $item['author-link'];
370 $item['when'] = DateTimeFormat::local($item['created'], 'r');
371 $item['ago'] = Temporal::getRelativeDate($item['created']);
372 $item['text'] = (($item['gravity'] == GRAVITY_PARENT)
373 ? $this->l10n->t("%s created a new post", $item['author-name'])
374 : $this->l10n->t("%s commented on %s's post", $item['author-name'], $item['parent-author-name']));