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\Content\Text\Plaintext;
28 use Friendica\Core\L10n;
29 use Friendica\Model\Contact;
30 use Friendica\Model\Post;
31 use Friendica\Model\Verb;
32 use Friendica\Navigation\Notifications\Entity;
33 use Friendica\Protocol\Activity;
35 class Notification extends BaseFactory implements ICanCreateFromTableRow
37 public function createFromTableRow(array $row): Entity\Notification
39 return new Entity\Notification(
41 Verb::getByID($row['vid']),
44 $row['target-uri-id'],
45 $row['parent-uri-id'],
46 new \DateTime($row['created'], new \DateTimeZone('UTC')),
52 public function createForUser(int $uid, int $vid, int $type, int $actorId, int $targetUriId, int $parentUriId): Entity\Notification
54 return new Entity\Notification(
64 public function createForRelationship(int $uid, int $contactId, string $verb): Entity\Notification
66 return new Entity\Notification(
69 Post\UserNotification::TYPE_NONE,
75 * @param Entity\Notification $Notification
76 * @param BaseURL $baseUrl
77 * @param L10n $userL10n Seeded with the language of the user we mean the notification for
79 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
81 public function getMessageFromNotification(Entity\Notification $Notification, BaseURL $baseUrl, L10n $userL10n)
85 $causer = $author = Contact::getById($Notification->actorId, ['id', 'name', 'url', 'pending']);
87 $this->logger->info('Causer not found', ['contact' => $Notification->actorId]);
91 if ($Notification->type === Post\UserNotification::TYPE_NONE) {
92 if ($causer['pending']) {
93 $msg = $userL10n->t('%1$s wants to follow you');
95 $msg = $userL10n->t('%1$s had started following you');
97 $title = $causer['name'];
98 $link = $baseUrl . '/contact/' . $causer['id'];
100 if (!$Notification->targetUriId) {
104 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])) {
105 $item = Post::selectFirst([], ['uri-id' => $Notification->parentUriId, 'uid' => [0, $Notification->uid]], ['order' => ['uid' => true]]);
107 $this->logger->info('Parent post not found', ['uri-id' => $Notification->parentUriId]);
111 $item = Post::selectFirst([], ['uri-id' => $Notification->targetUriId, 'uid' => [0, $Notification->uid]], ['order' => ['uid' => true]]);
113 $this->logger->info('Post not found', ['uri-id' => $Notification->targetUriId]);
117 if ($Notification->verb == Activity::POST) {
118 $item = Post::selectFirst([], ['uri-id' => $item['thr-parent-id'], 'uid' => [0, $Notification->uid]], ['order' => ['uid' => true]]);
120 $this->logger->info('Thread parent post not found', ['uri-id' => $item['thr-parent-id']]);
126 if ($item['owner-id'] != $item['author-id']) {
127 $cid = $item['owner-id'];
129 if (!empty($item['causer-id']) && ($item['causer-id'] != $item['author-id'])) {
130 $cid = $item['causer-id'];
133 if (($Notification->type === Post\UserNotification::TYPE_SHARED) && !empty($cid)) {
134 $causer = Contact::getById($cid, ['id', 'name', 'url']);
135 if (empty($causer)) {
136 $this->logger->info('Causer not found', ['causer' => $cid]);
139 } elseif (in_array($Notification->type, [Post\UserNotification::TYPE_COMMENT_PARTICIPATION, Post\UserNotification::TYPE_ACTIVITY_PARTICIPATION])) {
140 $author = Contact::getById($item['author-id'], ['id', 'name', 'url']);
141 if (empty($author)) {
142 $this->logger->info('Author not found', ['author' => $item['author-id']]);
147 $link = $baseUrl . '/display/' . urlencode($item['guid']);
149 $content = Plaintext::getPost($item, 70);
150 if (!empty($content['text'])) {
151 $title = '"' . trim(str_replace("\n", " ", $content['text'])) . '"';
156 switch ($Notification->verb) {
158 switch ($Notification->type) {
159 case Post\UserNotification::TYPE_DIRECT_COMMENT:
160 $msg = $userL10n->t('%1$s liked your comment %2$s');
162 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
163 $msg = $userL10n->t('%1$s liked your post %2$s');
167 case Activity::DISLIKE:
168 switch ($Notification->type) {
169 case Post\UserNotification::TYPE_DIRECT_COMMENT:
170 $msg = $userL10n->t('%1$s disliked your comment %2$s');
172 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
173 $msg = $userL10n->t('%1$s disliked your post %2$s');
177 case Activity::ANNOUNCE:
178 switch ($Notification->type) {
179 case Post\UserNotification::TYPE_DIRECT_COMMENT:
180 $msg = $userL10n->t('%1$s shared your comment %2$s');
182 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
183 $msg = $userL10n->t('%1$s shared your post %2$s');
188 switch ($Notification->type) {
189 case Post\UserNotification::TYPE_EXPLICIT_TAGGED:
190 $msg = $userL10n->t('%1$s tagged you on %2$s');
193 case Post\UserNotification::TYPE_IMPLICIT_TAGGED:
194 $msg = $userL10n->t('%1$s replied to you on %2$s');
197 case Post\UserNotification::TYPE_THREAD_COMMENT:
198 $msg = $userL10n->t('%1$s commented in your thread %2$s');
201 case Post\UserNotification::TYPE_DIRECT_COMMENT:
202 $msg = $userL10n->t('%1$s commented on your comment %2$s');
205 case Post\UserNotification::TYPE_COMMENT_PARTICIPATION:
206 case Post\UserNotification::TYPE_ACTIVITY_PARTICIPATION:
207 if (($causer['id'] == $author['id']) && ($title != '')) {
208 $msg = $userL10n->t('%1$s commented in their thread %2$s');
209 } elseif ($causer['id'] == $author['id']) {
210 $msg = $userL10n->t('%1$s commented in their thread');
211 } elseif ($title != '') {
212 $msg = $userL10n->t('%1$s commented in the thread %2$s from %3$s');
214 $msg = $userL10n->t('%1$s commented in the thread from %3$s');
218 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
219 $msg = $userL10n->t('%1$s commented on your thread %2$s');
222 case Post\UserNotification::TYPE_SHARED:
223 if (($causer['id'] != $author['id']) && ($title != '')) {
224 $msg = $userL10n->t('%1$s shared the post %2$s from %3$s');
225 } elseif ($causer['id'] != $author['id']) {
226 $msg = $userL10n->t('%1$s shared a post from %3$s');
227 } elseif ($title != '') {
228 $msg = $userL10n->t('%1$s shared the post %2$s');
230 $msg = $userL10n->t('%1$s shared a post');
239 // Name of the notification's causer
240 $message['causer'] = $causer['name'];
241 // Format for the "ping" mechanism
242 $message['notification'] = sprintf($msg, '{0}', $title, $author['name']);
243 // Plain text for the web push api
244 $message['plain'] = sprintf($msg, $causer['name'], $title, $author['name']);
245 // Rich text for other purposes
246 $message['rich'] = sprintf($msg,
247 '[url=' . $causer['url'] . ']' . $causer['name'] . '[/url]',
248 '[url=' . $link . ']' . $title . '[/url]',
249 '[url=' . $author['url'] . ']' . $author['name'] . '[/url]');