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;
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\Plaintext;
29 use Friendica\Core\L10n;
30 use Friendica\Model\Contact;
31 use Friendica\Model\Post;
32 use Friendica\Model\Verb;
33 use Friendica\Navigation\Notifications\Entity;
34 use Friendica\Network\HTTPException;
35 use Friendica\Protocol\Activity;
36 use Psr\Log\LoggerInterface;
38 class Notification extends BaseFactory implements ICanCreateFromTableRow
44 /** @var LocalRelationship */
45 private $localRelationshipRepo;
47 public function __construct(\Friendica\App\BaseURL $baseUrl, \Friendica\Core\L10n $l10n, \Friendica\Contact\LocalRelationship\Repository\LocalRelationship $localRelationshipRepo, LoggerInterface $logger)
49 parent::__construct($logger);
51 $this->baseUrl = $baseUrl;
53 $this->localRelationshipRepo = $localRelationshipRepo;
56 public function createFromTableRow(array $row): Entity\Notification
58 return new Entity\Notification(
60 Verb::getByID($row['vid']),
63 $row['target-uri-id'],
64 $row['parent-uri-id'],
65 new \DateTime($row['created'], new \DateTimeZone('UTC')),
72 public function createForUser(int $uid, int $vid, int $type, int $actorId, int $targetUriId, int $parentUriId): Entity\Notification
74 return new Entity\Notification(
86 * @param int $contactId Public contact id
88 * @return Entity\Notification
90 public function createForRelationship(int $uid, int $contactId, string $verb): Entity\Notification
92 return new Entity\Notification(
95 Post\UserNotification::TYPE_NONE,
101 * @param Entity\Notification $Notification
103 * @throws HTTPException\InternalServerErrorException
104 * @throws HTTPException\NotFoundException
106 public function getMessageFromNotification(Entity\Notification $Notification): array
110 $causer = $author = Contact::getById($Notification->actorId, ['id', 'name', 'url', 'contact-type', 'pending']);
111 if (empty($causer)) {
112 $this->logger->info('Causer not found', ['contact' => $Notification->actorId]);
116 if ($Notification->type === Post\UserNotification::TYPE_NONE) {
117 $localRelationship = $this->localRelationshipRepo->getForUserContact($Notification->uid, $Notification->actorId);
118 if ($localRelationship->pending) {
119 $msg = $this->l10n->t('%1$s wants to follow you');
121 $msg = $this->l10n->t('%1$s has started following you');
124 $title = $causer['name'];
125 $link = $this->baseUrl . '/contact/' . $causer['id'];
127 if (!$Notification->targetUriId) {
131 if (Post\ThreadUser::getIgnored($Notification->parentUriId, $Notification->uid)) {
132 $this->logger->info('Thread is ignored', ['parent-uri-id' => $Notification->parentUriId, 'type' => $Notification->type]);
136 if (in_array($Notification->type, [Post\UserNotification::TYPE_THREAD_COMMENT, Post\UserNotification::TYPE_COMMENT_PARTICIPATION, Post\UserNotification::TYPE_ACTIVITY_PARTICIPATION, Post\UserNotification::TYPE_EXPLICIT_TAGGED])) {
137 $item = Post::selectFirst([], ['uri-id' => $Notification->parentUriId, 'uid' => [0, $Notification->uid]], ['order' => ['uid' => true]]);
139 $this->logger->info('Parent post not found', ['uri-id' => $Notification->parentUriId]);
142 $link_item = Post::selectFirstPost(['guid'], ['uri-id' => $Notification->targetUriId]);
144 $link_item = $item = Post::selectFirst([], ['uri-id' => $Notification->targetUriId, 'uid' => [0, $Notification->uid]], ['order' => ['uid' => true]]);
146 $this->logger->info('Post not found', ['uri-id' => $Notification->targetUriId]);
150 if (($Notification->verb == Activity::POST) || ($Notification->type === Post\UserNotification::TYPE_SHARED)) {
151 $item = Post::selectFirst([], ['uri-id' => $item['thr-parent-id'], 'uid' => [0, $Notification->uid]], ['order' => ['uid' => true]]);
153 $this->logger->info('Thread parent post not found', ['uri-id' => $item['thr-parent-id']]);
158 if (($Notification->verb != Activity::POST) || !in_array($Notification->type, [Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT, Post\UserNotification::TYPE_IMPLICIT_TAGGED])) {
163 if (in_array($Notification->type, [Post\UserNotification::TYPE_COMMENT_PARTICIPATION, Post\UserNotification::TYPE_ACTIVITY_PARTICIPATION, Post\UserNotification::TYPE_SHARED])) {
164 $author = Contact::getById($item['author-id'], ['id', 'name', 'url', 'contact-type']);
165 if (empty($author)) {
166 $this->logger->info('Author not found', ['author' => $item['author-id']]);
171 $link = $this->baseUrl . '/display/' . urlencode($link_item['guid']);
173 $content = Plaintext::getPost($item, 70);
174 if (!empty($content['text'])) {
175 $title = '"' . trim(str_replace("\n", " ", $content['text'])) . '"';
180 $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]);
182 switch ($Notification->verb) {
184 switch ($Notification->type) {
185 case Post\UserNotification::TYPE_DIRECT_COMMENT:
186 $msg = $this->l10n->t('%1$s liked your comment on %2$s');
188 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
189 $msg = $this->l10n->t('%1$s liked your post %2$s');
193 case Activity::DISLIKE:
194 switch ($Notification->type) {
195 case Post\UserNotification::TYPE_DIRECT_COMMENT:
196 $msg = $this->l10n->t('%1$s disliked your comment on %2$s');
198 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
199 $msg = $this->l10n->t('%1$s disliked your post %2$s');
203 case Activity::ANNOUNCE:
204 switch ($Notification->type) {
205 case Post\UserNotification::TYPE_DIRECT_COMMENT:
206 $msg = $this->l10n->t('%1$s shared your comment %2$s');
208 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
209 $msg = $this->l10n->t('%1$s shared your post %2$s');
211 case Post\UserNotification::TYPE_SHARED:
212 if (($causer['id'] != $author['id']) && ($title != '')) {
213 $msg = $this->l10n->t('%1$s shared the post %2$s from %3$s');
214 } elseif ($causer['id'] != $author['id']) {
215 $msg = $this->l10n->t('%1$s shared a post from %3$s');
216 } elseif ($title != '') {
217 $msg = $this->l10n->t('%1$s shared the post %2$s');
219 $msg = $this->l10n->t('%1$s shared a post');
224 case Activity::ATTEND:
225 switch ($Notification->type) {
226 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
227 $msg = $this->l10n->t('%1$s wants to attend your event %2$s');
231 case Activity::ATTENDNO:
232 switch ($Notification->type) {
233 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
234 $msg = $this->l10n->t('%1$s does not want to attend your event %2$s');
238 case Activity::ATTENDMAYBE:
239 switch ($Notification->type) {
240 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
241 $msg = $this->l10n->t('%1$s maybe wants to attend your event %2$s');
246 switch ($Notification->type) {
247 case Post\UserNotification::TYPE_EXPLICIT_TAGGED:
248 $msg = $this->l10n->t('%1$s tagged you on %2$s');
251 case Post\UserNotification::TYPE_IMPLICIT_TAGGED:
252 $msg = $this->l10n->t('%1$s replied to you on %2$s');
255 case Post\UserNotification::TYPE_THREAD_COMMENT:
256 $msg = $this->l10n->t('%1$s commented in your thread %2$s');
259 case Post\UserNotification::TYPE_DIRECT_COMMENT:
260 $msg = $this->l10n->t('%1$s commented on your comment %2$s');
263 case Post\UserNotification::TYPE_COMMENT_PARTICIPATION:
264 case Post\UserNotification::TYPE_ACTIVITY_PARTICIPATION:
265 if (($causer['id'] == $author['id']) && ($title != '')) {
266 $msg = $this->l10n->t('%1$s commented in their thread %2$s');
267 } elseif ($causer['id'] == $author['id']) {
268 $msg = $this->l10n->t('%1$s commented in their thread');
269 } elseif ($title != '') {
270 $msg = $this->l10n->t('%1$s commented in the thread %2$s from %3$s');
272 $msg = $this->l10n->t('%1$s commented in the thread from %3$s');
276 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
277 $msg = $this->l10n->t('%1$s commented on your thread %2$s');
280 case Post\UserNotification::TYPE_SHARED:
281 if (($causer['id'] != $author['id']) && ($title != '')) {
282 $msg = $this->l10n->t('%1$s shared the post %2$s from %3$s');
283 } elseif ($causer['id'] != $author['id']) {
284 $msg = $this->l10n->t('%1$s shared a post from %3$s');
285 } elseif ($title != '') {
286 $msg = $this->l10n->t('%1$s shared the post %2$s');
288 $msg = $this->l10n->t('%1$s shared a post');
297 // Name of the notification's causer
298 $message['causer'] = $causer['name'];
299 // Format for the "ping" mechanism
300 $message['notification'] = sprintf($msg, '{0}', $title, $author['name']);
301 // Plain text for the web push api
302 $message['plain'] = sprintf($msg, $causer['name'], $title, $author['name']);
303 // Rich text for other purposes
304 $message['rich'] = sprintf($msg,
305 '[url=' . $causer['url'] . ']' . $causer['name'] . '[/url]',
306 '[url=' . $link . ']' . $title . '[/url]',
307 '[url=' . $author['url'] . ']' . $author['name'] . '[/url]');
308 $message['link'] = $link;
310 $this->logger->debug('Unhandled notification', ['notification' => $Notification]);