105 lines
3.8 KiB
PHP
105 lines
3.8 KiB
PHP
<?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;
|
|
use Friendica\Model\Contact;
|
|
use Friendica\Navigation\Notifications;
|
|
use Friendica\Navigation\Notifications\Exception\UnexpectedNotificationTypeException;
|
|
use Friendica\Object\Api\Mastodon\Notification as MstdnNotification;
|
|
use Friendica\Protocol\Activity;
|
|
use Psr\Log\LoggerInterface;
|
|
use Friendica\Navigation\Notifications\Entity;
|
|
use Friendica\Model\Post;
|
|
|
|
class Notification extends BaseFactory
|
|
{
|
|
/** @var Account */
|
|
private $mstdnAccountFactory;
|
|
/** @var Status */
|
|
private $mstdnStatusFactory;
|
|
|
|
public function __construct(LoggerInterface $logger, Account $mstdnAccountFactory, Status $mstdnStatusFactoryFactory)
|
|
{
|
|
parent::__construct($logger);
|
|
$this->mstdnAccountFactory = $mstdnAccountFactory;
|
|
$this->mstdnStatusFactory = $mstdnStatusFactoryFactory;
|
|
}
|
|
|
|
public function createFromNotification(Notifications\Entity\Notification $Notification): MstdnNotification
|
|
{
|
|
$type = self::getType($Notification);
|
|
if (empty($type)) {
|
|
throw new UnexpectedNotificationTypeException();
|
|
}
|
|
|
|
$account = $this->mstdnAccountFactory->createFromContactId($Notification->actorId, $Notification->uid);
|
|
|
|
if ($Notification->targetUriId) {
|
|
try {
|
|
$status = $this->mstdnStatusFactory->createFromUriId($Notification->targetUriId, $Notification->uid);
|
|
} catch (\Throwable $th) {
|
|
$status = null;
|
|
}
|
|
} else {
|
|
$status = null;
|
|
}
|
|
|
|
return new MstdnNotification($Notification->id, $type, $Notification->created, $account, $status);
|
|
}
|
|
|
|
/**
|
|
* Computes the Mastodon notification type from the given local notification
|
|
*
|
|
* @param Entity\Notification $Notification
|
|
* @return string
|
|
* @throws \Exception
|
|
*/
|
|
public static function getType(Entity\Notification $Notification): string
|
|
{
|
|
if (($Notification->verb == Activity::FOLLOW) && ($Notification->type === Post\UserNotification::TYPE_NONE)) {
|
|
$contact = Contact::getById($Notification->actorId, ['pending']);
|
|
$type = $contact['pending'] ? MstdnNotification::TYPE_INTRODUCTION : MstdnNotification::TYPE_FOLLOW;
|
|
} elseif (($Notification->verb == Activity::ANNOUNCE) &&
|
|
in_array($Notification->type, [Post\UserNotification::TYPE_DIRECT_COMMENT, Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT])) {
|
|
$type = MstdnNotification::TYPE_RESHARE;
|
|
} elseif (in_array($Notification->verb, [Activity::LIKE, Activity::DISLIKE]) &&
|
|
in_array($Notification->type, [Post\UserNotification::TYPE_DIRECT_COMMENT, Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT])) {
|
|
$type = MstdnNotification::TYPE_LIKE;
|
|
} elseif ($Notification->type === Post\UserNotification::TYPE_SHARED) {
|
|
$type = MstdnNotification::TYPE_POST;
|
|
} elseif (in_array($Notification->type, [
|
|
Post\UserNotification::TYPE_EXPLICIT_TAGGED,
|
|
Post\UserNotification::TYPE_IMPLICIT_TAGGED,
|
|
Post\UserNotification::TYPE_DIRECT_COMMENT,
|
|
Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT,
|
|
Post\UserNotification::TYPE_THREAD_COMMENT
|
|
])) {
|
|
$type = MstdnNotification::TYPE_MENTION;
|
|
} else {
|
|
return '';
|
|
}
|
|
|
|
return $type;
|
|
}
|
|
}
|