]> BookStack Code Mirror - bookstack/blob - app/App/HomeController.php
Dockerfile: Don't cache 50MB of lists and use a single layer, make it pretty
[bookstack] / app / App / HomeController.php
1 <?php
2
3 namespace BookStack\App;
4
5 use BookStack\Activity\ActivityQueries;
6 use BookStack\Entities\Models\Page;
7 use BookStack\Entities\Queries\EntityQueries;
8 use BookStack\Entities\Queries\QueryRecentlyViewed;
9 use BookStack\Entities\Queries\QueryTopFavourites;
10 use BookStack\Entities\Tools\PageContent;
11 use BookStack\Http\Controller;
12 use BookStack\Uploads\FaviconHandler;
13 use BookStack\Util\SimpleListOptions;
14 use Illuminate\Http\Request;
15
16 class HomeController extends Controller
17 {
18     public function __construct(
19         protected EntityQueries $queries,
20     ) {
21     }
22
23     /**
24      * Display the homepage.
25      */
26     public function index(
27         Request $request,
28         ActivityQueries $activities,
29         QueryRecentlyViewed $recentlyViewed,
30         QueryTopFavourites $topFavourites,
31     ) {
32         $activity = $activities->latest(10);
33         $draftPages = [];
34
35         if ($this->isSignedIn()) {
36             $draftPages = $this->queries->pages->currentUserDraftsForList()
37                 ->orderBy('updated_at', 'desc')
38                 ->with('book')
39                 ->take(6)
40                 ->get();
41         }
42
43         $recentFactor = count($draftPages) > 0 ? 0.5 : 1;
44         $recents = $this->isSignedIn() ?
45             $recentlyViewed->run(12 * $recentFactor, 1)
46             : $this->queries->books->visibleForList()->orderBy('created_at', 'desc')->take(12 * $recentFactor)->get();
47         $favourites = $topFavourites->run(6);
48         $recentlyUpdatedPages = $this->queries->pages->visibleForList()
49             ->where('draft', false)
50             ->orderBy('updated_at', 'desc')
51             ->take($favourites->count() > 0 ? 5 : 10)
52             ->get();
53
54         $homepageOptions = ['default', 'books', 'bookshelves', 'page'];
55         $homepageOption = setting('app-homepage-type', 'default');
56         if (!in_array($homepageOption, $homepageOptions)) {
57             $homepageOption = 'default';
58         }
59
60         $commonData = [
61             'activity'             => $activity,
62             'recents'              => $recents,
63             'recentlyUpdatedPages' => $recentlyUpdatedPages,
64             'draftPages'           => $draftPages,
65             'favourites'           => $favourites,
66         ];
67
68         // Add required list ordering & sorting for books & shelves views.
69         if ($homepageOption === 'bookshelves' || $homepageOption === 'books') {
70             $key = $homepageOption;
71             $view = setting()->getForCurrentUser($key . '_view_type');
72             $listOptions = SimpleListOptions::fromRequest($request, $key)->withSortOptions([
73                 'name' => trans('common.sort_name'),
74                 'created_at' => trans('common.sort_created_at'),
75                 'updated_at' => trans('common.sort_updated_at'),
76             ]);
77
78             $commonData = array_merge($commonData, [
79                 'view'        => $view,
80                 'listOptions' => $listOptions,
81             ]);
82         }
83
84         if ($homepageOption === 'bookshelves') {
85             $shelves = $this->queries->shelves->visibleForListWithCover()
86                 ->orderBy($commonData['listOptions']->getSort(), $commonData['listOptions']->getOrder())
87                 ->paginate(18);
88             $data = array_merge($commonData, ['shelves' => $shelves]);
89
90             return view('home.shelves', $data);
91         }
92
93         if ($homepageOption === 'books') {
94             $books = $this->queries->books->visibleForListWithCover()
95                 ->orderBy($commonData['listOptions']->getSort(), $commonData['listOptions']->getOrder())
96                 ->paginate(18);
97             $data = array_merge($commonData, ['books' => $books]);
98
99             return view('home.books', $data);
100         }
101
102         if ($homepageOption === 'page') {
103             $homepageSetting = setting('app-homepage', '0:');
104             $id = intval(explode(':', $homepageSetting)[0]);
105             /** @var Page $customHomepage */
106             $customHomepage = $this->queries->pages->start()->where('draft', '=', false)->findOrFail($id);
107             $pageContent = new PageContent($customHomepage);
108             $customHomepage->html = $pageContent->render(false);
109
110             return view('home.specific-page', array_merge($commonData, ['customHomepage' => $customHomepage]));
111         }
112
113         return view('home.default', $commonData);
114     }
115
116     /**
117      * Show the view for /robots.txt.
118      */
119     public function robots()
120     {
121         $sitePublic = setting('app-public', false);
122         $allowRobots = config('app.allow_robots');
123
124         if ($allowRobots === null) {
125             $allowRobots = $sitePublic;
126         }
127
128         return response()
129             ->view('misc.robots', ['allowRobots' => $allowRobots])
130             ->header('Content-Type', 'text/plain');
131     }
132
133     /**
134      * Show the route for 404 responses.
135      */
136     public function notFound()
137     {
138         return response()->view('errors.404', [], 404);
139     }
140
141     /**
142      * Serve the application favicon.
143      * Ensures a 'favicon.ico' file exists at the web root location (if writable) to be served
144      * directly by the webserver in the future.
145      */
146     public function favicon(FaviconHandler $favicons)
147     {
148         $exists = $favicons->restoreOriginalIfNotExists();
149         return response()->file($exists ? $favicons->getPath() : $favicons->getOriginalPath());
150     }
151
152     /**
153      * Serve a PWA application manifest.
154      */
155     public function pwaManifest(PwaManifestBuilder $manifestBuilder)
156     {
157         return response()->json($manifestBuilder->build());
158     }
159 }