diff --git a/app/Notification/Filter.php b/app/Notification/Filter.php index 7c3687b0c..aeed7c2db 100644 --- a/app/Notification/Filter.php +++ b/app/Notification/Filter.php @@ -2,6 +2,9 @@ namespace Gazelle\Notification; +use Gazelle\Manager\User as UserManager; +use Gazelle\User as User; + class Filter extends \Gazelle\Base { protected int $id; protected array $field = []; @@ -23,9 +26,9 @@ class Filter extends \Gazelle\Base { 'user' => 'Users', ]; - public function create(int $userId): int { + public function create(User $user): int { $set = ['UserID', 'Label']; - $args = [$userId, $this->field['label']]; + $args = [$user->id, $this->field['label']]; foreach ($this->fieldMap as $field => $column) { if (isset($this->field[$field])) { $set[] = $column; @@ -83,11 +86,10 @@ class Filter extends \Gazelle\Base { return $this; } - public function setUsers(\Gazelle\Manager\User $userMan, string $data): static { - $usernames = $this->multiLineSplit($data); - foreach ($usernames as $username) { + public function setUsers($data, UserManager $userMan = new UserManager()): static { + foreach ($this->multiLineSplit($data) as $username) { $user = $userMan->findByUsername($username); - if ($user && !$user->isParanoid('notifications')) { + if ($user instanceof User && !$user->isParanoid('notifications')) { $this->field['user'][] = $user->id; } } @@ -118,7 +120,7 @@ class Filter extends \Gazelle\Base { return null; } - public function modify(int $userId, int $filterId): int { + public function modify(User $user, int $filterId): int { $set = []; $args = []; foreach ($this->fieldMap as $field => $column) { @@ -130,7 +132,7 @@ class Filter extends \Gazelle\Base { $args[] = $this->arg((string)$field); } } - $args[] = $userId; + $args[] = $user->id; $args[] = $filterId; self::$db->prepared_query(" UPDATE users_notify_filters SET diff --git a/sections/user/notify_handle.php b/sections/user/notify_handle.php index db3550cff..d7dbdd549 100644 --- a/sections/user/notify_handle.php +++ b/sections/user/notify_handle.php @@ -12,17 +12,24 @@ if (!$Viewer->permitted('site_torrents_notify')) { authorize(); $releaseTypes = new ReleaseType()->list(); - -$formId = (int)$_POST['formid']; +$formId = (int)$_POST['formid']; +$tags = trim($_POST['tags' . $formId]); +$notTags = trim($_POST['nottags' . $formId]); +if (strlen($tags) > 500) { + Error400::error('Tag list cannot exceed 500 characters'); +} +if (strlen($notTags) > 500) { + Error400::error('"Not Tag" list cannot exceed 500 characters'); +} $filter = new Notification\Filter() ->setYears((int)$_POST['fromyear' . $formId], (int)$_POST['toyear' . $formId]) - ->setUsers(new Manager\User(), $_POST['users' . $formId]) + ->setUsers($_POST['users' . $formId]) ->setBoolean('exclude_va', isset($_POST['excludeva' . $formId])) ->setBoolean('new_groups_only', isset($_POST['newgroupsonly' . $formId])) ->setMultiLine('artist', $_POST['artists' . $formId]) - ->setMultiLine('tag', $_POST['tags' . $formId]) - ->setMultiLine('not_tag', $_POST['nottags' . $formId]) + ->setMultiLine('tag', $tags) + ->setMultiLine('not_tag', $notTags) ->setMultiLine('record_label', $_POST['recordlabel' . $formId]) ->setMultiValue('category', array_map(fn($id) => CATEGORY[$id], $_POST['categories' . $formId] ?? [])) ->setMultiValue('format', array_map(fn($id) => FORMAT[$id], $_POST['formats' . $formId] ?? [])) @@ -48,9 +55,9 @@ if ($error) { } if ($filterId) { - $filter->modify($Viewer->id(), $filterId); + $filter->modify($Viewer, $filterId); } else { - $filter->create($Viewer->id()); + $filter->create($Viewer); } $Cache->delete_multi(["u_notify_" . $Viewer->id(), "notify_artists_" . $Viewer->id()]); diff --git a/tests/phpunit/NotificationUploadTest.php b/tests/phpunit/NotificationUploadTest.php index 45f68a886..9ad4f54a0 100644 --- a/tests/phpunit/NotificationUploadTest.php +++ b/tests/phpunit/NotificationUploadTest.php @@ -97,7 +97,7 @@ class NotificationUploadTest extends TestCase { ->setLabel('Artists') ->setMultiLine('artist', $artistName); $this->assertTrue($artistFilter->isConfigured(), 'filter-artist-configured'); - $filter['artist'] = $artistFilter->create($this->userList['artist']->id()); + $filter['artist'] = $artistFilter->create($this->userList['artist']); $this->assertGreaterThan(0, $filter['artist'], 'filter-artist-created'); $nextFilter = $filter['artist'] + 1; @@ -108,7 +108,7 @@ class NotificationUploadTest extends TestCase { ->setMultiLine('artist', '') // TODO: INSERT fails on not null assertion ->setMultiValue('encoding', ['Lossless', '24bit Lossless']) ->setMultiValue('media', ['WEB', 'CD']) - ->create($this->userList['enc.med']->id()), + ->create($this->userList['enc.med']), 'filter-enc.med-created' ); @@ -118,7 +118,7 @@ class NotificationUploadTest extends TestCase { ->setLabel('No compilations') ->setMultiLine('artist', '') ->setBoolean('exclude_va', true) - ->create($this->userList['xva']->id()), + ->create($this->userList['xva']), 'filter-exclude-va-created' ); @@ -128,7 +128,7 @@ class NotificationUploadTest extends TestCase { ->setLabel('Release') ->setMultiLine('artist', '') ->setMultiLine('release_type', "Single") - ->create($this->userList['release']->id()), + ->create($this->userList['release']), 'filter-tag-created' ); @@ -138,7 +138,7 @@ class NotificationUploadTest extends TestCase { ->setLabel('Tags') ->setMultiLine('artist', '') ->setMultiLine('tag', "electronic\n\n") - ->create($this->userList['tag']->id()), + ->create($this->userList['tag']), 'filter-tag-created' ); @@ -148,7 +148,7 @@ class NotificationUploadTest extends TestCase { ->setLabel('Two Tags') ->setMultiLine('artist', '') ->setMultiLine('tag', "jazz\nelectronic\n") - ->create($this->userList['tag2yes']->id()), + ->create($this->userList['tag2yes']), 'filter-two-tag-created' ); @@ -158,7 +158,7 @@ class NotificationUploadTest extends TestCase { ->setLabel('Two Tags+') ->setMultiLine('artist', '') ->setMultiLine('tag', "jazz.rock\nhard.bop\n") - ->create($this->userList['tag2no']->id()), + ->create($this->userList['tag2no']), 'filter-two-tag-no-created' ); @@ -168,7 +168,7 @@ class NotificationUploadTest extends TestCase { ->setLabel('Not these tags') ->setMultiLine('artist', '') ->setMultiLine('not_tag', "funk\nfolk\n") - ->create($this->userList['tagno']->id()), + ->create($this->userList['tagno']), 'filter-not-tag-created' ); @@ -177,8 +177,8 @@ class NotificationUploadTest extends TestCase { $filter['user'] = new Notification\Filter() ->setLabel('Users') ->setMultiLine('artist', '') - ->setUsers(new Manager\User(), $this->torrent->uploader()->username()) - ->create($this->userList['user']->id()), + ->setUsers($this->torrent->uploader()->username()) + ->create($this->userList['user']), 'filter-users-created' ); @@ -188,7 +188,7 @@ class NotificationUploadTest extends TestCase { ->setLabel('Tags') ->setMultiLine('artist', '') ->setMultiLine('category', "Music") - ->create($this->torrent->uploaderId()), + ->create($this->torrent->uploader()), 'filter-self-created' ); @@ -198,7 +198,7 @@ class NotificationUploadTest extends TestCase { ->setLabel('Year') ->setMultiLine('artist', '') ->setYears(2020, 2022) - ->create($this->userList['year']->id()), + ->create($this->userList['year']), 'filter-year-created' ); @@ -210,9 +210,9 @@ class NotificationUploadTest extends TestCase { $this->assertEquals(NotificationTicketState::Pending, $ticket->state(), 'ntick-state'); // it should be pending, so make it active - $this->assertNull($ticketManager->findByExclusion(NotificationTicketState::Pending, exclude: [$this->torrent->id()]), 'ntick-pending-exclude'); + $this->assertNull($ticketManager->findByExclusion(NotificationTicketState::Pending, exclude: [$this->torrent->id]), 'ntick-pending-exclude'); $ticket = $ticketManager->findByExclusion(NotificationTicketState::Pending, exclude: []); - $this->assertEquals($this->torrent->id(), $ticket?->torrentId(), 'ntick-pending-torrent-id'); + $this->assertEquals($this->torrent->id, $ticket?->torrentId(), 'ntick-pending-torrent-id'); $ticket?->setActive(); $this->assertEquals($ticket?->state(), NotificationTicketState::Active, 'ntick-active-value'); $this->assertEquals(1, new Manager\Notification()->ticketStats()['active']['total'], 'notifier-ticket-stats-now-active'); @@ -271,21 +271,21 @@ class NotificationUploadTest extends TestCase { $this->assertEquals(0, $this->userList['release']->removeNotificationFilter($filter['tag']), 'user-remove-other-filter'); // look for an unread user notification - $notifier = new Notification\Torrent($this->userList['artist']->id()); + $notifier = new Notification\Torrent($this->userList['artist']->id); $this->assertEquals(1, $notifier->total(), 'notifier-artist-total'); $unreadList = $notifier->unreadList(1, 0); $this->assertCount(1, $unreadList, 'notifier-artist-unread-list'); - $this->assertEquals($this->torrent->id(), $unreadList[0]['torrentId'], 'notifier-artist-unread-torrent-id'); + $this->assertEquals($this->torrent->id, $unreadList[0]['torrentId'], 'notifier-artist-unread-torrent-id'); // catch notifications unset($notifier); - $notifier = new Notification\Torrent($this->userList['enc.med']->id()); + $notifier = new Notification\Torrent($this->userList['enc.med']->id); $this->assertEquals(1, $notifier->catchup(), 'notifier-encmed-catchup'); $this->assertCount(1, $notifier->unreadList(1, 0), 'notifier-encmed-unread'); // FIXME: it's actually read+unread // clear unread user notifications unset($notifier); - $notifier = new Notification\Torrent($this->userList['tag']->id()); + $notifier = new Notification\Torrent($this->userList['tag']->id); $this->assertEquals(1, $notifier->catchupFilter($filter['tag']), 'notifier-tag-catchup-filter'); $this->assertCount(1, $notifier->unreadList(1, 0), 'notifier-tag-catchup-unread'); $this->assertEquals(1, $notifier->clearFilter($filter['tag']), 'notifier-tag-clear-filter'); @@ -301,7 +301,7 @@ class NotificationUploadTest extends TestCase { ->setLabel('Record Labels') ->setMultiLine('artist', '') // TODO: INSERT fails on not null assertion ->setMultiLine('record.label', "Unitest Artists Corporation") - ->create($this->userList['record.label']->id()); + ->create($this->userList['record.label']); // create the ticket and pretend it is seeding $ticketManager = new Manager\NotificationTicket(); @@ -314,7 +314,7 @@ class NotificationUploadTest extends TestCase { // ticket has been handled unset($ticket); - $ticket = $ticketManager->findById($this->torrent->id()); + $ticket = $ticketManager->findById($this->torrent->id); $this->assertTrue($ticket?->isDone(), 'ntick-is-done'); } @@ -326,7 +326,7 @@ class NotificationUploadTest extends TestCase { ->setLabel('Backlog') ->setMultiLine('artist', '') // TODO: INSERT fails on not null assertion ->setMultiLine('record.label', "Unitest Artists Corporation") - ->create($this->userList['backlog']->id()); + ->create($this->userList['backlog']); $ticketManager = new Manager\NotificationTicket(); $ticket = $ticketManager->create($this->torrent); @@ -343,11 +343,11 @@ class NotificationUploadTest extends TestCase { // ticket has been handled unset($ticket); - $ticket = $ticketManager->findById($this->torrent->id()); + $ticket = $ticketManager->findById($this->torrent->id); $this->assertTrue($ticket?->isDone(), 'backlog-is-done'); $rss = new Feed()->byFeedName($this->userList['backlog'], 'torrents_music'); - $link = SITE_URL . "/torrents.php?id={$this->torrent->groupId()}&torrentid={$this->torrent->id()}&action=download&torrent_pass={$this->userList['backlog']->announceKey()}"; + $link = SITE_URL . "/torrents.php?id={$this->torrent->groupId()}&torrentid={$this->torrent->id}&action=download&torrent_pass={$this->userList['backlog']->announceKey()}"; $tags = implode(',', $this->torrent->group()->tagNameList()); $this->assertStringContainsString("$link", $rss, 'notif-rss-guid'); $this->assertStringContainsString("", $rss, 'notif-rss-tags'); @@ -362,7 +362,7 @@ class NotificationUploadTest extends TestCase { ->setLabel('Stale') ->setMultiLine('artist', '') // TODO: INSERT fails on not null assertion ->setMultiLine('record.label', "Unitest Artists Corporation") - ->create($this->userList['backlog']->id()); + ->create($this->userList['backlog']); $ticketManager = new Manager\NotificationTicket(); $ticket = $ticketManager->create($this->torrent); @@ -372,12 +372,12 @@ class NotificationUploadTest extends TestCase { } unset($ticket); - $ticket = $ticketManager->findById($this->torrent->id()); + $ticket = $ticketManager->findById($this->torrent->id); $this->assertTrue($ticket?->isPending(), 'ntick-stale-is-pending'); $manager->processBacklog($ticketManager, $this->torMan); unset($ticket); - $ticket = $ticketManager->findById($this->torrent->id()); + $ticket = $ticketManager->findById($this->torrent->id); $this->assertTrue($ticket?->isStale(), 'ntick-stale-is-stale'); } @@ -390,7 +390,7 @@ class NotificationUploadTest extends TestCase { ->setLabel('New Group') ->setMultiLine('artist', '') // TODO: INSERT fails on not null assertion ->setBoolean('new_groups_only', true) - ->create($this->userList['new.grp']->id()); + ->create($this->userList['new.grp']); $ticketManager = new Manager\NotificationTicket(); $ticket = $ticketManager->create($this->torrent); @@ -400,7 +400,7 @@ class NotificationUploadTest extends TestCase { $manager = new Manager\Notification(); $manager->handleTicket($ticket, $this->torMan); - $notifier = new Notification\Torrent($this->userList['new.grp']->id()); + $notifier = new Notification\Torrent($this->userList['new.grp']->id); $this->assertEquals(1, $notifier->total(), 'notifier-new-group-1-total'); $this->assertEquals(1, $notifier->catchupFilter($filter), 'notifier-new-group-catchup'); $this->assertEquals(1, $notifier->clearFilter($filter), 'notifier-new-group-clear'); @@ -429,7 +429,7 @@ class NotificationUploadTest extends TestCase { Helper::generateTorrentSeed($newTorrent, $newTorrent->uploader()); $manager->handleTicket($ticket, $this->torMan); - $notifier = new Notification\Torrent($this->userList['new.grp']->id()); + $notifier = new Notification\Torrent($this->userList['new.grp']->id); $this->assertEquals(0, $notifier->total(), 'notifier-no-new-group-2-total'); $stats = $manager->ticketStats(); @@ -451,7 +451,7 @@ class NotificationUploadTest extends TestCase { ->setLabel('Search') ->setMultiLine('release_type', "Compilation") ->setMultiLine('artist', '') - ->create($this->userList['search']->id()); + ->create($this->userList['search']); $torrent = $this->torMan->create( tgroup: $this->torrent->group(), @@ -490,10 +490,10 @@ class NotificationUploadTest extends TestCase { $item = current($page); $this->assertEquals($filter, $item['id'], 'notif-search-filter-id'); - $this->assertEquals($filter, $item['filter']->id(), 'notif-search-filter-id'); + $this->assertEquals($filter, $item['filter']->id, 'notif-search-filter-id'); $result = $item['result']; - $this->assertEquals($torrent->id(), $result[0]['torrent']->id(), 'notif-search-torrent-id'); + $this->assertEquals($torrent->id, $result[0]['torrent']->id, 'notif-search-torrent-id'); $this->assertEquals(1, $result[0]['unread'], 'notif-search-unread'); } }