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\Factory\Api\Mastodon;
24 use Friendica\BaseFactory;
25 use Friendica\Model\Contact;
26 use Friendica\Navigation\Notifications;
27 use Friendica\Navigation\Notifications\Exception\UnexpectedNotificationTypeException;
28 use Friendica\Object\Api\Mastodon\Notification as MstdnNotification;
29 use Friendica\Protocol\Activity;
30 use Psr\Log\LoggerInterface;
31 use Friendica\Navigation\Notifications\Entity;
32 use Friendica\Model\Post;
34 class Notification extends BaseFactory
37 private $mstdnAccountFactory;
39 private $mstdnStatusFactory;
41 public function __construct(LoggerInterface $logger, Account $mstdnAccountFactory, Status $mstdnStatusFactoryFactory)
43 parent::__construct($logger);
44 $this->mstdnAccountFactory = $mstdnAccountFactory;
45 $this->mstdnStatusFactory = $mstdnStatusFactoryFactory;
49 * @param Notifications\Entity\Notification $Notification
51 * @return MstdnNotification
52 * @throws UnexpectedNotificationTypeException
54 public function createFromNotification(Notifications\Entity\Notification $Notification): MstdnNotification
56 $type = self::getType($Notification);
58 throw new UnexpectedNotificationTypeException();
61 $account = $this->mstdnAccountFactory->createFromContactId($Notification->actorId, $Notification->uid);
63 if ($Notification->targetUriId) {
65 $status = $this->mstdnStatusFactory->createFromUriId($Notification->targetUriId, $Notification->uid);
66 } catch (\Throwable $th) {
73 return new MstdnNotification($Notification->id, $type, $Notification->created, $account, $status, $Notification->dismissed);
77 * Computes the Mastodon notification type from the given local notification
79 * @param Entity\Notification $Notification
83 public static function getType(Entity\Notification $Notification): string
85 if (($Notification->verb == Activity::FOLLOW) && ($Notification->type === Post\UserNotification::TYPE_NONE)) {
86 $contact = Contact::getById($Notification->actorId, ['pending', 'uri-id', 'uid']);
87 if (($contact['uid'] == 0) && !empty($contact['uri-id'])) {
88 $contact = Contact::selectFirst(['pending'], ['uri-id' => $contact['uri-id'], 'uid' => $Notification->uid]);
91 if (!isset($contact['pending'])) {
95 $type = $contact['pending'] ? MstdnNotification::TYPE_INTRODUCTION : MstdnNotification::TYPE_FOLLOW;
96 } elseif (($Notification->verb == Activity::ANNOUNCE) &&
97 in_array($Notification->type, [Post\UserNotification::TYPE_DIRECT_COMMENT, Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT])) {
98 $type = MstdnNotification::TYPE_RESHARE;
99 } elseif (in_array($Notification->verb, [Activity::LIKE, Activity::DISLIKE]) &&
100 in_array($Notification->type, [Post\UserNotification::TYPE_DIRECT_COMMENT, Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT])) {
101 $type = MstdnNotification::TYPE_LIKE;
102 } elseif ($Notification->type === Post\UserNotification::TYPE_SHARED) {
103 $type = MstdnNotification::TYPE_POST;
104 } elseif (in_array($Notification->type, [
105 Post\UserNotification::TYPE_EXPLICIT_TAGGED,
106 Post\UserNotification::TYPE_IMPLICIT_TAGGED,
107 Post\UserNotification::TYPE_DIRECT_COMMENT,
108 Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT,
109 Post\UserNotification::TYPE_THREAD_COMMENT
111 $type = MstdnNotification::TYPE_MENTION;