[COMPONENT][Subscription] Fix Notifications

This commit is contained in:
Diogo Peralta Cordeiro 2022-01-06 11:26:48 +00:00
parent 0c245fcb6e
commit e056920de4
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
7 changed files with 40 additions and 19 deletions

View File

@ -31,7 +31,7 @@ use App\Entity\Actor;
use App\Util\Formatting;
use Component\Feed\Controller as C;
use Component\Search\Util\Parser;
use Component\Subscription\Entity\Subscription;
use Component\Subscription\Entity\ActorSubscription;
use Doctrine\Common\Collections\ExpressionBuilder;
use Doctrine\ORM\Query\Expr;
use Doctrine\ORM\QueryBuilder;
@ -82,7 +82,7 @@ class Feed extends Component
public function onSearchQueryAddJoins(QueryBuilder &$note_qb, QueryBuilder &$actor_qb): bool
{
$note_qb->leftJoin(Subscription::class, 'subscription', Expr\Join::WITH, 'note.actor_id = subscription.subscribed_id')
$note_qb->leftJoin(ActorSubscription::class, 'subscription', Expr\Join::WITH, 'note.actor_id = subscription.subscribed_id')
->leftJoin(Actor::class, 'note_actor', Expr\Join::WITH, 'note.actor_id = note_actor.id');
return Event::next;
}

View File

