3 * @copyright Copyright (C) 2010-2021, 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\Factory\Notification;
26 use Friendica\App\BaseURL;
27 use Friendica\BaseFactory;
28 use Friendica\Collection\Api\Notifications as ApiNotifications;
29 use Friendica\Content\Text\BBCode;
30 use Friendica\Core\L10n;
31 use Friendica\Core\PConfig\IPConfig;
32 use Friendica\Core\Protocol;
33 use Friendica\Core\Session\ISession;
34 use Friendica\Database\Database;
35 use Friendica\Model\Contact;
36 use Friendica\Model\Post;
37 use Friendica\Module\BaseNotifications;
38 use Friendica\Network\HTTPException\InternalServerErrorException;
39 use Friendica\Object\Api\Friendica\Notification as ApiNotification;
40 use Friendica\Protocol\Activity;
41 use Friendica\Repository;
42 use Friendica\Util\DateTimeFormat;
43 use Friendica\Util\Proxy;
44 use Friendica\Util\Temporal;
45 use Friendica\Util\XML;
46 use Psr\Log\LoggerInterface;
49 * Factory for creating notification objects based on items
50 * Currently, there are the following types of item based notifications:
56 class Notification extends BaseFactory
60 /** @var Repository\Notification */
61 private $notification;
67 public function __construct(LoggerInterface $logger, Database $dba, Repository\Notification $notification, BaseURL $baseUrl, L10n $l10n, App $app, IPConfig $pConfig, ISession $session)
69 parent::__construct($logger);
72 $this->notification = $notification;
73 $this->baseUrl = $baseUrl;
78 * Format the item query in an usable array
80 * @param array $item The item from the db query
82 * @return array The item, extended with the notification-specific information
84 * @throws InternalServerErrorException
87 private function formatItem(array $item)
89 $item['seen'] = ($item['unseen'] > 0 ? false : true);
91 // For feed items we use the user's contact, since the avatar is mostly self choosen.
92 if (!empty($item['network']) && $item['network'] == Protocol::FEED) {
93 $item['author-avatar'] = $item['contact-avatar'];
96 $item['label'] = (($item['gravity'] == GRAVITY_PARENT) ? 'post' : 'comment');
97 $item['link'] = $this->baseUrl->get(true) . '/display/' . $item['parent-guid'];
98 $item['image'] = $item['author-avatar'];
99 $item['url'] = $item['author-link'];
100 $item['text'] = (($item['gravity'] == GRAVITY_PARENT)
101 ? $this->l10n->t("%s created a new post", $item['author-name'])
102 : $this->l10n->t("%s commented on %s's post", $item['author-name'], $item['parent-author-name']));
103 $item['when'] = DateTimeFormat::local($item['created'], 'r');
104 $item['ago'] = Temporal::getRelativeDate($item['created']);
112 * @return \Friendica\Object\Notification\Notification
114 * @throws InternalServerErrorException
116 private function createFromItem(array $item)
118 $item = $this->formatItem($item);
120 // Transform the different types of notification in an usable array
121 switch ($item['verb'] ?? '') {
123 return new \Friendica\Object\Notification\Notification([
125 'link' => $this->baseUrl->get(true) . '/display/' . $item['parent-guid'],
126 'image' => $item['author-avatar'],
127 'url' => $item['author-link'],
128 'text' => $this->l10n->t("%s liked %s's post", $item['author-name'], $item['parent-author-name']),
129 'when' => $item['when'],
130 'ago' => $item['ago'],
131 'seen' => $item['seen']]);
133 case Activity::DISLIKE:
134 return new \Friendica\Object\Notification\Notification([
135 'label' => 'dislike',
136 'link' => $this->baseUrl->get(true) . '/display/' . $item['parent-guid'],
137 'image' => $item['author-avatar'],
138 'url' => $item['author-link'],
139 'text' => $this->l10n->t("%s disliked %s's post", $item['author-name'], $item['parent-author-name']),
140 'when' => $item['when'],
141 'ago' => $item['ago'],
142 'seen' => $item['seen']]);
144 case Activity::ATTEND:
145 return new \Friendica\Object\Notification\Notification([
147 'link' => $this->baseUrl->get(true) . '/display/' . $item['parent-guid'],
148 'image' => $item['author-avatar'],
149 'url' => $item['author-link'],
150 'text' => $this->l10n->t("%s is attending %s's event", $item['author-name'], $item['parent-author-name']),
151 'when' => $item['when'],
152 'ago' => $item['ago'],
153 'seen' => $item['seen']]);
155 case Activity::ATTENDNO:
156 return new \Friendica\Object\Notification\Notification([
157 'label' => 'attendno',
158 'link' => $this->baseUrl->get(true) . '/display/' . $item['parent-guid'],
159 'image' => $item['author-avatar'],
160 'url' => $item['author-link'],
161 'text' => $this->l10n->t("%s is not attending %s's event", $item['author-name'], $item['parent-author-name']),
162 'when' => $item['when'],
163 'ago' => $item['ago'],
164 'seen' => $item['seen']]);
166 case Activity::ATTENDMAYBE:
167 return new \Friendica\Object\Notification\Notification([
168 'label' => 'attendmaybe',
169 'link' => $this->baseUrl->get(true) . '/display/' . $item['parent-guid'],
170 'image' => $item['author-avatar'],
171 'url' => $item['author-link'],
172 'text' => $this->l10n->t("%s may attending %s's event", $item['author-name'], $item['parent-author-name']),
173 'when' => $item['when'],
174 'ago' => $item['ago'],
175 'seen' => $item['seen']]);
177 case Activity::FRIEND:
178 if (!isset($item['object'])) {
179 return new \Friendica\Object\Notification\Notification([
181 'link' => $item['link'],
182 'image' => $item['image'],
183 'url' => $item['url'],
184 'text' => $item['text'],
185 'when' => $item['when'],
186 'ago' => $item['ago'],
187 'seen' => $item['seen']]);
190 $xmlHead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">";
191 $obj = XML::parseString($xmlHead . $item['object']);
192 $item['fname'] = $obj->title;
194 return new \Friendica\Object\Notification\Notification([
196 'link' => $this->baseUrl->get(true) . '/display/' . $item['parent-guid'],
197 'image' => $item['author-avatar'],
198 'url' => $item['author-link'],
199 'text' => $this->l10n->t("%s is now friends with %s", $item['author-name'], $item['fname']),
200 'when' => $item['when'],
201 'ago' => $item['ago'],
202 'seen' => $item['seen']]);
205 return new \Friendica\Object\Notification\Notification($item);
211 * Get system notifications
213 * @param bool $seen False => only include notifications into the query
214 * which aren't marked as "seen"
215 * @param int $start Start the query at this point
216 * @param int $limit Maximum number of query results
218 * @return \Friendica\Module\Notifications\Notification[]
220 public function getSystemList(bool $seen = false, int $start = 0, int $limit = BaseNotifications::DEFAULT_PAGE_LIMIT)
222 $conditions = ['uid' => local_user()];
225 $conditions['seen'] = false;
229 $params['order'] = ['date' => 'DESC'];
230 $params['limit'] = [$start, $limit];
232 $formattedNotifications = [];
234 $notifications = $this->notification->select($conditions, $params);
236 foreach ($notifications as $notification) {
237 $formattedNotifications[] = new \Friendica\Object\Notification\Notification([
238 'label' => 'notification',
239 'link' => $this->baseUrl->get(true) . '/notification/' . $notification->id,
240 'image' => Contact::getAvatarUrlForUrl($notification->url, $notification->uid, Proxy::SIZE_MICRO),
241 'url' => $notification->url,
242 'text' => strip_tags(BBCode::convert($notification->msg)),
243 'when' => DateTimeFormat::local($notification->date, 'r'),
244 'ago' => Temporal::getRelativeDate($notification->date),
245 'seen' => $notification->seen]);
247 } catch (Exception $e) {
248 $this->logger->warning('Select failed.', ['conditions' => $conditions, 'exception' => $e]);
251 return $formattedNotifications;
255 * Get network notifications
257 * @param bool $seen False => only include notifications into the query
258 * which aren't marked as "seen"
259 * @param int $start Start the query at this point
260 * @param int $limit Maximum number of query results
262 * @return \Friendica\Object\Notification\Notification[]
264 public function getNetworkList(bool $seen = false, int $start = 0, int $limit = BaseNotifications::DEFAULT_PAGE_LIMIT)
266 $conditions = ['wall' => false, 'uid' => local_user()];
269 $conditions['unseen'] = true;
272 $fields = ['id', 'parent', 'verb', 'author-name', 'unseen', 'author-link', 'author-avatar', 'contact-avatar',
273 'network', 'created', 'object', 'parent-author-name', 'parent-author-link', 'parent-guid', 'gravity'];
274 $params = ['order' => ['received' => true], 'limit' => [$start, $limit]];
276 $formattedNotifications = [];
279 $items = Post::selectForUser(local_user(), $fields, $conditions, $params);
281 while ($item = $this->dba->fetch($items)) {
282 $formattedNotifications[] = $this->createFromItem($item);
284 } catch (Exception $e) {
285 $this->logger->warning('Select failed.', ['conditions' => $conditions, 'exception' => $e]);
288 return $formattedNotifications;
292 * Get personal notifications
294 * @param bool $seen False => only include notifications into the query
295 * which aren't marked as "seen"
296 * @param int $start Start the query at this point
297 * @param int $limit Maximum number of query results
299 * @return \Friendica\Object\Notification\Notification[]
301 public function getPersonalList(bool $seen = false, int $start = 0, int $limit = BaseNotifications::DEFAULT_PAGE_LIMIT)
303 $condition = ["NOT `wall` AND `uid` = ? AND `author-id` = ?", local_user(), public_contact()];
306 $condition[0] .= " AND `unseen`";
309 $fields = ['id', 'parent', 'verb', 'author-name', 'unseen', 'author-link', 'author-avatar', 'contact-avatar',
310 'network', 'created', 'object', 'parent-author-name', 'parent-author-link', 'parent-guid', 'gravity'];
311 $params = ['order' => ['received' => true], 'limit' => [$start, $limit]];
313 $formattedNotifications = [];
316 $items = Post::selectForUser(local_user(), $fields, $condition, $params);
318 while ($item = $this->dba->fetch($items)) {
319 $formattedNotifications[] = $this->createFromItem($item);
321 } catch (Exception $e) {
322 $this->logger->warning('Select failed.', ['conditions' => $condition, 'exception' => $e]);
325 return $formattedNotifications;
329 * Get home notifications
331 * @param bool $seen False => only include notifications into the query
332 * which aren't marked as "seen"
333 * @param int $start Start the query at this point
334 * @param int $limit Maximum number of query results
336 * @return \Friendica\Object\Notification\Notification[]
338 public function getHomeList(bool $seen = false, int $start = 0, int $limit = BaseNotifications::DEFAULT_PAGE_LIMIT)
340 $condition = ['wall' => true, 'uid' => local_user()];
343 $condition['unseen'] = true;
346 $fields = ['id', 'parent', 'verb', 'author-name', 'unseen', 'author-link', 'author-avatar', 'contact-avatar',
347 'network', 'created', 'object', 'parent-author-name', 'parent-author-link', 'parent-guid', 'gravity'];
348 $params = ['order' => ['received' => true], 'limit' => [$start, $limit]];
350 $formattedNotifications = [];
353 $items = Post::selectForUser(local_user(), $fields, $condition, $params);
355 while ($item = $this->dba->fetch($items)) {
356 $item = $this->formatItem($item);
358 // Overwrite specific fields, not default item format
359 $item['label'] = 'comment';
360 $item['text'] = $this->l10n->t("%s commented on %s's post", $item['author-name'], $item['parent-author-name']);
362 $formattedNotifications[] = $this->createFromItem($item);
364 } catch (Exception $e) {
365 $this->logger->warning('Select failed.', ['conditions' => $condition, 'exception' => $e]);
368 return $formattedNotifications;
372 * @param int $uid The user id of the API call
373 * @param array $params Additional parameters
375 * @return ApiNotifications
379 public function getApiList(int $uid, array $params = ['order' => ['seen' => 'ASC', 'date' => 'DESC'], 'limit' => 50])
381 $notifies = $this->notification->select(['uid' => $uid], $params);
383 /** @var ApiNotification[] $notifications */
386 foreach ($notifies as $notify) {
387 $notifications[] = new ApiNotification($notify);
390 return new ApiNotifications($notifications);