2021-05-09 07:50:05 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (C) 2010-2021, the Friendica project
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Friendica\Factory\Api\Mastodon;
|
|
|
|
|
|
|
|
use Friendica\BaseFactory;
|
2021-06-05 16:36:45 -04:00
|
|
|
use Friendica\Database\Database;
|
2021-06-01 17:13:16 -04:00
|
|
|
use Friendica\Model\Contact;
|
2021-06-01 01:51:03 -04:00
|
|
|
use Friendica\Model\Post;
|
|
|
|
use Friendica\Model\Verb;
|
|
|
|
use Friendica\Protocol\Activity;
|
2021-06-05 16:36:45 -04:00
|
|
|
use Psr\Log\LoggerInterface;
|
2021-05-09 07:50:05 -04:00
|
|
|
|
|
|
|
class Notification extends BaseFactory
|
|
|
|
{
|
2021-06-05 16:36:45 -04:00
|
|
|
/** @var Database */
|
|
|
|
private $dba;
|
|
|
|
/** @var Account */
|
|
|
|
private $mstdnAccountFactory;
|
|
|
|
/** @var Status */
|
|
|
|
private $mstdnStatusFactory;
|
2021-06-08 18:09:32 -04:00
|
|
|
|
2021-06-05 16:36:45 -04:00
|
|
|
public function __construct(LoggerInterface $logger, Database $dba, Account $mstdnAccountFactory, Status $mstdnStatusFactoryFactory)
|
|
|
|
{
|
|
|
|
parent::__construct($logger);
|
2021-06-08 18:09:32 -04:00
|
|
|
$this->dba = $dba;
|
2021-06-05 16:36:45 -04:00
|
|
|
$this->mstdnAccountFactory = $mstdnAccountFactory;
|
2021-06-08 18:09:32 -04:00
|
|
|
$this->mstdnStatusFactory = $mstdnStatusFactoryFactory;
|
2021-06-05 16:36:45 -04:00
|
|
|
}
|
|
|
|
|
2021-06-01 10:23:12 -04:00
|
|
|
public function createFromNotificationId(int $id)
|
2021-05-09 07:50:05 -04:00
|
|
|
{
|
2021-06-05 16:36:45 -04:00
|
|
|
$notification = $this->dba->selectFirst('notification', [], ['id' => $id]);
|
|
|
|
if (!$this->dba->isResult($notification)) {
|
2021-05-09 07:50:05 -04:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
follow = Someone followed you
|
|
|
|
follow_request = Someone requested to follow you
|
|
|
|
mention = Someone mentioned you in their status
|
|
|
|
reblog = Someone boosted one of your statuses
|
|
|
|
favourite = Someone favourited one of your statuses
|
|
|
|
poll = A poll you have voted in or created has ended
|
|
|
|
status = Someone you enabled notifications for has posted a status
|
|
|
|
*/
|
|
|
|
|
2021-06-01 17:13:16 -04:00
|
|
|
if (($notification['vid'] == Verb::getID(Activity::FOLLOW)) && ($notification['type'] == Post\UserNotification::NOTIF_NONE)) {
|
|
|
|
$contact = Contact::getById($notification['actor-id'], ['pending']);
|
2021-06-08 18:09:32 -04:00
|
|
|
$type = $contact['pending'] ? $type = 'follow_request' : 'follow';
|
2021-06-01 17:13:16 -04:00
|
|
|
} elseif (($notification['vid'] == Verb::getID(Activity::ANNOUNCE)) &&
|
2021-06-01 01:51:03 -04:00
|
|
|
in_array($notification['type'], [Post\UserNotification::NOTIF_DIRECT_COMMENT, Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT])) {
|
|
|
|
$type = 'reblog';
|
|
|
|
} elseif (in_array($notification['vid'], [Verb::getID(Activity::LIKE), Verb::getID(Activity::DISLIKE)]) &&
|
|
|
|
in_array($notification['type'], [Post\UserNotification::NOTIF_DIRECT_COMMENT, Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT])) {
|
|
|
|
$type = 'favourite';
|
|
|
|
} elseif ($notification['type'] == Post\UserNotification::NOTIF_SHARED) {
|
|
|
|
$type = 'status';
|
|
|
|
} elseif (in_array($notification['type'], [Post\UserNotification::NOTIF_EXPLICIT_TAGGED,
|
|
|
|
Post\UserNotification::NOTIF_IMPLICIT_TAGGED, Post\UserNotification::NOTIF_DIRECT_COMMENT,
|
|
|
|
Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT, Post\UserNotification::NOTIF_THREAD_COMMENT])) {
|
|
|
|
$type = 'mention';
|
|
|
|
} else {
|
|
|
|
return null;
|
2021-05-09 07:50:05 -04:00
|
|
|
}
|
|
|
|
|
2021-06-05 16:36:45 -04:00
|
|
|
$account = $this->mstdnAccountFactory->createFromContactId($notification['actor-id'], $notification['uid']);
|
2021-05-09 07:50:05 -04:00
|
|
|
|
2021-06-01 01:51:03 -04:00
|
|
|
if (!empty($notification['target-uri-id'])) {
|
2021-05-09 07:50:05 -04:00
|
|
|
try {
|
2021-06-05 16:36:45 -04:00
|
|
|
$status = $this->mstdnStatusFactory->createFromUriId($notification['target-uri-id'], $notification['uid']);
|
2021-05-09 07:50:05 -04:00
|
|
|
} catch (\Throwable $th) {
|
|
|
|
$status = null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$status = null;
|
|
|
|
}
|
|
|
|
|
2021-06-01 01:51:03 -04:00
|
|
|
return new \Friendica\Object\Api\Mastodon\Notification($id, $type, $notification['created'], $account, $status);
|
2021-05-09 07:50:05 -04:00
|
|
|
}
|
|
|
|
}
|