2022-01-02 20:37:15 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types = 1);
|
|
|
|
|
2022-01-03 00:57:37 +00:00
|
|
|
// {{{ License
|
|
|
|
|
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
|
|
//
|
|
|
|
// GNU social 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.
|
|
|
|
//
|
|
|
|
// GNU social 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 GNU social. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
// }}}
|
|
|
|
|
2022-01-02 20:37:15 +00:00
|
|
|
namespace Component\Subscription;
|
|
|
|
|
2022-01-05 17:39:10 +00:00
|
|
|
use App\Core\Cache;
|
2022-01-03 00:57:37 +00:00
|
|
|
use App\Core\DB\DB;
|
|
|
|
use App\Core\Event;
|
|
|
|
use function App\Core\I18n\_m;
|
2022-01-02 20:37:15 +00:00
|
|
|
use App\Core\Modules\Component;
|
2022-01-04 21:52:24 +00:00
|
|
|
use App\Core\Router\RouteLoader;
|
2022-01-05 17:39:10 +00:00
|
|
|
use App\Core\Router\Router;
|
2022-01-03 00:57:37 +00:00
|
|
|
use App\Entity\Activity;
|
|
|
|
use App\Entity\Actor;
|
|
|
|
use App\Entity\LocalUser;
|
2022-01-05 17:39:10 +00:00
|
|
|
use App\Util\Common;
|
|
|
|
use App\Util\Exception\DuplicateFoundException;
|
|
|
|
use App\Util\Exception\NotFoundException;
|
2022-01-03 00:57:37 +00:00
|
|
|
use App\Util\Exception\ServerException;
|
2022-03-13 18:23:19 +00:00
|
|
|
use Component\Notification\Entity\Attention;
|
2022-01-05 17:39:10 +00:00
|
|
|
use Component\Subscription\Controller\Subscribers as SubscribersController;
|
|
|
|
use Component\Subscription\Controller\Subscriptions as SubscriptionsController;
|
|
|
|
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2022-01-02 20:37:15 +00:00
|
|
|
|
|
|
|
class Subscription extends Component
|
|
|
|
{
|
2022-01-04 21:52:24 +00:00
|
|
|
public function onAddRoute(RouteLoader $r): bool
|
|
|
|
{
|
2022-01-05 17:39:10 +00:00
|
|
|
$r->connect(id: 'actor_subscribe_add', uri_path: '/actor/subscribe/{object_id<\d+>}', target: [SubscribersController::class, 'subscribersAdd']);
|
|
|
|
$r->connect(id: 'actor_subscribe_remove', uri_path: '/actor/unsubscribe/{object_id<\d+>}', target: [SubscribersController::class, 'subscribersRemove']);
|
|
|
|
$r->connect(id: 'actor_subscriptions_id', uri_path: '/actor/{id<\d+>}/subscriptions', target: [SubscriptionsController::class, 'subscriptionsByActorId']);
|
|
|
|
$r->connect(id: 'actor_subscribers_id', uri_path: '/actor/{id<\d+>}/subscribers', target: [SubscribersController::class, 'subscribersByActorId']);
|
2022-01-04 21:52:24 +00:00
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2022-01-03 00:57:37 +00:00
|
|
|
/**
|
2022-01-05 17:39:10 +00:00
|
|
|
* To use after Subscribe/Unsubscribe and DB::flush()
|
|
|
|
*
|
|
|
|
* @param Actor|int|LocalUser $subject The Actor who subscribed or unsubscribed
|
|
|
|
* @param Actor|int|LocalUser $object The Actor who was subscribed or unsubscribed from
|
|
|
|
*/
|
|
|
|
public static function refreshSubscriptionCount(int|Actor|LocalUser $subject, int|Actor|LocalUser $object): array
|
|
|
|
{
|
|
|
|
$subscriber_id = \is_int($subject) ? $subject : $subject->getId();
|
|
|
|
$subscribed_id = \is_int($object) ? $object : $object->getId();
|
|
|
|
|
|
|
|
$cache_subscriber = Cache::delete(Actor::cacheKeys($subscriber_id)['subscribed']);
|
|
|
|
$cache_subscribed = Cache::delete(Actor::cacheKeys($subscribed_id)['subscribers']);
|
|
|
|
|
2022-01-30 16:41:54 +00:00
|
|
|
return [$cache_subscriber, $cache_subscribed];
|
2022-01-05 17:39:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Persists a new Subscription Entity from Subject to Object (Actor being subscribed) and Activity
|
2022-01-03 00:57:37 +00:00
|
|
|
*
|
|
|
|
* A new notification is then handled, informing all interested Actors of this action
|
|
|
|
*
|
2022-01-05 17:39:10 +00:00
|
|
|
* @param Actor|int|LocalUser $subject The actor performing the subscription
|
|
|
|
* @param Actor|int|LocalUser $object The target of the subscription
|
|
|
|
*
|
|
|
|
* @throws DuplicateFoundException
|
|
|
|
* @throws NotFoundException
|
2022-01-03 00:57:37 +00:00
|
|
|
* @throws ServerException
|
2022-01-05 17:39:10 +00:00
|
|
|
*
|
|
|
|
* @return null|Activity a new Activity if changes were made
|
|
|
|
*
|
|
|
|
* @see self::refreshSubscriptionCount() to delete cache after this action
|
2022-01-03 00:57:37 +00:00
|
|
|
*/
|
2022-01-05 17:39:10 +00:00
|
|
|
public static function subscribe(int|Actor|LocalUser $subject, int|Actor|LocalUser $object, string $source = 'web'): ?Activity
|
2022-01-03 00:57:37 +00:00
|
|
|
{
|
2022-01-05 17:39:10 +00:00
|
|
|
$subscriber_id = \is_int($subject) ? $subject : $subject->getId();
|
|
|
|
$subscribed_id = \is_int($object) ? $object : $object->getId();
|
2022-01-03 00:57:37 +00:00
|
|
|
$opts = [
|
|
|
|
'subscriber_id' => $subscriber_id,
|
|
|
|
'subscribed_id' => $subscribed_id,
|
|
|
|
];
|
2022-01-06 11:26:48 +00:00
|
|
|
$subscription = DB::findOneBy(table: Entity\ActorSubscription::class, criteria: $opts, return_null: true);
|
2022-01-03 00:57:37 +00:00
|
|
|
$activity = null;
|
|
|
|
if (\is_null($subscription)) {
|
2022-03-13 18:23:19 +00:00
|
|
|
DB::persist($subscription = Entity\ActorSubscription::create($opts));
|
2022-01-03 00:57:37 +00:00
|
|
|
$activity = Activity::create([
|
|
|
|
'actor_id' => $subscriber_id,
|
|
|
|
'verb' => 'subscribe',
|
2022-03-13 18:23:19 +00:00
|
|
|
'object_type' => Actor::schemaName(),
|
2022-01-03 00:57:37 +00:00
|
|
|
'object_id' => $subscribed_id,
|
|
|
|
'source' => $source,
|
|
|
|
]);
|
|
|
|
DB::persist($activity);
|
2022-03-13 18:23:19 +00:00
|
|
|
DB::persist(Attention::create(['object_type' => Activity::schemaName(), 'object_id' => $activity->getId(), 'target_id' => $subscribed_id]));
|
2022-01-03 00:57:37 +00:00
|
|
|
|
|
|
|
Event::handle('NewNotification', [
|
2022-01-05 17:39:10 +00:00
|
|
|
\is_int($subject) ? $subject : Actor::getById($subscriber_id),
|
2022-01-03 00:57:37 +00:00
|
|
|
$activity,
|
2022-03-13 18:23:19 +00:00
|
|
|
[$subscribed_id],
|
2022-01-05 17:39:10 +00:00
|
|
|
_m('{subject} subscribed to {object}.', ['{subject}' => $activity->getActorId(), '{object}' => $activity->getObjectId()]),
|
2022-01-03 00:57:37 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
return $activity;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-01-05 17:39:10 +00:00
|
|
|
* Removes the Subscription Entity created beforehand, by the same Actor, and on the same object
|
2022-01-03 00:57:37 +00:00
|
|
|
*
|
|
|
|
* Informs all interested Actors of this action, handling out the NewNotification event
|
|
|
|
*
|
2022-01-05 17:39:10 +00:00
|
|
|
* @param Actor|int|LocalUser $subject The actor undoing the subscription
|
|
|
|
* @param Actor|int|LocalUser $object The target of the subscription
|
|
|
|
*
|
|
|
|
* @throws DuplicateFoundException
|
|
|
|
* @throws NotFoundException
|
2022-01-03 00:57:37 +00:00
|
|
|
* @throws ServerException
|
2022-01-05 17:39:10 +00:00
|
|
|
*
|
|
|
|
* @return null|Activity a new Activity if changes were made
|
|
|
|
*
|
|
|
|
* @see self::refreshSubscriptionCount() to delete cache after this action
|
2022-01-03 00:57:37 +00:00
|
|
|
*/
|
2022-01-05 17:39:10 +00:00
|
|
|
public static function unsubscribe(int|Actor|LocalUser $subject, int|Actor|LocalUser $object, string $source = 'web'): ?Activity
|
2022-01-03 00:57:37 +00:00
|
|
|
{
|
2022-01-05 17:39:10 +00:00
|
|
|
$subscriber_id = \is_int($subject) ? $subject : $subject->getId();
|
|
|
|
$subscribed_id = \is_int($object) ? $object : $object->getId();
|
2022-01-03 00:57:37 +00:00
|
|
|
$opts = [
|
|
|
|
'subscriber_id' => $subscriber_id,
|
|
|
|
'subscribed_id' => $subscribed_id,
|
|
|
|
];
|
2022-01-06 11:26:48 +00:00
|
|
|
$subscription = DB::findOneBy(table: Entity\ActorSubscription::class, criteria: $opts, return_null: true);
|
2022-01-03 00:57:37 +00:00
|
|
|
$activity = null;
|
|
|
|
if (!\is_null($subscription)) {
|
|
|
|
// Remove Subscription
|
|
|
|
DB::remove($subscription);
|
2022-03-13 18:23:19 +00:00
|
|
|
$previous_follow_activity = DB::findBy(Activity::class, ['verb' => 'subscribe', 'object_type' => Actor::schemaName(), 'object_id' => $subscribed_id], order_by: ['created' => 'DESC'])[0];
|
2022-01-03 00:57:37 +00:00
|
|
|
// Store Activity
|
|
|
|
$activity = Activity::create([
|
|
|
|
'actor_id' => $subscriber_id,
|
|
|
|
'verb' => 'undo',
|
2022-03-13 18:23:19 +00:00
|
|
|
'object_type' => Activity::schemaName(),
|
2022-01-03 00:57:37 +00:00
|
|
|
'object_id' => $previous_follow_activity->getId(),
|
|
|
|
'source' => $source,
|
|
|
|
]);
|
|
|
|
DB::persist($activity);
|
2022-01-05 17:39:10 +00:00
|
|
|
|
2022-03-13 18:23:19 +00:00
|
|
|
DB::persist(Attention::create(['object_type' => Activity::schemaName(), 'object_id' => $activity->getId(), 'target_id' => $subscribed_id]));
|
2022-01-05 17:39:10 +00:00
|
|
|
Event::handle('NewNotification', [
|
|
|
|
\is_int($subject) ? $subject : Actor::getById($subscriber_id),
|
|
|
|
$activity,
|
2022-03-13 18:23:19 +00:00
|
|
|
[],
|
2022-01-05 17:39:10 +00:00
|
|
|
_m('{subject} unsubscribed from {object}.', ['{subject}' => $activity->getActorId(), '{object}' => $previous_follow_activity->getObjectId()]),
|
|
|
|
]);
|
2022-01-03 00:57:37 +00:00
|
|
|
}
|
|
|
|
return $activity;
|
|
|
|
}
|
2022-01-05 17:39:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides ``\App\templates\cards\profile\view.html.twig`` an **additional action** to be performed **on the given
|
|
|
|
* Actor** (which the profile card of is currently being rendered).
|
|
|
|
*
|
|
|
|
* In the case of ``\App\Component\Subscription``, the action added allows a **LocalUser** to **subscribe** or
|
|
|
|
* **unsubscribe** a given **Actor**.
|
|
|
|
*
|
|
|
|
* @param Actor $object The Actor on which the action is to be performed
|
|
|
|
* @param array $actions An array containing all actions added to the
|
|
|
|
* current profile, this event adds an action to it
|
|
|
|
*
|
|
|
|
* @throws DuplicateFoundException
|
|
|
|
* @throws NotFoundException
|
|
|
|
* @throws ServerException
|
|
|
|
*
|
|
|
|
* @return bool EventHook
|
|
|
|
*/
|
|
|
|
public function onAddProfileActions(Request $request, Actor $object, array &$actions): bool
|
|
|
|
{
|
|
|
|
// Action requires a user to be logged in
|
|
|
|
// We know it's a LocalUser, which has the same id as Actor
|
|
|
|
// We don't want the Actor to unfollow itself
|
|
|
|
if ((\is_null($subject = Common::user())) || ($subject->getId() === $object->getId())) {
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's retrieve from here this subject came from to redirect it to previous location
|
|
|
|
$from = $request->query->has('from')
|
|
|
|
? $request->query->get('from')
|
|
|
|
: $request->getPathInfo();
|
|
|
|
|
|
|
|
// Who is the subject attempting to subscribe to?
|
|
|
|
$object_id = $object->getId();
|
|
|
|
|
|
|
|
// The id of both the subject and object
|
|
|
|
$opts = [
|
|
|
|
'subscriber_id' => $subject->getId(),
|
|
|
|
'subscribed_id' => $object_id,
|
|
|
|
];
|
|
|
|
|
|
|
|
// If subject is not subbed to object already, then route it to add subscription
|
|
|
|
// Else, route to remove subscription
|
2022-01-06 11:26:48 +00:00
|
|
|
$subscribe_action_url = ($not_subscribed_already = \is_null(DB::findOneBy(table: Entity\ActorSubscription::class, criteria: $opts, return_null: true))) ? Router::url(
|
2022-01-05 17:39:10 +00:00
|
|
|
'actor_subscribe_add',
|
|
|
|
[
|
|
|
|
'object_id' => $object_id,
|
|
|
|
'from' => $from . '#profile-' . $object_id,
|
|
|
|
],
|
|
|
|
Router::ABSOLUTE_PATH,
|
|
|
|
) : Router::url(
|
|
|
|
'actor_subscribe_remove',
|
|
|
|
[
|
|
|
|
'object_id' => $object_id,
|
|
|
|
'from' => $from . '#profile-' . $object_id,
|
|
|
|
],
|
|
|
|
Router::ABSOLUTE_PATH,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Finally, create an array with proper keys set accordingly
|
|
|
|
// to provide Profile Card template, the info it needs in order to render it properly
|
|
|
|
$action_extra_class = $not_subscribed_already ? 'add-actor-button-container' : 'remove-actor-button-container';
|
|
|
|
$title = $not_subscribed_already ? 'Subscribe ' . $object->getNickname() : 'Unsubscribe ' . $object->getNickname();
|
|
|
|
$subscribe_action = [
|
|
|
|
'url' => $subscribe_action_url,
|
|
|
|
'title' => _m($title),
|
|
|
|
'classes' => 'button-container note-actions-unset ' . $action_extra_class,
|
|
|
|
'id' => 'add-actor-button-container-' . $object_id,
|
|
|
|
];
|
|
|
|
|
|
|
|
$actions[] = $subscribe_action;
|
|
|
|
|
|
|
|
return Event::next;
|
|
|
|
}
|
2022-01-02 20:37:15 +00:00
|
|
|
}
|