| 
									
										
										
										
											2021-04-14 19:59:37 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-21 20:14:22 +01:00
										 |  |  | declare(strict_types=1); | 
					
						
							| 
									
										
										
										
											2021-10-10 09:26:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-14 19:59:37 +00:00
										 |  |  | // {{{ 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\Controller; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-21 20:14:22 +01:00
										 |  |  | use App\Core\Controller; | 
					
						
							| 
									
										
										
										
											2021-04-14 19:59:37 +00:00
										 |  |  | use App\Core\DB\DB; | 
					
						
							| 
									
										
										
										
											2021-04-15 00:57:29 +00:00
										 |  |  | use App\Core\Event; | 
					
						
							| 
									
										
										
										
											2021-10-21 20:14:22 +01:00
										 |  |  | use App\Core\Form; | 
					
						
							|  |  |  | use App\Util\Common; | 
					
						
							|  |  |  | use App\Util\Exception\InvalidFormException; | 
					
						
							|  |  |  | use App\Util\Exception\NoLoggedInUser; | 
					
						
							|  |  |  | use App\Util\Exception\NoSuchNoteException; | 
					
						
							|  |  |  | use App\Util\Exception\RedirectException; | 
					
						
							|  |  |  | use Plugin\Favourite\Entity\Favourite as FavouriteEntity; | 
					
						
							|  |  |  | use Symfony\Component\Form\Extension\Core\Type\SubmitType; | 
					
						
							| 
									
										
										
										
											2021-04-14 19:59:37 +00:00
										 |  |  | use Symfony\Component\HttpFoundation\Request; | 
					
						
							| 
									
										
										
										
											2021-10-21 20:14:22 +01:00
										 |  |  | use function App\Core\I18n\_m; | 
					
						
							| 
									
										
										
										
											2021-04-14 19:59:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-21 20:14:22 +01:00
										 |  |  | class Favourite extends Controller | 
					
						
							| 
									
										
										
										
											2021-04-14 19:59:37 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-10-21 20:14:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @throws RedirectException | 
					
						
							|  |  |  |      * @throws NoSuchNoteException | 
					
						
							|  |  |  |      * @throws InvalidFormException | 
					
						
							|  |  |  |      * @throws \App\Util\Exception\ServerException | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function noteAddFavourite(Request $request, int $id): bool|array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $user = Common::ensureLoggedIn(); | 
					
						
							|  |  |  |         $opts = ['id' => $id]; | 
					
						
							|  |  |  |         $add_favourite_note = DB::find('note', $opts); | 
					
						
							|  |  |  |         if (!$user || $add_favourite_note === null) { | 
					
						
							|  |  |  |             throw new NoSuchNoteException(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $form_add_to_favourite = Form::create([ | 
					
						
							|  |  |  |             ['add_favourite', SubmitType::class, | 
					
						
							|  |  |  |                 [ | 
					
						
							|  |  |  |                     'label' => _m('Favourite note!'), | 
					
						
							|  |  |  |                     'attr'  => [ | 
					
						
							|  |  |  |                         'title' => _m('Favourite this note!') | 
					
						
							|  |  |  |                     ], | 
					
						
							|  |  |  |                 ], | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $form_add_to_favourite->handleRequest($request); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($form_add_to_favourite->isSubmitted()) { | 
					
						
							|  |  |  |             $opts = ['note_id' => $id, 'actor_id' => $user->getId()]; | 
					
						
							|  |  |  |             DB::persist(FavouriteEntity::create($opts)); | 
					
						
							|  |  |  |             DB::flush(); | 
					
						
							|  |  |  |             // TODO: proper redirect from where the user came from
 | 
					
						
							|  |  |  |             throw new RedirectException(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return [ | 
					
						
							|  |  |  |             '_template'         => 'favourite/add_to_favourites.html.twig', | 
					
						
							|  |  |  |             'note'              => $add_favourite_note, | 
					
						
							|  |  |  |             'add_favourite'     => $form_add_to_favourite->createView(), | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @throws RedirectException | 
					
						
							|  |  |  |      * @throws NoSuchNoteException | 
					
						
							|  |  |  |      * @throws InvalidFormException | 
					
						
							|  |  |  |      * @throws \App\Util\Exception\ServerException | 
					
						
							|  |  |  |      * @throws NoLoggedInUser | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function noteRemoveFavourite(Request $request, int $id): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $user = Common::ensureLoggedIn(); | 
					
						
							|  |  |  |         $opts = ['note_id' => $id, 'actor_id' => $user->getId()]; | 
					
						
							|  |  |  |         $remove_favourite_note = DB::find('favourite', $opts); | 
					
						
							|  |  |  |         if (!$user || $remove_favourite_note === null) { | 
					
						
							|  |  |  |             throw new NoSuchNoteException(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $form_remove_favourite = Form::create([ | 
					
						
							|  |  |  |             ['remove_favourite', SubmitType::class, | 
					
						
							|  |  |  |                 [ | 
					
						
							|  |  |  |                     'label' => _m('Remove favourite'), | 
					
						
							|  |  |  |                     'attr'  => [ | 
					
						
							|  |  |  |                         'title' => _m('Remove note from favourites.') | 
					
						
							|  |  |  |                     ], | 
					
						
							|  |  |  |                 ], | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $form_remove_favourite->handleRequest($request); | 
					
						
							|  |  |  |         if ($form_remove_favourite->isSubmitted()) { | 
					
						
							|  |  |  |             DB::remove($remove_favourite_note); | 
					
						
							|  |  |  |             DB::flush(); | 
					
						
							|  |  |  |             // TODO: proper redirect from where the user came from
 | 
					
						
							|  |  |  |             throw new RedirectException(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $note = DB::find('note', ['id' => $id]); | 
					
						
							|  |  |  |         return [ | 
					
						
							|  |  |  |             '_template'         => 'favourite/remove_from_favourites.html.twig', | 
					
						
							|  |  |  |             'note'              => $note, | 
					
						
							|  |  |  |             'remove_favourite'  => $form_remove_favourite->createView(), | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-16 17:04:05 +01:00
										 |  |  |     public function favouritesByActorId(Request $request, int $id) | 
					
						
							| 
									
										
										
										
											2021-04-14 19:59:37 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-09-16 17:04:05 +01:00
										 |  |  |         $notes = DB::dql( | 
					
						
							| 
									
										
										
										
											2021-10-10 09:26:18 +01:00
										 |  |  |             'select n from App\Entity\Note n, Plugin\Favourite\Entity\Favourite f ' | 
					
						
							|  |  |  |             . 'where n.id = f.note_id ' | 
					
						
							|  |  |  |             . 'and f.actor_id = :id ' | 
					
						
							|  |  |  |             . 'order by f.created DESC', | 
					
						
							|  |  |  |             ['id' => $id], | 
					
						
							| 
									
										
										
										
											2021-09-16 17:04:05 +01:00
										 |  |  |         ); | 
					
						
							| 
									
										
										
										
											2021-04-14 19:59:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-06 19:49:03 +01:00
										 |  |  |         $notes_out = null; | 
					
						
							|  |  |  |         Event::handle('FormatNoteList', [$notes, &$notes_out]); | 
					
						
							| 
									
										
										
										
											2021-04-14 19:59:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return [ | 
					
						
							| 
									
										
										
										
											2021-10-21 20:14:22 +01:00
										 |  |  |             '_template' => 'network/feed.html.twig', | 
					
						
							|  |  |  |             'notes' => $notes_out, | 
					
						
							| 
									
										
										
										
											2021-10-10 09:26:18 +01:00
										 |  |  |             'page_title' => 'Favourites timeline.', | 
					
						
							| 
									
										
										
										
											2021-04-14 19:59:37 +00:00
										 |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-10-21 20:14:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-16 17:04:05 +01:00
										 |  |  |     public function favouritesByActorNickname(Request $request, string $nickname) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $user = DB::findOneBy('local_user', ['nickname' => $nickname]); | 
					
						
							|  |  |  |         return self::favouritesByActorId($request, $user->getId()); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-04-14 19:59:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      *  Reverse favourites stream | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array template | 
					
						
							| 
									
										
										
										
											2021-10-21 20:14:22 +01:00
										 |  |  |      * @throws NoLoggedInUser user not logged in | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-04-14 19:59:37 +00:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-10-10 09:26:18 +01:00
										 |  |  |     public function reverseFavouritesByActorId(Request $request, int $id): array | 
					
						
							| 
									
										
										
										
											2021-04-14 19:59:37 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-10-10 09:26:18 +01:00
										 |  |  |         $notes = DB::dql( | 
					
						
							|  |  |  |             'select n from App\Entity\Note n, Plugin\Favourite\Entity\Favourite f ' | 
					
						
							| 
									
										
										
										
											2021-10-21 20:14:22 +01:00
										 |  |  |             . 'where n.id = f.note_id ' | 
					
						
							|  |  |  |             . 'and f.actor_id != :id ' | 
					
						
							|  |  |  |             . 'and n.actor_id = :id ' | 
					
						
							|  |  |  |             . 'order by f.created DESC', | 
					
						
							| 
									
										
										
										
											2021-10-10 09:26:18 +01:00
										 |  |  |             ['id' => $id], | 
					
						
							| 
									
										
										
										
											2021-09-16 17:04:05 +01:00
										 |  |  |         ); | 
					
						
							| 
									
										
										
										
											2021-04-14 19:59:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-06 19:49:03 +01:00
										 |  |  |         $notes_out = null; | 
					
						
							| 
									
										
										
										
											2021-08-07 21:52:00 +00:00
										 |  |  |         Event::handle('FormatNoteList', [$notes, &$notes_out]); | 
					
						
							| 
									
										
										
										
											2021-04-14 19:59:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return [ | 
					
						
							| 
									
										
										
										
											2021-10-21 20:14:22 +01:00
										 |  |  |             '_template' => 'network/feed.html.twig', | 
					
						
							|  |  |  |             'notes' => $notes, | 
					
						
							| 
									
										
										
										
											2021-10-10 09:26:18 +01:00
										 |  |  |             'page_title' => 'Reverse favourites timeline.', | 
					
						
							| 
									
										
										
										
											2021-04-14 19:59:37 +00:00
										 |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-10-21 20:14:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-16 17:04:05 +01:00
										 |  |  |     public function reverseFavouritesByActorNickname(Request $request, string $nickname) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $user = DB::findOneBy('local_user', ['nickname' => $nickname]); | 
					
						
							|  |  |  |         return self::reverseFavouritesByActorId($request, $user->getId()); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-04-14 19:59:37 +00:00
										 |  |  | } |