[COMPONENT][Posting][Tag][Group][Conversation][RightPanel] Rename posting_form.to to posting_form.in, fill in with current group. Refactor context_actor

This commit is contained in:
Hugo Sales 2021-12-25 11:23:25 +00:00
parent 0f54d2121e
commit b604ee3146
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
5 changed files with 102 additions and 42 deletions

View File

@ -169,4 +169,15 @@ class Conversation extends Component
return Event::next; return Event::next;
} }
public function onPostingGetContextActor(Request $request, Actor $actor, ?Actor $context_actor)
{
$to_query = $request->get('actor_id');
if (!\is_null($to_query)) {
// Getting the actor itself
$context_actor = Actor::getById((int) $to_query);
return Event::stop;
}
return Event::next;
}
} }

View File

@ -72,4 +72,39 @@ class Group extends Component
} }
return Event::next; return Event::next;
} }
/**
* If in a group route, get the current group
*/
private function getGroupFromContext(Request $request): ?Actor
{
if (str_starts_with($request->get('_route'), 'group_actor_view_')) {
if (!\is_null($id = $request->get('id'))) {
return Actor::getById((int) $id);
} elseif (!\is_null($nickname = $request->get('nickname'))) {
return Actor::getByNickname($nickname, type: Actor::GROUP);
}
}
return null;
}
public function onPostingFillTargetChoices(Request $request, Actor $actor, array &$targets)
{
$group = $this->getGroupFromContext($request);
if (!\is_null($group)) {
$nick = '!' . $group->getNickname();
$targets[$nick] = $nick;
}
return Event::next;
}
public function onPostingGetContextActor(Request $request, Actor $actor, ?Actor $context_actor)
{
$ctx = $this->getGroupFromContext($request);
if (!\is_null($ctx)) {
$context_actor = $ctx;
return Event::stop;
}
return Event::next;
}
} }

View File

