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 (($Notification->type === Post\UserNotification::TYPE_SHARED) && ($Notification->verb == Activity::ANNOUNCE))) {
119 $item = Post::selectFirst([], ['uri-id' => $item['thr-parent-id'], 'uid' => [0, $Notification->uid]], ['order' => ['uid' => true]]);
121 $this->logger->info('Thread parent post not found', ['uri-id' => $item['thr-parent-id']]);
127 if ($Notification->type === Post\UserNotification::TYPE_SHARED) {
128 $author = Contact::getById($item['author-id'], ['id', 'name', 'url']);
129 if (empty($author)) {
130 $this->logger->info('Author not found', ['author' => $item['author-id']]);
133 } elseif (in_array($Notification->type, [Post\UserNotification::TYPE_COMMENT_PARTICIPATION, Post\UserNotification::TYPE_ACTIVITY_PARTICIPATION])) {
134 $author = Contact::getById($item['author-id'], ['id', 'name', 'url']);
135 if (empty($author)) {
136 $this->logger->info('Author not found', ['author' => $item['author-id']]);
141 $link = $baseUrl . '/display/' . urlencode($item['guid']);
143 $content = Plaintext::getPost($item, 70);
144 if (!empty($content['text'])) {
145 $title = '"' . trim(str_replace("\n", " ", $content['text'])) . '"';
150 $this->logger->debug('Got verb and type', ['verb' => $Notification->verb, 'type' => $Notification->type]);
152 switch ($Notification->verb) {
154 switch ($Notification->type) {
155 case Post\UserNotification::TYPE_DIRECT_COMMENT:
156 $msg = $userL10n->t('%1$s liked your comment %2$s');
158 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
159 $msg = $userL10n->t('%1$s liked your post %2$s');
163 case Activity::DISLIKE:
164 switch ($Notification->type) {
165 case Post\UserNotification::TYPE_DIRECT_COMMENT:
166 $msg = $userL10n->t('%1$s disliked your comment %2$s');
168 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
169 $msg = $userL10n->t('%1$s disliked your post %2$s');
173 case Activity::ANNOUNCE:
174 switch ($Notification->type) {
175 case Post\UserNotification::TYPE_DIRECT_COMMENT:
176 $msg = $userL10n->t('%1$s shared your comment %2$s');
178 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
179 $msg = $userL10n->t('%1$s shared your post %2$s');
181 case Post\UserNotification::TYPE_SHARED:
182 if (($causer['id'] != $author['id']) && ($title != '')) {
183 $msg = $userL10n->t('%1$s shared the post %2$s from %3$s');
184 } elseif ($causer['id'] != $author['id']) {
185 $msg = $userL10n->t('%1$s shared a post from %3$s');
186 } elseif ($title != '') {
187 $msg = $userL10n->t('%1$s shared the post %2$s');
189 $msg = $userL10n->t('%1$s shared a post');
195 switch ($Notification->type) {
196 case Post\UserNotification::TYPE_EXPLICIT_TAGGED:
197 $msg = $userL10n->t('%1$s tagged you on %2$s');
200 case Post\UserNotification::TYPE_IMPLICIT_TAGGED:
201 $msg = $userL10n->t('%1$s replied to you on %2$s');
204 case Post\UserNotification::TYPE_THREAD_COMMENT:
205 $msg = $userL10n->t('%1$s commented in your thread %2$s');
208 case Post\UserNotification::TYPE_DIRECT_COMMENT:
209 $msg = $userL10n->t('%1$s commented on your comment %2$s');
212 case Post\UserNotification::TYPE_COMMENT_PARTICIPATION:
213 case Post\UserNotification::TYPE_ACTIVITY_PARTICIPATION:
214 if (($causer['id'] == $author['id']) && ($title != '')) {
215 $msg = $userL10n->t('%1$s commented in their thread %2$s');
216 } elseif ($causer['id'] == $author['id']) {
217 $msg = $userL10n->t('%1$s commented in their thread');
218 } elseif ($title != '') {
219 $msg = $userL10n->t('%1$s commented in the thread %2$s from %3$s');
221 $msg = $userL10n->t('%1$s commented in the thread from %3$s');
225 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
226 $msg = $userL10n->t('%1$s commented on your thread %2$s');
234 // Name of the notification's causer
235 $message['causer'] = $causer['name'];
236 // Format for the "ping" mechanism
237 $message['notification'] = sprintf($msg, '{0}', $title, $author['name']);
238 // Plain text for the web push api
239 $message['plain'] = sprintf($msg, $causer['name'], $title, $author['name']);
240 // Rich text for other purposes
241 $message['rich'] = sprintf($msg,
242 '[url=' . $causer['url'] . ']' . $causer['name'] . '[/url]',
243 '[url=' . $link . ']' . $title . '[/url]',
244 '[url=' . $author['url'] . ']' . $author['name'] . '[/url]');