@ -39,7 +39,7 @@ use Component\Collection\Util\ActorControllerTrait;
use Component\Collection\Util\Controller\FeedController;
use Component\Group\Entity\GroupMember;
use Component\Group\Entity\LocalGroup;
use Component\Subscription\Entity\Subscription;
use Component\Subscription\Entity\ActorSubscription;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Request;
@ -92,7 +92,7 @@ class Group extends FeedController
'group_id' => $group->getId(),
'nickname' => $nickname,
]));
DB::persist(Subscription::create([
DB::persist(ActorSubscription::create([
'subscriber' => $group->getId(),
'subscribed' => $group->getId(),
]));
@ -116,7 +116,7 @@ class Group extends FeedController
} else {
if (!\is_null($actor)
&& \is_null(Cache::get(
Subscription::cacheKeys($actor, $group)['subscribed'],
ActorSubscription::cacheKeys($actor, $group)['subscribed'],
fn () => DB::findOneBy('subscription', [
'subscriber' => $actor->getId(),
'subscribed' => $group->getId(),
@ -126,14 +126,14 @@ class Group extends FeedController
$subscribe_form = Form::create([['subscribe', SubmitType::class, ['label' => _m('Subscribe to this group')]]]);
$subscribe_form->handleRequest($request);
if ($subscribe_form->isSubmitted() && $subscribe_form->isValid()) {
DB::persist(Subscription::create([
DB::persist(ActorSubscription::create([
'subscriber' => $actor->getId(),
'subscribed' => $group->getId(),
]));
DB::flush();
Cache::delete(E\Actor::cacheKeys($group->getId())['subscriber']);
Cache::delete(E\Actor::cacheKeys($actor->getId())['subscribed']);
Cache::delete(Subscription::cacheKeys($actor, $group)['subscribed']);
Cache::delete(ActorSubscription::cacheKeys($actor, $group)['subscribed']);
}
}
}

View File

@ -41,7 +41,7 @@ use DateTimeInterface;
* @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
class Subscription extends Entity
class ActorSubscription extends Entity
{
// {{{ Autocode
// @codeCoverageIgnoreStart
@ -114,10 +114,31 @@ class Subscription extends Entity
];
}
/**
* @see Entity->getNotificationTargetIds
*/
public function getNotificationTargetIds(array $ids_already_known = [], ?int $sender_id = null, bool $include_additional = true): array
{
if (!\array_key_exists('object', $ids_already_known)) {
$target_ids = [$this->getSubscribedId()]; // The object of any subscription is the one subscribed (or unsubscribed)
} else {
$target_ids = $ids_already_known['object'];
}
// Additional actors that should know about this
if ($include_additional && \array_key_exists('additional', $ids_already_known)) {
array_push($target_ids, ...$ids_already_known['additional']);
} else {
return $target_ids;
}
return array_unique($target_ids);
}
public static function schemaDef(): array
{
return [
'name' => 'subscription',
'name' => 'actor_subscription',
'fields' => [
'subscriber_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Actor.id', 'multiplicity' => 'one to one', 'name' => 'subscription_subscriber_fkey', 'not null' => true, 'description' => 'actor listening'],
'subscribed_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Actor.id', 'multiplicity' => 'one to one', 'name' => 'subscription_subscribed_fkey', 'not null' => true, 'description' => 'actor being listened to'],

View File

@ -97,10 +97,10 @@ class Subscription extends Component
'subscriber_id' => $subscriber_id,
'subscribed_id' => $subscribed_id,
];
$subscription = DB::findOneBy(table: Entity\Subscription::class, criteria: $opts, return_null: true);
$subscription = DB::findOneBy(table: Entity\ActorSubscription::class, criteria: $opts, return_null: true);
$activity = null;
if (\is_null($subscription)) {
DB::persist(Entity\Subscription::create($opts));
DB::persist(Entity\ActorSubscription::create($opts));
$activity = Activity::create([
'actor_id' => $subscriber_id,
'verb' => 'subscribe',
@ -144,7 +144,7 @@ class Subscription extends Component
'subscriber_id' => $subscriber_id,
'subscribed_id' => $subscribed_id,
];
$subscription = DB::findOneBy(table: Entity\Subscription::class, criteria: $opts, return_null: true);
$subscription = DB::findOneBy(table: Entity\ActorSubscription::class, criteria: $opts, return_null: true);
$activity = null;
if (!\is_null($subscription)) {
// Remove Subscription
@ -212,7 +212,7 @@ class Subscription extends Component
// If subject is not subbed to object already, then route it to add subscription
// Else, route to remove subscription
$subscribe_action_url = ($not_subscribed_already = \is_null(DB::findOneBy(table: Entity\Subscription::class, criteria: $opts, return_null: true))) ? Router::url(
$subscribe_action_url = ($not_subscribed_already = \is_null(DB::findOneBy(table: Entity\ActorSubscription::class, criteria: $opts, return_null: true))) ? Router::url(
'actor_subscribe_add',
[
'object_id' => $object_id,

View File

@ -30,7 +30,7 @@ use App\Util\Exception\ServerException;
use App\Util\Form\FormFields;
use App\Util\Nickname;
use Component\Language\Entity\ActorLanguage;
use Component\Subscription\Entity\Subscription;
use Component\Subscription\Entity\ActorSubscription;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use LogicException;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
@ -165,7 +165,7 @@ class Security extends Controller
$user,
function (int $id) use ($user) {
// Self subscription for the Home feed and alike
DB::persist(Subscription::create(['subscriber_id' => $id, 'subscribed_id' => $id]));
DB::persist(ActorSubscription::create(['subscriber_id' => $id, 'subscribed_id' => $id]));
Feed::createDefaultFeeds($id, $user);
DB::persist(ActorLanguage::create([
'actor_id' => $id,

View File

@ -12,7 +12,7 @@ use App\Entity\Note;
use Component\Group\Entity\GroupInbox;
use Component\Group\Entity\GroupMember;
use Component\Group\Entity\LocalGroup;
use Component\Subscription\Entity\Subscription;
use Component\Subscription\Entity\ActorSubscription;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
@ -38,7 +38,7 @@ class CoreFixtures extends Fixture
$local_entities[$nick] = $ent;
$manager->persist($ent);
// Add self subscriptions
$manager->persist(Subscription::create(['subscriber' => $actor->getId(), 'subscribed' => $actor->getId()]));
$manager->persist(ActorSubscription::create(['subscriber' => $actor->getId(), 'subscribed' => $actor->getId()]));
$actors[$nick] = $actor;
}

View File

@ -37,7 +37,7 @@ use App\Util\Nickname;
use Component\Avatar\Avatar;
use Component\Language\Entity\ActorLanguage;
use Component\Language\Entity\Language;
use Component\Subscription\Entity\Subscription;
use Component\Subscription\Entity\ActorSubscription;
use DateTimeInterface;
use Functional as F;
@ -334,7 +334,7 @@ class Actor extends Entity
{
return Cache::get(
self::cacheKeys($this->getId())[$which],
fn() => DB::count(Subscription::class, [$column => $this->getId()]) - ($this->getIsLocal() ? 1 : 0)
fn() => DB::count(ActorSubscription::class, [$column => $this->getId()]) - ($this->getIsLocal() ? 1 : 0)
);
}