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\Database\Database;
26 use Friendica\Model\Contact;
27 use Friendica\Model\Post;
28 use Friendica\Model\Verb;
29 use Friendica\Protocol\Activity;
30 use Psr\Log\LoggerInterface;
32 class Notification extends BaseFactory
37 private $mstdnAccountFactory;
39 private $mstdnStatusFactory;
41 public function __construct(LoggerInterface $logger, Database $dba, Account $mstdnAccountFactory, Status $mstdnStatusFactoryFactory)
43 parent::__construct($logger);
45 $this->mstdnAccountFactory = $mstdnAccountFactory;
46 $this->mstdnStatusFactory = $mstdnStatusFactoryFactory;
49 public function createFromNotificationId(int $id)
51 $notification = $this->dba->selectFirst('notification', [], ['id' => $id]);
52 if (!$this->dba->isResult($notification)) {
56 follow = Someone followed you
57 follow_request = Someone requested to follow you
58 mention = Someone mentioned you in their status
59 reblog = Someone boosted one of your statuses
60 favourite = Someone favourited one of your statuses
61 poll = A poll you have voted in or created has ended
62 status = Someone you enabled notifications for has posted a status
65 if (($notification['vid'] == Verb::getID(Activity::FOLLOW)) && ($notification['type'] == Post\UserNotification::NOTIF_NONE)) {
66 $contact = Contact::getById($notification['actor-id'], ['pending']);
67 $type = $contact['pending'] ? $type = 'follow_request' : 'follow';
68 } elseif (($notification['vid'] == Verb::getID(Activity::ANNOUNCE)) &&
69 in_array($notification['type'], [Post\UserNotification::NOTIF_DIRECT_COMMENT, Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT])) {
71 } elseif (in_array($notification['vid'], [Verb::getID(Activity::LIKE), Verb::getID(Activity::DISLIKE)]) &&
72 in_array($notification['type'], [Post\UserNotification::NOTIF_DIRECT_COMMENT, Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT])) {
74 } elseif ($notification['type'] == Post\UserNotification::NOTIF_SHARED) {
76 } elseif (in_array($notification['type'], [Post\UserNotification::NOTIF_EXPLICIT_TAGGED,
77 Post\UserNotification::NOTIF_IMPLICIT_TAGGED, Post\UserNotification::NOTIF_DIRECT_COMMENT,
78 Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT, Post\UserNotification::NOTIF_THREAD_COMMENT])) {
84 $account = $this->mstdnAccountFactory->createFromContactId($notification['actor-id'], $notification['uid']);
86 if (!empty($notification['target-uri-id'])) {
88 $status = $this->mstdnStatusFactory->createFromUriId($notification['target-uri-id'], $notification['uid']);
89 } catch (\Throwable $th) {
96 return new \Friendica\Object\Api\Mastodon\Notification($id, $type, $notification['created'], $account, $status);