2022-01-17 01:04:56 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types = 1);
|
|
|
|
|
|
|
|
// {{{ 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/>.
|
|
|
|
// }}}
|
|
|
|
/**
|
|
|
|
* WebMonetization for GNU social
|
|
|
|
*
|
|
|
|
* @package GNUsocial
|
|
|
|
* @category Plugin
|
|
|
|
*
|
|
|
|
* @author Phablulo <phablulo@gmail.com>
|
|
|
|
* @copyright 2018-2019, 2021 Free Software Foundation, Inc http://www.fsf.org
|
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Plugin\PinnedNotes;
|
|
|
|
|
2022-03-27 23:19:09 +09:00
|
|
|
use App\Core\DB;
|
2022-01-17 01:04:56 +09:00
|
|
|
use App\Core\Event;
|
|
|
|
use App\Core\Modules\Plugin;
|
2022-03-28 00:43:59 +09:00
|
|
|
use App\Core\Router;
|
2022-01-17 01:04:56 +09:00
|
|
|
use App\Entity\Actor;
|
|
|
|
use App\Entity\LocalUser;
|
|
|
|
use App\Entity\Note;
|
|
|
|
use App\Util\Common;
|
|
|
|
use App\Util\Formatting;
|
2022-01-17 10:55:18 +09:00
|
|
|
use App\Util\Nickname;
|
2022-01-17 01:04:56 +09:00
|
|
|
use Component\Collection\Collection;
|
|
|
|
use Doctrine\Common\Collections\ExpressionBuilder;
|
|
|
|
use Doctrine\ORM\Query\Expr;
|
|
|
|
|
|
|
|
use Doctrine\ORM\QueryBuilder;
|
2022-04-04 05:40:32 +09:00
|
|
|
use EventResult;
|
2022-01-17 01:04:56 +09:00
|
|
|
use Plugin\PinnedNotes\Controller as C;
|
|
|
|
|
2022-04-04 05:40:32 +09:00
|
|
|
use Plugin\PinnedNotes\Entity as E;
|
2022-01-17 01:04:56 +09:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
|
|
|
|
class PinnedNotes extends Plugin
|
|
|
|
{
|
2022-04-04 05:40:32 +09:00
|
|
|
public function onAddRoute(Router $r): EventResult
|
2022-01-17 01:04:56 +09:00
|
|
|
{
|
|
|
|
// Pin and unpin notes
|
|
|
|
$r->connect(id: 'toggle_note_pin', uri_path: '/object/note/{id<\d+>}/pin', target: [C\PinnedNotes::class, 'togglePin']);
|
2022-01-17 10:55:18 +09:00
|
|
|
// list of user pins, by id and nickname
|
|
|
|
// it's meant to be used by the ActivityPub plugin
|
|
|
|
$r->connect(
|
|
|
|
id: 'list_pinned_notes_by_id',
|
|
|
|
uri_path: '/actor/{id<\d+>}/pinned_notes',
|
|
|
|
target: [C\PinnedNotes::class, 'listPinsById'],
|
|
|
|
);
|
|
|
|
$r->connect(
|
|
|
|
id: 'list_pinned_notes_by_nickname',
|
|
|
|
uri_path: '/@{nickname<' . Nickname::DISPLAY_FMT . '>}/pinned_notes',
|
|
|
|
target: [C\PinnedNotes::class, 'listPinsByNickname'],
|
|
|
|
);
|
|
|
|
|
2022-01-17 01:04:56 +09:00
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2022-04-04 05:40:32 +09:00
|
|
|
public function onBeforeFeed(Request $request, &$res): EventResult
|
2022-01-17 01:04:56 +09:00
|
|
|
{
|
|
|
|
$path = $request->attributes->get('_route');
|
|
|
|
if ($path === 'actor_view_nickname') {
|
|
|
|
$actor = LocalUser::getByNickname($request->attributes->get('nickname'))->getActor();
|
|
|
|
} elseif ($path === 'actor_view_id') {
|
|
|
|
$actor = DB::findOneBy(Actor::class, ['id' => $request->attributes->get('id')]);
|
|
|
|
} else {
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
|
|
|
$locale = Common::currentLanguage()->getLocale();
|
|
|
|
$notes = Collection::query('pinned:true actor:' . $actor->getId(), 1, $locale, $actor);
|
|
|
|
|
2022-03-07 23:08:07 +09:00
|
|
|
$res[] = Formatting::twigRenderFile('PinnedNotes/notes.html.twig', ['pinned_notes' => $notes['notes']]);
|
2022-01-17 01:04:56 +09:00
|
|
|
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2022-04-04 05:40:32 +09:00
|
|
|
public function onCollectionQueryAddJoins(QueryBuilder &$note_qb, QueryBuilder &$actor_qb): EventResult
|
2022-01-17 01:04:56 +09:00
|
|
|
{
|
2022-03-07 23:08:07 +09:00
|
|
|
if (!\in_array('pinned_notes', $note_qb->getAllAliases())) {
|
|
|
|
$note_qb->leftJoin(E\PinnedNotes::class, 'pinned_notes', Expr\Join::WITH, 'note.id = pinned_notes.note_id');
|
|
|
|
}
|
2022-01-17 01:04:56 +09:00
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2022-04-04 05:40:32 +09:00
|
|
|
public function onCollectionQueryCreateExpression(ExpressionBuilder $eb, string $term, ?string $language, ?Actor $actor, &$note_expr, &$actor_expr): EventResult
|
2022-01-17 01:04:56 +09:00
|
|
|
{
|
|
|
|
if ($term === 'pinned:true') {
|
2022-03-07 23:08:07 +09:00
|
|
|
$note_expr = $eb->neq('pinned_notes', null);
|
2022-01-17 01:04:56 +09:00
|
|
|
return Event::stop;
|
|
|
|
}
|
|
|
|
if (str_starts_with($term, 'actor:')) {
|
|
|
|
$actor_id = (int) mb_substr($term, 6);
|
|
|
|
$actor_expr = $eb->eq('actor.id', $actor_id);
|
|
|
|
return Event::stop;
|
|
|
|
}
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2022-04-04 05:40:32 +09:00
|
|
|
public function onAddNoteActions(Request $request, Note $note, array &$actions): EventResult
|
2022-01-17 01:04:56 +09:00
|
|
|
{
|
|
|
|
$user = Common::user();
|
|
|
|
if ($user->getId() !== $note->getActorId()) {
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
|
|
|
$opts = ['note_id' => $note->getId(), 'actor_id' => $user->getId()];
|
|
|
|
$is_pinned = !\is_null(DB::findOneBy(E\PinnedNotes::class, $opts, return_null: true));
|
|
|
|
|
|
|
|
$router_args = ['id' => $note->getId()];
|
|
|
|
$router_type = Router::ABSOLUTE_PATH;
|
|
|
|
$action_url = Router::url('toggle_note_pin', $router_args, $router_type);
|
|
|
|
$action_url .= '?from=' . urlencode($request->getRequestUri()); // so we can go back
|
|
|
|
|
|
|
|
$actions[] = [
|
|
|
|
'url' => $action_url,
|
|
|
|
'title' => ($is_pinned ? 'Unpin' : 'Pin') . ' this note',
|
|
|
|
'classes' => 'button-container pin-button-container ' . ($is_pinned ? 'pinned' : ''),
|
|
|
|
'id' => 'pin-button-container-' . $note->getId(),
|
|
|
|
];
|
|
|
|
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2022-04-04 05:40:32 +09:00
|
|
|
public function onEndShowStyles(array &$styles, string $route): EventResult
|
2022-01-17 01:04:56 +09:00
|
|
|
{
|
|
|
|
$styles[] = 'plugins/PinnedNotes/assets/css/pinned-notes.css';
|
|
|
|
return Event::next;
|
|
|
|
}
|
2022-01-17 02:56:53 +09:00
|
|
|
|
|
|
|
// Activity Pub handling stuff
|
2022-02-24 02:39:11 +09:00
|
|
|
|
2022-04-04 05:40:32 +09:00
|
|
|
public function onActivityStreamsTwoContext(array &$activity_streams_two_context): EventResult
|
2022-02-24 02:39:11 +09:00
|
|
|
{
|
|
|
|
$activity_streams_two_context[] = ['toot' => 'http://joinmastodon.org/ns#'];
|
|
|
|
$activity_streams_two_context[] = [
|
|
|
|
'featured' => [
|
|
|
|
'@id' => 'toot:featured',
|
|
|
|
'@type' => '@id',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2022-04-04 05:40:32 +09:00
|
|
|
public function onActivityPubAddActivityStreamsTwoData(string $type_name, &$type): EventResult
|
2022-01-17 02:56:53 +09:00
|
|
|
{
|
2022-02-24 02:39:11 +09:00
|
|
|
if ($type_name === 'Person') {
|
2022-02-25 10:05:28 +09:00
|
|
|
$actor = \Plugin\ActivityPub\Util\Explorer::getOneFromUri($type->get('id'));
|
2022-01-17 10:55:18 +09:00
|
|
|
$router_args = ['id' => $actor->getId()];
|
|
|
|
$router_type = Router::ABSOLUTE_URL;
|
|
|
|
$action_url = Router::url('list_pinned_notes_by_id', $router_args, $router_type);
|
|
|
|
|
|
|
|
$type->set('featured', $action_url);
|
2022-01-17 02:56:53 +09:00
|
|
|
}
|
|
|
|
return Event::next;
|
|
|
|
}
|
2022-01-17 01:04:56 +09:00
|
|
|
}
|