2021-12-24 09:38:06 +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/>.
|
|
|
|
// }}}
|
|
|
|
/**
|
|
|
|
* Attachments Albums for GNU social
|
|
|
|
*
|
|
|
|
* @package GNUsocial
|
|
|
|
* @category Plugin
|
2021-12-26 02:31:16 +09:00
|
|
|
*
|
2021-12-24 09:38:06 +09:00
|
|
|
* @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\AttachmentCollections;
|
|
|
|
|
2022-03-27 23:19:09 +09:00
|
|
|
use App\Core\DB;
|
2021-12-26 02:31:16 +09:00
|
|
|
use App\Core\Event;
|
2022-01-03 05:04:52 +09:00
|
|
|
use function App\Core\I18n\_m;
|
2022-01-05 06:58:49 +09:00
|
|
|
use App\Core\Modules\Plugin;
|
2022-03-28 00:43:59 +09:00
|
|
|
use App\Core\Router;
|
2021-12-30 09:51:12 +09:00
|
|
|
use App\Entity\Actor;
|
2021-12-26 02:31:16 +09:00
|
|
|
use App\Entity\Feed;
|
|
|
|
use App\Entity\LocalUser;
|
|
|
|
use App\Util\Nickname;
|
2022-01-05 06:58:49 +09:00
|
|
|
use Component\Collection\Util\MetaCollectionTrait;
|
2022-01-03 03:40:09 +09:00
|
|
|
use Plugin\AttachmentCollections\Controller\AttachmentCollections as AttachmentCollectionsController;
|
2021-12-30 09:51:12 +09:00
|
|
|
use Plugin\AttachmentCollections\Entity\AttachmentCollection;
|
2021-12-28 13:39:09 +09:00
|
|
|
use Plugin\AttachmentCollections\Entity\AttachmentCollectionEntry;
|
2021-12-26 02:31:16 +09:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2021-12-24 09:38:06 +09:00
|
|
|
|
2022-01-05 06:58:49 +09:00
|
|
|
class AttachmentCollections extends Plugin
|
2021-12-24 09:38:06 +09:00
|
|
|
{
|
2022-01-05 06:58:49 +09:00
|
|
|
use MetaCollectionTrait;
|
2022-02-26 23:09:41 +09:00
|
|
|
protected const SLUG = 'collection';
|
|
|
|
protected const PLURAL_SLUG = 'collections';
|
2021-12-30 09:51:12 +09:00
|
|
|
protected function createCollection(Actor $owner, array $vars, string $name)
|
|
|
|
{
|
|
|
|
$col = AttachmentCollection::create([
|
|
|
|
'name' => $name,
|
|
|
|
'actor_id' => $owner->getId(),
|
|
|
|
]);
|
|
|
|
DB::persist($col);
|
|
|
|
DB::persist(AttachmentCollectionEntry::create([
|
2022-02-06 01:15:34 +09:00
|
|
|
'attachment_id' => $vars['vars']['attachment_id'],
|
|
|
|
'note_id' => $vars['vars']['note_id'],
|
|
|
|
'attachment_collection_id' => $col->getId(),
|
2021-12-30 09:51:12 +09:00
|
|
|
]));
|
|
|
|
}
|
2022-01-07 21:23:37 +09:00
|
|
|
protected function removeItem(Actor $owner, array $vars, array $items, array $collections)
|
2021-12-30 09:51:12 +09:00
|
|
|
{
|
|
|
|
return DB::dql(<<<'EOF'
|
|
|
|
DELETE FROM \Plugin\AttachmentCollections\Entity\AttachmentCollectionEntry AS entry
|
|
|
|
WHERE entry.attachment_id = :attach_id AND entry.note_id = :note_id
|
2022-02-06 01:15:34 +09:00
|
|
|
AND entry.attachment_collection_id IN (
|
2021-12-30 09:51:12 +09:00
|
|
|
SELECT album.id FROM \Plugin\AttachmentCollections\Entity\AttachmentCollection AS album
|
|
|
|
WHERE album.actor_id = :user_id
|
|
|
|
AND album.id IN (:ids)
|
|
|
|
)
|
|
|
|
EOF, [
|
|
|
|
'attach_id' => $vars['vars']['attachment_id'],
|
|
|
|
'note_id' => $vars['vars']['note_id'],
|
|
|
|
'user_id' => $owner->getId(),
|
|
|
|
'ids' => $items,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2022-01-07 21:23:37 +09:00
|
|
|
protected function addItem(Actor $owner, array $vars, array $items, array $collections)
|
2021-12-30 09:51:12 +09:00
|
|
|
{
|
|
|
|
foreach ($items as $id) {
|
|
|
|
// prevent user from putting something in a collection (s)he doesn't own:
|
|
|
|
if (\in_array($id, $collections)) {
|
|
|
|
DB::persist(AttachmentCollectionEntry::create([
|
2022-02-06 01:15:34 +09:00
|
|
|
'attachment_id' => $vars['vars']['attachment_id'],
|
|
|
|
'note_id' => $vars['vars']['note_id'],
|
|
|
|
'attachment_collection_id' => $id,
|
2021-12-30 09:51:12 +09:00
|
|
|
]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function shouldAddToRightPanel(Actor $user, $vars, Request $request): bool
|
|
|
|
{
|
|
|
|
return $vars['path'] === 'note_attachment_show';
|
|
|
|
}
|
2022-02-06 01:15:34 +09:00
|
|
|
|
2021-12-30 09:51:12 +09:00
|
|
|
protected function getCollectionsBy(Actor $owner, ?array $vars = null, bool $ids_only = false): array
|
|
|
|
{
|
|
|
|
if (\is_null($vars)) {
|
|
|
|
$res = DB::findBy(AttachmentCollection::class, ['actor_id' => $owner->getId()]);
|
|
|
|
} else {
|
|
|
|
$res = DB::dql(
|
2022-02-07 06:37:04 +09:00
|
|
|
'select e.attachment_collection_id from attachment_collection_entry e '
|
|
|
|
. 'inner join attachment_collection as a '
|
|
|
|
. 'with a.id = e.attachment_collection_id '
|
|
|
|
. 'where e.attachment_id = :attachment_id '
|
|
|
|
. 'and e.note_id = :note_id '
|
|
|
|
. 'and a.actor_id = :actor_id',
|
|
|
|
['actor_id' => $owner->getId(), 'note_id' => $vars['vars']['note_id'], 'attachment_id' => $vars['vars']['attachment_id']],
|
2021-12-30 09:51:12 +09:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!$ids_only) {
|
|
|
|
return $res;
|
|
|
|
}
|
2022-02-06 01:15:34 +09:00
|
|
|
return array_map(fn ($x) => $x['attachment_collection_id'], $res);
|
2021-12-30 09:51:12 +09:00
|
|
|
}
|
|
|
|
|
2022-03-28 00:43:59 +09:00
|
|
|
public function onAddRoute(Router $r): bool
|
2021-12-24 09:38:06 +09:00
|
|
|
{
|
|
|
|
// View all collections by actor id and nickname
|
|
|
|
$r->connect(
|
2021-12-26 02:31:16 +09:00
|
|
|
id: 'collections_view_by_actor_id',
|
|
|
|
uri_path: '/actor/{id<\d+>}/collections',
|
2022-01-03 05:04:52 +09:00
|
|
|
target: [AttachmentCollectionsController::class, 'collectionsViewByActorId'],
|
2021-12-24 09:38:06 +09:00
|
|
|
);
|
|
|
|
$r->connect(
|
2021-12-26 02:31:16 +09:00
|
|
|
id: 'collections_view_by_nickname',
|
|
|
|
uri_path: '/@{nickname<' . Nickname::DISPLAY_FMT . '>}/collections',
|
2022-01-03 05:04:52 +09:00
|
|
|
target: [AttachmentCollectionsController::class, 'collectionsViewByActorNickname'],
|
2021-12-24 09:38:06 +09:00
|
|
|
);
|
|
|
|
// View notes from a collection by actor id and nickname
|
|
|
|
$r->connect(
|
2021-12-26 02:31:16 +09:00
|
|
|
id: 'collection_notes_view_by_actor_id',
|
|
|
|
uri_path: '/actor/{id<\d+>}/collections/{cid<\d+>}',
|
2022-01-03 03:40:09 +09:00
|
|
|
target: [AttachmentCollectionsController::class, 'collectionsEntryViewNotesByActorId'],
|
2021-12-24 09:38:06 +09:00
|
|
|
);
|
|
|
|
$r->connect(
|
2021-12-26 02:31:16 +09:00
|
|
|
id: 'collection_notes_view_by_nickname',
|
|
|
|
uri_path: '/@{nickname<' . Nickname::DISPLAY_FMT . '>}/collections/{cid<\d+>}',
|
2022-01-03 03:40:09 +09:00
|
|
|
target: [AttachmentCollectionsController::class, 'collectionsEntryViewNotesByNickname'],
|
2021-12-24 09:38:06 +09:00
|
|
|
);
|
|
|
|
return Event::next;
|
|
|
|
}
|
2021-12-28 13:39:09 +09:00
|
|
|
|
2021-12-24 09:38:06 +09:00
|
|
|
public function onCreateDefaultFeeds(int $actor_id, LocalUser $user, int &$ordering)
|
|
|
|
{
|
|
|
|
DB::persist(Feed::create([
|
|
|
|
'actor_id' => $actor_id,
|
2021-12-26 02:31:16 +09:00
|
|
|
'url' => Router::url($route = 'collections_view_by_nickname', ['nickname' => $user->getNickname()]),
|
|
|
|
'route' => $route,
|
|
|
|
'title' => _m('Attachment Collections'),
|
2021-12-24 09:38:06 +09:00
|
|
|
'ordering' => $ordering++,
|
|
|
|
]));
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
}
|