3 * @copyright Copyright (C) 2010-2021, 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;
48 public function createFromNotification(Notifications\Entity\Notification $Notification): MstdnNotification
50 $type = self::getType($Notification);
52 throw new UnexpectedNotificationTypeException();
55 $account = $this->mstdnAccountFactory->createFromContactId($Notification->actorId, $Notification->uid);
57 if ($Notification->targetUriId) {
59 $status = $this->mstdnStatusFactory->createFromUriId($Notification->targetUriId, $Notification->uid);
60 } catch (\Throwable $th) {
67 return new MstdnNotification($Notification->id, $type, $Notification->created, $account, $status);
71 * Computes the Mastodon notification type from the given local notification
73 * @param Entity\Notification $Notification
77 public static function getType(Entity\Notification $Notification): string
79 if (($Notification->verb == Activity::FOLLOW) && ($Notification->type === Post\UserNotification::TYPE_NONE)) {
80 $contact = Contact::getById($Notification->actorId, ['pending']);
81 $type = $contact['pending'] ? MstdnNotification::TYPE_INTRODUCTION : MstdnNotification::TYPE_FOLLOW;
82 } elseif (($Notification->verb == Activity::ANNOUNCE) &&
83 in_array($Notification->type, [Post\UserNotification::TYPE_DIRECT_COMMENT, Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT])) {
84 $type = MstdnNotification::TYPE_RESHARE;
85 } elseif (in_array($Notification->verb, [Activity::LIKE, Activity::DISLIKE]) &&
86 in_array($Notification->type, [Post\UserNotification::TYPE_DIRECT_COMMENT, Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT])) {
87 $type = MstdnNotification::TYPE_LIKE;
88 } elseif ($Notification->type === Post\UserNotification::TYPE_SHARED) {
89 $type = MstdnNotification::TYPE_POST;
90 } elseif (in_array($Notification->type, [
91 Post\UserNotification::TYPE_EXPLICIT_TAGGED,
92 Post\UserNotification::TYPE_IMPLICIT_TAGGED,
93 Post\UserNotification::TYPE_DIRECT_COMMENT,
94 Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT,
95 Post\UserNotification::TYPE_THREAD_COMMENT
97 $type = MstdnNotification::TYPE_MENTION;