1
1
const { Request, Response, NextFunction } = require ( 'express' )
2
2
const Logger = require ( '../Logger' )
3
3
const Database = require ( '../Database' )
4
- const libraryItemsBookFilters = require ( '../utils/queries/libraryItemsBookFilters' )
4
+
5
+ const RssFeedManager = require ( '../managers/RssFeedManager' )
5
6
6
7
/**
7
8
* @typedef RequestUserObject
@@ -22,10 +23,10 @@ class RSSFeedController {
22
23
* @param {Response } res
23
24
*/
24
25
async getAll ( req , res ) {
25
- const feeds = await this . rssFeedManager . getFeeds ( )
26
+ const feeds = await RssFeedManager . getFeeds ( )
26
27
res . json ( {
27
- feeds : feeds . map ( ( f ) => f . toJSON ( ) ) ,
28
- minified : feeds . map ( ( f ) => f . toJSONMinified ( ) )
28
+ feeds : feeds . map ( ( f ) => f . toOldJSON ( ) ) ,
29
+ minified : feeds . map ( ( f ) => f . toOldJSONMinified ( ) )
29
30
} )
30
31
}
31
32
@@ -62,12 +63,12 @@ class RSSFeedController {
62
63
}
63
64
64
65
// Check that this slug is not being used for another feed (slug will also be the Feed id)
65
- if ( await this . rssFeedManager . findFeedBySlug ( reqBody . slug ) ) {
66
+ if ( await RssFeedManager . checkExistsBySlug ( reqBody . slug ) ) {
66
67
Logger . error ( `[RSSFeedController] Cannot open RSS feed because slug "${ reqBody . slug } " is already in use` )
67
68
return res . status ( 400 ) . send ( 'Slug already in use' )
68
69
}
69
70
70
- const feed = await this . rssFeedManager . openFeedForItem ( req . user . id , itemExpanded , reqBody )
71
+ const feed = await RssFeedManager . openFeedForItem ( req . user . id , itemExpanded , reqBody )
71
72
if ( ! feed ) {
72
73
Logger . error ( `[RSSFeedController] Failed to open RSS feed for item "${ itemExpanded . media . title } "` )
73
74
return res . status ( 500 ) . send ( 'Failed to open RSS feed' )
@@ -87,35 +88,37 @@ class RSSFeedController {
87
88
* @param {Response } res
88
89
*/
89
90
async openRSSFeedForCollection ( req , res ) {
90
- const options = req . body || { }
91
-
92
- const collection = await Database . collectionModel . findByPk ( req . params . collectionId )
93
- if ( ! collection ) return res . sendStatus ( 404 )
91
+ const reqBody = req . body || { }
94
92
95
93
// Check request body options exist
96
- if ( ! options . serverAddress || ! options . slug ) {
94
+ if ( ! reqBody . serverAddress || ! reqBody . slug || typeof reqBody . serverAddress !== 'string' || typeof reqBody . slug !== 'string' ) {
97
95
Logger . error ( `[RSSFeedController] Invalid request body to open RSS feed` )
98
96
return res . status ( 400 ) . send ( 'Invalid request body' )
99
97
}
100
98
101
99
// Check that this slug is not being used for another feed (slug will also be the Feed id)
102
- if ( await this . rssFeedManager . findFeedBySlug ( options . slug ) ) {
103
- Logger . error ( `[RSSFeedController] Cannot open RSS feed because slug "${ options . slug } " is already in use` )
100
+ if ( await RssFeedManager . checkExistsBySlug ( reqBody . slug ) ) {
101
+ Logger . error ( `[RSSFeedController] Cannot open RSS feed because slug "${ reqBody . slug } " is already in use` )
104
102
return res . status ( 400 ) . send ( 'Slug already in use' )
105
103
}
106
104
107
- const collectionExpanded = await collection . getOldJsonExpanded ( )
108
- const collectionItemsWithTracks = collectionExpanded . books . filter ( ( li ) => li . media . tracks . length )
105
+ const collection = await Database . collectionModel . getExpandedById ( req . params . collectionId )
106
+ if ( ! collection ) return res . sendStatus ( 404 )
109
107
110
108
// Check collection has audio tracks
111
- if ( ! collectionItemsWithTracks . length ) {
109
+ if ( ! collection . books . some ( ( book ) => book . includedAudioFiles . length ) ) {
112
110
Logger . error ( `[RSSFeedController] Cannot open RSS feed for collection "${ collection . name } " because it has no audio tracks` )
113
111
return res . status ( 400 ) . send ( 'Collection has no audio tracks' )
114
112
}
115
113
116
- const feed = await this . rssFeedManager . openFeedForCollection ( req . user . id , collectionExpanded , req . body )
114
+ const feed = await RssFeedManager . openFeedForCollection ( req . user . id , collection , reqBody )
115
+ if ( ! feed ) {
116
+ Logger . error ( `[RSSFeedController] Failed to open RSS feed for collection "${ collection . name } "` )
117
+ return res . status ( 500 ) . send ( 'Failed to open RSS feed' )
118
+ }
119
+
117
120
res . json ( {
118
- feed : feed . toJSONMinified ( )
121
+ feed : feed . toOldJSONMinified ( )
119
122
} )
120
123
}
121
124
@@ -128,37 +131,37 @@ class RSSFeedController {
128
131
* @param {Response } res
129
132
*/
130
133
async openRSSFeedForSeries ( req , res ) {
131
- const options = req . body || { }
132
-
133
- const series = await Database . seriesModel . findByPk ( req . params . seriesId )
134
- if ( ! series ) return res . sendStatus ( 404 )
134
+ const reqBody = req . body || { }
135
135
136
136
// Check request body options exist
137
- if ( ! options . serverAddress || ! options . slug ) {
137
+ if ( ! reqBody . serverAddress || ! reqBody . slug || typeof reqBody . serverAddress !== 'string' || typeof reqBody . slug !== 'string' ) {
138
138
Logger . error ( `[RSSFeedController] Invalid request body to open RSS feed` )
139
139
return res . status ( 400 ) . send ( 'Invalid request body' )
140
140
}
141
141
142
142
// Check that this slug is not being used for another feed (slug will also be the Feed id)
143
- if ( await this . rssFeedManager . findFeedBySlug ( options . slug ) ) {
144
- Logger . error ( `[RSSFeedController] Cannot open RSS feed because slug "${ options . slug } " is already in use` )
143
+ if ( await RssFeedManager . checkExistsBySlug ( reqBody . slug ) ) {
144
+ Logger . error ( `[RSSFeedController] Cannot open RSS feed because slug "${ reqBody . slug } " is already in use` )
145
145
return res . status ( 400 ) . send ( 'Slug already in use' )
146
146
}
147
147
148
- const seriesJson = series . toOldJSON ( )
149
-
150
- // Get books in series that have audio tracks
151
- seriesJson . books = ( await libraryItemsBookFilters . getLibraryItemsForSeries ( series ) ) . filter ( ( li ) => li . media . numTracks )
148
+ const series = await Database . seriesModel . getExpandedById ( req . params . seriesId )
149
+ if ( ! series ) return res . sendStatus ( 404 )
152
150
153
151
// Check series has audio tracks
154
- if ( ! seriesJson . books . length ) {
155
- Logger . error ( `[RSSFeedController] Cannot open RSS feed for series "${ seriesJson . name } " because it has no audio tracks` )
152
+ if ( ! series . books . some ( ( book ) => book . includedAudioFiles . length ) ) {
153
+ Logger . error ( `[RSSFeedController] Cannot open RSS feed for series "${ series . name } " because it has no audio tracks` )
156
154
return res . status ( 400 ) . send ( 'Series has no audio tracks' )
157
155
}
158
156
159
- const feed = await this . rssFeedManager . openFeedForSeries ( req . user . id , seriesJson , req . body )
157
+ const feed = await RssFeedManager . openFeedForSeries ( req . user . id , series , req . body )
158
+ if ( ! feed ) {
159
+ Logger . error ( `[RSSFeedController] Failed to open RSS feed for series "${ series . name } "` )
160
+ return res . status ( 500 ) . send ( 'Failed to open RSS feed' )
161
+ }
162
+
160
163
res . json ( {
161
- feed : feed . toJSONMinified ( )
164
+ feed : feed . toOldJSONMinified ( )
162
165
} )
163
166
}
164
167
@@ -170,8 +173,16 @@ class RSSFeedController {
170
173
* @param {RequestWithUser } req
171
174
* @param {Response } res
172
175
*/
173
- closeRSSFeed ( req , res ) {
174
- this . rssFeedManager . closeRssFeed ( req , res )
176
+ async closeRSSFeed ( req , res ) {
177
+ const feed = await Database . feedModel . findByPk ( req . params . id )
178
+ if ( ! feed ) {
179
+ Logger . error ( `[RSSFeedController] Cannot close RSS feed because feed "${ req . params . id } " does not exist` )
180
+ return res . sendStatus ( 404 )
181
+ }
182
+
183
+ await RssFeedManager . handleCloseFeed ( feed )
184
+
185
+ res . sendStatus ( 200 )
175
186
}
176
187
177
188
/**
0 commit comments