3 * @copyright Copyright (C) 2010-2023, 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;
24 use Friendica\App\BaseURL;
25 use Friendica\BaseFactory;
26 use Friendica\Capabilities\ICanCreateFromTableRow;
27 use Friendica\Contact\LocalRelationship\Repository\LocalRelationship;
28 use Friendica\Content\Text\BBCode;
29 use Friendica\Content\Text\Plaintext;
30 use Friendica\Core\Cache\Enum\Duration;
31 use Friendica\Core\Cache\Capability\ICanCache;
32 use Friendica\Core\L10n;
33 use Friendica\Model\Contact;
34 use Friendica\Model\Post;
35 use Friendica\Model\User;
36 use Friendica\Model\Verb;
37 use Friendica\Navigation\Notifications\Entity;
38 use Friendica\Network\HTTPException;
39 use Friendica\Protocol\Activity;
40 use Psr\Log\LoggerInterface;
42 class Notification extends BaseFactory implements ICanCreateFromTableRow
48 /** @var LocalRelationship */
49 private $localRelationshipRepo;
53 public function __construct(\Friendica\App\BaseURL $baseUrl, \Friendica\Core\L10n $l10n, \Friendica\Contact\LocalRelationship\Repository\LocalRelationship $localRelationshipRepo, LoggerInterface $logger, ICanCache $cache)
55 parent::__construct($logger);
57 $this->baseUrl = $baseUrl;
59 $this->localRelationshipRepo = $localRelationshipRepo;
60 $this->cache = $cache;
63 public function createFromTableRow(array $row): Entity\Notification
65 return new Entity\Notification(
67 Verb::getByID($row['vid']),
70 $row['target-uri-id'],
71 $row['parent-uri-id'],
72 new \DateTime($row['created'], new \DateTimeZone('UTC')),
79 public function createForUser(int $uid, int $vid, int $type, int $actorId, int $targetUriId, int $parentUriId): Entity\Notification
81 return new Entity\Notification(
93 * @param int $contactId Public contact id
95 * @return Entity\Notification
97 public function createForRelationship(int $uid, int $contactId, string $verb): Entity\Notification
99 return new Entity\Notification(
102 Post\UserNotification::TYPE_NONE,
108 * @param Entity\Notification $Notification
110 * @throws HTTPException\InternalServerErrorException
111 * @throws HTTPException\NotFoundException
113 public function getMessageFromNotification(Entity\Notification $Notification): array
117 $cachekey = 'Notification:' . $Notification->id;
118 $result = $this->cache->get($cachekey);
119 if (!is_null($result)) {
123 $user = User::getById($Notification->uid, ['language']);
124 $l10n = $this->l10n->withLang($user['language']);
126 $causer = $author = Contact::getById($Notification->actorId, ['id', 'name', 'url', 'contact-type', 'pending']);
127 if (empty($causer)) {
128 $this->logger->info('Causer not found', ['contact' => $Notification->actorId]);
132 if ($Notification->type === Post\UserNotification::TYPE_NONE) {
133 if ($causer['pending']) {
134 $msg = $l10n->t('%1$s wants to follow you');
136 $msg = $l10n->t('%1$s has started following you');
139 $title = $causer['name'];
140 $link = $this->baseUrl . '/contact/' . $causer['id'];
142 if (!$Notification->targetUriId) {
146 if (Post\ThreadUser::getIgnored($Notification->parentUriId, $Notification->uid)) {
147 $this->logger->info('Thread is ignored', ['parent-uri-id' => $Notification->parentUriId, 'type' => $Notification->type]);
151 if (in_array($Notification->type, [Post\UserNotification::TYPE_THREAD_COMMENT, Post\UserNotification::TYPE_COMMENT_PARTICIPATION, Post\UserNotification::TYPE_ACTIVITY_PARTICIPATION, Post\UserNotification::TYPE_FOLLOW, Post\UserNotification::TYPE_EXPLICIT_TAGGED])) {
152 $item = Post::selectFirst([], ['uri-id' => $Notification->parentUriId, 'uid' => [0, $Notification->uid]], ['order' => ['uid' => true]]);
154 $this->logger->info('Parent post not found', ['uri-id' => $Notification->parentUriId]);
157 $link_item = Post::selectFirstPost(['guid'], ['uri-id' => $Notification->targetUriId]);
159 $link_item = $item = Post::selectFirst([], ['uri-id' => $Notification->targetUriId, 'uid' => [0, $Notification->uid]], ['order' => ['uid' => true]]);
161 $this->logger->info('Post not found', ['uri-id' => $Notification->targetUriId]);
165 if (($Notification->verb == Activity::POST) || ($Notification->type === Post\UserNotification::TYPE_SHARED)) {
166 $thrparentid = $item['thr-parent-id'];
167 $item = Post::selectFirst([], ['uri-id' => $thrparentid, 'uid' => [0, $Notification->uid]], ['order' => ['uid' => true]]);
169 $this->logger->info('Thread parent post not found', ['uri-id' => $thrparentid]);
174 if (($Notification->verb != Activity::POST) || !in_array($Notification->type, [Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT, Post\UserNotification::TYPE_IMPLICIT_TAGGED])) {
179 if (in_array($Notification->type, [Post\UserNotification::TYPE_COMMENT_PARTICIPATION, Post\UserNotification::TYPE_ACTIVITY_PARTICIPATION, Post\UserNotification::TYPE_FOLLOW, Post\UserNotification::TYPE_SHARED])) {
180 $author = Contact::getById($item['author-id'], ['id', 'name', 'url', 'contact-type']);
181 if (empty($author)) {
182 $this->logger->info('Author not found', ['author' => $item['author-id']]);
187 // Final check on $link_item
188 // @see https://github.com/friendica/friendica/issues/11632#issuecomment-1183365937
189 if (empty($link_item)) {
190 $this->logger->info('Link item is still empty. Dumping whole Notification object:', [$Notification]);
194 $link = $this->baseUrl . '/display/' . urlencode($link_item['guid']);
196 $body = BBCode::toPlaintext($item['body'], false);
197 $title = Plaintext::shorten($body, 70);
198 if (!empty($title)) {
199 $title = '"' . trim(str_replace("\n", " ", $title)) . '"';
202 $this->logger->debug('Got verb and type', ['verb' => $Notification->verb, 'type' => $Notification->type, 'causer' => $causer['id'], 'author' => $author['id'], 'item' => $item['id'], 'uid' => $Notification->uid]);
204 switch ($Notification->verb) {
206 switch ($Notification->type) {
207 case Post\UserNotification::TYPE_DIRECT_COMMENT:
208 $msg = $l10n->t('%1$s liked your comment on %2$s');
210 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
211 $msg = $l10n->t('%1$s liked your post %2$s');
215 case Activity::DISLIKE:
216 switch ($Notification->type) {
217 case Post\UserNotification::TYPE_DIRECT_COMMENT:
218 $msg = $l10n->t('%1$s disliked your comment on %2$s');
220 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
221 $msg = $l10n->t('%1$s disliked your post %2$s');
225 case Activity::ANNOUNCE:
226 switch ($Notification->type) {
227 case Post\UserNotification::TYPE_DIRECT_COMMENT:
228 $msg = $l10n->t('%1$s shared your comment %2$s');
230 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
231 $msg = $l10n->t('%1$s shared your post %2$s');
233 case Post\UserNotification::TYPE_SHARED:
234 if (($causer['id'] != $author['id']) && ($title != '')) {
235 $msg = $l10n->t('%1$s shared the post %2$s from %3$s');
236 } elseif ($causer['id'] != $author['id']) {
237 $msg = $l10n->t('%1$s shared a post from %3$s');
238 } elseif ($title != '') {
239 $msg = $l10n->t('%1$s shared the post %2$s');
241 $msg = $l10n->t('%1$s shared a post');
246 case Activity::ATTEND:
247 switch ($Notification->type) {
248 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
249 $msg = $l10n->t('%1$s wants to attend your event %2$s');
253 case Activity::ATTENDNO:
254 switch ($Notification->type) {
255 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
256 $msg = $l10n->t('%1$s does not want to attend your event %2$s');
260 case Activity::ATTENDMAYBE:
261 switch ($Notification->type) {
262 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
263 $msg = $l10n->t('%1$s maybe wants to attend your event %2$s');
268 switch ($Notification->type) {
269 case Post\UserNotification::TYPE_EXPLICIT_TAGGED:
270 $msg = $l10n->t('%1$s tagged you on %2$s');
273 case Post\UserNotification::TYPE_IMPLICIT_TAGGED:
274 $msg = $l10n->t('%1$s replied to you on %2$s');
277 case Post\UserNotification::TYPE_THREAD_COMMENT:
278 $msg = $l10n->t('%1$s commented in your thread %2$s');
281 case Post\UserNotification::TYPE_DIRECT_COMMENT:
282 $msg = $l10n->t('%1$s commented on your comment %2$s');
285 case Post\UserNotification::TYPE_COMMENT_PARTICIPATION:
286 case Post\UserNotification::TYPE_ACTIVITY_PARTICIPATION:
287 case Post\UserNotification::TYPE_FOLLOW;
288 if (($causer['id'] == $author['id']) && ($title != '')) {
289 $msg = $l10n->t('%1$s commented in their thread %2$s');
290 } elseif ($causer['id'] == $author['id']) {
291 $msg = $l10n->t('%1$s commented in their thread');
292 } elseif ($title != '') {
293 $msg = $l10n->t('%1$s commented in the thread %2$s from %3$s');
295 $msg = $l10n->t('%1$s commented in the thread from %3$s');
299 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
300 $msg = $l10n->t('%1$s commented on your thread %2$s');
303 case Post\UserNotification::TYPE_SHARED:
304 if (($causer['id'] != $author['id']) && ($title != '')) {
305 $msg = $l10n->t('%1$s shared the post %2$s from %3$s');
306 } elseif ($causer['id'] != $author['id']) {
307 $msg = $l10n->t('%1$s shared a post from %3$s');
308 } elseif ($title != '') {
309 $msg = $l10n->t('%1$s shared the post %2$s');
311 $msg = $l10n->t('%1$s shared a post');
315 case Post\UserNotification::TYPE_QUOTED:
316 $msg = $l10n->t('%1$s shared your post %2$s');
324 // Name of the notification's causer
325 $message['causer'] = $causer['name'];
326 // Format for the "ping" mechanism
327 $message['notification'] = sprintf($msg, '{0}', $title, $author['name']);
328 // Plain text for the web push api
329 $message['plain'] = sprintf($msg, $causer['name'], $title, $author['name']);
330 // Rich text for other purposes
331 $message['rich'] = sprintf($msg,
332 '[url=' . $causer['url'] . ']' . $causer['name'] . '[/url]',
333 '[url=' . $link . ']' . $title . '[/url]',
334 '[url=' . $author['url'] . ']' . $author['name'] . '[/url]');
335 $message['link'] = $link;
336 $this->cache->set($cachekey, $message, Duration::HOUR);
338 $this->logger->debug('Unhandled notification', ['notification' => $Notification]);