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', 'contact-type', '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) || ($Notification->type === Post\UserNotification::TYPE_SHARED)) {
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 (in_array($Notification->type, [Post\UserNotification::TYPE_COMMENT_PARTICIPATION, Post\UserNotification::TYPE_ACTIVITY_PARTICIPATION, Post\UserNotification::TYPE_SHARED])) {
127 $author = Contact::getById($item['author-id'], ['id', 'name', 'url', 'contact-type']);
128 if (empty($author)) {
129 $this->logger->info('Author not found', ['author' => $item['author-id']]);
134 $link = $baseUrl . '/display/' . urlencode($item['guid']);
136 $content = Plaintext::getPost($item, 70);
137 if (!empty($content['text'])) {
138 $title = '"' . trim(str_replace("\n", " ", $content['text'])) . '"';
143 $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]);
145 switch ($Notification->verb) {
147 switch ($Notification->type) {
148 case Post\UserNotification::TYPE_DIRECT_COMMENT:
149 $msg = $userL10n->t('%1$s liked your comment %2$s');
151 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
152 $msg = $userL10n->t('%1$s liked your post %2$s');
156 case Activity::DISLIKE:
157 switch ($Notification->type) {
158 case Post\UserNotification::TYPE_DIRECT_COMMENT:
159 $msg = $userL10n->t('%1$s disliked your comment %2$s');
161 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
162 $msg = $userL10n->t('%1$s disliked your post %2$s');
166 case Activity::ANNOUNCE:
167 switch ($Notification->type) {
168 case Post\UserNotification::TYPE_DIRECT_COMMENT:
169 $msg = $userL10n->t('%1$s shared your comment %2$s');
171 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
172 $msg = $userL10n->t('%1$s shared your post %2$s');
174 case Post\UserNotification::TYPE_SHARED:
175 if (($causer['id'] != $author['id']) && ($title != '')) {
176 $msg = $userL10n->t('%1$s shared the post %2$s from %3$s');
177 } elseif ($causer['id'] != $author['id']) {
178 $msg = $userL10n->t('%1$s shared a post from %3$s');
179 } elseif ($title != '') {
180 $msg = $userL10n->t('%1$s shared the post %2$s');
182 $msg = $userL10n->t('%1$s shared a post');
187 case Activity::ATTEND:
188 switch ($Notification->type) {
189 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
190 $msg = $userL10n->t('%1$s wants to attend your event %2$s');
194 case Activity::ATTENDNO:
195 switch ($Notification->type) {
196 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
197 $msg = $userL10n->t('%1$s does not want to attend your event %2$s');
201 case Activity::ATTENDMAYBE:
202 switch ($Notification->type) {
203 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
204 $msg = $userL10n->t('%1$s maybe wants to attend your event %2$s');
209 switch ($Notification->type) {
210 case Post\UserNotification::TYPE_EXPLICIT_TAGGED:
211 $msg = $userL10n->t('%1$s tagged you on %2$s');
214 case Post\UserNotification::TYPE_IMPLICIT_TAGGED:
215 $msg = $userL10n->t('%1$s replied to you on %2$s');
218 case Post\UserNotification::TYPE_THREAD_COMMENT:
219 $msg = $userL10n->t('%1$s commented in your thread %2$s');
222 case Post\UserNotification::TYPE_DIRECT_COMMENT:
223 $msg = $userL10n->t('%1$s commented on your comment %2$s');
226 case Post\UserNotification::TYPE_COMMENT_PARTICIPATION:
227 case Post\UserNotification::TYPE_ACTIVITY_PARTICIPATION:
228 if (($causer['id'] == $author['id']) && ($title != '')) {
229 $msg = $userL10n->t('%1$s commented in their thread %2$s');
230 } elseif ($causer['id'] == $author['id']) {
231 $msg = $userL10n->t('%1$s commented in their thread');
232 } elseif ($title != '') {
233 $msg = $userL10n->t('%1$s commented in the thread %2$s from %3$s');
235 $msg = $userL10n->t('%1$s commented in the thread from %3$s');
239 case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
240 $msg = $userL10n->t('%1$s commented on your thread %2$s');
243 case Post\UserNotification::TYPE_SHARED:
244 if (($causer['id'] != $author['id']) && ($title != '')) {
245 $msg = $userL10n->t('%1$s shared the post %2$s from %3$s');
246 } elseif ($causer['id'] != $author['id']) {
247 $msg = $userL10n->t('%1$s shared a post from %3$s');
248 } elseif ($title != '') {
249 $msg = $userL10n->t('%1$s shared the post %2$s');
251 $msg = $userL10n->t('%1$s shared a post');
260 // Name of the notification's causer
261 $message['causer'] = $causer['name'];
262 // Format for the "ping" mechanism
263 $message['notification'] = sprintf($msg, '{0}', $title, $author['name']);
264 // Plain text for the web push api
265 $message['plain'] = sprintf($msg, $causer['name'], $title, $author['name']);
266 // Rich text for other purposes
267 $message['rich'] = sprintf($msg,
268 '[url=' . $causer['url'] . ']' . $causer['name'] . '[/url]',
269 '[url=' . $link . ']' . $title . '[/url]',
270 '[url=' . $author['url'] . ']' . $author['name'] . '[/url]');