[Favourite] Add backend support for favourite

This commit is contained in:
rainydaysavings 2020-09-04 15:18:58 +01:00 committed by Hugo Sales
parent b6fb0255da
commit 8a0418d8cf
No known key found for this signature in database
GPG Key ID: 7D0C7EAFC9D835A0
4 changed files with 87 additions and 10 deletions

View File

@ -0,0 +1,69 @@
<?php
// {{{ 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/>.
// }}}
namespace Plugin\Favourite;
use App\Core\DB\DB;
use App\Core\Event;
use App\Core\Form;
use function App\Core\I18n\_m;
use App\Core\Module;
use App\Entity\Favourite as Fave;
use App\Entity\Note;
use App\Util\Common;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Request;
class Favourite extends Module
{
public function onAddNoteActions(Request $request, Note $note, array &$actions)
{
$opts = ['note_id' => $note->getId(), 'gsactor_id' => Common::ensureLoggedIn()->getActor()->getId()];
$is_set = DB::find('favourite', $opts) != null;
$form = Form::create([
['is_set', HiddenType::class, ['data' => $is_set ? '1' : '0']],
['note_id', HiddenType::class, ['data' => $note->getId()]],
['favourite', SubmitType::class, ['label' => _m('Favourite')]],
]);
$form->handleRequest($request);
if ($form->isSubmitted()) {
$data = $form->getData();
var_dump($data);
$fave = DB::find('favourite', $opts);
if ($data['note_id'] == $note->getId() && $form->isValid()) {
// Loose comparison
if (!$data['is_set'] && ($fave == null)) {
DB::persist(Fave::create($opts));
DB::flush();
} else {
DB::remove($fave);
DB::flush();
}
} else {
// TODO display errors
}
}
$actions[] = $form->createView();
return Event::next;
}
}

View File

@ -49,6 +49,7 @@ class Extension extends AbstractExtension
/** Twig function to output the 'active' class if the current route matches the given route */
new TwigFunction('active', [Runtime::class, 'isCurrentRouteActive']),
new TwigFunction('is_route', [Runtime::class, 'isCurrentRoute']),
new TwigFunction('get_note_actions', [Runtime::class, 'getNoteActions']),
];
}
}

View File

@ -30,6 +30,8 @@
namespace App\Twig;
use App\Core\Event;
use App\Entity\Note;
use App\Util\Formatting;
use Functional as F;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
@ -56,6 +58,13 @@ class Runtime implements RuntimeExtensionInterface, EventSubscriberInterface
return F\some($routes, F\partial_left([Formatting::class, 'startsWith'], $current_route)) ? $class : '';
}
public function getNoteActions(Note $note)
{
$actions = [];
Event::handle('add_note_actions', [$this->request, $note, &$actions]);
return $actions;
}
// ----------------------------------------------------------
// Request is not a service, can't find a better way to get it

View File

@ -14,21 +14,19 @@
{% endfor %}
</div>
<div class="note-actions">
{% for act in get_note_actions(note) %}
<div>
<svg class="icon icon-heart">
<use xlink:href="#icon-heart"></use>
</svg>
{{ form(act) }}
</div>
{% endfor %}
<a href="{{ path('note_reply', {'reply_to': note.getId()}) }}">
<svg class="icon icon-reply">
<use xlink:href="#icon-reply"></use>
</svg>
</a>
<a href="{{ path('note_recycle', {'repeat_of': note.getId()}) }}">
<svg class="icon icon-recycle">
<use xlink:href="#icon-recycle"></use>
</svg>
</a>
<a href="{{ path('note_reply', {'reply_to': note.getId()}) }}">
<svg class="icon icon-heart">
<use xlink:href="#icon-heart"></use>
</svg>
</a>
</div>
<div class="replies">
{% for reply in note.getReplies() %}