-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathappController.ts
More file actions
248 lines (225 loc) · 7.87 KB
/
appController.ts
File metadata and controls
248 lines (225 loc) · 7.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/**
* @module controllers/appController
* @description Application controller module handling main application routes and views.
*/
import asyncHandler from 'express-async-handler';
import { Response } from 'express';
import appConfig from '../config/app';
import {
buildCanonical,
buildSources,
fetchAndUpdatePosters,
fetchOmdbData,
getResumeRedirect,
getSeriesDetail,
upsertSeriesProgress,
upsertMovieWatched,
} from '../helpers/appHelper';
import { getLatest, invalidateLatest, setLatest } from '../helpers/cache';
import http from '../helpers/httpClient';
import type { AuthRequest } from '../types/interfaces';
const SERVER_PREF_COOKIE = 'preferredServer';
/**
* @namespace appController
* @description Controller object containing methods for handling web application routes and views.
* This controller manages the main functionality of the application including home page display,
* content viewing, and search operations.
*
* @property {function} getHome - Renders home page with latest movies and TV shows
* @property {function} getView - Renders specific content view for movies or TV episodes
* @property {function} getSearch - Processes search queries and renders results
*
* @example
* // Using getHome to render the home page
* appController.getHome(req, res);
*
* // Using getView to display specific content
* appController.getView(req, res);
*
* // Using getSearch to find content
* appController.getSearch(req, res);
*/
const appController = {
/**
* Handles the home route to retrieve and render the latest movies and TV shows.
*
* @function getHome
* @async
* @param {AuthRequest} req - The HTTP request object with query parameters and user auth
* @param {Response} res - The HTTP response object for rendering
* @throws {Error} If API requests fail or rendering encounters an error
*
* @description
* This function performs the following operations:
* 1. Extracts query parameters for search and content type
* 2. Fetches latest movies from VIDSRC API
* 3. Fetches latest TV shows from VIDSRC API
* 4. Updates poster images for both movies and shows
* 5. Renders the home page with all gathered data
*
* @example
* // Route handler usage
* router.get('/', appController.getHome);
*/
getHome: asyncHandler(async (req: AuthRequest, res: Response) => {
const query = (req.query.q as string) || '';
const type = (req.query.type as string) || 'movie';
const canonical = res.locals.APP_URL;
const cached = getLatest();
if (cached) {
return res.render('index', {
newMovies: cached.movies,
newSeries: cached.series,
query,
type,
canonical,
card: res.locals.CARD_TYPE,
user: req.user,
});
}
const [axiosMovieResponse, axiosSeriesResponse] = await Promise.all([
http.get(`https://${appConfig.VIDSRC_DOMAIN}/movies/latest/page-1.json`),
http.get(`https://${appConfig.VIDSRC_DOMAIN}/tvshows/latest/page-1.json`),
]);
let newMovies = axiosMovieResponse.data.result || [];
let newSeries = axiosSeriesResponse.data.result || [];
await Promise.all([
fetchAndUpdatePosters(newMovies),
fetchAndUpdatePosters(newSeries),
]);
setLatest({ movies: newMovies, series: newSeries });
res.render('index', {
newMovies,
newSeries,
query,
type,
canonical,
card: res.locals.CARD_TYPE,
user: req.user,
});
}),
/**
* Invalidates the cached latest movies and series.
* Useful for deployments or scheduled cache refreshes.
*/
clearCache: asyncHandler(async (_req: AuthRequest, res: Response) => {
invalidateLatest();
res.json({ cleared: true });
}),
/**
* Handles the "getView" route for rendering a view page based on the provided query parameters.
*
* @function getView
* @async
* @param {AuthRequest} req - The request object, containing parameters like `q`, `id`, `type`, `season`, and `episode`.
* @param {Response} res - The response object, used for rendering the view or sending a response.
* @returns {void}
*
* This function processes the route parameters to determine the content type (movie or series). It dynamically constructs
* the iframe source URL and the canonical URL for SEO purposes, and fetches additional data from the OMDb API. Depending on
* the `type` parameter (series or default movie), it includes additional fields for season and episode when rendering the view.
*/
getView: asyncHandler(async (req: AuthRequest, res: Response) => {
const query = req.params.q || '';
const id = req.params.id;
const type = req.params.type as 'movie' | 'series';
const cookieHeader =
typeof req.headers?.cookie === 'string' ? req.headers.cookie : '';
const match = cookieHeader.match(
new RegExp(`(?:^|;\\s*)${SERVER_PREF_COOKIE}=(1|2)(?:;|$)`)
);
const preferredServer = match ? (match[1] as '1' | '2') : undefined;
if (type === 'series') {
let season = req.params.season;
let episode = req.params.episode;
if ((!season || !episode) && req.user) {
const redirectTo = await getResumeRedirect(req.user.id, id);
if (redirectTo) {
return res.redirect(redirectTo);
}
}
season = season || '1';
episode = episode || '1';
if (req.user) {
await upsertSeriesProgress(req.user.id, id, season, episode);
}
const { server1Src, server2Src, iframeSrc, currentServer } = buildSources(id, 'series', {
season,
episode,
preferredServer,
});
const canonical = buildCanonical(res.locals.APP_URL, id, type, season, episode);
const data = await fetchOmdbData(id, false);
const seriesDetail = await getSeriesDetail(id, Number(season));
return res.render('view', {
data,
iframeSrc,
server1Src,
server2Src,
currentServer,
serverPreferenceKey: SERVER_PREF_COOKIE,
query,
id,
type,
season,
episode,
seriesDetail,
canonical,
user: req.user,
});
}
// movie branch
let watched = false;
if (req.user) {
const history = await upsertMovieWatched(req.user.id, id);
watched = history?.watched || false;
}
const { server1Src, server2Src, iframeSrc, currentServer } = buildSources(id, 'movie', {
preferredServer,
});
const canonical = buildCanonical(res.locals.APP_URL, id, type);
const data = await fetchOmdbData(id, false);
res.render('view', {
data,
iframeSrc,
server1Src,
server2Src,
currentServer,
serverPreferenceKey: SERVER_PREF_COOKIE,
query,
id,
type,
canonical,
user: req.user,
watched,
});
}),
/**
* Handles the search functionality for the application.
* Processes the search query and type from the request, fetches search results from the OMDB API, and renders the
* search results page.
*
* @function getSearch
* @async
* @param {AuthRequest} req - The request object, including query parameters for the search query and type.
* @param {Response} res - The response object used to render the search page or perform a redirect.
* @throws {Error} Propagates errors due to issues in fetching data from OMDB or rendering the response.
*/
getSearch: asyncHandler(async (req: AuthRequest, res: Response) => {
const query = (req.query.q as string).trim();
const type = (req.query.type as string) || 'movie';
const omdbSearch = await fetchOmdbData(query, true, type);
const results = omdbSearch.Search || [];
const canonical = `${res.locals.APP_URL}/search/?q=${query}&type=${type}`;
if (!query) res.redirect('/');
res.render('search', {
query,
results,
type,
canonical,
card: res.locals.CARD_TYPE,
user: req.user,
});
}),
};
export default appController;