[COMPONENT][Group] Add group subscribe button

This commit is contained in:
Hugo Sales 2021-12-27 18:21:19 +00:00
parent 559ec3df39
commit 5c10448080
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
2 changed files with 43 additions and 15 deletions

View File

@ -30,7 +30,6 @@ use App\Core\Form;
use function App\Core\I18n\_m;
use App\Core\Log;
use App\Core\UserRoles;
use App\Entity\Actor;
use App\Entity as E;
use App\Util\Common;
use App\Util\Exception\ClientException;
@ -61,16 +60,18 @@ class Group extends ActorController
public function groupViewNickname(Request $request, string $nickname)
{
Nickname::validate($nickname, which: Nickname::CHECK_LOCAL_GROUP); // throws
$group = LocalGroup::getActorByNickname($nickname);
$group = LocalGroup::getActorByNickname($nickname);
$actor = Common::actor();
$subscribe_form = null;
if (\is_null($group)) {
$actor = Common::actor();
if (!\is_null($actor)) {
$form = Form::create([
$create_form = Form::create([
['create', SubmitType::class, ['label' => _m('Create this group')]],
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$create_form->handleRequest($request);
if ($create_form->isSubmitted() && $create_form->isValid()) {
Log::info(
_m(
'Actor id:{actor_id} nick:{actor_nick} created the group {nickname}',
@ -78,9 +79,9 @@ class Group extends ActorController
),
);
DB::persist($group = Actor::create([
DB::persist($group = E\Actor::create([
'nickname' => $nickname,
'type' => Actor::GROUP,
'type' => E\Actor::GROUP,
'is_local' => true,
'roles' => UserRoles::BOT,
]));
@ -98,17 +99,40 @@ class Group extends ActorController
'is_admin' => true,
]));
DB::flush();
Cache::delete(Actor::cacheKeys($actor->getId())['subscriber']);
Cache::delete(Actor::cacheKeys($actor->getId())['subscribed']);
Cache::delete(E\Actor::cacheKeys($actor->getId())['subscriber']);
Cache::delete(E\Actor::cacheKeys($actor->getId())['subscribed']);
throw new RedirectException();
}
return [
'_template' => 'group/view.html.twig',
'nickname' => $nickname,
'create_form' => $form->createView(),
'create_form' => $create_form->createView(),
];
}
} else {
if (!\is_null($actor)
&& \is_null(Cache::get(
E\Subscription::cacheKeys($actor, $group)['subscribed'],
fn () => DB::findOneBy('subscription', [
'subscriber' => $actor->getId(),
'subscribed' => $group->getId(),
], return_null: true),
))
) {
$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(E\Subscription::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(E\Subscription::cacheKeys($actor, $group)['subscribed']);
}
}
}
$notes = !\is_null($group) ? DB::dql(
@ -123,10 +147,11 @@ class Group extends ActorController
) : [];
return [
'_template' => 'group/view.html.twig',
'actor' => $group,
'nickname' => $group?->getNickname() ?? $nickname,
'notes' => $notes,
'_template' => 'group/view.html.twig',
'actor' => $group,
'nickname' => $group?->getNickname() ?? $nickname,
'notes' => $notes,
'subscribe_form' => $subscribe_form?->createView(),
];
}

View File

@ -10,6 +10,9 @@
{% endblock stylesheets %}
{% block body %}
{% if subscribe_form is defined and subscribe_form is not null %}
{{ form(subscribe_form) }}
{% endif %}
{% if actor is defined and actor is not null %}
{% block profile_view %}