@ -23,7 +23,6 @@ declare(strict_types = 1);
namespace Component\Posting; namespace Component\Posting;
use App\Core\Cache;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Entity; use App\Core\Entity;
use App\Core\Event; use App\Core\Event;
@ -49,6 +48,7 @@ use Component\Attachment\Entity\ActorToAttachment;
use Component\Attachment\Entity\Attachment; use Component\Attachment\Entity\Attachment;
use Component\Attachment\Entity\AttachmentToNote; use Component\Attachment\Entity\AttachmentToNote;
use Component\Conversation\Conversation; use Component\Conversation\Conversation;
use Functional as F;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\FileType; use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\SubmitType;
@ -76,15 +76,6 @@ class Posting extends Component
$actor = $user->getActor(); $actor = $user->getActor();
$actor_id = $user->getId(); $actor_id = $user->getId();
$to_tags = [];
$tags = Cache::get(
"actor-circle-{$actor_id}",
fn () => DB::dql('select c.tag from App\Entity\ActorCircle c where c.tagger = :tagger', ['tagger' => $actor_id]),
);
foreach ($tags as $t) {
$t = $t['tag'];
$to_tags[$t] = $t;
}
$placeholder_strings = ['How are you feeling?', 'Have something to share?', 'How was your day?']; $placeholder_strings = ['How are you feeling?', 'Have something to share?', 'How was your day?'];
Event::handle('PostingPlaceHolderString', [&$placeholder_strings]); Event::handle('PostingPlaceHolderString', [&$placeholder_strings]);
@ -94,32 +85,25 @@ class Posting extends Component
Event::handle('PostingInitialContent', [&$initial_content]); Event::handle('PostingInitialContent', [&$initial_content]);
$available_content_types = [ $available_content_types = [
'Plain Text' => 'text/plain', _m('Plain Text') => 'text/plain',
]; ];
Event::handle('PostingAvailableContentTypes', [&$available_content_types]); Event::handle('PostingAvailableContentTypes', [&$available_content_types]);
// TODO: this needs work $in_targets = [];
// This is where we'd plug in the group in which the actor is posting, or whom they're replying to Event::handle('PostingFillTargetChoices', [$request, $actor, &$in_targets]);
// store local note needs to know what conversation it is
// Conversation adds the respective query string on route url, for groups it should be handled by an event
$to_query = $request->get('actor_id');
$context_actor = null;
// Actor is posting in a group? $context_actor = null;
if (!\is_null($to_query)) { Event::handle('PostingGetContextActor', [$request, $actor, &$context_actor]);
// Getting the actor itself
$context_actor = Actor::getById((int) $to_query); $form_params = [];
// Adding it to the to_tags array TODO: this is wrong if (!empty($in_targets)) {
$to_tags[] = $context_actor->getNickname(); $form_params[] = ['in', ChoiceType::class, ['label' => _m('In:'), 'multiple' => false, 'expanded' => false, 'choices' => $in_targets]];
} }
$form_params = [ $form_params[] = ['visibility', ChoiceType::class, ['label' => _m('Visibility:'), 'multiple' => false, 'expanded' => false, 'data' => 'public', 'choices' => [_m('Public') => 'public', _m('Instance') => 'instance', _m('Private') => 'private']]];
['to', ChoiceType::class, ['label' => _m('To:'), 'multiple' => false, 'expanded' => false, 'choices' => $to_tags]], $form_params[] = ['content', TextareaType::class, ['label' => _m('Content:'), 'data' => $initial_content, 'attr' => ['placeholder' => _m($placeholder)], 'constraints' => [new Length(['max' => Common::config('site', 'text_limit')])]]];
['visibility', ChoiceType::class, ['label' => _m('Visibility:'), 'multiple' => false, 'expanded' => false, 'data' => 'public', 'choices' => [_m('Public') => 'public', _m('Instance') => 'instance', _m('Private') => 'private']]], $form_params[] = ['attachments', FileType::class, ['label' => _m('Attachments:'), 'multiple' => true, 'required' => false, 'invalid_message' => _m('Attachment not valid.')]];
['content', TextareaType::class, ['label' => _m('Content:'), 'data' => $initial_content, 'attr' => ['placeholder' => _m($placeholder)], 'constraints' => [new Length(['max' => Common::config('site', 'text_limit')])]]], $form_params[] = FormFields::language($actor, $context_actor, label: _m('Note language:'), help: _m('The selected language will be federated and added as a lang attribute, preferred language can be set up in settings'));
['attachments', FileType::class, ['label' => _m('Attachments:'), 'multiple' => true, 'required' => false, 'invalid_message' => _m('Attachment not valid.')]],
FormFields::language($actor, $context_actor, label: _m('Note language:'), help: _m('The selected language will be federated and added as a lang attribute, preferred language can be set up in settings')),
];
if (\count($available_content_types) > 1) { if (\count($available_content_types) > 1) {
$form_params[] = ['content_type', ChoiceType::class, $form_params[] = ['content_type', ChoiceType::class,
@ -156,6 +140,7 @@ class Posting extends Component
$content_type, $content_type,
$data['language'], $data['language'],
$data['attachments'], $data['attachments'],
target: $data['in'] ?? null,
process_note_content_extra_args: $extra_args, process_note_content_extra_args: $extra_args,
); );
@ -193,6 +178,7 @@ class Posting extends Component
?string $language = null, ?string $language = null,
array $attachments = [], array $attachments = [],
array $processed_attachments = [], array $processed_attachments = [],
?string $target = null,
array $process_note_content_extra_args = [], array $process_note_content_extra_args = [],
) { ) {
$rendered = null; $rendered = null;
@ -253,18 +239,30 @@ class Posting extends Component
]); ]);
DB::persist($activity); DB::persist($activity);
$mentioned = []; if (!\is_null($target)) {
foreach ($mentions as $mention) { switch ($target[0]) {
foreach ($mention['mentioned'] as $m) { case '!':
if (!\is_null($m)) { $mentions[] = [
$mentioned[] = $m->getId(); 'mentioned' => [Actor::getByNickname(mb_substr($target, 1), type: Actor::GROUP)],
'type' => 'group',
'text' => mb_substr($target, 1),
];
break;
default:
throw new ClientException(_m('Unkown target type give in \'in\' field: ' . $target));
}
}
if ($m->isGroup()) { $mentioned = [];
DB::persist(GroupInbox::create([ foreach (F\unique(F\flat_map($mentions, fn (array $m) => $m['mentioned'] ?? []), fn (Actor $a) => $a->getId()) as $m) {
'group_id' => $m->getId(), if (!\is_null($m)) {
'activity_id' => $activity->getId(), $mentioned[] = $m->getId();
]));
} if ($m->isGroup()) {
DB::persist(GroupInbox::create([
'group_id' => $m->getId(),
'activity_id' => $activity->getId(),
]));
} }
} }
} }

View File

@ -22,7 +22,9 @@
<div class="section-form"> <div class="section-form">
<fieldset> <fieldset>
{{ form_start(blocks['post_form']) }} {{ form_start(blocks['post_form']) }}
{{ form_row(blocks['post_form'].to) }} {% if blocks['post_form'].in is defined %}
{{ form_row(blocks['post_form'].in) }}
{% endif %}
{{ form_row(blocks['post_form'].visibility) }} {{ form_row(blocks['post_form'].visibility) }}
{{ form_row(blocks['post_form'].content) }} {{ form_row(blocks['post_form'].content) }}
{{ form_row(blocks['post_form'].attachments) }} {{ form_row(blocks['post_form'].attachments) }}

View File

@ -207,4 +207,18 @@ class Tag extends Component
} }
return Event::next; return Event::next;
} }
public function onPostingFillTargetChoices(Request $request, Actor $actor, array &$targets)
{
$actor_id = $actor->getId();
$tags = Cache::get(
"actor-circle-{$actor_id}",
fn () => DB::dql('select c.tag from actor_circle c where c.tagger = :tagger', ['tagger' => $actor_id]),
);
foreach ($tags as $t) {
$t = '#' . $t['tag'];
$targets[$t] = $t;
}
return Event::next;
}
} }